public class ProceduralParseTreeCodeGenerator extends AbstractParseTreeVisitor implements ParseTreeCodeGenerator
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:
ASTPtRootNode
Red |
Red |
Modifier and Type | Field and Description |
---|---|
protected java.lang.String |
_childCode
The fire() method code.
|
protected int |
_depth
The depth, used for debugging and indenting.
|
protected Token |
_evaluatedChildToken
Temporary storage for the result of evaluating a child node.
|
protected static java.util.Map<java.lang.String,java.lang.String> |
_functionMap
The map of functions.
|
protected ProgramCodeGenerator |
_generator
The code generator.
|
protected java.lang.StringBuffer |
_initializeCode
The initialize() method code.
|
protected java.lang.StringBuffer |
_preinitializeCode
The preinitialize() method code.
|
protected ParserScope |
_scope
The scope for evaluation.
|
protected java.lang.StringBuffer |
_sharedCode
Shared code code.
|
protected java.lang.StringBuffer |
_trace
Used for debugging.
|
protected ParseTreeTypeInference |
_typeInference
Used for type checking.
|
protected java.lang.StringBuffer |
_wrapupCode
The wrapup() method code.
|
Constructor and Description |
---|
ProceduralParseTreeCodeGenerator(ProgramCodeGenerator generator)
Create a ProceduralParseTreeCodeGenerator that is used by
the given code generator to generate code for expressions.
|
Modifier and Type | Method and 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.String |
escapeForTargetLanguage(java.lang.String string)
Given a string, escape special characters as necessary.
|
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 |
generateFireCode()
Generate code that corresponds with the fire() method.
|
java.lang.String |
generateInitializeCode()
Generate code that corresponds with the initialize() method.
|
java.lang.String |
generatePreinitializeCode()
Generate code that corresponds with the preinitialize() method.
|
java.lang.String |
generateSharedCode()
Generate shared code.
|
java.lang.String |
generateWrapupCode()
Generate code that corresponds with the wrapup() method.
|
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.
|
_unsupportedVisitException, _visitAllChildren, _visitChild, visitAssignmentNode, visitUnionConstructNode
protected Token _evaluatedChildToken
protected java.lang.String _childCode
protected ProgramCodeGenerator _generator
protected java.lang.StringBuffer _initializeCode
protected java.lang.StringBuffer _preinitializeCode
protected java.lang.StringBuffer _sharedCode
protected java.lang.StringBuffer _wrapupCode
protected ParserScope _scope
protected ParseTreeTypeInference _typeInference
protected java.lang.StringBuffer _trace
protected int _depth
protected static java.util.Map<java.lang.String,java.lang.String> _functionMap
public ProceduralParseTreeCodeGenerator(ProgramCodeGenerator generator)
generator
- The given code generator.public Token evaluateParseTree(ASTPtRootNode node) throws IllegalActionException
node
- The root of the parse tree.IllegalActionException
- If an parse error occurs.public Token evaluateParseTree(ASTPtRootNode node, ParserScope scope) throws IllegalActionException
evaluateParseTree
in interface ParseTreeCodeGenerator
node
- The root of the parse tree.scope
- The scope for evaluation.IllegalActionException
- If an error occurs during
evaluation.public java.lang.String traceParseTreeEvaluation(ASTPtRootNode node, ParserScope scope) throws IllegalActionException
traceParseTreeEvaluation
in interface ParseTreeCodeGenerator
node
- The root of the parse tree.scope
- The scope for evaluation.IllegalActionException
- If an error occurs during
evaluation.public java.lang.String generateFireCode()
generateFireCode
in interface ParseTreeCodeGenerator
public java.lang.String generateInitializeCode()
public java.lang.String generatePreinitializeCode()
public java.lang.String generateSharedCode()
public java.lang.String generateWrapupCode()
public java.lang.String escapeForTargetLanguage(java.lang.String string)
\\ becomes \\\\ which means: \{ becomes \\{ \} becomes \\} \( becomes \\( \) becomes \\) and \\" becomes \" newline becomes \n
escapeForTargetLanguage
in interface ParseTreeCodeGenerator
string
- The string to escape.StringUtilities.escapeForXML(String)
public void visitArrayConstructNode(ASTPtArrayConstructNode node) throws IllegalActionException
visitArrayConstructNode
in interface ParseTreeVisitor
visitArrayConstructNode
in class AbstractParseTreeVisitor
node
- The specified node.IllegalActionException
- If an parse error occurs.public void visitBitwiseNode(ASTPtBitwiseNode node) throws IllegalActionException
visitBitwiseNode
in interface ParseTreeVisitor
visitBitwiseNode
in class AbstractParseTreeVisitor
node
- The specified node.IllegalActionException
- If an parse error occurs.public void visitFunctionApplicationNode(ASTPtFunctionApplicationNode node) throws IllegalActionException
MatlabUtilities.evaluate(String, Set, ParserScope)
.visitFunctionApplicationNode
in interface ParseTreeVisitor
visitFunctionApplicationNode
in class AbstractParseTreeVisitor
node
- The specified node.IllegalActionException
- If an parse error occurs.public void visitFunctionDefinitionNode(ASTPtFunctionDefinitionNode node) throws IllegalActionException
visitFunctionDefinitionNode
in interface ParseTreeVisitor
visitFunctionDefinitionNode
in class AbstractParseTreeVisitor
node
- The specified node.IllegalActionException
- If an parse error occurs.public void visitFunctionalIfNode(ASTPtFunctionalIfNode node) throws IllegalActionException
visitFunctionalIfNode
in interface ParseTreeVisitor
visitFunctionalIfNode
in class AbstractParseTreeVisitor
node
- The specified node.IllegalActionException
- If an parse error occurs.public void visitLeafNode(ASTPtLeafNode node) throws IllegalActionException
visitLeafNode
in interface ParseTreeVisitor
visitLeafNode
in class AbstractParseTreeVisitor
node
- The specified node.IllegalActionException
- If an parse error occurs.public void visitLogicalNode(ASTPtLogicalNode node) throws IllegalActionException
visitLogicalNode
in interface ParseTreeVisitor
visitLogicalNode
in class AbstractParseTreeVisitor
node
- The specified node.IllegalActionException
- If an parse error occurs.public void visitMatrixConstructNode(ASTPtMatrixConstructNode node) throws IllegalActionException
visitMatrixConstructNode
in interface ParseTreeVisitor
visitMatrixConstructNode
in class AbstractParseTreeVisitor
node
- The specified node.IllegalActionException
- If an parse error occurs.public void visitMethodCallNode(ASTPtMethodCallNode node) throws IllegalActionException
visitMethodCallNode
in interface ParseTreeVisitor
visitMethodCallNode
in class AbstractParseTreeVisitor
node
- The specified node.IllegalActionException
- If an parse error occurs.public void visitPowerNode(ASTPtPowerNode node) throws IllegalActionException
visitPowerNode
in interface ParseTreeVisitor
visitPowerNode
in class AbstractParseTreeVisitor
node
- The specified node.IllegalActionException
- If an parse error occurs.public void visitProductNode(ASTPtProductNode node) throws IllegalActionException
visitProductNode
in interface ParseTreeVisitor
visitProductNode
in class AbstractParseTreeVisitor
node
- The specified node.IllegalActionException
- If an parse error occurs.public void visitRecordConstructNode(ASTPtRecordConstructNode node) throws IllegalActionException
visitRecordConstructNode
in interface ParseTreeVisitor
visitRecordConstructNode
in class AbstractParseTreeVisitor
node
- The record constructor node.IllegalActionException
- If an parse error occurs.public void visitRelationalNode(ASTPtRelationalNode node) throws IllegalActionException
visitRelationalNode
in interface ParseTreeVisitor
visitRelationalNode
in class AbstractParseTreeVisitor
IllegalActionException
public void visitShiftNode(ASTPtShiftNode node) throws IllegalActionException
visitShiftNode
in interface ParseTreeVisitor
visitShiftNode
in class AbstractParseTreeVisitor
node
- The specified node.IllegalActionException
- If an parse error occurs.public void visitSumNode(ASTPtSumNode node) throws IllegalActionException
visitSumNode
in interface ParseTreeVisitor
visitSumNode
in class AbstractParseTreeVisitor
node
- The specified node.IllegalActionException
- If an parse error occurs.public void visitUnaryNode(ASTPtUnaryNode node) throws IllegalActionException
visitUnaryNode
in interface ParseTreeVisitor
visitUnaryNode
in class AbstractParseTreeVisitor
node
- The specified node.IllegalActionException
- If an parse error occurs.protected void _assert(boolean flag, ASTPtRootNode node, java.lang.String message)
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.InternalErrorException
- If the assertion is violated.
Note that this is a runtime exception, so it need not be declared
explicitly.protected java.lang.String _codeGenType(Type ptType)
ptType
- The given Ptolemy type.protected Token[] _evaluateAllChildren(ASTPtRootNode node) throws IllegalActionException
node
- The node whose children are evaluated.IllegalActionException
- If an parse error occurs.protected void _evaluateArrayIndex(ASTPtRootNode node, Token value, Type type) throws IllegalActionException
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.IllegalActionException
- If an parse error occurs.protected Token _evaluateChild(ASTPtRootNode node, int i) throws IllegalActionException
node
- The given node.i
- The given index.IllegalActionException
- If an parse error occurs.protected java.lang.String _powCall(java.lang.String x, java.lang.String y)
x
- The first argument for pow().y
- The second argument for pow().protected java.lang.String _specializeArgument(java.lang.String function, int argumentIndex, Type argumentType, java.lang.String argumentCode)
function
- The functionargumentIndex
- The index of the argument to be specializedargumentType
- The type of the the argument.argumentCode
- The code for the argument.protected java.lang.String _specializeReturnValue(java.lang.String function, Type returnType, java.lang.String returnCode)
function
- The functionreturnType
- The return type of the functionreturnCode
- 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.protected void _trace(java.lang.String string)
string
- The given message.protected void _traceEnter(ASTPtRootNode node)
node
- The given node.protected void _traceLeave(ASTPtRootNode node)
node
- The given node.