org.nfunk.jep
Class EvaluatorVisitor

java.lang.Object
  extended by org.nfunk.jep.EvaluatorVisitor
All Implemented Interfaces:
EvaluatorI, ParserVisitor
Direct Known Subclasses:
VectorEvaluator, XEvaluatorVisitor

public class EvaluatorVisitor
extends java.lang.Object
implements ParserVisitor, EvaluatorI

This class is used for the evaluation of an expression. It uses the Visitor design pattern to traverse the function tree and evaluate the expression using a stack.

Function nodes are evaluated by first evaluating all the children nodes, then applying the function class associated with the node. Variable and constant nodes are evaluated by pushing their value onto the stack.

Some changes implemented by rjm. Nov 03. Added hook to SpecialEvaluationI. Clears stack before evaluation. Simplifies error handling by making visit methods throw ParseException. Changed visit(ASTVarNode node) so messages not calculated every time.


Field Summary
protected static boolean debug
          Debug flag
protected  java.util.Stack stack
          Stack used for evaluating the expression
protected  SymbolTable symTab
          The symbol table for variable lookup
protected  boolean trapNullValues
          TrapNull
 
Constructor Summary
EvaluatorVisitor()
          Constructor.
 
Method Summary
 java.lang.Object eval(Node node)
          Evaluates a given node, in the current context.
 java.lang.Object eval(PostfixMathCommandI pfmc, Node[] children)
          Evaluates a PostfixMathCommandI with given arguments.
 java.lang.Object getValue(Node topNode, SymbolTable symTab_in)
          Returns the value of the expression as an object.
 boolean isTrapNullValues()
          Tests whether null variable values are trapped by evaluator.
 void setTrapNullValues(boolean b)
          Sets the behaviour when a variable's value is null.
 java.lang.Object visit(ASTConstant node, java.lang.Object data)
          Visit a constant node.
 java.lang.Object visit(ASTFunNode node, java.lang.Object data)
          Visit a function node.
 java.lang.Object visit(ASTStart node, java.lang.Object data)
          This method should never be called when evaluating a normal expression.
 java.lang.Object visit(ASTVarNode node, java.lang.Object data)
          Visit a variable node.
 java.lang.Object visit(SimpleNode node, java.lang.Object data)
          This method should never be called when evaluation a normal expression.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

stack

protected java.util.Stack stack
Stack used for evaluating the expression


symTab

protected SymbolTable symTab
The symbol table for variable lookup


debug

protected static final boolean debug
Debug flag

See Also:
Constant Field Values

trapNullValues

protected boolean trapNullValues
TrapNull

Constructor Detail

EvaluatorVisitor

public EvaluatorVisitor()
Constructor. Initialise the stack member

Method Detail

getValue

public java.lang.Object getValue(Node topNode,
                                 SymbolTable symTab_in)
                          throws ParseException
Returns the value of the expression as an object. The expression tree is specified with its top node. The algorithm uses a stack for evaluation.

The symTab parameter can be null, if no variables are expected in the expression. If a variable is found, an error is added to the error list.

An exception is thrown, if an error occurs during evaluation.

Returns:
The value of the expression as an object.
Throws:
ParseException - if there is a problem with the evaluation.

eval

public java.lang.Object eval(Node node)
                      throws ParseException
Evaluates a given node, in the current context.

Specified by:
eval in interface EvaluatorI
Parameters:
node - The node to evaluate
Returns:
result of the evaluation
Throws:
ParseException - if errors occur during evaluation;

eval

public java.lang.Object eval(PostfixMathCommandI pfmc,
                             Node[] children)
                      throws ParseException
Evaluates a PostfixMathCommandI with given arguments. Not used in normal use.

Parameters:
pfmc - the command to evaluate.
children - the parameters to the function.
Returns:
the value of the function
Throws:
ParseException

visit

public java.lang.Object visit(SimpleNode node,
                              java.lang.Object data)
                       throws ParseException
This method should never be called when evaluation a normal expression.

Specified by:
visit in interface ParserVisitor
Throws:
ParseException

visit

public java.lang.Object visit(ASTStart node,
                              java.lang.Object data)
                       throws ParseException
This method should never be called when evaluating a normal expression.

Specified by:
visit in interface ParserVisitor
Throws:
ParseException

visit

public java.lang.Object visit(ASTFunNode node,
                              java.lang.Object data)
                       throws ParseException
Visit a function node. The values of the child nodes are first pushed onto the stack. Then the function class associated with the node is used to evaluate the function.

If a function implements SpecialEvaluationI then the evaluate method of PFMC is called.

Specified by:
visit in interface ParserVisitor
Throws:
ParseException

visit

public java.lang.Object visit(ASTVarNode node,
                              java.lang.Object data)
                       throws ParseException
Visit a variable node. The value of the variable is obtained from the symbol table (symTab) and pushed onto the stack.

Specified by:
visit in interface ParserVisitor
Throws:
ParseException

visit

public java.lang.Object visit(ASTConstant node,
                              java.lang.Object data)
Visit a constant node. The value of the constant is pushed onto the stack.

Specified by:
visit in interface ParserVisitor

isTrapNullValues

public boolean isTrapNullValues()
Tests whether null variable values are trapped by evaluator.

Returns:
true is nulls are trapped

setTrapNullValues

public void setTrapNullValues(boolean b)
Sets the behaviour when a variable's value is null. If true an exception will be thrown is a variable value is null. If false then the value will be passed to other functions, this may cause error else where.

Parameters:
b -


http://www.singularsys.com/jep Copyright © 2007 Singular Systems