11package dotty .tools .repl
22
3- import java .io .PrintStream
3+ import java .io .{ PrintStream , OutputStream }
44
55import dotty .tools .dotc .ast .Trees ._
66import dotty .tools .dotc .ast .{tpd , untpd }
@@ -54,6 +54,8 @@ case class State(objectIndex: Int,
5454/** Main REPL instance, orchestrating input, compilation and presentation */
5555class ReplDriver (settings : Array [String ],
5656 out : PrintStream = Console .out,
57+ initialCommands : Option [String ] = None ,
58+ cleanupCommands : Option [String ] = None ,
5759 classLoader : Option [ClassLoader ] = None ) extends Driver {
5860
5961 /** Overridden to `false` in order to not have to give sources on the
@@ -121,13 +123,28 @@ class ReplDriver(settings: Array[String],
121123 }
122124 }
123125
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+
124136 @ tailrec def loop (state : State ): State = {
125137 val res = readLine(state)
126138 if (res == Quit ) state
127139 else loop(interpret(res)(state))
128140 }
129141
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+ }
131148 finally terminal.close()
132149 }
133150
@@ -136,9 +153,20 @@ class ReplDriver(settings: Array[String],
136153 interpret(parsed)
137154 }
138155
156+ private def ignored : PrintStream = {
157+ val ignoringOutputStream = new OutputStream {
158+ def write (b : Int ): Unit = {}
159+ }
160+
161+ new PrintStream (ignoringOutputStream)
162+ }
163+
139164 private def withRedirectedOutput (op : => State ): State =
140165 Console .withOut(out) { Console .withErr(out) { op } }
141166
167+ private def withSilencedOutput (op : => State ): State =
168+ Console .withOut(ignored) { Console .withErr(out) { op } }
169+
142170 private def newRun (state : State ) = {
143171 val run = compiler.newRun(rootCtx.fresh.setReporter(newStoreReporter), state.objectIndex)
144172 state.copy(context = run.runContext)
0 commit comments