Is there a TreeVisitor for visiting expression trees in evaluation order?
-
18-04-2021 - |
Pregunta
Using the Java Compiler Tree API, one can traverse the leaf tree of a TreePath
and its children using a TreeVisitor
.
Is there a TreeVisitor
implementation that visits all "nodes" in evaluation order? For example, if 7 - 8 * 2 + 10
were parsed as:
_____+__ / \ - 10 / \ 7 * / \ 8 2
Is there a TreeVisitor
that will visit the BinaryTree
for 8 * 2
followed by the BinaryTree
for 7 - (8 * 2)
, then the BinaryTree
for (7 - (8 * 2)) + 10
?
Solución
Where better to look than the source of javac
(langtools)!
In the Analyze and Generate phase of compilation, multiple passes are made through each compilation unit syntax tree. One pass in particular, Gen, generates the bytecode compilations of method implementations. The bulk of the Gen pass is apparently in com.sun.tools.javac.jvm.Gen
, which implements JCTree.Visitor
.