Some enums, presented to a macro ...
var enumlist {.compileTime.} : NimNode
macro gettheenums(body : untyped) : untyped =
enumlist = body
result = body
macro letstest(body : typed) : untyped =
echo astGenRepr(enumlist)
gettheenums:
type dtlist = enum e1a,e2a,e3a
type etlist = enum e1b,e1c,e1d
letstest:
var dummy = ""
# the macro prints the enum-related AST....
I want to ask the compiler about enum membership of particular NimIdent Nodes. I can do it "manually" - making the enums "statically" available first with a global {.compileTime.} NimNode , then parsing through the nnkStmtList / nnkTypeSection/nnkTypeDef/nnkEnumTy Tree. But, since the compiler already made some typechecks (and the macro got the typed AST) , I would think that the compiler could resolve the IdentNodes themselves too. (I wouldn't expect it automatically for the untyped AST though) . How can this be made possible? I didn't find an appropriate function in the macro API. Is there a simple solution available? Thanks.
Can parseEnum help you with what you want to do?
Thank you for your assistance. At the very end, {.experimental: "dynamicBindSym".} did the trick for me. I got three different solutions: via typedesc, via template (forwarding) and with bindSym at compile-time. The least mentioned was I was looking for.
import macros
{.experimental: "dynamicBindSym".}
type
Qa = object
field: int
Qb = array[0..23,NimNode]
Qc = tuple[tfa : int, tfb : int, tfc : string]
macro helper(a : typed, acomm : string): untyped =
echo "helper ",acomm," ", treeRepr getImpl a
macro resolve(ta : typedesc, tb : untyped, tc : untyped) : untyped =
echo "entry ", ta.strVal, tb.strVal, tc.strVal
var dyn = strVal tc
echo "typedesc ", treeRepr getImpl ta
template tpl(v) = helper v, "forwarded"
let retv = getAst tpl tb
let NNode = bindsym dyn
echo "dyn. bind ", treeRepr getImpl NNode
result = retv
resolve(Qa,Qb,Qc)
# prints type description in order : Qa, Qc, Qb
echo " done. "