Class ProceduralParseTreeCodeGenerator
- java.lang.Object
- 
- ptolemy.data.expr.AbstractParseTreeVisitor
- 
- ptolemy.cg.kernel.generic.program.procedural.ProceduralParseTreeCodeGenerator
 
 
- 
- All Implemented Interfaces:
- ParseTreeCodeGenerator,- ParseTreeVisitor
 - Direct Known Subclasses:
- CParseTreeCodeGenerator,- JavaParseTreeCodeGenerator
 
 public class ProceduralParseTreeCodeGenerator extends AbstractParseTreeVisitor implements ParseTreeCodeGenerator A base class visitor for parse trees of the expression language.A derived class would Evaluate a parse tree given a reference to its root node and generate C or Java code. 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. This class has the following limitations: - It is a copy of ParseTreeEvaluator from data/expr and thus has lots of code for evaluating expressions, which we don't need
- It is not properly converting types: We need to add logic to convert types.
- The .tcl test has known failures involving nulls.
- It does not evaluate constants.
 - Since:
- Ptolemy II 10.0
- Version:
- $Id$
- Author:
- Man-Kit Leung
- See Also:
- ASTPtRootNode
- Pt.AcceptedRating:
- Red
- Pt.ProposedRating:
- Red
 
- 
- 
Field SummaryFields Modifier and Type Field Description protected java.lang.String_childCodeThe fire() method code.protected int_depthThe depth, used for debugging and indenting.protected Token_evaluatedChildTokenTemporary storage for the result of evaluating a child node.protected static java.util.Map<java.lang.String,java.lang.String>_functionMapThe map of functions.protected ProgramCodeGenerator_generatorThe code generator.protected java.lang.StringBuffer_initializeCodeThe initialize() method code.protected java.lang.StringBuffer_preinitializeCodeThe preinitialize() method code.protected ParserScope_scopeThe scope for evaluation.protected java.lang.StringBuffer_sharedCodeShared code code.protected java.lang.StringBuffer_traceUsed for debugging.protected ParseTreeTypeInference_typeInferenceUsed for type checking.protected java.lang.StringBuffer_wrapupCodeThe wrapup() method code.
 - 
Constructor SummaryConstructors Constructor Description ProceduralParseTreeCodeGenerator(ProgramCodeGenerator generator)Create a ProceduralParseTreeCodeGenerator that is used by the given code generator to generate code for expressions.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description 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 java.lang.String_codeGenType(Type ptType)Get the corresponding type in code generation from the given Ptolemy type.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 void_evaluateArrayIndex(ASTPtRootNode node, Token value, Type type)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 java.lang.String_powCall(java.lang.String x, java.lang.String y)Return the string for the the pow() call.protected java.lang.String_specializeArgument(java.lang.String function, int argumentIndex, Type argumentType, java.lang.String argumentCode)Specialize an argument of a function.protected java.lang.String_specializeReturnValue(java.lang.String function, Type returnType, java.lang.String returnCode)Specialize the return value of a function.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.java.lang.StringescapeForTargetLanguage(java.lang.String string)Given a string, escape special characters as necessary.TokenevaluateParseTree(ASTPtRootNode node)Evaluate the parse tree with the specified root node.TokenevaluateParseTree(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.StringgenerateFireCode()Generate code that corresponds with the fire() method.java.lang.StringgenerateInitializeCode()Generate code that corresponds with the initialize() method.java.lang.StringgeneratePreinitializeCode()Generate code that corresponds with the preinitialize() method.java.lang.StringgenerateSharedCode()Generate shared code.java.lang.StringgenerateWrapupCode()Generate code that corresponds with the wrapup() method.java.lang.StringtraceParseTreeEvaluation(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.voidvisitArrayConstructNode(ASTPtArrayConstructNode node)Construct an ArrayToken that contains the tokens from the children of the specified node.voidvisitBitwiseNode(ASTPtBitwiseNode node)Evaluate a bitwise operator on the children of the specified node, where the particular operator is property of the node.voidvisitFunctionalIfNode(ASTPtFunctionalIfNode node)Evaluate the first child, and depending on its (boolean) result, evaluate either the second or the third child.voidvisitFunctionApplicationNode(ASTPtFunctionApplicationNode node)Apply a function to the children of the specified node.voidvisitFunctionDefinitionNode(ASTPtFunctionDefinitionNode node)Define a function, where the children specify the argument types and the expression.voidvisitLeafNode(ASTPtLeafNode node)Evaluate a numeric constant or an identifier.voidvisitLogicalNode(ASTPtLogicalNode node)Evaluate a logical AND or OR on the children of the specified node.voidvisitMatrixConstructNode(ASTPtMatrixConstructNode node)Construct a matrix containing the children nodes.voidvisitMethodCallNode(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.voidvisitPowerNode(ASTPtPowerNode node)Evaluate the power operator on the children of the specified node.voidvisitProductNode(ASTPtProductNode node)Multiply the children of the specified node.voidvisitRecordConstructNode(ASTPtRecordConstructNode node)Construct a record by assigning the fields values given by the children nodes.voidvisitRelationalNode(ASTPtRelationalNode node)voidvisitShiftNode(ASTPtShiftNode node)Apply a shift operator to the children of the specified node.voidvisitSumNode(ASTPtSumNode node)Apply a sum operator to the children of the specified node.voidvisitUnaryNode(ASTPtUnaryNode node)Apply a unary operator to the single child of the specified node.- 
Methods inherited from class ptolemy.data.expr.AbstractParseTreeVisitor_unsupportedVisitException, _visitAllChildren, _visitChild, visitAssignmentNode, visitUnionConstructNode
 
- 
 
- 
- 
- 
Field Detail- 
_evaluatedChildTokenprotected Token _evaluatedChildToken Temporary storage for the result of evaluating a child node. This is protected so that derived classes can access it.
 - 
_childCodeprotected java.lang.String _childCode The fire() method code.
 - 
_generatorprotected ProgramCodeGenerator _generator The code generator.
 - 
_initializeCodeprotected java.lang.StringBuffer _initializeCode The initialize() method code.
 - 
_preinitializeCodeprotected java.lang.StringBuffer _preinitializeCode The preinitialize() method code.
 - 
_sharedCodeprotected java.lang.StringBuffer _sharedCode Shared code code.
 - 
_wrapupCodeprotected java.lang.StringBuffer _wrapupCode The wrapup() method code.
 - 
_scopeprotected ParserScope _scope The scope for evaluation.
 - 
_typeInferenceprotected ParseTreeTypeInference _typeInference Used for type checking.
 - 
_traceprotected java.lang.StringBuffer _trace Used for debugging.
 - 
_depthprotected int _depth The depth, used for debugging and indenting.
 - 
_functionMapprotected static java.util.Map<java.lang.String,java.lang.String> _functionMap The map of functions.
 
- 
 - 
Constructor Detail- 
ProceduralParseTreeCodeGeneratorpublic ProceduralParseTreeCodeGenerator(ProgramCodeGenerator generator) Create a ProceduralParseTreeCodeGenerator that is used by the given code generator to generate code for expressions.- Parameters:
- generator- The given code generator.
 
 
- 
 - 
Method Detail- 
evaluateParseTreepublic 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 parse error occurs.
 
 - 
evaluateParseTreepublic 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.- Specified by:
- evaluateParseTreein interface- ParseTreeCodeGenerator
- 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.
 
 - 
traceParseTreeEvaluationpublic 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.- Specified by:
- traceParseTreeEvaluationin interface- ParseTreeCodeGenerator
- 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.
 
 - 
generateFireCodepublic java.lang.String generateFireCode() Generate code that corresponds with the fire() method.- Specified by:
- generateFireCodein interface- ParseTreeCodeGenerator
- Returns:
- The generated code.
 
 - 
generateInitializeCodepublic java.lang.String generateInitializeCode() Generate code that corresponds with the initialize() method.- Returns:
- The generated code.
 
 - 
generatePreinitializeCodepublic java.lang.String generatePreinitializeCode() Generate code that corresponds with the preinitialize() method.- Returns:
- The generated code.
 
 - 
generateSharedCodepublic java.lang.String generateSharedCode() Generate shared code.- Returns:
- The generated code.
 
 - 
generateWrapupCodepublic java.lang.String generateWrapupCode() Generate code that corresponds with the wrapup() method.- Returns:
- The generated code.
 
 - 
escapeForTargetLanguagepublic java.lang.String escapeForTargetLanguage(java.lang.String string) Given a string, escape special characters as necessary. For C and Java, we do:\\ becomes \\\\ which means: \{ becomes \\{ \} becomes \\} \( becomes \\( \) becomes \\) and \\" becomes \" newline becomes \n- Specified by:
- escapeForTargetLanguagein interface- ParseTreeCodeGenerator
- Parameters:
- string- The string to escape.
- Returns:
- A new string with special characters replaced.
- See Also:
- StringUtilities.escapeForXML(String)
 
 - 
visitArrayConstructNodepublic void visitArrayConstructNode(ASTPtArrayConstructNode node) throws IllegalActionException Construct an ArrayToken that contains the tokens from the children of the specified node.- Specified by:
- visitArrayConstructNodein interface- ParseTreeVisitor
- Overrides:
- visitArrayConstructNodein class- AbstractParseTreeVisitor
- Parameters:
- node- The specified node.
- Throws:
- IllegalActionException- If an parse error occurs.
 
 - 
visitBitwiseNodepublic 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:
- visitBitwiseNodein interface- ParseTreeVisitor
- Overrides:
- visitBitwiseNodein class- AbstractParseTreeVisitor
- Parameters:
- node- The specified node.
- Throws:
- IllegalActionException- If an parse error occurs.
 
 - 
visitFunctionApplicationNodepublic 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 inMatlabUtilities.evaluate(String, Set, ParserScope).- Specified by:
- visitFunctionApplicationNodein interface- ParseTreeVisitor
- Overrides:
- visitFunctionApplicationNodein class- AbstractParseTreeVisitor
- Parameters:
- node- The specified node.
- Throws:
- IllegalActionException- If an parse error occurs.
 
 - 
visitFunctionDefinitionNodepublic 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:
- visitFunctionDefinitionNodein interface- ParseTreeVisitor
- Overrides:
- visitFunctionDefinitionNodein class- AbstractParseTreeVisitor
- Parameters:
- node- The specified node.
- Throws:
- IllegalActionException- If an parse error occurs.
 
 - 
visitFunctionalIfNodepublic 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:
- visitFunctionalIfNodein interface- ParseTreeVisitor
- Overrides:
- visitFunctionalIfNodein class- AbstractParseTreeVisitor
- Parameters:
- node- The specified node.
- Throws:
- IllegalActionException- If an parse error occurs.
 
 - 
visitLeafNodepublic 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:
- visitLeafNodein interface- ParseTreeVisitor
- Overrides:
- visitLeafNodein class- AbstractParseTreeVisitor
- Parameters:
- node- The specified node.
- Throws:
- IllegalActionException- If an parse error occurs.
 
 - 
visitLogicalNodepublic void visitLogicalNode(ASTPtLogicalNode node) throws IllegalActionException Evaluate a logical AND or OR on the children of the specified node.- Specified by:
- visitLogicalNodein interface- ParseTreeVisitor
- Overrides:
- visitLogicalNodein class- AbstractParseTreeVisitor
- Parameters:
- node- The specified node.
- Throws:
- IllegalActionException- If an parse error occurs.
 
 - 
visitMatrixConstructNodepublic void visitMatrixConstructNode(ASTPtMatrixConstructNode node) throws IllegalActionException Construct a matrix containing the children nodes. The specified node ends up with a MatrixToken value.- Specified by:
- visitMatrixConstructNodein interface- ParseTreeVisitor
- Overrides:
- visitMatrixConstructNodein class- AbstractParseTreeVisitor
- Parameters:
- node- The specified node.
- Throws:
- IllegalActionException- If an parse error occurs.
 
 - 
visitMethodCallNodepublic 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:
- visitMethodCallNodein interface- ParseTreeVisitor
- Overrides:
- visitMethodCallNodein class- AbstractParseTreeVisitor
- Parameters:
- node- The specified node.
- Throws:
- IllegalActionException- If an parse error occurs.
 
 - 
visitPowerNodepublic void visitPowerNode(ASTPtPowerNode node) throws IllegalActionException Evaluate the power operator on the children of the specified node.- Specified by:
- visitPowerNodein interface- ParseTreeVisitor
- Overrides:
- visitPowerNodein class- AbstractParseTreeVisitor
- Parameters:
- node- The specified node.
- Throws:
- IllegalActionException- If an parse error occurs.
 
 - 
visitProductNodepublic void visitProductNode(ASTPtProductNode node) throws IllegalActionException Multiply the children of the specified node.- Specified by:
- visitProductNodein interface- ParseTreeVisitor
- Overrides:
- visitProductNodein class- AbstractParseTreeVisitor
- Parameters:
- node- The specified node.
- Throws:
- IllegalActionException- If an parse error occurs.
 
 - 
visitRecordConstructNodepublic void visitRecordConstructNode(ASTPtRecordConstructNode node) throws IllegalActionException Construct a record by assigning the fields values given by the children nodes.- Specified by:
- visitRecordConstructNodein interface- ParseTreeVisitor
- Overrides:
- visitRecordConstructNodein class- AbstractParseTreeVisitor
- Parameters:
- node- The record constructor node.
- Throws:
- IllegalActionException- If an parse error occurs.
 
 - 
visitRelationalNodepublic void visitRelationalNode(ASTPtRelationalNode node) throws IllegalActionException - Specified by:
- visitRelationalNodein interface- ParseTreeVisitor
- Overrides:
- visitRelationalNodein class- AbstractParseTreeVisitor
- Throws:
- IllegalActionException
 
 - 
visitShiftNodepublic void visitShiftNode(ASTPtShiftNode node) throws IllegalActionException Apply a shift operator to the children of the specified node.- Specified by:
- visitShiftNodein interface- ParseTreeVisitor
- Overrides:
- visitShiftNodein class- AbstractParseTreeVisitor
- Parameters:
- node- The specified node.
- Throws:
- IllegalActionException- If an parse error occurs.
 
 - 
visitSumNodepublic void visitSumNode(ASTPtSumNode node) throws IllegalActionException Apply a sum operator to the children of the specified node.- Specified by:
- visitSumNodein interface- ParseTreeVisitor
- Overrides:
- visitSumNodein class- AbstractParseTreeVisitor
- Parameters:
- node- The specified node.
- Throws:
- IllegalActionException- If an parse error occurs.
 
 - 
visitUnaryNodepublic void visitUnaryNode(ASTPtUnaryNode node) throws IllegalActionException Apply a unary operator to the single child of the specified node.- Specified by:
- visitUnaryNodein interface- ParseTreeVisitor
- Overrides:
- visitUnaryNodein class- AbstractParseTreeVisitor
- Parameters:
- node- The specified node.
- Throws:
- IllegalActionException- If an parse error occurs.
 
 - 
_assertprotected 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.
 
 - 
_codeGenTypeprotected java.lang.String _codeGenType(Type ptType) Get the corresponding type in code generation from the given Ptolemy type.- Parameters:
- ptType- The given Ptolemy type.
- Returns:
- The code generation type.
 
 - 
_evaluateAllChildrenprotected 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.
- Returns:
- The array of resulting tokens.
- Throws:
- IllegalActionException- If an parse error occurs.
 
 - 
_evaluateArrayIndexprotected void _evaluateArrayIndex(ASTPtRootNode node, Token value, Type type) 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.
- type- The element type.
- Throws:
- IllegalActionException- If an parse error occurs.
 
 - 
_evaluateChildprotected 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.- Parameters:
- node- The given node.
- i- The given index.
- Returns:
- The resulting token.
- Throws:
- IllegalActionException- If an parse error occurs.
 
 - 
_powCallprotected java.lang.String _powCall(java.lang.String x, java.lang.String y)Return the string for the the pow() call.- Parameters:
- x- The first argument for pow().
- y- The second argument for pow().
- Returns:
- The string to invoke the pow() function.
 
 - 
_specializeArgumentprotected java.lang.String _specializeArgument(java.lang.String function, int argumentIndex, Type argumentType, java.lang.String argumentCode)Specialize an argument of a function. The function "$arrayRepeat" is handled specially here.- Parameters:
- function- The function
- argumentIndex- The index of the argument to be specialized
- argumentType- The type of the the argument.
- argumentCode- The code for the argument.
- Returns:
- the specialized return value.
 
 - 
_specializeReturnValueprotected java.lang.String _specializeReturnValue(java.lang.String function, Type returnType, java.lang.String returnCode)Specialize the return value of a function. The function "$arraySum" is handled specially here.- Parameters:
- function- The function
- returnType- The return type of the function
- returnCode- If the function is "$arraySum", and the return type is primitive, then the value of the returnCode parameter is returned with the payload and codeGen type returned. Otherwise, just the returnCode is returned.
- Returns:
- the specialized return value.
 
 - 
_traceprotected 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.- Parameters:
- string- The given message.
 
 - 
_traceEnterprotected 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.- Parameters:
- node- The given node.
 
 - 
_traceLeaveprotected 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.- Parameters:
- node- The given node.
 
 
- 
 
-