I'm using the nim programming language and am doing some metaprogramming.
I want to write a DSL of some sorts that enables generating procs. For that I want to pass some nim-code into a macro (e.g. a proc definition) and eventually generate the proc from that.
Now I know you can capture nim-code inside of a macro with syntax such as this:
macro generateMapper(body: untyped): untyped =
echo body.repr
generateMapper():
proc useThisToGenerateAProc(source: A, target: B)
Which puts proc useThisToGenerateAProc(source: A, target: B) into body, but echo'ing body.repr doesn't show anything.
How can I see what NimNodes are actually inside body or other NimNodes in general etc.?
You'll want to use these procs from
std/macros(in order of most verbose representation to least):These will print you a representation at compile-time of the NimNode and everything it contains. AstGenRepr of those 3 is the one that is closest to the code you'd actually write inside a macro where you deal with the Nodes yourself.
An example with
astGenRepr:This prints (With comments from myself):
Compared to that the more compact
treeRepr:And the even more compact
lisprepr: