Package ptolemy.actor.ptalon
Class AbstractPtalonEvaluator
- java.lang.Object
-
- ptolemy.actor.ptalon.AbstractPtalonEvaluator
-
- Direct Known Subclasses:
PtalonEvaluator
public abstract class AbstractPtalonEvaluator extends java.lang.Object
A helper class to store information, like variable scope info, for the Ptalon compiler. This class manages references to all elements created as a result of parsing a Ptalon file, and whether they have been instantiated in Ptolemy.- Since:
- Ptolemy II 6.1
- Version:
- $Id$
- Author:
- Adam Cataldo, Elaine Cheong
- Pt.AcceptedRating:
- Yellow (celaine)
- Pt.ProposedRating:
- Yellow (celaine)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
AbstractPtalonEvaluator.IfTree
This is a representation of an if/else construct in Ptalon.protected class
AbstractPtalonEvaluator.PtalonExpressionScope
FIXME comment
-
Field Summary
Fields Modifier and Type Field Description protected PtalonActor
_actor
The actor in which this PtalonCompilerInfo is used.protected AbstractPtalonEvaluator.IfTree
_currentIfTree
Some descendent of the root tree to which new input symbols should be added.protected java.util.Hashtable<java.lang.String,java.net.URL>
_imports
A list of the import symbols and their corresponding files.protected AbstractPtalonEvaluator.PtalonExpressionScope
_scope
The expression scope for this code manager.protected java.util.Map<java.lang.String,TypedIOPort>
_transparentRelations
Maps names of transparent relations to ports, which should be multiports.
-
Constructor Summary
Constructors Constructor Description AbstractPtalonEvaluator(PtalonActor actor)
Create a new AbstractPtalonEvaluator in the specified PtalonActor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected static java.lang.String
_getIndentPrefix(int level)
Return a number of spaces that is proportional to the argument.protected int
_getTimesEntered()
Return the number of times the current if tree has been entered.protected java.lang.String
_getType(java.lang.String symbol)
Return the type associated with the given symbol in the current scope.protected java.lang.String
_getTypeForScope(java.lang.String symbol)
Return the type associated with the given symbol in the current scope.protected boolean
_inNewWhileIteration()
Return true if in a new iteration of a while block.protected boolean
_isPreservingTransformation()
protected void
_processAttributes(NamedObj object)
protected void
_resetParameters(boolean reset)
Set whether to reset parameters when the actor is populated and the parameters already exist for the actor.protected void
_setPreservingTransformation(boolean b)
void
addActorParameter(java.lang.String name)
Add a PtalonParameter to the PtalonActor with the specified name.void
addActorParameter(java.lang.String name, java.lang.String expression)
Add an invisible PtalonParameter to the PtalonActor with the specified name.void
addInPort(java.lang.String name)
Add a TypedIOPort to the PtalonActor with the specified name, and input flow type.void
addOutPort(java.lang.String name)
Add a TypedIOPort to the PtalonActor with the specified name, and output flow type.void
addParameter(java.lang.String name)
Add a Parameter to the PtalonActor with the specified name.void
addParameter(java.lang.String name, java.lang.String expression)
Add an invisible Parameter to the PtalonActor with the specified name and the given expression as its value.void
addPort(java.lang.String name)
Add a TypedIOPort to the PtalonActor with the specified name.void
addRelation(java.lang.String name)
Add a TypedIORelation to the PtalonActor with the specified name.void
addSymbol(java.lang.String name, java.lang.String type)
Add a symbol with the given name and type to the symbol table at the current level of the if-tree hierarchy.void
addTransparentRelation(java.lang.String name)
Add a transparent relation to the PtalonActor with the specified name.void
assignInternalParameters()
Assign any internal parameters in the order they were set.void
enterForScope(java.lang.String scope, PtalonAST forBlock, PtalonPopulator populator)
Enter the named for-block subscope.void
enterIfScope(java.lang.String scope)
Enter the named subscope.void
enterTransformation(boolean incremental)
boolean
evaluateBoolean(java.lang.String expression)
Evaluate the given expression and return its boolean value.Token
evaluateExpression(java.lang.String expression)
Evaluate the given expression and return the corresponding token.void
evaluateForScope()
Evaluate the current for block, assuming there is one.java.lang.String
evaluateString(java.lang.String expression)
Evaluate the given input expression and return a string representation of it, or null, if there is some reason it cannot be evaluated.void
exitForScope()
Exit the current for scope.void
exitIfScope()
Exit the current if scope.void
exitTransformation()
java.lang.String
getMappedName(java.lang.String symbol)
Get the unique name for the symbol in the PtalonActor.boolean
hasUnassignedParameters()
Return whether there are unassigned parameters to be handled with theassignInternalParameters()
method.boolean
inScope(java.lang.String symbol)
Return true if the given symbol exists in the current scope.boolean
isCreated(java.lang.String symbol)
Return true if an entity was created in PtalonActor for the given symbol.boolean
isForReady()
Return true if the boolean for the current conditional is ready to be entered.boolean
isIfReady()
Return true if the boolean for the current conditional is ready to be entered.boolean
isReady()
Return true if the current piece of code is ready to be entered.void
negateObject(java.lang.String name)
void
optionalObject(java.lang.String name)
java.lang.String
popForStatement()
Pop out of the scope of the current for statement and into its container block's scope.java.lang.String
popIfStatement()
Pop out of the scope of the current if statement and into its container block's scope.void
preserveObject(java.lang.String name)
void
pushForStatement(java.lang.String variable, java.lang.String initExpr, java.lang.String satExpr)
Push into the scope of a new for statement contained as a sub-block of the current (FIXME: if or for) statement.void
pushIfStatement()
Push into the scope of a new if statement contained as a sub-block of the current if statement.void
removeObject(java.lang.String name)
void
setActiveBranch(boolean branch)
Set the active branch for the current if statement.void
setActorSymbol(java.lang.String symbol)
Set the symbol in the PtalonCode which represents this AbstractPtalonEvaluator's actor.void
setCurrentBranch(boolean branch)
Set the current branch that's being walked.void
setNextExpression(java.lang.String nextExpr)
Set the next expression for the current for statement scope, assuming the current scope is a for statement and not an if statement.void
startAtTop()
Prepare the compiler to start at the outermost scope of the Ptalon program during run time.
-
-
-
Field Detail
-
_actor
protected PtalonActor _actor
The actor in which this PtalonCompilerInfo is used.
-
_currentIfTree
protected AbstractPtalonEvaluator.IfTree _currentIfTree
Some descendent of the root tree to which new input symbols should be added.
-
_imports
protected java.util.Hashtable<java.lang.String,java.net.URL> _imports
A list of the import symbols and their corresponding files.
-
_scope
protected AbstractPtalonEvaluator.PtalonExpressionScope _scope
The expression scope for this code manager.
-
_transparentRelations
protected java.util.Map<java.lang.String,TypedIOPort> _transparentRelations
Maps names of transparent relations to ports, which should be multiports. A key may map to null if no port has been assigned to it.
-
-
Constructor Detail
-
AbstractPtalonEvaluator
public AbstractPtalonEvaluator(PtalonActor actor)
Create a new AbstractPtalonEvaluator in the specified PtalonActor.- Parameters:
actor
- The actor to manage the code for.
-
-
Method Detail
-
addActorParameter
public void addActorParameter(java.lang.String name) throws PtalonRuntimeException
Add a PtalonParameter to the PtalonActor with the specified name.- Parameters:
name
- The name of the parameter.- Throws:
PtalonRuntimeException
- If the symbol does not exist, or if the symbol already has a parameter associated with it, or if an IllegalActionException is thrown trying to create the parameter.
-
addActorParameter
public void addActorParameter(java.lang.String name, java.lang.String expression) throws PtalonRuntimeException
Add an invisible PtalonParameter to the PtalonActor with the specified name.- Parameters:
name
- The name of the parameter.expression
- The expression representing the parameter.- Throws:
PtalonRuntimeException
- If the symbol does not exist, or if the symbol already has a parameter associated with it, or if an IllegalActionException is thrown trying to create the parameter.
-
addInPort
public void addInPort(java.lang.String name) throws PtalonRuntimeException
Add a TypedIOPort to the PtalonActor with the specified name, and input flow type.- Parameters:
name
- The name of the port.- Throws:
PtalonRuntimeException
- If the symbol does not exist, or if the symbol already has a port associated with it, or if an IllegalActionException is thrown trying to create the port.
-
addOutPort
public void addOutPort(java.lang.String name) throws PtalonRuntimeException
Add a TypedIOPort to the PtalonActor with the specified name, and output flow type.- Parameters:
name
- The name of the port.- Throws:
PtalonRuntimeException
- If the symbol does not exist, or if the symbol already has a port associated with it, or if an IllegalActionException is thrown trying to create the port.
-
addParameter
public void addParameter(java.lang.String name) throws PtalonRuntimeException
Add a Parameter to the PtalonActor with the specified name.- Parameters:
name
- The name of the parameter.- Throws:
PtalonRuntimeException
- If the symbol does not exist, or if the symbol already has a parameter associated with it, or if an IllegalActionException is thrown trying to create the parameter.
-
addParameter
public void addParameter(java.lang.String name, java.lang.String expression) throws PtalonRuntimeException
Add an invisible Parameter to the PtalonActor with the specified name and the given expression as its value.- Parameters:
name
- The name of the parameter.expression
- The expression representing the parameter.- Throws:
PtalonRuntimeException
- If the symbol does not exist, or if the symbol already has a parameter associated with it, or if an IllegalActionException is thrown trying to create the parameter.
-
addPort
public void addPort(java.lang.String name) throws PtalonRuntimeException
Add a TypedIOPort to the PtalonActor with the specified name.- Parameters:
name
- The name of the port.- Throws:
PtalonRuntimeException
- If the symbol does not exist, or if the symbol already has a port associated with it, or if an IllegalActionException is thrown trying to create the port.
-
addRelation
public void addRelation(java.lang.String name) throws PtalonRuntimeException
Add a TypedIORelation to the PtalonActor with the specified name.- Parameters:
name
- The name of the relation.- Throws:
PtalonRuntimeException
- If the symbol does not exist, or if the symbol already has a relation associated with it, or if an IllegalActionException is thrown trying to create the relation.
-
addSymbol
public void addSymbol(java.lang.String name, java.lang.String type) throws PtalonScopeException
Add a symbol with the given name and type to the symbol table at the current level of the if-tree hierarchy.- Parameters:
name
- The symbol name.type
- The symbol type.- Throws:
PtalonScopeException
- If a symbol with this name has already been added somewhere in the current scope.
-
addTransparentRelation
public void addTransparentRelation(java.lang.String name) throws PtalonRuntimeException
Add a transparent relation to the PtalonActor with the specified name. A transparent relation is not the same as a relation. Rather, it provides a means for connecting multiple ports to an input port. It is transparent in that if this transparent relation is connected to an input port I, then any connections made to the transparent relation will be as if they were connected directly to I, instead of through a relation.- Parameters:
name
- The name of the relation.- Throws:
PtalonRuntimeException
- If in a new for iteration but no containing for block is found.
-
assignInternalParameters
public void assignInternalParameters() throws PtalonRuntimeException
Assign any internal parameters in the order they were set.- Throws:
PtalonRuntimeException
- If there is any trouble assigning parameter values.
-
enterForScope
public void enterForScope(java.lang.String scope, PtalonAST forBlock, PtalonPopulator populator) throws PtalonRuntimeException
Enter the named for-block subscope.- Parameters:
scope
- The named subscope.forBlock
- The AST for the subscope.populator
- The PtalonPopulator that called this statement.- Throws:
PtalonRuntimeException
- If the subscope does not exist.
-
enterIfScope
public void enterIfScope(java.lang.String scope) throws PtalonRuntimeException
Enter the named subscope.- Parameters:
scope
- The named subscope.- Throws:
PtalonRuntimeException
- If the subscope does not exist.
-
enterTransformation
public void enterTransformation(boolean incremental) throws PtalonRuntimeException
- Throws:
PtalonRuntimeException
-
evaluateBoolean
public boolean evaluateBoolean(java.lang.String expression) throws PtalonRuntimeException
Evaluate the given expression and return its boolean value. The expression should return a boolean value, otherwise an exception is thrown.- Parameters:
expression
- The expression to evaluate.- Returns:
- The boolean result of evaluation.
- Throws:
PtalonRuntimeException
- If the result is not a boolean.
-
evaluateExpression
public Token evaluateExpression(java.lang.String expression) throws PtalonRuntimeException
Evaluate the given expression and return the corresponding token.- Parameters:
expression
- The expression to evaluate.- Returns:
- The result of evaluation.
- Throws:
PtalonRuntimeException
- If unable to evaluate the expression.
-
evaluateForScope
public void evaluateForScope() throws PtalonRuntimeException
Evaluate the current for block, assuming there is one.- Throws:
PtalonRuntimeException
- If there is any trouble evaluating this for block.
-
evaluateString
public java.lang.String evaluateString(java.lang.String expression)
Evaluate the given input expression and return a string representation of it, or null, if there is some reason it cannot be evaluated.- Parameters:
expression
- The input expression.- Returns:
- The evaluated value, or null if evaluation is not possible.
-
exitForScope
public void exitForScope() throws PtalonRuntimeException
Exit the current for scope.- Throws:
PtalonRuntimeException
- If not in a for-block scope.
-
exitIfScope
public void exitIfScope() throws PtalonRuntimeException
Exit the current if scope.- Throws:
PtalonRuntimeException
- If already at the top-level if scope.
-
exitTransformation
public void exitTransformation() throws PtalonRuntimeException
- Throws:
PtalonRuntimeException
-
getMappedName
public java.lang.String getMappedName(java.lang.String symbol) throws PtalonRuntimeException
Get the unique name for the symbol in the PtalonActor.- Parameters:
symbol
- The symbol to test.- Returns:
- The unique name.
- Throws:
PtalonRuntimeException
- If no such symbol exists.
-
hasUnassignedParameters
public boolean hasUnassignedParameters()
Return whether there are unassigned parameters to be handled with theassignInternalParameters()
method.- Returns:
- true if there are unassigned parameters, and false otherwise.
-
inScope
public boolean inScope(java.lang.String symbol)
Return true if the given symbol exists in the current scope.- Parameters:
symbol
- The symbol to test.- Returns:
- true If the given symbol exists in the current scope.
-
isCreated
public boolean isCreated(java.lang.String symbol) throws PtalonRuntimeException
Return true if an entity was created in PtalonActor for the given symbol. This symbol is assumed to be in the current scope.- Parameters:
symbol
- The symbol to test.- Returns:
- true If an entity was created for this symbol.
- Throws:
PtalonRuntimeException
- If the symbol is not in the current scope.
-
isForReady
public boolean isForReady() throws PtalonRuntimeException
Return true if the boolean for the current conditional is ready to be entered. It is ready when all ports, parameters, and relations in the containing scope have been created, when all parameters in the containing scope have been assigned values, and when in a branch of an if-block or for-block that is active.- Returns:
- true If the current for-block scope is ready to be entered.
- Throws:
PtalonRuntimeException
- If it is thrown trying to access a parameter.
-
isIfReady
public boolean isIfReady() throws PtalonRuntimeException
Return true if the boolean for the current conditional is ready to be entered. It is ready when all ports, parameters, and relations in the containing scope have been created, when all parameters in the containing scope have been assigned values, and when in a branch of an if-block that is active.- Returns:
- true If the current if-block scope is ready to be entered.
- Throws:
PtalonRuntimeException
- If it is thrown trying to access a parameter.
-
isReady
public boolean isReady() throws PtalonRuntimeException
Return true if the current piece of code is ready to be entered. This is used by port, parameter, and relation declarations only. It is ready when all ports, parameters, and relations in the containing scope have been created, when all parameters in the containing scope have been assigned values, and when in a branch of an if-block that is active.- Returns:
- true If the current if-block scope is ready to be entered.
- Throws:
PtalonRuntimeException
- If it is thrown trying to access a parameter.
-
negateObject
public void negateObject(java.lang.String name) throws PtalonRuntimeException
- Throws:
PtalonRuntimeException
-
optionalObject
public void optionalObject(java.lang.String name) throws PtalonRuntimeException
- Throws:
PtalonRuntimeException
-
popForStatement
public java.lang.String popForStatement() throws PtalonScopeException
Pop out of the scope of the current for statement and into its container block's scope.- Returns:
- The unique name of the for-statement block being exited.
- Throws:
PtalonScopeException
- If the current scope is already the outermost scope.
-
popIfStatement
public java.lang.String popIfStatement() throws PtalonScopeException
Pop out of the scope of the current if statement and into its container block's scope.- Returns:
- The unique name of the if-statement block being exited.
- Throws:
PtalonScopeException
- If the current scope is already the outermost scope.
-
preserveObject
public void preserveObject(java.lang.String name) throws PtalonRuntimeException
- Throws:
PtalonRuntimeException
-
pushForStatement
public void pushForStatement(java.lang.String variable, java.lang.String initExpr, java.lang.String satExpr)
Push into the scope of a new for statement contained as a sub-block of the current (FIXME: if or for) statement.- Parameters:
variable
- The variable associated with the for statement.initExpr
- The expression representing the initial value for the variable.satExpr
- The expression evaluated before executing the for statement body.
-
pushIfStatement
public void pushIfStatement()
Push into the scope of a new if statement contained as a sub-block of the current if statement.
-
removeObject
public void removeObject(java.lang.String name) throws PtalonRuntimeException
- Throws:
PtalonRuntimeException
-
setActiveBranch
public void setActiveBranch(boolean branch)
Set the active branch for the current if statement.- Parameters:
branch
- The branch to set.
-
setActorSymbol
public void setActorSymbol(java.lang.String symbol) throws PtalonScopeException
Set the symbol in the PtalonCode which represents this AbstractPtalonEvaluator's actor.- Parameters:
symbol
- The name of this actor in the Ptalon file.- Throws:
PtalonScopeException
- If the symbol has been added already, or if there is some problem accessing its associated file.
-
setCurrentBranch
public void setCurrentBranch(boolean branch)
Set the current branch that's being walked.- Parameters:
branch
- True if the true branch is being walked.
-
setNextExpression
public void setNextExpression(java.lang.String nextExpr)
Set the next expression for the current for statement scope, assuming the current scope is a for statement and not an if statement.- Parameters:
nextExpr
- The expression to represent the next statement.
-
startAtTop
public void startAtTop()
Prepare the compiler to start at the outermost scope of the Ptalon program during run time.
-
_getIndentPrefix
protected static java.lang.String _getIndentPrefix(int level)
Return a number of spaces that is proportional to the argument. If the argument is negative or zero, return an empty string.- Parameters:
level
- The level of indenting represented by the spaces.- Returns:
- A string with zero or more spaces.
-
_getTimesEntered
protected int _getTimesEntered()
Return the number of times the current if tree has been entered.- Returns:
- The number of times the current if tree has been entered.
-
_getType
protected java.lang.String _getType(java.lang.String symbol) throws PtalonScopeException
Return the type associated with the given symbol in the current scope.- Parameters:
symbol
- The symbol under test.- Returns:
- The type associated with the given symbol.
- Throws:
PtalonScopeException
- If the symbol is not in the current scope.
-
_getTypeForScope
protected java.lang.String _getTypeForScope(java.lang.String symbol) throws PtalonScopeException
Return the type associated with the given symbol in the current scope. This is the same as getType, but it is used to avoid a name conflict in PtalonEvaluator.PtalonExpressionScope- Parameters:
symbol
- The symbol under test.- Returns:
- The type associated with the given symbol.
- Throws:
PtalonScopeException
- If the symbol is not in the current scope.
-
_inNewWhileIteration
protected boolean _inNewWhileIteration()
Return true if in a new iteration of a while block.- Returns:
- true If in a new iteration of a while block.
-
_isPreservingTransformation
protected boolean _isPreservingTransformation()
-
_processAttributes
protected void _processAttributes(NamedObj object) throws PtalonRuntimeException
- Parameters:
object
-- Throws:
PtalonRuntimeException
-
_resetParameters
protected void _resetParameters(boolean reset)
Set whether to reset parameters when the actor is populated and the parameters already exist for the actor.- Parameters:
reset
- Whether to reset parameters.
-
_setPreservingTransformation
protected void _setPreservingTransformation(boolean b)
-
-