you may worked with AST and:
- sometimes you forgot the order of child nodes
- sometimes you found your simple macro so complex to maintain
- sometimes you wish there was a simpler solution
well, that's the purpose of this repo we provide some additional functionalities to work with AST simpler
this examples may change your mind to use this library or may not but I found them reasonable
imagine we have a AST representation of a a proc definition
code:
proc myProc(arg1: bool, arg2: int = 4): bool {.pragm1, pragma2.}=
discardAST repr:
ProcDef
Ident "myProc"
Empty
Empty
FormalParams
Ident "bool"
IdentDefs
Ident "arg1"
Ident "bool"
Empty
IdentDefs
Ident "arg2"
Ident "int"
IntLit 4
Pragma
Ident "pragm1"
Ident "pragma2"
Empty
StmtList
DiscardStmt
Emptywithout having the corresponding AST representation you [may] forgot the postion of proc name, return type, argumnets, pragma ,... to write this:
macro myMacro(body: untyped): untyped=
let returnType = body[3][0]
but with macroplus you don't need to remember such things:
let returnType = body.RoutineReturnTypehere RoutineReturnType is just a template
or imagine you wanna get arguments
let args = body.RoutineArgumentsand you wanna iterate over them. instead of writing this:
for arg in args:
if arg[0].strVal == "something":
if arg[2].strval == ...:
...you could write
for arg in args:
if arg[IdentDefName].strVal == "something":
if arg[IdentDefDefaultVal] == ...:
...isn't it more readable and understandable?
assume node is type of NimNode
node[callArgs][0] # expensive and bad
node[callArgs[0]] # cheap and good
node[callArgs[^1]] # yes even backwards indexes- add
rchildreniterator
- add intuitive indexing
macroutils has more features along with enhancing readability, like "pattern matching"/"traversing AST"/"advanced code quoting"/"typed NimNodes" which can be really helpful.
when dealing with AST, most of the times I found myself writing specific functionalities for my edge cases which macroutils cannot cover. so I made this library that contains just small and tiny utilities.