【发布时间】:2020-12-31 09:54:53
【问题描述】:
我想使用 nim 访问完整模块(文件)的 AST。我发现,任何宏都可以用作自定义编译指示,所以我在文件 foo.nim 中做了类似的事情:
import macros
macro getAst(ast: untyped): untyped =
echo "ast = ", treeRepr(ast)
{.getAst.} # <-- This should invoke the getAst() macro with the AST of the file
proc hello() =
echo "hello world"
但我得到编译器错误cannot attach a custom pragma to 'foo'
有没有办法做到这一点?我发现我可以像这样将宏用作块宏,但我真的更喜欢通过 pragma 使用它。
getAst:
proc hello() =
echo "hello world"
【问题讨论】:
标签: macros metaprogramming nim-lang