Hi, I've been trying to create a macro and I've hit a wall. The point of the macro is to convert a const value into the name it is assigned to (I'd love to use enum but I'm dealing with the OpenGL wrapper here).
First, here is an example of the working function I want to create with a macro:
import opengl
proc const_name(typ: int): string =
if typ == cGL_INT:
return "cGL_INT"
if typ == cGL_FLOAT:
return "cGL_FLOAT"
if typ == cGL_DOUBLE:
return "cGL_DOUBLE"
echo const_name(cGL_INT)
Right now this is where I'm at in trying to translate this into a macro version:
import macros
import opengl
macro const_name_check(v: expr, n: varargs[expr]): stmt =
result = newNimNode(nnkStmtList, n)
for i in 0..n.len-1:
var
n_i = n[i]
n_i_lit = toStrLit(n[i])
result.add(quote do:
if `v` == `n_i`:
return `n_i_lit`
)
proc const_name(typ: int): string =
const_name_check(typ, cGL_INT, cGL_FLOAT, cGL_DOUBLE)
echo const_name(cGL_INT)
However when I try to run this program it throws the following error:
... example.nim(12, 17) Error: type mismatch: got (int, expr) but expected one of: system.==(x: int16, y: int16): bool system.==(x: cstring, y: cstring): bool system.==(x: set[T], y: set[T]): bool system.==(x: pointer, y: pointer): bool system.==(x: char, y: char): bool system.==(x: T, y: T): bool system.==(x: ref T, y: ref T): bool system.==(x: string, y: string): bool system.==(x: ptr T, y: ptr T): bool system.==(x: seq[T], y: seq[T]): bool system.==(x: T, y: T): bool system.==(x: int8, y: int8): bool system.==(x: float, y: float): bool macros.==(a: TNimrodIdent, b: TNimrodIdent): bool system.==(x: int64, y: int64): bool system.==(x: int32, y: int32): bool macros.==(a: PNimrodNode, b: PNimrodNode): bool system.==(x: int, y: int): bool system.==(x: bool, y: bool): bool system.==(x: TEnum, y: TEnum): bool
Would appreciate some insight as to where I'm going wrong. Cheers!