当前位置: 动力学知识库 > 问答 > 编程问答 >

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

问题描述:

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?

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: