@@ -77,7 +77,9 @@ case class Completions(cursor: Int,
77
77
/** Main REPL instance, orchestrating input, compilation and presentation */
78
78
class ReplDriver (settings : Array [String ],
79
79
protected val out : PrintStream = System .out,
80
- protected val classLoader : Option [ClassLoader ] = None ) extends Driver {
80
+ protected val classLoader : Option [ClassLoader ] = None ,
81
+ initialCommands : Array [String ] = Array .empty,
82
+ cleanupCommands : Array [String ] = Array .empty) extends Driver {
81
83
82
84
/** Overridden to `false` in order to not have to give sources on the
83
85
* commandline
@@ -139,12 +141,24 @@ class ReplDriver(settings: Array[String],
139
141
}
140
142
}
141
143
144
+ final def runWrapper (initialState : State = initState): State = {
145
+ val state = runBootstrapCommands(initialCommands)(initialState)
146
+ val userState = runUntilQuit(state)
147
+ runBootstrapCommands(cleanupCommands)(userState)
148
+ }
149
+
142
150
final def run (input : String )(implicit state : State ): State =
143
151
run(ParseResult (input)(state.run.runContext))(state.newRun(compiler, rootCtx))
144
152
145
153
final def run (res : ParseResult )(implicit state : State ): State =
146
154
interpret(res)
147
155
156
+ final def runBootstrapCommands (cmds : Array [String ])(implicit state : State ): State = {
157
+ cmds.map(ParseResult .apply(_)(rootCtx)).map(Silent .apply(_)).foldLeft(state) { (s, cmd) =>
158
+ interpret(cmd)(s)
159
+ }
160
+ }
161
+
148
162
/** Extract possible completions at the index of `cursor` in `expr` */
149
163
protected [this ] final def completions (cursor : Int , expr : String , state0 : State ): Completions = {
150
164
// TODO move some of this logic to `Interactive`
@@ -179,12 +193,12 @@ class ReplDriver(settings: Array[String],
179
193
private def extractImports (trees : List [untpd.Tree ])(implicit context : Context ): List [(untpd.Import , String )] =
180
194
trees.collect { case imp : untpd.Import => (imp, imp.show) }
181
195
182
- private def interpret (res : ParseResult )(implicit state : State ): State = {
183
- val isSilent = res.isInstanceOf [Silent ]
184
- val parseResult = res match {
185
- case Silent (v) => v
186
- case _ => res
196
+ private def interpret (res : Parsing )(implicit state : State ): State = {
197
+ val (parseResult, isSilent) = res match {
198
+ case Silent (x) => (x, true )
199
+ case x : ParseResult => (x, false )
187
200
}
201
+
188
202
parseResult match {
189
203
case parsed : Parsed =>
190
204
compile(parsed, isSilent)
0 commit comments