@@ -9,6 +9,7 @@ import java.io.{
9
9
import java .lang .{Class , ClassLoader , Thread , System , StringBuffer }
10
10
import java .net .{URL , URLClassLoader }
11
11
12
+ import scala .annotation .tailrec
12
13
import scala .collection .immutable .ListSet
13
14
import scala .collection .mutable
14
15
import scala .collection .mutable .{ListBuffer , HashSet , ArrayBuffer }
@@ -204,36 +205,46 @@ class CompilingInterpreter(
204
205
! runCtx.reporter.hasErrors
205
206
}
206
207
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 {
212
211
case None => Interpreter .Incomplete
213
212
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
+ }
222
218
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
231
233
}
232
- else Interpreter . Error
234
+ interpretInner( Some (rest))
233
235
}
234
236
}
235
237
}
236
238
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
+
237
248
private def loadAndSetValue (objectName : String , value : AnyRef ) = {
238
249
/** This terrible string is the wrapped class's full name inside the
239
250
* classloader:
@@ -318,7 +329,6 @@ class CompilingInterpreter(
318
329
private class Request (val line : String , val lineName : String )(implicit ctx : Context ) {
319
330
private val trees = {
320
331
val parsed = parse(line)
321
- previousOutput.clear() // clear previous error reporting
322
332
parsed match {
323
333
case Some (ts) => ts
324
334
case None => Nil
0 commit comments