Is there a TreeVisitor for visiting expression trees in evaluation order?

555 views Asked by At

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?

1

There are 1 answers

0
Daniel Trebbien On BEST ANSWER

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.