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()
ExtendedInterpretergetHook in interface ExtendedInterpreterpublic void disableHook()
ExtendedInterpreterExtendedInterpreter.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 ExtendedInterpreterpublic void enableHook()
ExtendedInterpreterExtendedInterpreter.disableHook() to enable again the calling mechanism.enableHook in interface ExtendedInterpreterpublic void registerHook(InterpreterHook hook)
registerHook in interface Interpreterpublic Reader getReader()
ExtendedInterpretergetReader in interface ExtendedInterpreterpublic void setReader(Reader reader)
setReader in interface Interpreterreader - the reader to setScriptContext.setReader(java.io.Reader)public Writer getWriter()
ExtendedInterpretergetWriter in interface ExtendedInterpreterpublic void setWriter(Writer writer)
setWriter in interface Interpreterwriter - the writer to setScriptContext.setWriter(Writer)public Writer getErrorWriter()
ExtendedInterpretergetErrorWriter in interface ExtendedInterpreterpublic void setErrorWriter(Writer errorWriter)
setErrorWriter in interface InterpretererrorWriter - the errorWriter to setScriptContext.setErrorWriter(Writer)public void setProgram(BuildableProgram buildableProgram)
InterpretersetProgram in interface Interpreterpublic HierarchicalVariableMap getVariables()
ExtendedInterpretergetVariables in interface ExtendedInterpreterpublic CommandSub getSubroutine(String name)
ExtendedInterpretergetSubroutine in interface ExtendedInterpretername - the name of the subroutinepublic void registerFunctions(Class<?> klass)
Interpreter@FunctionregisterFunctions in interface Interpreterklass - the class the defines the functions.public void execute()
throws ExecutionException
Interpreterexecute in interface InterpreterExecutionExceptionpublic void execute(Command startCommand) throws ExecutionException
ExtendedInterpreterstartCommandexecute in interface ExtendedInterpreterExecutionExceptionpublic void setVariable(String name, Object value) throws ExecutionException
InterpretersetVariable in interface Interpretername - the name of the global variablevalue - the value to be setExecutionExceptionpublic Object getVariable(String name) throws ExecutionException
InterpreterBasicDoubleValue then the
implementation should return a Double.getVariable in interface Interpretername - the name of the variableExecutionExceptionpublic Object call(String functionName, Object[] arguments) throws ExecutionException
Interpretercall in interface InterpreterfunctionName - the name of the function in the program codearguments - the arguments to the functionnull if the function does not
return valueExecutionExceptionpublic BuildableProgram getProgram()
getProgram in interface ExtendedInterpreterpublic void setNextCommand(Command nextCommand)
ExtendedInterpretersetNextCommand in interface ExtendedInterpreternextCommand - is the next command to execute after the current commandpublic Command getCurrentCommand()
ExtendedInterpretergetCurrentCommand in interface ExtendedInterpreterpublic Map<String,Object> getMap()
ExtendedInterpreterThe 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 ExtendedInterpreterpublic Factory getFactory()
public void setFactory(Factory factory)
FactoryManagedsetFactory in interface FactoryManagedfactory - the factory that manages the instance.public Map<String,Class<?>> getUseMap()
ExtendedInterpreteruse 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 ExtendedInterpreterpublic Method getJavaMethod(Class<?> klass, String methodName) throws ExecutionException
ExtendedInterpreterMETHOD 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 doublegetJavaMethod in interface ExtendedInterpreterExecutionExceptionpublic void registerJavaMethod(String alias, Class<?> klass, String methodName, Class<?>[] argumentTypes) throws BasicRuntimeException
ExtendedInterpreteruse 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 ExtendedInterpreterBasicRuntimeExceptionpublic void push(Command command)
ExtendedInterpreterCalling 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 ExtendedInterpretercommand - the command from which a subroutine call was executed.public void push()
ExtendedInterpreterExtendedInterpreter.push(Command) and pushes the currently executing command
on the stack.push in interface ExtendedInterpreterpublic Command pop()
ExtendedInterpreterpop in interface ExtendedInterpreterpublic void setReturnValue(RightValue returnValue)
ExtendedInterpretersetReturnValue in interface ExtendedInterpreterreturnValue - the value that the subroutine will returnpublic RightValue getReturnValue()
ExtendedInterpretergetReturnValue in interface ExtendedInterpreterpublic Configuration getConfiguration()
getConfiguration in interface ExtendedInterpreterCopyright © 2013 Verhas and Verhas Software Craftsmen. All Rights Reserved.