1
1
package dotty .tools .repl
2
2
3
- import java .io .PrintStream
3
+ import java .io .{ PrintStream , OutputStream }
4
4
5
5
import dotty .tools .dotc .ast .Trees ._
6
6
import dotty .tools .dotc .ast .{tpd , untpd }
@@ -54,6 +54,8 @@ case class State(objectIndex: Int,
54
54
/** Main REPL instance, orchestrating input, compilation and presentation */
55
55
class ReplDriver (settings : Array [String ],
56
56
out : PrintStream = Console .out,
57
+ initialCommands : Option [String ] = None ,
58
+ cleanupCommands : Option [String ] = None ,
57
59
classLoader : Option [ClassLoader ] = None ) extends Driver {
58
60
59
61
/** Overridden to `false` in order to not have to give sources on the
@@ -121,13 +123,28 @@ class ReplDriver(settings: Array[String],
121
123
}
122
124
}
123
125
126
+ def readAll (rawCommands : Option [String ])(initialState : State ): State = {
127
+ rawCommands match {
128
+ case None => initialState
129
+ case Some (cmds) => {
130
+ val command = ParseResult (cmds)(rootCtx)
131
+ interpret(command)(initialState)
132
+ }
133
+ }
134
+ }
135
+
124
136
@ tailrec def loop (state : State ): State = {
125
137
val res = readLine(state)
126
138
if (res == Quit ) state
127
139
else loop(interpret(res)(state))
128
140
}
129
141
130
- try withRedirectedOutput { loop(initState) }
142
+ try {
143
+ // TODO: how to chain it well?
144
+ val a = withSilencedOutput { readAll(initialCommands)(initState) }
145
+ val b = withRedirectedOutput { loop(a) }
146
+ withSilencedOutput { readAll(cleanupCommands)(b) }
147
+ }
131
148
finally terminal.close()
132
149
}
133
150
@@ -136,9 +153,20 @@ class ReplDriver(settings: Array[String],
136
153
interpret(parsed)
137
154
}
138
155
156
+ private def ignored : PrintStream = {
157
+ val ignoringOutputStream = new OutputStream {
158
+ def write (b : Int ): Unit = {}
159
+ }
160
+
161
+ new PrintStream (ignoringOutputStream)
162
+ }
163
+
139
164
private def withRedirectedOutput (op : => State ): State =
140
165
Console .withOut(out) { Console .withErr(out) { op } }
141
166
167
+ private def withSilencedOutput (op : => State ): State =
168
+ Console .withOut(ignored) { Console .withErr(out) { op } }
169
+
142
170
private def newRun (state : State ) = {
143
171
val run = compiler.newRun(rootCtx.fresh.setReporter(newStoreReporter), state.objectIndex)
144
172
state.copy(context = run.runContext)
0 commit comments