Packages

class IMain extends Imports with PresentationCompilation

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
Linear Supertypes
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. IMain
  2. PresentationCompilation
  3. Imports
  4. AnyRef
  5. Any
Implicitly
  1. by IMainOps
  2. by any2stringadd
  3. by StringFormat
  4. by Ensuring
  5. by ArrowAssoc
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new IMain(initialSettings: Settings)
  2. new IMain(initialSettings: Settings, out: JPrintWriter)

Type Members

  1. abstract class PhaseDependentOps extends AnyRef
  2. 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.

  3. implicit class ReplTypeOps extends AnyRef
  4. class Request extends AnyRef

    One line of code submitted by the user for interpretation

  5. 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.

    1. 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
  6. abstract class PresentationCompileResult extends AnyRef
    Definition Classes
    PresentationCompilation

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. def +(other: String): String
    Implicit
    This member is added by an implicit conversion from IMain to any2stringadd[IMain] performed by method any2stringadd in scala.Predef.
    Definition Classes
    any2stringadd
  4. def ->[B](y: B): (IMain, B)
    Implicit
    This member is added by an implicit conversion from IMain to ArrowAssoc[IMain] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc
    Annotations
    @inline()
  5. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  6. 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.

  7. def allDefinedNames: List[Global.Name]
  8. def allHandlers: collection.immutable.List[(memberHandlers)#MemberHandler]
  9. def allImportedNames: collection.immutable.List[Global.Name]
    Definition Classes
    Imports
  10. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  11. def backticked(s: String): String
  12. def beQuietDuring[T](body: ⇒ T): T

    Temporarily be quiet

  13. def beSilentDuring[T](operation: ⇒ T): T
  14. def bind[T](name: String, value: T)(implicit arg0: reflect.api.JavaUniverse.TypeTag[T], arg1: ClassTag[T]): Result
  15. def bind(p: NamedParam): Result
  16. 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 a line package, is used to compile a custom eval 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

  17. def classLoader: util.AbstractFileClassLoader
  18. def classOfTerm(id: String): Option[JClass]
  19. def cleanMemberDecl(owner: Global.Symbol, member: Global.Name): Global.Type
  20. def cleanTypeAfterTyper(sym: ⇒ Global.Symbol): Global.Type
  21. def clearExecutionWrapper(): Unit
  22. def clone(): AnyRef
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )
  23. 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.

  24. def compileSources(sources: SourceFile*): Boolean

    Compile an nsc SourceFile.

    Compile an nsc SourceFile. Returns true if there are no compilation errors, or false otherwise.

  25. def compileSourcesKeepingRun(sources: SourceFile*): (Boolean, Run)
  26. def compileString(code: String): Boolean

    Compile a string.

    Compile a string. Returns true if there are no compilation errors, or false otherwise.

  27. def compilerClasspath: Seq[URL]
  28. def dealiasNonPublic(tp: Global.Type): Global.Type
  29. def debugging[T](msg: String)(res: T): T
  30. def definedSymbolList: collection.immutable.List[Global.Symbol]
  31. def definedTerms: collection.immutable.List[Global.TermName]
  32. def definedTypes: List[Global.TypeName]
  33. def directBind[T](name: String, value: T)(implicit arg0: reflect.api.JavaUniverse.TypeTag[T], arg1: ClassTag[T]): Result
  34. def directBind(p: NamedParam): Result
  35. def directBind(name: String, boundType: String, value: Any): Result
  36. 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
  37. 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
  38. 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
  39. final def ensureClassLoader(): Unit
  40. def ensuring(cond: (IMain) ⇒ Boolean, msg: ⇒ Any): IMain
    Implicit
    This member is added by an implicit conversion from IMain to Ensuring[IMain] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  41. def ensuring(cond: (IMain) ⇒ Boolean): IMain
    Implicit
    This member is added by an implicit conversion from IMain to Ensuring[IMain] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  42. def ensuring(cond: Boolean, msg: ⇒ Any): IMain
    Implicit
    This member is added by an implicit conversion from IMain to Ensuring[IMain] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  43. def ensuring(cond: Boolean): IMain
    Implicit
    This member is added by an implicit conversion from IMain to Ensuring[IMain] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  44. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  45. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  46. def executionWrapper: String
  47. def finalize(): Unit
    Definition Classes
    IMain → AnyRef
  48. def flatPath(sym: Global.Symbol): String
  49. 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
    @inline()
  50. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  51. def getClassIfDefined(path: String): Global.Symbol
  52. def getModuleIfDefined(path: String): Global.Symbol
  53. lazy val global: Global
  54. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  55. def implicitSymbolsBySource: List[(Global.Symbol, List[Global.Symbol])]
    Definition Classes
    Imports
  56. 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
  57. def importHandlers: collection.immutable.List[(memberHandlers)#ImportHandler]
  58. def importedSymbols: collection.immutable.List[Global.Symbol]
    Definition Classes
    Imports
  59. def importedSymbolsBySource: List[(Global.Symbol, List[Global.Symbol])]

    Tuples of (source, imported symbols) in the order they were imported.

    Tuples of (source, imported symbols) in the order they were imported.

    Definition Classes
    Imports
  60. def importedTermSymbols: collection.immutable.List[Global.TermSymbol]
    Definition Classes
    Imports
  61. def importsCode(wanted: Set[Global.Name], wrapper: Wrapper, definesClass: Boolean, generousImports: Boolean): ComputedImports
    Attributes
    protected
    Definition Classes
    Imports
  62. def initialize(postInitSignal: ⇒ Unit): Unit
  63. def initializeSynchronous(): Unit
  64. def interpret(line: String, synthetic: Boolean): Result
  65. 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.

  66. def interpretSynthetic(line: String): Result
  67. 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
  68. lazy val isClassBased: Boolean
  69. def isInitializeComplete: Boolean
  70. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  71. lazy val isettings: ISettings

    interpreter settings

  72. 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
  73. def languageSymbols: collection.immutable.List[Global.Symbol]
    Definition Classes
    Imports
  74. def languageWildcardHandlers: collection.immutable.List[(memberHandlers)#ImportHandler]
    Definition Classes
    Imports
  75. def languageWildcardSyms: List[Global.Symbol]

    Symbols whose contents are language-defined to be imported.

    Symbols whose contents are language-defined to be imported.

    Definition Classes
    Imports
  76. def lastRequest: Request
  77. def lastWarnings: List[(Global.Position, String)]
  78. lazy val memberHandlers: MemberHandlers { val intp: IMain.this.type }
  79. 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.

  80. def namedDefinedTerms: collection.immutable.List[Global.TermName]
  81. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  82. def newCompiler(settings: Settings, reporter: Reporter): ReplGlobal

    Instantiate a compiler.

    Instantiate a compiler. Overridable.

    Attributes
    protected
  83. 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 to StoreReporter to access type errors.

    Definition Classes
    PresentationCompilation
  84. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  85. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  86. def onlyTerms(xs: List[Global.Name]): List[Global.TermName]
    Attributes
    protected
  87. def onlyTypes(xs: List[Global.Name]): List[Global.TypeName]
    Attributes
    protected
  88. def originalPath(sym: Global.Symbol): String
  89. def originalPath(name: Global.Name): String
  90. def originalPath(name: String): String
  91. val out: JPrintWriter
    Attributes
    protected
  92. def parentClassLoader: ClassLoader

    Parent classloader.

    Parent classloader. Overridable.

    Attributes
    protected
  93. var partialInput: String
  94. def prevRequestList: collection.immutable.List[Request]
  95. 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
  96. def quietBind(p: NamedParam): Result
  97. def quietRun[T](code: String): Result
  98. val readInstanceName: String

    For class based repl mode we use an .INSTANCE accessor.

  99. def readRootPath(readPath: String): Global.Symbol
  100. def rebind(p: NamedParam): Result
  101. def recordRequest(req: Request): Unit
  102. def replScope: Global.Scope
  103. lazy val reporter: ReplReporter
  104. def reset(): Unit

    Reset this interpreter, forgetting all user-specified requests.

  105. def resetClassLoader(): Unit
  106. def runtimeClassAndTypeOfTerm(id: String): Option[(JClass, Global.Type)]
  107. def runtimeMirror: Mirror
  108. def runtimeTypeOfTerm(id: String): Global.Type
  109. def sessionImportedSymbols: collection.immutable.List[Global.Symbol]
    Definition Classes
    Imports
  110. def sessionWildcards: List[Global.Type]

    Types which have been wildcard imported, such as: val x = "abc" ; import x._ // type java.lang.String import java.lang.String._ // object java.lang.String

    Types which have been wildcard imported, such as: val x = "abc" ; import x._ // type java.lang.String import java.lang.String._ // object java.lang.String

    Used by tab completion.

    XXX right now this gets import x._ and import java.lang.String._, but doesn't figure out import String._. There's a lot of ad hoc scope twiddling which should be swept away in favor of digging into the compiler scopes.

    Definition Classes
    Imports
  111. def setExecutionWrapper(code: String): Unit
  112. def settings: Settings
  113. def showCodeIfDebugging(code: String): Unit
  114. def showDirectory(): Unit
  115. def symbolDefString(sym: Global.Symbol): String
  116. def symbolOfIdent(id: String): Global.Symbol
  117. def symbolOfLine(code: String): Global.Symbol
  118. def symbolOfName(id: Global.Name): Global.Symbol
  119. def symbolOfTerm(id: String): Global.Symbol
  120. def symbolOfType(id: String): Global.Symbol
  121. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  122. def toString(): String
    Definition Classes
    AnyRef → Any
  123. def translateEnclosingClass(n: String): Option[String]
  124. def translateOriginalPath(p: String): String
  125. def translatePath(path: String): Option[String]
  126. 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$$iw$X.class.

  127. 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.

  128. 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
  129. def typeOfExpression(expr: String, silent: Boolean = true): Global.Type
  130. def typeOfTerm(id: String): Global.Type
  131. def unqualifiedIds: List[String]
  132. def valueOfTerm(id: String): Option[Any]
  133. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  134. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  135. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )
  136. def withLabel[A](temp: String)(body: ⇒ A): A
  137. def withoutTruncating[A](body: ⇒ A): A
  138. def withoutUnwrapping(op: ⇒ Unit): Unit
  139. def withoutWarnings[T](body: ⇒ T): T
  140. def [B](y: B): (IMain, B)
    Implicit
    This member is added by an implicit conversion from IMain to ArrowAssoc[IMain] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc
  141. object deconstruct extends StructuredTypeStrings
  142. object exprTyper extends ExprTyper
  143. object flatOp extends PhaseDependentOps
  144. object naming extends Naming
  145. object parse

    Parse a line into and return parsing result (error, incomplete or success with list of trees)

  146. object replOutput extends ReplOutput
  147. object typerOp extends PhaseDependentOps
  148. object PresentationCompileResult
    Definition Classes
    PresentationCompilation

Deprecated Value Members

  1. 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.

  2. def virtualDirectory: ReplDir
    Annotations
    @deprecated
    Deprecated

    (Since version 2.11.0) Use replOutput.dir instead

Inherited from PresentationCompilation

Inherited from Imports

Inherited from AnyRef

Inherited from Any

Inherited by implicit conversion IMainOps from IMain to IMainOps

Inherited by implicit conversion any2stringadd from IMain to any2stringadd[IMain]

Inherited by implicit conversion StringFormat from IMain to StringFormat[IMain]

Inherited by implicit conversion Ensuring from IMain to Ensuring[IMain]

Inherited by implicit conversion ArrowAssoc from IMain to ArrowAssoc[IMain]

Ungrouped