Class Interpreter

java.lang.Object
ch.turic.Interpreter

public class Interpreter extends Object
Interprets and executes source code written in the programming language.

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 Details

    • Interpreter

      public Interpreter(String source)
    • Interpreter

      public Interpreter(Input source)
      Constructs an Interpreter instance with the specified input source.
      Parameters:
      source - the input source to be interpreted
    • Interpreter

      public Interpreter(Program code)
      Constructs an Interpreter instance with the specified command to be executed.
      Parameters:
      code - the command to be interpreted and executed
    • Interpreter

      public Interpreter(Path file) throws IOException
      Constructs an Interpreter instance 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 file
      RuntimeException - if the file type is unsupported
  • Method Details

    • getImportContext

      public Context getImportContext()
    • compileAndExecute

      public Object compileAndExecute() throws BadSyntax, ExecutionException
      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 errors
      ExecutionException - if an error occurs during execution
    • execute

      public Object execute(Command code)
      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

      public Program compile()
      Compiles the source code into an executable Program object. This method uses double-checked locking to ensure thread-safe compilation. If the code has already been compiled, the cached Program is returned. Otherwise, the source code is lexically analyzed and processed into a series of commands.
      Returns:
      the compiled Program object representing the executable commands
    • serialize

      public byte[] serialize()