Class CParseTreeCodeGenerator
- java.lang.Object
-
- ptolemy.data.expr.AbstractParseTreeVisitor
-
- ptolemy.cg.kernel.generic.program.procedural.ProceduralParseTreeCodeGenerator
-
- ptolemy.cg.kernel.generic.program.procedural.c.CParseTreeCodeGenerator
-
- All Implemented Interfaces:
ParseTreeCodeGenerator
,ParseTreeVisitor
public class CParseTreeCodeGenerator extends ProceduralParseTreeCodeGenerator
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.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 Summary
-
Fields inherited from class ptolemy.cg.kernel.generic.program.procedural.ProceduralParseTreeCodeGenerator
_childCode, _depth, _evaluatedChildToken, _functionMap, _generator, _initializeCode, _preinitializeCode, _scope, _sharedCode, _trace, _typeInference, _wrapupCode
-
-
Constructor Summary
Constructors Constructor Description CParseTreeCodeGenerator(ProgramCodeGenerator generator)
Create a CParseTreeCodeGenerator that is used by the given code generator to generate code for expressions.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.lang.String
_codeGenType(Type ptType)
Get the corresponding type in code generation from the given Ptolemy type.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.void
visitArrayConstructNode(ASTPtArrayConstructNode node)
Construct an ArrayToken that contains the tokens from the children of the specified node.void
visitFunctionalIfNode(ASTPtFunctionalIfNode node)
Evaluate the first child, and depending on its (boolean) result, evaluate either the second or the third child.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
visitMatrixConstructNode(ASTPtMatrixConstructNode node)
Construct a matrix containing the children nodes.void
visitProductNode(ASTPtProductNode node)
Multiply the children of the specified node.-
Methods inherited from class ptolemy.cg.kernel.generic.program.procedural.ProceduralParseTreeCodeGenerator
_assert, _evaluateAllChildren, _specializeArgument, _specializeReturnValue, _trace, _traceEnter, _traceLeave, escapeForTargetLanguage, evaluateParseTree, evaluateParseTree, generateFireCode, generateInitializeCode, generatePreinitializeCode, generateSharedCode, generateWrapupCode, traceParseTreeEvaluation, visitBitwiseNode, visitFunctionApplicationNode, visitLogicalNode, visitMethodCallNode, visitPowerNode, visitRecordConstructNode, visitRelationalNode, visitShiftNode, visitSumNode, visitUnaryNode
-
Methods inherited from class ptolemy.data.expr.AbstractParseTreeVisitor
_unsupportedVisitException, _visitAllChildren, _visitChild, visitAssignmentNode, visitUnionConstructNode
-
-
-
-
Constructor Detail
-
CParseTreeCodeGenerator
public CParseTreeCodeGenerator(ProgramCodeGenerator generator)
Create a CParseTreeCodeGenerator that is used by the given code generator to generate code for expressions.- Parameters:
generator
- The given code generator.
-
-
Method Detail
-
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 interfaceParseTreeVisitor
- Overrides:
visitArrayConstructNode
in classProceduralParseTreeCodeGenerator
- Parameters:
node
- The specified node.- Throws:
IllegalActionException
- If an parse 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 interfaceParseTreeVisitor
- Overrides:
visitFunctionDefinitionNode
in classProceduralParseTreeCodeGenerator
- Parameters:
node
- The specified node.- Throws:
IllegalActionException
- If an parse 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 interfaceParseTreeVisitor
- Overrides:
visitFunctionalIfNode
in classProceduralParseTreeCodeGenerator
- Parameters:
node
- The specified node.- Throws:
IllegalActionException
- If an parse 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 interfaceParseTreeVisitor
- Overrides:
visitLeafNode
in classProceduralParseTreeCodeGenerator
- Parameters:
node
- The specified node.- Throws:
IllegalActionException
- If an parse 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 interfaceParseTreeVisitor
- Overrides:
visitMatrixConstructNode
in classProceduralParseTreeCodeGenerator
- Parameters:
node
- The specified node.- Throws:
IllegalActionException
- If an parse error occurs.
-
visitProductNode
public void visitProductNode(ASTPtProductNode node) throws IllegalActionException
Multiply the children of the specified node.- Specified by:
visitProductNode
in interfaceParseTreeVisitor
- Overrides:
visitProductNode
in classProceduralParseTreeCodeGenerator
- Parameters:
node
- The specified node.- Throws:
IllegalActionException
- If an parse error occurs.
-
_codeGenType
protected java.lang.String _codeGenType(Type ptType)
Get the corresponding type in code generation from the given Ptolemy type.- Overrides:
_codeGenType
in classProceduralParseTreeCodeGenerator
- Parameters:
ptType
- The given Ptolemy type.- Returns:
- The code generation type.
-
_evaluateArrayIndex
protected void _evaluateArrayIndex(ASTPtRootNode node, Token value, Type type) throws IllegalActionException
Evaluate the array index operation represented by the given node.- Overrides:
_evaluateArrayIndex
in classProceduralParseTreeCodeGenerator
- 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.
-
_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.- Overrides:
_evaluateChild
in classProceduralParseTreeCodeGenerator
- Parameters:
node
- The given node.i
- The given index.- Returns:
- The resulting token.
- Throws:
IllegalActionException
- If an parse error occurs.
-
_powCall
protected java.lang.String _powCall(java.lang.String x, java.lang.String y)
Return the string for the the pow() call.- Overrides:
_powCall
in classProceduralParseTreeCodeGenerator
- Parameters:
x
- The first argument for pow().y
- The second argument for pow().- Returns:
- The string to invoke the pow() function.
-
-