public final class BasicExtendedInterpreter extends Object implements ExtendedInterpreter
Constructor and Description |
---|
BasicExtendedInterpreter() |
Modifier and Type | Method and Description |
---|---|
Object |
call(String functionName,
Object[] arguments)
Call a function defined by the program passing the objects as arguments.
|
void |
disableHook()
Temporarily disable the hooks.
|
void |
enableHook()
Enable the hook calls.
|
void |
execute()
Execute the program.
|
void |
execute(Command startCommand)
Execute the program starting at the command
startCommand |
Configuration |
getConfiguration() |
Command |
getCurrentCommand()
Get the command that is currently executing.
|
Writer |
getErrorWriter()
|
Factory |
getFactory() |
InterpreterHook |
getHook()
Get the hook object the interpreter has.
|
Method |
getJavaMethod(Class<?> klass,
String methodName)
Get the method named from the klass based on the declaration given in a
previously executed
METHOD basic command. |
Map<String,Object> |
getMap()
Since the Command objects should not contain runtime information there is
a need sometime to store information that is runtime.
|
BuildableProgram |
getProgram() |
Reader |
getReader()
|
RightValue |
getReturnValue()
Get the return value that was set by the execution of the subroutine.
|
CommandSub |
getSubroutine(String name)
Get a subroutine by its name.
|
Map<String,Class<?>> |
getUseMap()
Programs can access Java static methods from different packages.
|
Object |
getVariable(String name)
Get the value of a global variable.
|
HierarchicalVariableMap |
getVariables()
Get the global variables of the program.
|
Writer |
getWriter()
|
Command |
pop()
Pop the command from the top of the stack and also drop the last local
variables frame.
|
void |
push()
Same as
ExtendedInterpreter.push(Command) and pushes the currently executing command
on the stack. |
void |
push(Command command)
Push a command to the stack.
|
void |
registerFunctions(Class<?> klass)
Register the functions defined in the class.
|
void |
registerHook(InterpreterHook hook) |
void |
registerJavaMethod(String alias,
Class<?> klass,
String methodName,
Class<?>[] argumentTypes)
Register a BASIC function as Java method.
|
void |
setErrorWriter(Writer errorWriter) |
void |
setFactory(Factory factory)
When a factory creates an instance of a class implementing this interface
this method is called to register the factory that created the instance.
|
void |
setNextCommand(Command nextCommand)
Tell the interpreter that the next command to call is not the one that
follows the actual command but rather the one specified by the argument.
|
void |
setProgram(BuildableProgram buildableProgram)
Set the program to execute.
|
void |
setReader(Reader reader) |
void |
setReturnValue(RightValue returnValue)
Register the return value.
|
void |
setVariable(String name,
Object value)
Set the value of the global variable.
|
void |
setWriter(Writer writer) |
public InterpreterHook getHook()
ExtendedInterpreter
getHook
in interface ExtendedInterpreter
public void disableHook()
ExtendedInterpreter
ExtendedInterpreter.enableHook()
is called.
Hook disabling was designed with the special case in mind when a hook
wants to alter the return value returned from a subroutine. To do so the
hook method has to invoke the ExtendedInterpreter.setReturnValue(RightValue)
method, which was
actually calling the hook. To avoid the infinite loop and not to confuse
the other hook methods that are in the list sooner the hook method InterpreterHook.setReturnValue(RightValue)
should first disable the hook
mechanism, call back to the interpreter object and the enable the hook
mechanism again.
disableHook
in interface ExtendedInterpreter
public void enableHook()
ExtendedInterpreter
ExtendedInterpreter.disableHook()
to enable again the calling mechanism.enableHook
in interface ExtendedInterpreter
public void registerHook(InterpreterHook hook)
registerHook
in interface Interpreter
public Reader getReader()
ExtendedInterpreter
getReader
in interface ExtendedInterpreter
public void setReader(Reader reader)
setReader
in interface Interpreter
reader
- the reader to setScriptContext.setReader(java.io.Reader)
public Writer getWriter()
ExtendedInterpreter
getWriter
in interface ExtendedInterpreter
public void setWriter(Writer writer)
setWriter
in interface Interpreter
writer
- the writer to setScriptContext.setWriter(Writer)
public Writer getErrorWriter()
ExtendedInterpreter
getErrorWriter
in interface ExtendedInterpreter
public void setErrorWriter(Writer errorWriter)
setErrorWriter
in interface Interpreter
errorWriter
- the errorWriter to setScriptContext.setErrorWriter(Writer)
public void setProgram(BuildableProgram buildableProgram)
Interpreter
setProgram
in interface Interpreter
public HierarchicalVariableMap getVariables()
ExtendedInterpreter
getVariables
in interface ExtendedInterpreter
public CommandSub getSubroutine(String name)
ExtendedInterpreter
getSubroutine
in interface ExtendedInterpreter
name
- the name of the subroutinepublic void registerFunctions(Class<?> klass)
Interpreter
@Function
registerFunctions
in interface Interpreter
klass
- the class the defines the functions.public void execute() throws ExecutionException
Interpreter
execute
in interface Interpreter
ExecutionException
public void execute(Command startCommand) throws ExecutionException
ExtendedInterpreter
startCommand
execute
in interface ExtendedInterpreter
ExecutionException
public void setVariable(String name, Object value) throws ExecutionException
Interpreter
setVariable
in interface Interpreter
name
- the name of the global variablevalue
- the value to be setExecutionException
public Object getVariable(String name) throws ExecutionException
Interpreter
BasicDoubleValue
then the
implementation should return a Double
.getVariable
in interface Interpreter
name
- the name of the variableExecutionException
public Object call(String functionName, Object[] arguments) throws ExecutionException
Interpreter
call
in interface Interpreter
functionName
- the name of the function in the program codearguments
- the arguments to the functionnull
if the function does not
return valueExecutionException
public BuildableProgram getProgram()
getProgram
in interface ExtendedInterpreter
public void setNextCommand(Command nextCommand)
ExtendedInterpreter
setNextCommand
in interface ExtendedInterpreter
nextCommand
- is the next command to execute after the current commandpublic Command getCurrentCommand()
ExtendedInterpreter
getCurrentCommand
in interface ExtendedInterpreter
public Map<String,Object> getMap()
ExtendedInterpreter
The interpreter initializes the map when the command asks for it the first time. The life time of the map is the same as the life time of the interpreter. Thus when a program finishes the map is still available to the command when the Interface 'call' method is called or the interpreter is restarted.
The interpreter does not alter the map in any other way than initializing it to some map implementation containing initially no element.
getMap
in interface ExtendedInterpreter
public Factory getFactory()
public void setFactory(Factory factory)
FactoryManaged
setFactory
in interface FactoryManaged
factory
- the factory that manages the instance.public Map<String,Class<?>> getUseMap()
ExtendedInterpreter
use Math from java.lang as m(the part following the keyword 'as' is optional, in which case the Java name of the class is used). After this statement is executed the use map will contain the class
javal.lang.Math
for the key m
.getUseMap
in interface ExtendedInterpreter
public Method getJavaMethod(Class<?> klass, String methodName) throws ExecutionException
ExtendedInterpreter
METHOD
basic command. The basic command
METHOD has the form (example follows):
method sin from java.lang.Math is (double)that defines that the method
sin
is in the class
java.lang.Math
and accepts one argument, which is double
getJavaMethod
in interface ExtendedInterpreter
ExecutionException
public void registerJavaMethod(String alias, Class<?> klass, String methodName, Class<?>[] argumentTypes) throws BasicRuntimeException
ExtendedInterpreter
use class from package as basicClassReferencefor example
use Math from java.lang as mwhen the method
sin
is used, foe example
a = m.sin(1.0)the BASIC interpreter has to find the method
java.lang.Math.sin(Double x)
. The problem is that the method does
not exist because the argument is not Double
but rather
double
.
To help with this situation the BASIC program should declare the Java signature of the method using the BASIC command METHOD. For example:
method sin from java.lang.Math is (double) use as sinus(Note that the part
use as ...
is optional.)
After this command is executed the interpreter will use the defined signature to locate the method. You can write in the BASIC program
a = m.sinus(1.0)
registerJavaMethod()
registers the basic function alias, class,
java method name and the argument types so that later call to ExtendedInterpreter.getJavaMethod(Class, String)
can find the appropriate method.registerJavaMethod
in interface ExtendedInterpreter
BasicRuntimeException
public void push(Command command)
ExtendedInterpreter
Calling this method also starts a new local variable frame, thus evaluation of the actual argument values in a function call has to be executed before calling this method.
push
in interface ExtendedInterpreter
command
- the command from which a subroutine call was executed.public void push()
ExtendedInterpreter
ExtendedInterpreter.push(Command)
and pushes the currently executing command
on the stack.push
in interface ExtendedInterpreter
public Command pop()
ExtendedInterpreter
pop
in interface ExtendedInterpreter
public void setReturnValue(RightValue returnValue)
ExtendedInterpreter
setReturnValue
in interface ExtendedInterpreter
returnValue
- the value that the subroutine will returnpublic RightValue getReturnValue()
ExtendedInterpreter
getReturnValue
in interface ExtendedInterpreter
public Configuration getConfiguration()
getConfiguration
in interface ExtendedInterpreter
Copyright © 2013 Verhas and Verhas Software Craftsmen. All Rights Reserved.