Package com.singularsys.jep.walkers
Class PostfixTreeWalker
java.lang.Object
com.singularsys.jep.walkers.PostfixTreeWalker
- Direct Known Subclasses:
HookRemover
,PostfixEvaluator
,TreeAnalyzer
Base class for routines which use a non-recursive tree walker strategy.
The typical recursive strategy can use a lot of stack frames
for very large expressions these can cause a stack overflow exception.
Subclasses should implement the visit methods.
to traverse the various nodes. In general these methods should not recursively walk the child nodes.
This class uses a postfix traversal scheme hence the nodes of '1+2' will be visited in the order
1,2,+.
- Since:
- 3.2 depth of root node is now 1 not 0
- Author:
- Richard Morris
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
Whether to examine the children ofthis
nodes.protected abstract void
visit
(ASTConstant node, int nchildren, int depth1) Visit a constant nodeprotected abstract void
visit
(ASTFunNode node, int nchildren, int depth1) Visit a function nodeprotected abstract void
Visit an operator nodeprotected abstract void
visit
(ASTVarNode node, int nchildren, int depth1) Visit a variable nodeprotected final void
Start transversal of the expression.protected final void
walkSubEquations
(Node child) If asuppressExaminingChildren(Node)
returns true, then the children of the node will not be visited by default.
-
Constructor Details
-
PostfixTreeWalker
public PostfixTreeWalker()
-
-
Method Details
-
walk
Start transversal of the expression.- Parameters:
top
- top node for the expression- Throws:
JepException
-
suppressExaminingChildren
Whether to examine the children ofthis
nodes. In some cases it is necessary to jump out of traversal strategy to process a node.- Parameters:
node
-- Returns:
- false unless overridden by sub-class
-
walkSubEquations
If asuppressExaminingChildren(Node)
returns true, then the children of the node will not be visited by default. This method allows a subclass to visit the children of such a node if needed.For example an evaluator which wants to implement lazy evaluation for the logical AND operator the subclass will call this method when needed from its implementation of
visit(ASTOpNode, int, int)
.- Parameters:
child
- child of a node to start traversing from- Throws:
JepException
-
visit
Visit a function node- Parameters:
node
- the current node being visitednchildren
- number of children of the nodedepth1
- depth of tree, root node is depth 1.- Throws:
JepException
-
visit
Visit an operator node- Parameters:
node
- the current node being visitednchildren
- number of children of the nodedepth1
- depth of tree, root node is depth 1.- Throws:
JepException
-
visit
Visit a variable node- Parameters:
node
- the current node being visitednchildren
- number of children of the nodedepth1
- depth of tree, root node is depth 1.- Throws:
JepException
-
visit
Visit a constant node- Parameters:
node
- the current node being visitednchildren
- number of children of the nodedepth1
- depth of tree, root node is depth 1.- Throws:
JepException
-