Interface Interpreter
-
- All Superinterfaces:
InternalInterpreter
- All Known Implementing Classes:
BasicInterpreter
public interface Interpreter extends InternalInterpreter
An interpreter instance executes a program.- Author:
- Peter Verhas
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description java.lang.Objectcall(java.lang.String functionName, java.lang.Object[] arguments)Call a function defined by the program passing the objects as arguments.voiddisableHook()Temporarily disable the hooks.voidenableHook()Enable the hook calls.voidexecute()Execute the program.voidexecute(Command startCommand)Execute the program starting at the commandstartCommandConfigurationgetConfiguration()java.io.WritergetErrorOutput()ScriptContext.getReader()InterpreterHookgetHook()Get the hook object the interpreter has.java.io.ReadergetInput()java.io.WritergetOutput()RightValuegetReturnValue()Get the return value that was set by the execution of the subroutine.CommandSubgetSubroutine(java.lang.String name)Get a subroutine by its name.java.lang.ObjectgetVariable(java.lang.String name)Get the value of a variable.HierarchicalVariableMapgetVariables()Get the global variables of the program.voidregisterFunctions(java.lang.Class<?> klass)Register the functions defined in the class.voidregisterHook(InterpreterHook hook)voidregisterJavaMethod(java.lang.String alias, java.lang.Class<?> klass, java.lang.String methodName, java.lang.Class<?>[] argumentTypes)Register a BASIC function as Java method.voidsetErrorOutput(java.io.Writer writer)voidsetInput(java.io.Reader reader)voidsetOutput(java.io.Writer writer)voidsetReturnValue(RightValue returnValue)Register the return value.voidsetVariable(java.lang.String name, java.lang.Object value)Set the value of the variable.-
Methods inherited from interface com.scriptbasic.interfaces.InternalInterpreter
getJavaMethod, getMap, getProgram, getUseMap, pop, push, push, setNextCommand, setProgram
-
-
-
-
Method Detail
-
registerHook
void registerHook(InterpreterHook hook)
-
execute
void execute() throws ScriptBasicExceptionExecute the program.- Throws:
ScriptBasicException- in case of exception
-
setVariable
void setVariable(java.lang.String name, java.lang.Object value) throws ScriptBasicExceptionSet the value of the variable. If the program is in a local scope and there is a variable with the given name in the local scope then the value of that variable is set, even if there is a global variable with that name.- Parameters:
name- the name of the global variablevalue- the value to be set- Throws:
ScriptBasicException- in case of exception
-
getVariable
java.lang.Object getVariable(java.lang.String name) throws ScriptBasicExceptionGet the value of a variable. Since this is not a BASIC interpreter method, but rather a method that helps the embedding of the interpreter the returned value is a raw Java object and not a RightValue. Thus if the variable value is for example aBasicDoubleValuethen the implementation will return aDouble.- Parameters:
name- the name of the variable- Returns:
- the value of the variable
- Throws:
ScriptBasicException- in case of exception
-
call
java.lang.Object call(java.lang.String functionName, java.lang.Object[] arguments) throws ScriptBasicExceptionCall a function defined by the program passing the objects as arguments.- Parameters:
functionName- the name of the function in the program codearguments- the arguments to the function- Returns:
- the returned object, or
nullif the function does not return value - Throws:
ScriptBasicException- in case of exception
-
registerFunctions
void registerFunctions(java.lang.Class<?> klass)
Register the functions defined in the class. Functions that can be called from a BASIC program but implemented in Java are static methods that are registered for the interpreter. The easiest way to define these methods are to create a class and annotate the methods that serve as BASIC functions with the annotation@BasicFunction- Parameters:
klass- the class the defines the functions.
-
getConfiguration
Configuration getConfiguration()
-
execute
void execute(Command startCommand) throws ScriptBasicException
Execute the program starting at the commandstartCommand- Parameters:
startCommand- where the execution has to start- Throws:
ScriptBasicException- in case of exception during the execution
-
getSubroutine
CommandSub getSubroutine(java.lang.String name)
Get a subroutine by its name.- Parameters:
name- the name of the subroutine- Returns:
- the "SUB" command that starts the subroutine
-
getReturnValue
RightValue getReturnValue()
Get the return value that was set by the execution of the subroutine.- Returns:
- return value
-
setReturnValue
void setReturnValue(RightValue returnValue)
Register the return value. This method is called from a subroutine.- Parameters:
returnValue- the value that the subroutine will return
-
getVariables
HierarchicalVariableMap getVariables()
Get the global variables of the program.- Returns:
- the variables.
-
registerJavaMethod
void registerJavaMethod(java.lang.String alias, java.lang.Class<?> klass, java.lang.String methodName, java.lang.Class<?>[] argumentTypes) throws BasicRuntimeExceptionRegister a BASIC function as Java method. Java methods may be overloaded but BASIC functions can not. When a BASIC program wants to use a Java method it has to declare it asuse class from package as basicClassReference
for exampleuse Math from java.lang as m
when the methodsinis used, foe examplea = m.sin(1.0)
the BASIC interpreter has to find the methodjava.lang.Math.sin(Double x). The problem is that the method does not exist because the argument is notDoublebut ratherdouble.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 partuse 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 toInternalInterpreter.getJavaMethod(Class, String)can find the appropriate method.- Parameters:
alias- the alias how the function will be named in basicklass- the class where the static method ismethodName- the java name of the methodargumentTypes- the types of the arguments to be used to help to identify overloaded methods- Throws:
BasicRuntimeException- in case of exception
-
getInput
java.io.Reader getInput()
-
setInput
void setInput(java.io.Reader reader)
- Parameters:
reader- parameter- See Also:
ScriptContext.setReader(Reader)
-
getOutput
java.io.Writer getOutput()
-
setOutput
void setOutput(java.io.Writer writer)
- Parameters:
writer- parameter- See Also:
ScriptContext.setWriter(Writer)
-
getErrorOutput
java.io.Writer getErrorOutput()
ScriptContext.getReader()- Returns:
- return value
-
setErrorOutput
void setErrorOutput(java.io.Writer writer)
- Parameters:
writer- parameter- See Also:
ScriptContext.setErrorWriter(Writer)
-
disableHook
void disableHook()
Temporarily disable the hooks. Following this call the hooks will not be called until theenableHook()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
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 methodInterpreterHook.setReturnValue(RightValue)should first disable the hook mechanism, call back to the interpreter object and the enable the hook mechanism again.
-
enableHook
void enableHook()
Enable the hook calls. This method has to be called after the call todisableHook()to enable again the calling mechanism.
-
getHook
InterpreterHook getHook()
Get the hook object the interpreter has.- Returns:
- return value
-
-