@@ -51,11 +51,7 @@ import scala.collection.JavaConverters._
51
51
case class State (objectIndex : Int ,
52
52
valIndex : Int ,
53
53
imports : List [untpd.Import ],
54
- run : Run ) {
55
-
56
- def newRun (comp : ReplCompiler , rootCtx : Context ): State =
57
- copy(run = comp.newRun(rootCtx, objectIndex))
58
- }
54
+ run : Run )
59
55
60
56
/** Main REPL instance, orchestrating input, compilation and presentation */
61
57
class ReplDriver (settings : Array [String ],
@@ -101,7 +97,7 @@ class ReplDriver(settings: Array[String],
101
97
102
98
protected [this ] var rootCtx : Context = _
103
99
protected [this ] var compiler : ReplCompiler = _
104
- protected [this ] var rendering : Rendering = _
100
+ private [this ] var rendering : Rendering = _
105
101
106
102
// initialize the REPL session as part of the constructor so that once `run`
107
103
// is called, we're in business
@@ -135,14 +131,8 @@ class ReplDriver(settings: Array[String],
135
131
136
132
@ tailrec def loop (state : State ): State = {
137
133
val res = readLine(state)
138
-
139
134
if (res == Quit ) state
140
- else {
141
- // readLine potentially destroys the run, so a new one is needed for the
142
- // rest of the interpretation:
143
- val freshState = state.newRun(compiler, rootCtx)
144
- loop(interpret(res)(freshState))
145
- }
135
+ else loop(interpret(res)(state))
146
136
}
147
137
148
138
try withRedirectedOutput { loop(initState) }
@@ -151,12 +141,16 @@ class ReplDriver(settings: Array[String],
151
141
152
142
final def run (input : String )(implicit state : State ): State = withRedirectedOutput {
153
143
val parsed = ParseResult (input)(state.run.runContext)
154
- interpret(parsed)(state.newRun(compiler, rootCtx))
144
+ interpret(parsed)
155
145
}
156
146
157
147
private def withRedirectedOutput (op : => State ): State =
158
148
Console .withOut(out) { Console .withErr(out) { op } }
159
149
150
+ private def newRun (state : State ) = {
151
+ val newRun = compiler.newRun(rootCtx.fresh.setReporter(newStoreReporter), state.objectIndex)
152
+ state.copy(run = newRun)
153
+ }
160
154
161
155
/** Extract possible completions at the index of `cursor` in `expr` */
162
156
protected [this ] final def completions (cursor : Int , expr : String , state0 : State ): List [Candidate ] = {
@@ -172,7 +166,7 @@ class ReplDriver(settings: Array[String],
172
166
/* complete = */ false // if true adds space when completing
173
167
)
174
168
}
175
- implicit val state = state0. newRun(compiler, rootCtx )
169
+ implicit val state = newRun(state0 )
176
170
compiler
177
171
.typeCheck(expr, errorsAllowed = true )
178
172
.map { tree =>
@@ -193,7 +187,7 @@ class ReplDriver(settings: Array[String],
193
187
private def interpret (res : ParseResult )(implicit state : State ): State = {
194
188
val newState = res match {
195
189
case parsed : Parsed if parsed.trees.nonEmpty =>
196
- compile(parsed).newRun(compiler, rootCtx )
190
+ compile(parsed, state )
197
191
198
192
case SyntaxErrors (src, errs, _) =>
199
193
displayErrors(errs)
@@ -213,12 +207,13 @@ class ReplDriver(settings: Array[String],
213
207
}
214
208
215
209
/** Compile `parsed` trees and evolve `state` in accordance */
216
- protected [ this ] final def compile (parsed : Parsed )( implicit state : State ): State = {
210
+ private def compile (parsed : Parsed , istate : State ): State = {
217
211
def extractNewestWrapper (tree : untpd.Tree ): Name = tree match {
218
212
case PackageDef (_, (obj : untpd.ModuleDef ) :: Nil ) => obj.name.moduleClassName
219
213
case _ => nme.NO_NAME
220
214
}
221
215
216
+ implicit val state = newRun(istate)
222
217
compiler
223
218
.compile(parsed)
224
219
.fold(
@@ -331,22 +326,15 @@ class ReplDriver(settings: Array[String],
331
326
val file = new java.io.File (path)
332
327
if (file.exists) {
333
328
val contents = scala.io.Source .fromFile(file).mkString
334
- ParseResult (contents)(state.run.runContext) match {
335
- case parsed : Parsed =>
336
- compile(parsed)
337
- case SyntaxErrors (_, errors, _) =>
338
- displayErrors(errors)
339
- case _ =>
340
- state
341
- }
329
+ run(contents)
342
330
}
343
331
else {
344
332
out.println(s """ Couldn't find file " ${file.getCanonicalPath}" """ )
345
333
state
346
334
}
347
335
348
336
case TypeOf (expr) =>
349
- compiler.typeOf(expr).fold(
337
+ compiler.typeOf(expr)(newRun(state)) .fold(
350
338
displayErrors,
351
339
res => out.println(SyntaxHighlighting (res))
352
340
)
0 commit comments