Class Interpreter
While this class is designed to be used from a single thread, it implements thread-safe compilation of the source code using double-checked locking pattern. This means that even if multiple threads accidentally execute the same interpreter instance, the source code will be compiled exactly once and the compiled code will be properly visible to all threads.
However, note that each execution creates a new Context instance, which
means that concurrent executions will not share state. This is not the recommended
usage pattern, and the interpreter should ideally be used from a single thread.
-
Constructor Summary
ConstructorsConstructorDescriptionInterpreter(Input source) Constructs an Interpreter instance with the specified input source.Interpreter(Program code) Constructs an Interpreter instance with the specified command to be executed.Interpreter(String source) Interpreter(Path file) Constructs anInterpreterinstance based on the given file. -
Method Summary
-
Constructor Details
-
Interpreter
-
Interpreter
Constructs an Interpreter instance with the specified input source.- Parameters:
source- the input source to be interpreted
-
Interpreter
Constructs an Interpreter instance with the specified command to be executed.- Parameters:
code- the command to be interpreted and executed
-
Interpreter
Constructs anInterpreterinstance based on the given file. Depending on the file extension, creates an interpreter for source code files ending with ".turi" or deserializes a compiled program from files ending with ".turc".- Parameters:
file- the path to the source or compiled program file- Throws:
IOException- if an I/O error occurs while reading the fileRuntimeException- if the file type is unsupported
-
-
Method Details
-
getImportContext
-
compileAndExecute
Executes the source code, compiling it first if necessary.While this method implements thread-safe compilation using double-checked locking, it is not designed for concurrent use. Each call creates a new execution context, so concurrent executions will not share state. It is recommended to use each Interpreter instance from a single thread.
- Returns:
- The result of executing the code
- Throws:
BadSyntax- if the source code contains syntax errorsExecutionException- if an error occurs during execution
-
execute
Executes the provided command within the current execution context. If an execution error occurs, it adapts the stack trace to include source-level information before re-throwing the exception.- Parameters:
code- the command to execute- Returns:
- the result of executing the command
- Throws:
ExecutionException- if an error occurs during the command execution, with an adapted stack trace for better debugging
-
compile
Compiles the source code into an executableProgramobject. This method uses double-checked locking to ensure thread-safe compilation. If the code has already been compiled, the cachedProgramis returned. Otherwise, the source code is lexically analyzed and processed into a series of commands.- Returns:
- the compiled
Programobject representing the executable commands
-
serialize
public byte[] serialize()
-