An interpreter for Scala code which is based on the dotc
compiler.
The overall approach is based on compiling the requested code and then using a Java classloader and Java reflection to run the code and access its results.
In more detail, a single compiler instance is used to accumulate all successfully compiled or interpreted Scala code. To "interpret" a line of code, the compiler generates a fresh object that includes the line of code and which has public definition(s) to export all variables defined by that code. To extract the result of an interpreted line to show the user, a second "result object" is created which imports the variables exported by the above object and then exports a single definition named "result". To accommodate user expressions that read from variables or methods defined in previous statements, "import" statements are used.
This interpreter shares the strengths and weaknesses of using the full compiler-to-Java. The main strength is that interpreted code behaves exactly as does compiled code, including running at full speed. The main weakness is that redefining classes and methods is not handled properly, because rebinding at the Java level is technically difficult.
Constructors
Members
A GenBCode phase that uses virtualDirectory
for its output
A GenBCode phase that uses virtualDirectory
for its output
One line of code submitted by the user for interpretation
One line of code submitted by the user for interpretation
Trait collecting info about one of the statements of an interpreter request
Trait collecting info about one of the statements of an interpreter request
class loader used to load compiled code
class loader used to load compiled code
the compiler's classpath, as URL's
the compiler's classpath, as URL's
next internal variable number to use
next internal variable number to use
next line number to use
next line number to use
next result variable number to use
next result variable number to use
the previous requests this interpreter has processed
the previous requests this interpreter has processed
whether to print out result lines
whether to print out result lines
directory to save .class files to
directory to save .class files to
Temporarily be quiet
Temporarily be quiet
This bind is implemented by creating an object with a set method and a
field value
. The value is then set via Java reflection.
Example: We want to bind... [Int] [Int]
This bind is implemented by creating an object with a set method and a
field value
. The value is then set via Java reflection.
Example: We want to bind a value List(1,2,3)
to identifier list
from
sbt. The bind method accomplishes this by creating the following:
{{{
object ContainerObjectWithUniqueID {
var value: List[Int] = _
def set(x: Any) = value = x.asInstanceOf[List[Int]]
}
val list = ContainerObjectWithUniqueID.value
}}}
Between the object being created and the value being assigned, the value inside the object is set via reflection.
Clean up a string for output
Clean up a string for output
Compile a SourceFile. Returns the root context of the run that compiled the file.
Compile a SourceFile. Returns the root context of the run that compiled the file.
Compile a string. Returns true if there are no compilation errors, or false otherwise.
Compile a string. Returns true if there are no compilation errors, or false otherwise.
Suppresses output and saves it for lastOutput
to collect
Suppresses output and saves it for lastOutput
to collect
Interpret one line of input. All feedback, including parse errors and evaluation results, are printed via the context's reporter. Values defined are ava...
Interpret one line of input. All feedback, including parse errors and evaluation results, are printed via the context's reporter. Values defined are available for future interpreted strings.
Check if a name looks like it was generated by newVarName
Check if a name looks like it was generated by newVarName
Gets the last output not printed immediately
Gets the last output not printed immediately
allocate a fresh internal variable name
allocate a fresh internal variable name
allocate a fresh line name
allocate a fresh line name
allocate a fresh variable name
allocate a fresh variable name
next internal variable number to use
next internal variable number to use
next line number to use
next line number to use
next result variable number to use
next result variable number to use
Parse a line into a sequence of trees. Returns None if the input is incomplete.
Parse a line into a sequence of trees. Returns None if the input is incomplete.
Phases of this compiler use REPLGenBCode
instead of GenBCode
.
Phases of this compiler use REPLGenBCode
instead of GenBCode
.
whether to print out result lines
whether to print out result lines
generate a string using a routine that wants to write on a stream
generate a string using a routine that wants to write on a stream
Truncate a string if it is longer than settings.maxPrintString
Truncate a string if it is longer than settings.maxPrintString