@@ -156,7 +156,7 @@ class CompilerAndRunner(
156
156
}
157
157
}
158
158
159
- val reporter = new KojoReporter {
159
+ val runReporter = new KojoReporter {
160
160
def lineMod = prefixLines + 1 // we added an extra line after the prefix in the code template.
161
161
def offsetMod = offsetDelta + 1 // we added an extra newline char after the prefix
162
162
}
@@ -167,7 +167,51 @@ class CompilerAndRunner(
167
167
}
168
168
169
169
val compiler = classLoader.asContext {
170
- new Global (runSettings, reporter)
170
+ new Global (runSettings, runReporter)
171
+ }
172
+
173
+ object SRSwitcher {
174
+ val Run = 1
175
+ val Exec = 2
176
+ private var prevMode = Run
177
+
178
+ private var verbose = false
179
+ private def debugPrintln (s : => String ): Unit = {
180
+ if (verbose) {
181
+ println(s)
182
+ }
183
+ }
184
+
185
+ def adjust (currMode : Int ): Unit = {
186
+ if (prevMode == Exec && currMode == Run ) {
187
+ debugPrintln(" Changing to compiler run settings/reporter" )
188
+ compiler.currentSettings = runSettings
189
+ compiler.reporter = runReporter
190
+ prevMode = Run
191
+ }
192
+ if (prevMode == Run && currMode == Exec ) {
193
+ debugPrintln(" Changing to compiler exec settings/reporter" )
194
+ compiler.currentSettings = execSettings
195
+ compiler.reporter = execReporter
196
+ prevMode = Exec
197
+ }
198
+ }
199
+
200
+ def newRunSettings (setReporter : Boolean ): Unit = {
201
+ debugPrintln(" Changing to *new* compiler run settings" )
202
+ compiler.currentSettings = makeRunSettings
203
+ if (setReporter && prevMode == Exec ) {
204
+ debugPrintln(" Changing to compiler run reporter" )
205
+ compiler.reporter = runReporter
206
+ }
207
+ prevMode = Run
208
+ }
209
+
210
+ def restoreRunSettings (): Unit = {
211
+ debugPrintln(" Restoring compiler run settings" )
212
+ compiler.currentSettings = runSettings
213
+ verbose = if (System .getProperty(" kojo.compiler.sr.verbose" ) == " true" ) true else false
214
+ }
171
215
}
172
216
173
217
def pfxWithCounter = " %s%d%s" .format(prefixHeader, counter, prefix)
@@ -202,30 +246,30 @@ class CompilerAndRunner(
202
246
def compileForRunning (code0 : String , stopPhase : List [String ] = List (" cleanup" )): Results .Result = {
203
247
codeForRunning(code0)
204
248
.map { code =>
205
- compiler.currentSettings = runSettings
206
- compiler.reporter = reporter
249
+ SRSwitcher .adjust( SRSwitcher . Run )
250
+ var sChanged = false
207
251
if (compiler.settings.stopAfter.value != stopPhase) {
208
- // There seems to be a bug in the PhasesSetting contains method
209
- // which makes the compiler not see the new stopAfter value
210
- // So we make a new Settings
211
- compiler.currentSettings = makeRunSettings
252
+ SRSwitcher .newRunSettings(false )
253
+ sChanged = true
212
254
compiler.settings.stopAfter.value = stopPhase
213
255
}
214
256
215
257
val run = new compiler.Run
216
- reporter .reset()
258
+ runReporter .reset()
217
259
run.compileSources(List (new BatchSourceFile (" scripteditor" , code)))
218
260
// println(s"[Debug] Script checking done till phase: ${compiler.globalPhase.prev}")
219
- if (reporter.hasErrors) IR .Error else IR .Success
261
+ if (sChanged) {
262
+ SRSwitcher .restoreRunSettings()
263
+ }
264
+ if (runReporter.hasErrors) IR .Error else IR .Success
220
265
}
221
266
.getOrElse(IR .Error )
222
267
}
223
268
224
269
def compileForExecing (code0 : String ): Results .Result = {
225
270
codeForExecing(code0)
226
271
.map { code =>
227
- compiler.currentSettings = execSettings
228
- compiler.reporter = execReporter
272
+ SRSwitcher .adjust(SRSwitcher .Exec )
229
273
val run = new compiler.Run
230
274
execReporter.reset()
231
275
run.compileSources(List (new BatchSourceFile (" scripteditor" , code)))
@@ -322,16 +366,16 @@ class CompilerAndRunner(
322
366
def parse (code0 : String , browseAst : Boolean ) = {
323
367
codeForRunning(code0)
324
368
.map { code =>
325
- compiler.currentSettings = makeRunSettings
369
+ SRSwitcher .newRunSettings( true )
326
370
compiler.settings.stopAfter.value = stopPhase()
327
371
if (browseAst) {
328
372
compiler.settings.browse.value = stopPhase()
329
373
}
330
374
val run = new compiler.Run
331
- reporter .reset()
375
+ runReporter .reset()
332
376
run.compileSources(List (new BatchSourceFile (" scripteditor" , code)))
333
-
334
- if (reporter .hasErrors) {
377
+ SRSwitcher .restoreRunSettings()
378
+ if (runReporter .hasErrors) {
335
379
IR .Error
336
380
}
337
381
else {
0 commit comments