class IMain extends Imports with PresentationCompilation with Closeable
An interpreter for Scala code.
The main public entry points are compile(), interpret(), and bind(). The compile() method loads a complete Scala file. The interpret() method executes one line of Scala code at the request of the user. The bind() method binds an object to a variable that can then be used by later interpreted code.
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 member(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 members called "$eval" and "$print". 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.
- Self Type
- IMain
- Source
- IMain.scala
- Alphabetic
- By Inheritance
- IMain
- Closeable
- AutoCloseable
- PresentationCompilation
- Imports
- AnyRef
- Any
- by IMainOps
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
- Public
- All
Instance Constructors
- new IMain(initialSettings: Settings)
- new IMain(initialSettings: Settings, out: JPrintWriter)
Type Members
- abstract class PhaseDependentOps extends AnyRef
-
class
ReadEvalPrint extends AnyRef
Here is where we:
Here is where we:
1) Read some source code, and put it in the "read" object. 2) Evaluate the read object, and put the result in the "eval" object. 3) Create a String for human consumption, and put it in the "print" object.
Read! Eval! Print! Some of that not yet centralized here.
- implicit class ReplTypeOps extends AnyRef
-
class
Request extends AnyRef
One line of code submitted by the user for interpretation
-
case class
ComputedImports(header: String, prepend: String, append: String, access: String) extends Product with Serializable
Compute imports that allow definitions from previous requests to be visible in a new request.
Compute imports that allow definitions from previous requests to be visible in a new request. Returns three or four pieces of related code:
0. Header code fragment that should go at the beginning of the compilation unit, specifically, import Predef.
- An initial code fragment that should go before the code of the new request.
2. A code fragment that should go after the code of the new request.
3. An access path which can be traversed to access any bindings inside code wrapped by #1 and #2 .
The argument is a set of Names that need to be imported.
Limitations: This method is not as precise as it could be. (1) It does not process wildcard imports to see what exactly they import. (2) If it imports any names from a request, it imports all of them, which is not really necessary. (3) It imports multiple same-named implicits, but only the last one imported is actually usable.
- Definition Classes
- Imports
-
abstract
class
PresentationCompileResult extends AnyRef
- Definition Classes
- PresentationCompilation
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
- def +(other: String): String
- def ->[B](y: B): (IMain, B)
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
addUrlsToClassPath(urls: URL*): Unit
Adds all specified jars to the compile and runtime classpaths.
Adds all specified jars to the compile and runtime classpaths.
- urls
The list of items to add to the compile and runtime classpaths.
- Note
Currently only supports jars, not directories.
- def allDefinedNames: List[Global.Name]
- def allHandlers: collection.immutable.List[(memberHandlers)#MemberHandler]
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
- def backticked(s: String): String
-
def
beQuietDuring[T](body: ⇒ T): T
Temporarily be quiet
- def beSilentDuring[T](operation: ⇒ T): T
- def bind[T](name: String, value: T)(implicit arg0: reflect.api.JavaUniverse.TypeTag[T], arg1: ClassTag[T]): Result
- def bind(p: NamedParam): Result
-
def
bind(name: String, boundType: String, value: Any, modifiers: List[String] = Nil): Result
Bind a specified name to a specified value.
Bind a specified name to a specified value. The name may later be used by expressions passed to interpret.
A fresh
ReadEvalPrint
, which defines aline
package, is used to compile a customeval
object that wraps the bound value.If the bound value is successfully installed, then bind the name by interpreting
val name = $line42.$eval.value
.- name
the variable name to bind
- boundType
the type of the variable, as a string
- value
the object value to bind to it
- returns
an indication of whether the binding succeeded
- def classLoader: util.AbstractFileClassLoader
- def classOfTerm(id: String): Option[JClass]
- def cleanMemberDecl(owner: Global.Symbol, member: Global.Name): Global.Type
- def cleanTypeAfterTyper(sym: ⇒ Global.Symbol): Global.Type
- def clearExecutionWrapper(): Unit
-
def
clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
def
close(): Unit
This instance is no longer needed, so release any resources it is using.
This instance is no longer needed, so release any resources it is using. The reporter's output gets flushed.
- Definition Classes
- IMain → Closeable → AutoCloseable
-
def
compileSources(sources: SourceFile*): Boolean
Compile an nsc SourceFile.
Compile an nsc SourceFile. Returns true if there are no compilation errors, or false otherwise.
- def compileSourcesKeepingRun(sources: SourceFile*): (Boolean, Run)
-
def
compileString(code: String): Boolean
Compile a string.
Compile a string. Returns true if there are no compilation errors, or false otherwise.
- def compilerClasspath: Seq[URL]
- def dealiasNonPublic(tp: Global.Type): Global.Type
- def debugging[T](msg: String)(res: T): T
- def definedSymbolList: collection.immutable.List[Global.Symbol]
- def definedTerms: collection.immutable.List[Global.TermName]
- def definedTypes: List[Global.TypeName]
- def directBind[T](name: String, value: T)(implicit arg0: reflect.api.JavaUniverse.TypeTag[T], arg1: ClassTag[T]): Result
- def directBind(p: NamedParam): Result
- def directBind(name: String, boundType: String, value: Any): Result
-
def
echoKind: ((tpe: _1.intp.global.Type, kind: _1.intp.global.Kind, verbose: Boolean)Unit) forSome {val _1: IMainOps}
- Implicit
- This member is added by an implicit conversion from IMain to IMainOps performed by method IMainOps in scala.tools.nsc.interpreter.
- Definition Classes
- IMainOps
-
def
echoTypeSignature: ((sym: _1.intp.global.Symbol, verbose: Boolean)Unit) forSome {val _1: IMainOps}
- Implicit
- This member is added by an implicit conversion from IMain to IMainOps performed by method IMainOps in scala.tools.nsc.interpreter.
- Definition Classes
- IMainOps
-
def
echoTypeStructure: ((sym: _1.intp.global.Symbol)Unit) forSome {val _1: IMainOps}
- Implicit
- This member is added by an implicit conversion from IMain to IMainOps performed by method IMainOps in scala.tools.nsc.interpreter.
- Definition Classes
- IMainOps
- final def ensureClassLoader(): Unit
- def ensuring(cond: (IMain) ⇒ Boolean, msg: ⇒ Any): IMain
- def ensuring(cond: (IMain) ⇒ Boolean): IMain
- def ensuring(cond: Boolean, msg: ⇒ Any): IMain
- def ensuring(cond: Boolean): IMain
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def executionWrapper: String
-
def
finalize(): Unit
- Definition Classes
- IMain → AnyRef
- def flatPath(sym: Global.Symbol): String
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def getClassIfDefined(path: String): Global.Symbol
- def getModuleIfDefined(path: String): Global.Symbol
- lazy val global: Global
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
implicitSymbolsBySource: List[(Global.Symbol, List[Global.Symbol])]
- Definition Classes
- Imports
-
def
implicitsCommand(line: String): String
- Implicit
- This member is added by an implicit conversion from IMain to IMainOps performed by method IMainOps in scala.tools.nsc.interpreter.
- Definition Classes
- IMainOps
- def importHandlers: collection.immutable.List[(memberHandlers)#ImportHandler]
-
def
importsCode(wanted: Set[Global.Name], wrapper: Wrapper, generousImports: Boolean): ComputedImports
- Attributes
- protected
- Definition Classes
- Imports
- def initialize(postInitSignal: ⇒ Unit): Unit
- def initializeSynchronous(): Unit
- def interpret(line: String, synthetic: Boolean): Result
-
def
interpret(line: String): Result
Interpret one line of input.
Interpret one line of input. All feedback, including parse errors and evaluation results, are printed via the supplied compiler's reporter. Values defined are available for future interpreted strings.
The return value is whether the line was interpreter successfully, e.g. that there were no parse errors.
- def interpretSynthetic(line: String): Result
-
val
intp: IMain
- Implicit
- This member is added by an implicit conversion from IMain to IMainOps performed by method IMainOps in scala.tools.nsc.interpreter.
- Definition Classes
- IMainOps
- lazy val isClassBased: Boolean
- def isInitializeComplete: Boolean
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
lazy val
isettings: ISettings
interpreter settings
-
def
kindCommandInternal(typeString: String, verbose: Boolean): Unit
- Implicit
- This member is added by an implicit conversion from IMain to IMainOps performed by method IMainOps in scala.tools.nsc.interpreter.
- Definition Classes
- IMainOps
-
def
languageWildcardHandlers: collection.immutable.List[(memberHandlers)#ImportHandler]
- Definition Classes
- Imports
- def lastRequest: Request
- def lastWarnings: List[(Global.Position, String)]
- lazy val memberHandlers: MemberHandlers { val intp: IMain.this.type }
-
def
mostRecentVar: String
Returns the name of the most recent interpreter result.
Returns the name of the most recent interpreter result. Mostly this exists so you can conveniently invoke methods on the previous result.
- def namedDefinedTerms: collection.immutable.List[Global.TermName]
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
newCompiler(settings: Settings, reporter: Reporter): ReplGlobal
Instantiate a compiler.
Instantiate a compiler. Overridable.
- Attributes
- protected
-
def
newPresentationCompiler(): interactive.Global
Create an instance of the presentation compiler with a classpath comprising the REPL's configured classpath and the classes output by previously compiled REPL lines.
Create an instance of the presentation compiler with a classpath comprising the REPL's configured classpath and the classes output by previously compiled REPL lines.
You may directly interact with this compiler from any thread, although you must not access it concurrently from multiple threads.
You may downcast the
reporter
toStoreReporter
to access type errors.- Definition Classes
- PresentationCompilation
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
def
onlyTerms(xs: List[Global.Name]): List[Global.TermName]
- Attributes
- protected
-
def
onlyTypes(xs: List[Global.Name]): List[Global.TypeName]
- Attributes
- protected
- def originalPath(sym: Global.Symbol): String
- def originalPath(name: Global.Name): String
- def originalPath(name: String): String
-
val
out: JPrintWriter
- Attributes
- protected
-
def
parentClassLoader: ClassLoader
Parent classloader.
Parent classloader. Overridable.
- Attributes
- protected
- var partialInput: String
- def prevRequestList: collection.immutable.List[Request]
-
def
printAfterTyper(msg: ⇒ String): Unit
- Implicit
- This member is added by an implicit conversion from IMain to IMainOps performed by method IMainOps in scala.tools.nsc.interpreter.
- Definition Classes
- IMainOps
- def quietBind(p: NamedParam): Result
- def quietRun[T](code: String): Result
- val readInstanceName: String
- def readRootPath(readPath: String): Global.Symbol
- def rebind(p: NamedParam): Result
- def recordRequest(req: Request): Unit
- def replScope: Global.Scope
- lazy val reporter: ReplReporter
-
def
reset(): Unit
Reset this interpreter, forgetting all user-specified requests.
- def resetClassLoader(): Unit
- def runtimeClassAndTypeOfTerm(id: String): Option[(JClass, Global.Type)]
- def runtimeMirror: Mirror
- def runtimeTypeOfTerm(id: String): Global.Type
- def setExecutionWrapper(code: String): Unit
- def settings: Settings
- def showCodeIfDebugging(code: String): Unit
- def showDirectory(): Unit
- def symbolDefString(sym: Global.Symbol): String
- def symbolOfIdent(id: String): Global.Symbol
- def symbolOfLine(code: String): Global.Symbol
- def symbolOfName(id: Global.Name): Global.Symbol
- def symbolOfTerm(id: String): Global.Symbol
- def symbolOfType(id: String): Global.Symbol
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AnyRef → Any
- def translateEnclosingClass(n: String): Option[String]
- def translateOriginalPath(p: String): String
- def translatePath(path: String): Option[String]
-
def
translateSimpleResource(path: String): Option[String]
If path represents a class resource in the default package, see if the corresponding symbol has a class file that is a REPL artifact residing at a different resource path.
If path represents a class resource in the default package, see if the corresponding symbol has a class file that is a REPL artifact residing at a different resource path. Translate X.class to $line3/$read$iw$X.class.
-
def
tryTwice(op: ⇒ Global.Symbol): Global.Symbol
It's a bit of a shotgun approach, but for now we will gain in robustness.
It's a bit of a shotgun approach, but for now we will gain in robustness. Try a symbol-producing operation at phase typer, and if that is NoSymbol, try again at phase flatten. I'll be able to lose this and run only from exitingTyper as soon as I figure out exactly where a flat name is sneaking in when calculating imports.
-
def
typeCommandInternal(expr: String, verbose: Boolean): Unit
TODO - -n normalize -l label with case class parameter names -c complete - leave nothing out
TODO - -n normalize -l label with case class parameter names -c complete - leave nothing out
- Implicit
- This member is added by an implicit conversion from IMain to IMainOps performed by method IMainOps in scala.tools.nsc.interpreter.
- Definition Classes
- IMainOps
- def typeOfExpression(expr: String, silent: Boolean = true): Global.Type
- def typeOfTerm(id: String): Global.Type
- def unqualifiedIds: List[String]
- def valueOfTerm(id: String): Option[Any]
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
- def withLabel[A](temp: String)(body: ⇒ A): A
- def withoutTruncating[A](body: ⇒ A): A
- def withoutUnwrapping(op: ⇒ Unit): Unit
- def withoutWarnings[T](body: ⇒ T): T
- def →[B](y: B): (IMain, B)
- object deconstruct extends StructuredTypeStrings
- object exprTyper extends ExprTyper
- object flatOp extends PhaseDependentOps
- object naming extends Naming
-
object
parse
Parse a line into and return parsing result (error, incomplete or success with list of trees)
- object replOutput extends ReplOutput
- object typerOp extends PhaseDependentOps
-
object
PresentationCompileResult
- Definition Classes
- PresentationCompilation
Deprecated Value Members
-
def
formatted(fmtstr: String): String
- Implicit
- This member is added by an implicit conversion from IMain to StringFormat[IMain] performed by method StringFormat in scala.Predef.
- Definition Classes
- StringFormat
- Annotations
- @deprecated @inline()
- Deprecated
(Since version 2.12.16) Use
formatString.format(value)
instead ofvalue.formatted(formatString)
, or use thef""
string interpolator. In Java 15 and later,formatted
resolves to the new method in String which has reversed parameters.
-
def
setContextClassLoader(): Unit
- Annotations
- @deprecated
- Deprecated
(Since version 2.12.0) The thread context classloader is now set and restored around execution of REPL line, this method is now a no-op.
-
def
virtualDirectory: ReplDir
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) Use replOutput.dir instead
The Scala compiler and reflection APIs.