|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--simkin.ExecutableRoot | +--simkin.Interpreter
This class represents the Simkin interpreter. The interpreter can load, parse and interpret Simkin code.
The Interpreter maintains a list of global variables. This maps names to objects, which are available to all Simkin scripts.
The interpreter itself is a global object with the name "Interpreter".
The Interpreter implements the Executable
interface, supports the single field "tracing" which enables/disables tracing of method calls
New from Simkin 1.16: by default a different instance of the Interpreter is created for each thread. This can be overriden with the setInterpreterPerThread
method
Field Summary | |
static java.lang.Boolean |
g_False
false object |
static Null |
g_null
the null object |
static java.lang.Boolean |
g_True
true object |
static java.lang.Integer |
g_Zero
zero object |
Constructor Summary | |
Interpreter()
Default constructor - adds a global variable pointing to this interpreter |
Method Summary | |
void |
addGlobalVariable(java.lang.String name,
java.lang.Object r)
Adds a global variable to the current list |
static boolean |
boolValue(java.lang.Object value)
This method converts the given value into a boolean value |
java.lang.Object |
executeParseTree(java.lang.String location,
java.lang.Object obj,
ParseNode parseNode,
java.lang.Object[] args)
This method executes a piece of Simkin code from a parse tree. |
ExecuteResult |
executeString(java.lang.String location,
java.lang.Object obj,
java.lang.String code,
java.lang.Object[] args)
This method executes a piece of Simkin code within a string |
ExecuteResult |
executeStringExternalParams(java.lang.String location,
java.lang.Object obj,
java.util.Vector paramNames,
java.lang.String code,
java.lang.Object[] args)
This method executes a piece of Simkin code within a string, it assumes that the parameters have been defined outside the statement block. |
java.lang.Object |
findGlobalVariable(java.lang.String name)
looks for the given global variable. |
static double |
floatValue(java.lang.Object value)
This method converts the given value into a double value |
static Interpreter |
getInterpreter()
This method returns the global interpreter associated with all method calls. |
java.lang.Object |
getValue(java.lang.String field_name,
java.lang.String attrib)
Returns the value of the "tracing" variable - e.g. |
java.lang.Object |
getValueAt(java.lang.Object index,
java.lang.String attrib_name)
Returns null - this is not a collection |
static int |
intValue(java.lang.Object value)
This method converts the given value into an integer value |
java.lang.Object |
method(java.lang.String method_name,
java.lang.Object[] args)
The interpreter does not expose any methods to Simkin scripts |
ParseNode |
parse(java.lang.String location,
java.lang.String code)
This method parses a string full of Simkin code and returns the parse tree. |
ParseNode |
parseExternalParams(java.lang.String location,
java.util.Vector paramNames,
java.lang.String code)
This method parses a string of Simkin statements, excluding parameters and enclosing braces. |
java.lang.Object |
reflectiveGetValue(simkin.Context ctxt,
java.lang.Object owner,
java.lang.String field_name)
This function attempts to retrieve a public Java field using reflection |
java.lang.Object |
reflectiveMethodCall(simkin.Context ctxt,
java.lang.Object owner,
java.lang.String method_name,
java.lang.Object[] arguments)
This function attempts to make a public method call on a Java object by using reflection |
void |
reflectiveSetValue(simkin.Context ctxt,
java.lang.Object owner,
java.lang.String field_name,
java.lang.Object value)
This function attempts to set a public Java field using reflection |
void |
removeGlobalVariable(java.lang.String name)
Removes a global variable from the current list |
static void |
runtimeError(java.lang.String buffer)
this method is called when there is a runtime error and throws a RuntimeException |
static void |
setInterpreter(Interpreter i)
Sets a global interpreter to be used for method calls - you can use this to override the Interpreter with your own derived classes. |
static void |
setInterpreterPerThread(boolean flag)
This method enables or disables the interpreter-per-thread feature. |
void |
setStatementStepper(StatementStepper stepper)
this method sets an object to be called each time a statement is executed. |
void |
setTraceCallback(TraceCallback cb)
this method sets an object to receive trace messages. |
void |
setValue(java.lang.String field_name,
java.lang.String attrib,
java.lang.Object value)
Used to set the "tracing" variable - for example in Simkin: |
void |
setValueAt(java.lang.Object index,
java.lang.String attrib_name,
java.lang.Object value)
Does nothing - this is not a collection |
Methods inherited from class simkin.ExecutableRoot |
createIterator, createIterator |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final Null g_null
public static final java.lang.Boolean g_True
public static final java.lang.Boolean g_False
public static final java.lang.Integer g_Zero
Constructor Detail |
public Interpreter()
Method Detail |
public void setValue(java.lang.String field_name, java.lang.String attrib, java.lang.Object value) throws java.lang.RuntimeException, FieldNotSupportedException
Interpreter.tracing=true;
setValue
in class ExecutableRoot
simkin.Executable
field_name
- the name of the fieldattrib_name
- the name of the attribute to be set (can be null)value
- the value to be setjava.lang.RuntimeException
- - if there was a problem running the script (such as not having permission to access a field)FieldNotSupportedException
- - if the field could not be foundpublic java.lang.Object method(java.lang.String method_name, java.lang.Object[] args) throws java.lang.RuntimeException, ParseException, MethodNotSupportedException
method
in class ExecutableRoot
simkin.ExecutableRoot
public static boolean boolValue(java.lang.Object value)
If the object is an instance of Boolean, the boolean value is returned, otherwise toString() is called, and compared with "true"
public static int intValue(java.lang.Object value)
If the object is an instance of Integer, the integer value is returned, otherwise toString() is called, and converted to an integer
public static double floatValue(java.lang.Object value)
If the object is an instance of Double, the integer value is returned, otherwise toString() is called, and converted to a double
public java.lang.Object getValueAt(java.lang.Object index, java.lang.String attrib_name) throws java.lang.RuntimeException
getValueAt
in class ExecutableRoot
simkin.Executable
array_index
- an object whose value indicates the index of the item in the collectionattrib_name
- attribute name (null if no attribute specified)java.lang.RuntimeException
- - if there was a problem running the scriptFieldNotSupportedException
- - if the field could not be foundpublic void setValueAt(java.lang.Object index, java.lang.String attrib_name, java.lang.Object value) throws java.lang.RuntimeException
setValueAt
in class ExecutableRoot
simkin.Executable
array_index
- an object whose value indicates the index of the item in the collectionattrib_name
- the name of the attribute to be set (can be null)value
- the value to be setjava.lang.RuntimeException
- - if there was a problem running the scriptpublic java.lang.Object getValue(java.lang.String field_name, java.lang.String attrib) throws java.lang.RuntimeException, FieldNotSupportedException
tracing=Interpreter.tracing;
getValue
in class ExecutableRoot
simkin.Executable
field_name
- the name of the fieldattrib_name
- attribute name (null if no attribute specified)java.lang.RuntimeException
- - if there was a problem running the script (such as not having permission to access a field)FieldNotSupportedException
- - if the field could not be foundpublic java.lang.Object findGlobalVariable(java.lang.String name)
name
- - the name of the global variablepublic void addGlobalVariable(java.lang.String name, java.lang.Object r)
name
- - the name of the global variable, if one already exists it is replacedr
- - the object to be added to the listpublic void removeGlobalVariable(java.lang.String name)
name
- - the name of the global variable to be removedpublic ParseNode parse(java.lang.String location, java.lang.String code) throws ParseException
For example the following string could be passed:
(a,b){ a=b+1; return a; }
location
- used to help identify the code in error messagescode
- a string containing the Simkin codepublic ParseNode parseExternalParams(java.lang.String location, java.util.Vector paramNames, java.lang.String code) throws ParseException
For example the following string could be passed (note the absence of enclosing braces around the statement list):
a=b+1; return a;
location
- used to help identify the code in error messagesparamNames
- a vector of names for the parameterscode
- a string containing the Simkin codepublic ExecuteResult executeString(java.lang.String location, java.lang.Object obj, java.lang.String code, java.lang.Object[] args) throws java.lang.RuntimeException, ParseException
For example the following string could be passed:
(a,b){ a=b+1; return a; }
location
- a name used to identify the code in error messagesobj
- the object within whose context the code will executecode
- a string containing some Simkin codeargs
- and array of RValue arguments to the codeParseException
- this is thrown if there is a syntax error in the codejava.lang.RuntimeException
- this is thrown if there is an error during the execution of the code (such as method or field not found)public ExecuteResult executeStringExternalParams(java.lang.String location, java.lang.Object obj, java.util.Vector paramNames, java.lang.String code, java.lang.Object[] args) throws java.lang.RuntimeException, ParseException
For example the following string could be passed (note the absence of enclosing braces around the statement list):
a=b+1; return a;
location
- a name used to identify the code in error messagesobj
- the object within whose context the code will executeparamNames
- a vector of names for the parameterscode
- a string containing some Simkin codeargs
- and array of RValue arguments to the codeParseException
- this is thrown if there is a syntax error in the codejava.lang.RuntimeException
- this is thrown if there is an error during the execution of the code (such as method or field not found)public java.lang.Object executeParseTree(java.lang.String location, java.lang.Object obj, ParseNode parseNode, java.lang.Object[] args) throws java.lang.RuntimeException, ParseException
You can call this with a parse tree produced by parse
, parseExternalParams
,executeString
or executeStringExternalParams
. This can be useful for caching parse trees to improve performance.
location
- a name used to identify the code in error messagesobj
- the object within whose context the code will executeparseNode
- a pre-parsed tree of Simkin codeargs
- and array of RValue arguments to the codeParseException
- this is thrown if there is a syntax error in the codejava.lang.RuntimeException
- this is thrown if there is an error during the execution of the code (such as method or field not found)public static void runtimeError(java.lang.String buffer) throws java.lang.RuntimeException
public static Interpreter getInterpreter()
public static void setInterpreter(Interpreter i)
the
- Interpeter that will be used to execute methods - pass null to clear the current valuepublic static void setInterpreterPerThread(boolean flag)
If the feature is enabled, there is one interpreter for each thread. Calls to getInterpreter() will return the Interpreter defined for the current thread - or create one if one hasn't been encountered yet.
The default value for this is true.
flag
- true/false to enable or disable the featurepublic void setTraceCallback(TraceCallback cb)
public void setStatementStepper(StatementStepper stepper)
public java.lang.Object reflectiveMethodCall(simkin.Context ctxt, java.lang.Object owner, java.lang.String method_name, java.lang.Object[] arguments) throws java.lang.RuntimeException, MethodNotSupportedException
owner
- - the object to call a method onmethod_name
- - the name of the method to callarguments
- - the method arguments - types Integer, Double, Boolean and Character are converted to their primitive typesjava.lang.RuntimeException
- - if the method could not be calledMethodNotSupportedException
- - if the method could not be foundpublic void reflectiveSetValue(simkin.Context ctxt, java.lang.Object owner, java.lang.String field_name, java.lang.Object value) throws java.lang.RuntimeException, FieldNotSupportedException
owner
- - the object owning the fieldfield_name
- - the name of the fieldvalue
- - the value to assign to the fieldjava.lang.RuntimeException
- - if the field could not be set (e.g. if it was private)FieldNotSupportedException
- - if the field could not be foundpublic java.lang.Object reflectiveGetValue(simkin.Context ctxt, java.lang.Object owner, java.lang.String field_name) throws java.lang.RuntimeException, FieldNotSupportedException
owner
- - the object owning the fieldfield_name
- - the name of the fieldjava.lang.RuntimeException
- - if the field could not be retrieved (e.g. if it was private)FieldNotSupportedException
- - if the field could not be found
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |