I would like to create a macro that gets as argument a function call. The macro should then replace the function call with a call of a modified function. But since my macro should not generally modify everything in the function, but just annotated statements, I would like to know how I can do such annotations. Sadly user defined pragmas seem to be not suitable.
import macros
{. pragma: myAnnotation .}
proc foobar(i : int) : int =
var j {. myAnnotation .} : int = i + 2
return i * 2 + j
macro handlePragmas( arg: typed ): typed =
echo arg[0].symbol.getImpl.repr
discard
handlePragmas: foobar(13)
the output of the macro here is just
proc foobar(i: int): int =
var j: int = i + 2
return result = i * 2 + j
As you can see, the {. myAnnotation .} pragma is gone, and that is what I would need in my use case.
Yeah that's unfortunate. I planned on fixing this, maybe you can help out? The pragma is not eliminated if you declare your macro like
macro handlePragmas( arg: untyped ): typed
but I can imagine you like to work on sem'checked AST instead. If the whole excercise is code optimization, you can also consider to use a term rewriting macro instead. These run after sem'check too.