How can the AST be obtained for scala code after the macros have been expanded at runtime?
// 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()
Thanks!
After digging around, I have not found a way to get the post-macro-expanded AST at runtime using the current experimental reflect and compiler API docs, but I can use the compiler option -Ymacro-debug-lite
to print out each macro as it gets expanded at compile time. Also, there's scalamacros.org and scalameta.org which propose the next gen of AST inspection/manipulation and macros.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.