Skip to content

Commit 8669174

Browse files
committed
tentative fix for scala#1854
1 parent bf1082d commit 8669174

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import java.io.{
99
import java.lang.{Class, ClassLoader, Thread, System, StringBuffer}
1010
import java.net.{URL, URLClassLoader}
1111

12+
import scala.annotation.tailrec
1213
import scala.collection.immutable.ListSet
1314
import scala.collection.mutable
1415
import scala.collection.mutable.{ListBuffer, HashSet, ArrayBuffer}
@@ -204,36 +205,46 @@ class CompilingInterpreter(
204205
!runCtx.reporter.hasErrors
205206
}
206207

207-
override def interpret(line: String)(implicit ctx: Context): Interpreter.Result = {
208-
// if (prevRequests.isEmpty)
209-
// new Run(this) // initialize the compiler // (not sure this is needed)
210-
// parse
211-
parse(line) match {
208+
@tailrec
209+
private final def interpretInner(treesMaybe: Option[List[Tree]])(implicit ctx: Context): Interpreter.Result = {
210+
treesMaybe match {
212211
case None => Interpreter.Incomplete
213212
case Some(Nil) => Interpreter.Error // parse error or empty input
214-
case Some(tree :: Nil) if tree.isTerm && !tree.isInstanceOf[Assign] =>
215-
previousOutput.clear() // clear previous error reporting
216-
interpret(s"val $newVarName =\n$line")
217-
case Some(trees) =>
218-
previousOutput.clear() // clear previous error reporting
219-
val req = new Request(line, newLineName)
220-
if (!req.compile())
221-
Interpreter.Error // an error happened during compilation, e.g. a type error
213+
case Some(tree :: rest) =>
214+
if (tree.isTerm && !tree.isInstanceOf[Assign]) {
215+
// reparse literal to be assignment
216+
interpretInner(parse(s"val $newVarName = ${tree.show}").map(trees => trees ::: rest))
217+
}
222218
else {
223-
val (resultStrings, succeeded) = req.loadAndRun()
224-
if (delayOutput)
225-
previousOutput ++= resultStrings.map(clean)
226-
else if (printResults || !succeeded)
227-
resultStrings.foreach(x => out.print(clean(x)))
228-
if (succeeded) {
229-
prevRequests += req
230-
Interpreter.Success
219+
val req = new Request(tree.show, newLineName)
220+
if (!req.compile())
221+
Interpreter.Error // an error happened during compilation, e.g. a type error
222+
else {
223+
val (resultStrings, succeeded) = req.loadAndRun()
224+
if (delayOutput)
225+
previousOutput ++= resultStrings.map(clean)
226+
else if (printResults || !succeeded)
227+
resultStrings.foreach(x => out.print(clean(x)))
228+
if (succeeded) {
229+
prevRequests += req
230+
Interpreter.Success
231+
}
232+
else Interpreter.Error
231233
}
232-
else Interpreter.Error
234+
interpretInner(Some(rest))
233235
}
234236
}
235237
}
236238

239+
override def interpret(line: String)(implicit ctx: Context): Interpreter.Result = {
240+
// if (prevRequests.isEmpty)
241+
// new Run(this) // initialize the compiler // (not sure this is needed)
242+
// parse
243+
previousOutput.clear()
244+
val parsedLine = parse(line)
245+
interpretInner(parsedLine)
246+
}
247+
237248
private def loadAndSetValue(objectName: String, value: AnyRef) = {
238249
/** This terrible string is the wrapped class's full name inside the
239250
* classloader:
@@ -318,7 +329,6 @@ class CompilingInterpreter(
318329
private class Request(val line: String, val lineName: String)(implicit ctx: Context) {
319330
private val trees = {
320331
val parsed = parse(line)
321-
previousOutput.clear() // clear previous error reporting
322332
parsed match {
323333
case Some(ts) => ts
324334
case None => Nil

0 commit comments

Comments
 (0)