dumpTree
类似的宏在编译时打印 Nim 的 AST。我想知道如何在运行时打印相同的内容。用例是能够捕获输出并将其记录在 nimib 中。文档。
举个具体的例子:
import macros
dumpTree:
25 ⬇️
10 ➖
编译时生成:
StmtList
Command
IntLit 25
Ident "⬇️"
Command
IntLit 10
Ident "➖"
我想要一个与上述情况等效的 dumpTreeRuntime
:
echo """
StmtList
Command
IntLit 25
Ident "⬇️"
Command
IntLit 10
Ident "➖"
"""
最佳答案
import macros
macro treeToString(code: untyped): untyped =
return newLit(treeRepr(code))
when isMainModule:
let x = treeToString:
25 ⬇️
10 ➖
echo(x)
关于macros - 如何在运行时打印 Nim 的 AST?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76425870/