public abstract class CompiledExpression
extends java.lang.Object
implements java.lang.Cloneable
execute()
method. This method contains the
compiled bytecode instructions which define the essential operation of
the expression; it returns the overall result of execution.
Each custom subclass defines a specific return type which its
implementation of the execute
method will return. This is
accessed through the getReturnType()
method. Likewise, each custom
subclass may use zero or more libraries and zero or more variables. These
are stored at compile time via the setLibraries(java.lang.Object[])
and setVariables(com.goldencode.expr.Variable[])
methods. Though all three of these attributes would better
be stored as static members of the custom subclasses, a design decision
was made to store them as instance variables in the superclass instead.
This is deemed acceptable as a trade-off for the much more complicated
implementation which would have been required to do this dynamically.
Also, because the Expression
wrapper class manages a cache of
subclass instances, in practice no more than one instance of a custom
subclass exists at a time, such that these custom implementations are
essentially singletons.
Modifier and Type | Field and Description |
---|---|
private int |
activeAstId
ID of AST corresponding with most recently active code unit
|
private ExtraAst |
ast
AST associated with underlying expression
|
private java.lang.Object[] |
libraries
Objects upon which user functions are invoked by the expression
|
private java.lang.Class<?> |
returnType
Object type returned by concrete implementation of execute method
|
private Variable[] |
variables
Runtime variables accessed by the expression
|
Constructor and Description |
---|
CompiledExpression() |
Modifier and Type | Method and Description |
---|---|
protected CompiledExpression |
clone(SymbolResolver resolver)
Create a copy of this object which uses the same libraries and variables
as the original.
|
protected abstract java.lang.Object |
execute()
Each custom, concrete implementation of this abstract class will
implement this method to perform the operations described by the
underlying expression.
|
(package private) int |
getActiveAstId()
Get the ID of the AST node which corresponds with the code unit which
was last executing during a run of this expression's
execute()
method. |
(package private) ExtraAst |
getAst()
Return the AST associated with this compiled expression.
|
protected java.lang.Object |
getLib(int index)
Get the library object stored at the specified index.
|
(package private) java.lang.Class<?> |
getReturnType()
Reports the type of object which the
execute() method of this
object returns. |
protected java.lang.Object |
getVar(int index)
Get the value of the user variable stored at the specified
index.
|
protected void |
setActiveAstId(int activeAstId)
Store the ID of the AST which corresponds with the code unit which is
about to be executed.
|
(package private) void |
setAst(ExtraAst ast)
Set the AST associated with this compiled expression.
|
(package private) void |
setLibraries(java.lang.Object[] libraries)
Called by the
Compiler to set the library objects upon which
this expression will invoke methods at expression execution time. |
(package private) void |
setReturnType(java.lang.Class<?> returnType)
Set the type of object which the
execute() method of this object
is expected to return. |
protected void |
setVar(int index,
java.lang.Object value)
Set the value of the user variable stored at the specified
index.
|
(package private) void |
setVariables(Variable[] variables)
Called by the
Compiler to set the user variables upon which
this expression will invoke methods at expression execution time. |
protected static double |
testRound(double num)
This method is invoked when rounding floating point calculation results
to a fixed number of decimal places.
|
private java.lang.Class<?> returnType
private java.lang.Object[] libraries
private Variable[] variables
private ExtraAst ast
private int activeAstId
protected static double testRound(double num)
The purpose of this method is to test for these conditions and to throw an exception in the event one of them occurs. When this happens, it will be necessary for the expression's precision to be reduced to the point where the rounding operation will be meaningful.
num
- The intermediate numeric value to test against the boundary
conditions described above.num
. This value is returned for convenience in
assembling the bytecode for this test, in that the runtime
stack is left in the same condition when this method returns
successfully.java.lang.IllegalArgumentException
- if num
exceeds the range which can be represented
by a 64 bit long
value.protected final CompiledExpression clone(SymbolResolver resolver)
resolver
- Symbol resolver.protected java.lang.Object getLib(int index)
execute
method of compiled
expressions; it is not intended to be called directly by client code.index
- Index of the desired library object; this is known at compile
time and is hard-coded directly into the bytecode instructions
of a subclass' execute
method.setLibraries(java.lang.Object[])
protected java.lang.Object getVar(int index)
execute
method of compiled expressions; it is not intended to be called
directly by client code.index
- Index of the desired user variable; this is known at compile
time and is hard-coded directly into the bytecode instructions
of a subclass' execute
method.user variable
object, the
methods of which can be accessed by compiled expression
implementations.setVar(int, java.lang.Object)
,
setVariables(com.goldencode.expr.Variable[])
protected void setVar(int index, java.lang.Object value)
execute
method of compiled expressions; it is not intended to be called
directly by client code.
This method can only be compiled into an expression if the target
user variable is not registered
as read-only.
index
- Index of the target user variable; this is known at compile
time and is hard-coded directly into the bytecode instructions
of a subclass' execute
method.value
- The value to store within the user variable
object located at index
in the array of user
variables which this expression accesses. The type of this
parameter must be compatible with the type of the variable.getVar(int)
,
setVariables(com.goldencode.expr.Variable[])
protected void setActiveAstId(int activeAstId)
This method is only invoked by expressions which have been compiled with one (or more) of the following runtime flags set:
activeAstId
- ID of an AST node from the postfix representation of the
expression which was used to generate this object's class.
This ID will be unique within the expression's syntax tree.getActiveAstId()
ExtraAst getAst()
null
for expressions compiled with runtime
flags of ExpressionFlags.RT_ERROR
or ExpressionFlags.RT_DEBUG
.void setAst(ExtraAst ast)
ExpressionFlags.RT_ERROR
or ExpressionFlags.RT_DEBUG
.ast
- AST associated with this compiled expression.java.lang.Class<?> getReturnType()
execute()
method of this
object returns.execute
method.void setReturnType(java.lang.Class<?> returnType)
execute()
method of this object
is expected to return. This method is called directly by the Compiler
during expression compilation and should not be called by
any other code.returnType
- Class of the object returned by the execute
method.void setLibraries(java.lang.Object[] libraries)
Compiler
to set the library objects upon which
this expression will invoke methods at expression execution time.
This method should not be called by any other code. Bytecode
instructions are hard-coded into the expression's execute
method to access libraries at fixed indices in this array.libraries
- The array of libraries to be accessed by the expression at
execution time. This array will have a length of zero if the
expression references no library objects, but it will not be
null
.void setVariables(Variable[] variables)
Compiler
to set the user variables upon which
this expression will invoke methods at expression execution time.
This method should not be called by any other code. Bytecode
instructions are hard-coded into the expression's execute
method to access user variables at fixed indices in this array.variables
- The array of variables to be accessed by the expression at
execution time. This array will have a length of zero if the
expression references no user variables, but it will not be
null
.int getActiveAstId()
execute()
method. This ID is set at runtime by certain compiled expression
instances, to enable better exception handling and error reporting.-1
is returned for expressions which were not
compiled with error or debug diagnostics turned on.setActiveAstId(int)
protected abstract java.lang.Object execute()
Compiler
in bytecode form only (no
associated source code is generated). The basis for these instructions
is the infix notation expression string provided by the instance of
the Expression
class which is associated with an instance of
this class.
Different concrete implementations of this method will return different
objects, depending upon the structure of the underlying expression.
The exact data type returned by this method is determined by the getReturnType()
method. Most expressions return an object reference
(or null
). Assignment expressions and expressions whose
top level operation is a method call with a void
return
type always return null
from this method. If an expression
is written to return a primitive data type, that data will be wrapped
in the appropriate wrapper object before it is returned from this
method.
null
, as described above.