ptolemy.data.expr
Class ParseTreeEvaluator

java.lang.Object
  extended by ptolemy.data.expr.AbstractParseTreeVisitor
      extended by ptolemy.data.expr.ParseTreeEvaluator
All Implemented Interfaces:
ParseTreeVisitor
Direct Known Subclasses:
GTParameter.Evaluator, ParseTreeEvaluatorForGuardExpression, ParseTreeEvaluatorForGuardExpression

public class ParseTreeEvaluator
extends AbstractParseTreeVisitor

This class evaluates a parse tree given a reference to its root node. It implements a visitor that visits the parse tree in depth-first order, evaluating each node and storing the result as a token in the node. Two exceptions are logic nodes and the ternary if node (the ? : construct), which do not necessarily evaluate all children nodes.

Since:
Ptolemy II 2.1
Version:
$Id: ParseTreeEvaluator.java 57040 2010-01-27 20:52:32Z cxh $
Author:
Steve Neuendorffer
See Also:
ASTPtRootNode
Accepted Rating:
Yellow (neuendor)
Proposed Rating:
Green (neuendor)

Field Summary
private  int _depth
           
protected  Token _evaluatedChildToken
           
private  ParserScope _scope
           
private  java.lang.StringBuffer _trace
           
protected  ParseTreeTypeInference _typeInference
           
 
Constructor Summary
ParseTreeEvaluator()
           
 
Method Summary
protected  void _assert(boolean flag, ASTPtRootNode node, java.lang.String message)
          Assert that the given boolean value, which describes the given parse tree node, is true.
protected  Token[] _evaluateAllChildren(ASTPtRootNode node)
          Loop through all of the children of this node, visiting each one of them; this will cause their token value to be determined.
protected  Token _evaluateArrayIndex(ASTPtRootNode node, Token value, Token index)
          Evaluate the array index operation represented by the given node.
protected  Token _evaluateChild(ASTPtRootNode node, int i)
          Evaluate the child with the given index of the given node.
protected  Token _evaluateMatrixIndex(ASTPtRootNode node, Token value, Token rowIndex, Token columnIndex)
          Evaluate the Matrix index operation represented by the given node.
protected  Token _functionCall(java.lang.String functionName, Type[] argTypes, java.lang.Object[] argValues)
          Evaluate the specified function.
private  Token _invokeMethod(java.lang.Class<?> clazz, java.lang.Object object, java.lang.String methodName, Type[] argTypes, java.lang.Object[] argValues)
          Invoke a method of the class for the given object, or retrieve a field of it.
protected  Token _methodCall(java.lang.String methodName, Type[] argTypes, java.lang.Object[] argValues)
          Evaluate the specified method.
protected  void _trace(java.lang.String string)
          Add a record to the current trace corresponding to the given message.
protected  void _traceEnter(ASTPtRootNode node)
          Add a record to the current trace corresponding to the start of the evaluation of the given node.
protected  void _traceLeave(ASTPtRootNode node)
          Add a record to the current trace corresponding to the completion of the evaluation of the given node.
 Token evaluateParseTree(ASTPtRootNode node)
          Evaluate the parse tree with the specified root node.
 Token evaluateParseTree(ASTPtRootNode node, ParserScope scope)
          Evaluate the parse tree with the specified root node using the specified scope to resolve the values of variables.
 java.lang.String traceParseTreeEvaluation(ASTPtRootNode node, ParserScope scope)
          Trace the evaluation of the parse tree with the specified root node using the specified scope to resolve the values of variables.
 void visitArrayConstructNode(ASTPtArrayConstructNode node)
          Construct an ArrayToken that contains the tokens from the children of the specified node.
 void visitBitwiseNode(ASTPtBitwiseNode node)
          Evaluate a bitwise operator on the children of the specified node, where the particular operator is property of the node.
 void visitFunctionalIfNode(ASTPtFunctionalIfNode node)
          Evaluate the first child, and depending on its (boolean) result, evaluate either the second or the third child.
 void visitFunctionApplicationNode(ASTPtFunctionApplicationNode node)
          Apply a function to the children of the specified node.
 void visitFunctionDefinitionNode(ASTPtFunctionDefinitionNode node)
          Define a function, where the children specify the argument types and the expression.
 void visitLeafNode(ASTPtLeafNode node)
          Evaluate a numeric constant or an identifier.
 void visitLogicalNode(ASTPtLogicalNode node)
          Evaluate a logical AND or OR on the children of the specified node.
 void visitMatrixConstructNode(ASTPtMatrixConstructNode node)
          Construct a matrix containing the children nodes.
 void visitMethodCallNode(ASTPtMethodCallNode node)
          Apply a method to the children of the specified node, where the first child is the object on which the method is defined and the rest of the children are arguments.
 void visitPowerNode(ASTPtPowerNode node)
          Evaluate the power operator on the children of the specified node.
 void visitProductNode(ASTPtProductNode node)
          Multiply the children of the specified node.
 void visitRecordConstructNode(ASTPtRecordConstructNode node)
          Construct a record by assigning the fields values given by the children nodes.
 void visitRelationalNode(ASTPtRelationalNode node)
           
 void visitShiftNode(ASTPtShiftNode node)
          Apply a shift operator to the children of the specified node.
 void visitSumNode(ASTPtSumNode node)
          Apply a sum operator to the children of the specified node.
 void visitUnaryNode(ASTPtUnaryNode node)
          Apply a unary operator to the single child of the specified node.
 void visitUnionConstructNode(ASTPtUnionConstructNode node)
          Construct a union by assigning the label value given by the children nodes.
 
Methods inherited from class ptolemy.data.expr.AbstractParseTreeVisitor
_unsupportedVisitException, _visitAllChildren, _visitChild, visitAssignmentNode
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_evaluatedChildToken

protected Token _evaluatedChildToken

_typeInference

protected ParseTreeTypeInference _typeInference

_depth

private int _depth

_scope

private ParserScope _scope

_trace

private java.lang.StringBuffer _trace
Constructor Detail

ParseTreeEvaluator

public ParseTreeEvaluator()
Method Detail

evaluateParseTree

public Token evaluateParseTree(ASTPtRootNode node)
                        throws IllegalActionException
Evaluate the parse tree with the specified root node.

Parameters:
node - The root of the parse tree.
Returns:
The result of evaluation.
Throws:
IllegalActionException - If an evaluation error occurs.

evaluateParseTree

public Token evaluateParseTree(ASTPtRootNode node,
                               ParserScope scope)
                        throws IllegalActionException
Evaluate the parse tree with the specified root node using the specified scope to resolve the values of variables.

Parameters:
node - The root of the parse tree.
scope - The scope for evaluation.
Returns:
The result of evaluation.
Throws:
IllegalActionException - If an error occurs during evaluation.

traceParseTreeEvaluation

public java.lang.String traceParseTreeEvaluation(ASTPtRootNode node,
                                                 ParserScope scope)
                                          throws IllegalActionException
Trace the evaluation of the parse tree with the specified root node using the specified scope to resolve the values of variables.

Parameters:
node - The root of the parse tree.
scope - The scope for evaluation.
Returns:
The trace of the evaluation.
Throws:
IllegalActionException - If an error occurs during evaluation.

visitArrayConstructNode

public void visitArrayConstructNode(ASTPtArrayConstructNode node)
                             throws IllegalActionException
Construct an ArrayToken that contains the tokens from the children of the specified node.

Specified by:
visitArrayConstructNode in interface ParseTreeVisitor
Overrides:
visitArrayConstructNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an evaluation error occurs.

visitBitwiseNode

public void visitBitwiseNode(ASTPtBitwiseNode node)
                      throws IllegalActionException
Evaluate a bitwise operator on the children of the specified node, where the particular operator is property of the node.

Specified by:
visitBitwiseNode in interface ParseTreeVisitor
Overrides:
visitBitwiseNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an evaluation error occurs.

visitFunctionApplicationNode

public void visitFunctionApplicationNode(ASTPtFunctionApplicationNode node)
                                  throws IllegalActionException
Apply a function to the children of the specified node. This also handles indexing into matrices and arrays, which look like function calls. In the simplest cases, if the function is being applied to an expression that evaluated to a FunctionToken, an ArrayToken, or a MatrixToken, then the function application is simply applied to the available arguments. More complex is if the function is being applied to an expression that does not evaluate as above, resulting in three cases: Of primary interest is a function node that represents the invocation of a Java method registered with the expression parser. This method uses the reflection mechanism in the CachedMethod class to find the correct method, based on the types of the arguments and invoke it. See that class for information about how method arguments are matched. A second case is the eval() function, which is handled specially in this method. The argument to the function is evaluated, and the parsed as a string using the expression parser. The result is then evaluated *in this evaluator*. This has the effect that any identifiers are evaluated in the same scope as the original expression. A third case is the matlab() function, which is also handled specially in this method, allowing the evaluation of expressions in matlab if matlab is installed. The format of the function is covered in MatlabUtilities.evaluate(String, Set, ParserScope) .

Specified by:
visitFunctionApplicationNode in interface ParseTreeVisitor
Overrides:
visitFunctionApplicationNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an evaluation error occurs.

visitFunctionDefinitionNode

public void visitFunctionDefinitionNode(ASTPtFunctionDefinitionNode node)
                                 throws IllegalActionException
Define a function, where the children specify the argument types and the expression. The expression is not evaluated. The resulting token in the node is an instance of FunctionToken.

Specified by:
visitFunctionDefinitionNode in interface ParseTreeVisitor
Overrides:
visitFunctionDefinitionNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an evaluation error occurs.

visitFunctionalIfNode

public void visitFunctionalIfNode(ASTPtFunctionalIfNode node)
                           throws IllegalActionException
Evaluate the first child, and depending on its (boolean) result, evaluate either the second or the third child. The result of that evaluation becomes the result of the specified node.

Specified by:
visitFunctionalIfNode in interface ParseTreeVisitor
Overrides:
visitFunctionalIfNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an evaluation error occurs.

visitLeafNode

public void visitLeafNode(ASTPtLeafNode node)
                   throws IllegalActionException
Evaluate a numeric constant or an identifier. In the case of an identifier, its value is obtained from the scope or from the list of registered constants.

Specified by:
visitLeafNode in interface ParseTreeVisitor
Overrides:
visitLeafNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an evaluation error occurs.

visitLogicalNode

public void visitLogicalNode(ASTPtLogicalNode node)
                      throws IllegalActionException
Evaluate a logical AND or OR on the children of the specified node.

Specified by:
visitLogicalNode in interface ParseTreeVisitor
Overrides:
visitLogicalNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an evaluation error occurs.

visitMatrixConstructNode

public void visitMatrixConstructNode(ASTPtMatrixConstructNode node)
                              throws IllegalActionException
Construct a matrix containing the children nodes. The specified node ends up with a MatrixToken value.

Specified by:
visitMatrixConstructNode in interface ParseTreeVisitor
Overrides:
visitMatrixConstructNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an evaluation error occurs.

visitMethodCallNode

public void visitMethodCallNode(ASTPtMethodCallNode node)
                         throws IllegalActionException
Apply a method to the children of the specified node, where the first child is the object on which the method is defined and the rest of the children are arguments. This also handles indexing into a record, which looks the same.

Specified by:
visitMethodCallNode in interface ParseTreeVisitor
Overrides:
visitMethodCallNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an evaluation error occurs.

visitPowerNode

public void visitPowerNode(ASTPtPowerNode node)
                    throws IllegalActionException
Evaluate the power operator on the children of the specified node.

Specified by:
visitPowerNode in interface ParseTreeVisitor
Overrides:
visitPowerNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an evaluation error occurs.

visitProductNode

public void visitProductNode(ASTPtProductNode node)
                      throws IllegalActionException
Multiply the children of the specified node.

Specified by:
visitProductNode in interface ParseTreeVisitor
Overrides:
visitProductNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an evaluation error occurs.

visitRecordConstructNode

public void visitRecordConstructNode(ASTPtRecordConstructNode node)
                              throws IllegalActionException
Construct a record by assigning the fields values given by the children nodes.

Specified by:
visitRecordConstructNode in interface ParseTreeVisitor
Overrides:
visitRecordConstructNode in class AbstractParseTreeVisitor
Parameters:
node - The record constructor node.
Throws:
IllegalActionException - If an evaluation error occurs.

visitRelationalNode

public void visitRelationalNode(ASTPtRelationalNode node)
                         throws IllegalActionException
Specified by:
visitRelationalNode in interface ParseTreeVisitor
Overrides:
visitRelationalNode in class AbstractParseTreeVisitor
Throws:
IllegalActionException

visitShiftNode

public void visitShiftNode(ASTPtShiftNode node)
                    throws IllegalActionException
Apply a shift operator to the children of the specified node.

Specified by:
visitShiftNode in interface ParseTreeVisitor
Overrides:
visitShiftNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an evaluation error occurs.

visitSumNode

public void visitSumNode(ASTPtSumNode node)
                  throws IllegalActionException
Apply a sum operator to the children of the specified node.

Specified by:
visitSumNode in interface ParseTreeVisitor
Overrides:
visitSumNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException - If an evaluation error occurs.

visitUnaryNode

public void visitUnaryNode(ASTPtUnaryNode node)
                    throws IllegalActionException
Apply a unary operator to the single child of the specified node.

Specified by:
visitUnaryNode in interface ParseTreeVisitor
Overrides:
visitUnaryNode in class AbstractParseTreeVisitor
Parameters:
node - The specified node.
Throws:
IllegalActionException

visitUnionConstructNode

public void visitUnionConstructNode(ASTPtUnionConstructNode node)
                             throws IllegalActionException
Construct a union by assigning the label value given by the children nodes.

Specified by:
visitUnionConstructNode in interface ParseTreeVisitor
Overrides:
visitUnionConstructNode in class AbstractParseTreeVisitor
Parameters:
node - The union constructor node.
Throws:
IllegalActionException - If an evaluation error occurs.

_assert

protected void _assert(boolean flag,
                       ASTPtRootNode node,
                       java.lang.String message)
Assert that the given boolean value, which describes the given parse tree node, is true. If it is false, then throw a new InternalErrorException that describes the node and includes the given message.

Parameters:
flag - The flag that is asserted to be true.
node - The node on which the assertion is asserted.
message - The message to include in the exception.
Throws:
InternalErrorException - If the assertion is violated. Note that this is a runtime exception, so it need not be declared explicitly.

_evaluateAllChildren

protected Token[] _evaluateAllChildren(ASTPtRootNode node)
                                throws IllegalActionException
Loop through all of the children of this node, visiting each one of them; this will cause their token value to be determined.

Parameters:
node - The node whose children are evaluated.
Throws:
IllegalActionException - If an evaluation error occurs.

_evaluateArrayIndex

protected Token _evaluateArrayIndex(ASTPtRootNode node,
                                    Token value,
                                    Token index)
                             throws IllegalActionException
Evaluate the array index operation represented by the given node.

Parameters:
node - The node that caused this method to be called.
value - The token that is being indexed into, which must be an ArrayToken.
index - The index, which must be an integer token.
Returns:
The element of the given token at the given index.
Throws:
IllegalActionException - If an evaluation error occurs.

_evaluateChild

protected Token _evaluateChild(ASTPtRootNode node,
                               int i)
                        throws IllegalActionException
Evaluate the child with the given index of the given node. This is usually called while visiting the given node.

Throws:
IllegalActionException - If an evaluation error occurs.

_evaluateMatrixIndex

protected Token _evaluateMatrixIndex(ASTPtRootNode node,
                                     Token value,
                                     Token rowIndex,
                                     Token columnIndex)
                              throws IllegalActionException
Evaluate the Matrix index operation represented by the given node.

Parameters:
node - The node that caused this method to be called.
value - The token that is being indexed into, which must be a MatrixToken.
rowIndex - The row index, which must be an integer token.
columnIndex - The column index, which must be an integer token.
Returns:
The element of the given token at the given index.
Throws:
IllegalActionException - If an evaluation error occurs.

_functionCall

protected Token _functionCall(java.lang.String functionName,
                              Type[] argTypes,
                              java.lang.Object[] argValues)
                       throws IllegalActionException
Evaluate the specified function. The function must be defined as one of the registered functions with PtParser.

Parameters:
functionName - The function name.
argTypes - An array of argument types.
argValues - An array of argument values.
Throws:
IllegalActionException - If an evaluation error occurs.

_methodCall

protected Token _methodCall(java.lang.String methodName,
                            Type[] argTypes,
                            java.lang.Object[] argValues)
                     throws IllegalActionException
Evaluate the specified method. The object on which the method is evaluated should be the first argument.

Parameters:
methodName - The method name.
argTypes - An array of argument types.
argValues - An array of argument values.
Throws:
IllegalActionException - If an evaluation error occurs.

_trace

protected void _trace(java.lang.String string)
Add a record to the current trace corresponding to the given message. If the trace is null, do nothing.


_traceEnter

protected void _traceEnter(ASTPtRootNode node)
Add a record to the current trace corresponding to the start of the evaluation of the given node. If the trace is null, then do nothing.


_traceLeave

protected void _traceLeave(ASTPtRootNode node)
Add a record to the current trace corresponding to the completion of the evaluation of the given node. If the trace is null, then do nothing.


_invokeMethod

private Token _invokeMethod(java.lang.Class<?> clazz,
                            java.lang.Object object,
                            java.lang.String methodName,
                            Type[] argTypes,
                            java.lang.Object[] argValues)
                     throws IllegalActionException
Invoke a method of the class for the given object, or retrieve a field of it.

Throws:
IllegalActionException