org.nfunk.jep
Class EvaluatorVisitor

java.lang.Object
  extended byorg.nfunk.jep.EvaluatorVisitor
All Implemented Interfaces:
ParserVisitor
Direct Known Subclasses:
XEvaluatorVisitor

public class EvaluatorVisitor
extends java.lang.Object
implements ParserVisitor

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 handeling by making visit methods throw ParseException. Changed visit(ASTVarNode node) so messages not calculated every time.


Field Summary
protected  boolean errorFlag
          Flag for errors during evaluation
protected  java.util.Vector errorList
          The current error list
protected  java.util.Stack stack
          Stack used for evaluating the expression
protected  SymbolTable symTab
          The symbol table for variable lookup
 
Constructor Summary
EvaluatorVisitor()
          Constructor.
 
Method Summary
protected  void addToErrorList(java.lang.String errorStr)
          Adds an error message to the list of errors
 java.lang.Object getValue(Node topNode, java.util.Vector errorList_in, SymbolTable symTab_in)
          Returns the value of the expression as an object.
 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


errorList

protected java.util.Vector errorList
The current error list


symTab

protected SymbolTable symTab
The symbol table for variable lookup


errorFlag

protected boolean errorFlag
Flag for errors during evaluation

Constructor Detail

EvaluatorVisitor

public EvaluatorVisitor()
Constructor. Initialize the stack member

Method Detail

addToErrorList

protected void addToErrorList(java.lang.String errorStr)
Adds an error message to the list of errors


getValue

public java.lang.Object getValue(Node topNode,
                                 java.util.Vector errorList_in,
                                 SymbolTable symTab_in)
                          throws java.lang.Exception
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 errorList_in parameter is used to add error information that may occur during the evaluation. It is not required, and may be set to null if no error information is needed.

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:
java.lang.Exception

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


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