Scala:在运行时扩展宏
Scala: Expand Macros at Runtime
scala代码在运行时展开宏后如何获取AST?
// obtain toolbox
val tb = runtimeMirror(getClass.getClassLoader).mkToolBox()
// generate the AST
val tree = tb.parse(<...source code using one or more macros...>)
// let's look at the code
println(show(tree))
// let's see what the AST looks like
println(showRaw(tree))
// expand macros!
xTreem = ?????
// now let's look at the code and AST
println(show(xTreem))
println(showRaw(xTreem))
// compile and...
val f = tb.compile(xTreem)
// ... execute
f()
谢谢!
四处挖掘后,我还没有找到在运行时使用当前实验反射和编译器 API 文档获取 post-macro-expanded AST 的方法,但我可以使用编译器选项 -Ymacro-debug-lite
打印出每个在编译时展开的宏。此外,还有 scalamacros.org 和 scalameta.org 提出了下一代 AST inspection/manipulation 和宏。
scala代码在运行时展开宏后如何获取AST?
// obtain toolbox
val tb = runtimeMirror(getClass.getClassLoader).mkToolBox()
// generate the AST
val tree = tb.parse(<...source code using one or more macros...>)
// let's look at the code
println(show(tree))
// let's see what the AST looks like
println(showRaw(tree))
// expand macros!
xTreem = ?????
// now let's look at the code and AST
println(show(xTreem))
println(showRaw(xTreem))
// compile and...
val f = tb.compile(xTreem)
// ... execute
f()
谢谢!
四处挖掘后,我还没有找到在运行时使用当前实验反射和编译器 API 文档获取 post-macro-expanded AST 的方法,但我可以使用编译器选项 -Ymacro-debug-lite
打印出每个在编译时展开的宏。此外,还有 scalamacros.org 和 scalameta.org 提出了下一代 AST inspection/manipulation 和宏。