aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2016-03-17 16:57:10 +0100
committerMartin Odersky <odersky@gmail.com>2016-03-18 12:25:48 +0100
commitd8a7a59ef96127ca64f27e0cc2529d775b1fa9c9 (patch)
treed4e3a55d78195f82f750d3cac99ab59f2d6309e1
parente1fb19412c5dcc722e7df24e543aadf03a463c9a (diff)
downloaddotty-d8a7a59ef96127ca64f27e0cc2529d775b1fa9c9.tar.gz
dotty-d8a7a59ef96127ca64f27e0cc2529d775b1fa9c9.tar.bz2
dotty-d8a7a59ef96127ca64f27e0cc2529d775b1fa9c9.zip
Move all overridable bits into Config class
Central config class replaces mixture of parameters and fields. The fields were in part in the wrong class, where they could not easily be overridden.
-rw-r--r--src/dotty/tools/dotc/repl/InterpreterLoop.scala52
-rw-r--r--src/dotty/tools/dotc/repl/REPL.scala34
-rw-r--r--test/test/TestREPL.scala29
3 files changed, 60 insertions, 55 deletions
diff --git a/src/dotty/tools/dotc/repl/InterpreterLoop.scala b/src/dotty/tools/dotc/repl/InterpreterLoop.scala
index eedec3c82..4ac9602e7 100644
--- a/src/dotty/tools/dotc/repl/InterpreterLoop.scala
+++ b/src/dotty/tools/dotc/repl/InterpreterLoop.scala
@@ -21,10 +21,10 @@ import scala.concurrent.ExecutionContext.Implicits.global
* @author Lex Spoon
* @author Martin Odersky
*/
-class InterpreterLoop(
- compiler: Compiler,
- private var in: InteractiveReader,
- out: PrintWriter)(implicit ctx: Context) {
+class InterpreterLoop(compiler: Compiler, config: REPL.Config)(implicit ctx: Context) {
+ import config._
+
+ private var in = input
val interpreter = compiler.asInstanceOf[Interpreter]
@@ -52,24 +52,20 @@ class InterpreterLoop(
/** print a friendly help message */
def printHelp(): Unit = {
printWelcome()
- out.println("Type :load followed by a filename to load a Scala file.")
- out.println("Type :replay to reset execution and replay all previous commands.")
- out.println("Type :quit to exit the interpreter.")
+ output.println("Type :load followed by a filename to load a Scala file.")
+ output.println("Type :replay to reset execution and replay all previous commands.")
+ output.println("Type :quit to exit the interpreter.")
}
/** Print a welcome message */
def printWelcome(): Unit = {
- out.println(s"Welcome to Scala$version " + " (" +
+ output.println(s"Welcome to Scala$version " + " (" +
System.getProperty("java.vm.name") + ", Java " + System.getProperty("java.version") + ")." )
- out.println("Type in expressions to have them evaluated.")
- out.println("Type :help for more information.")
- out.flush()
+ output.println("Type in expressions to have them evaluated.")
+ output.println("Type :help for more information.")
+ output.flush()
}
- /** Prompt to print when awaiting input */
- val prompt = "scala> "
- val continuationPrompt = " | "
-
val version = ".next (pre-alpha)"
/** The first interpreted command always takes a couple of seconds
@@ -92,7 +88,7 @@ class InterpreterLoop(
val (keepGoing, finalLineOpt) = command(line)
if (keepGoing) {
finalLineOpt.foreach(addReplay)
- out.flush()
+ output.flush()
repl()
}
}
@@ -103,16 +99,16 @@ class InterpreterLoop(
new FileReader(filename)
} catch {
case _: IOException =>
- out.println("Error opening file: " + filename)
+ output.println("Error opening file: " + filename)
return
}
val oldIn = in
val oldReplay = replayCommandsRev
try {
val inFile = new BufferedReader(fileIn)
- in = new SimpleReader(inFile, out, false)
- out.println("Loading " + filename + "...")
- out.flush
+ in = new SimpleReader(inFile, output, false)
+ output.println("Loading " + filename + "...")
+ output.flush
repl()
} finally {
in = oldIn
@@ -124,10 +120,10 @@ class InterpreterLoop(
/** create a new interpreter and replay all commands so far */
def replay(): Unit = {
for (cmd <- replayCommands) {
- out.println("Replaying: " + cmd)
- out.flush() // because maybe cmd will have its own output
+ output.println("Replaying: " + cmd)
+ output.flush() // because maybe cmd will have its own output
command(cmd)
- out.println
+ output.println
}
}
@@ -138,12 +134,12 @@ class InterpreterLoop(
def withFile(command: String)(action: String => Unit): Unit = {
val spaceIdx = command.indexOf(' ')
if (spaceIdx <= 0) {
- out.println("That command requires a filename to be specified.")
+ output.println("That command requires a filename to be specified.")
return
}
val filename = command.substring(spaceIdx).trim
if (!new File(filename).exists) {
- out.println("That file does not exist")
+ output.println("That file does not exist")
return
}
action(filename)
@@ -169,7 +165,7 @@ class InterpreterLoop(
else if (line matches replayRegexp)
replay()
else if (line startsWith ":")
- out.println("Unknown command. Type :help for help.")
+ output.println("Unknown command. Type :help for help.")
else
shouldReplay = interpretStartingWith(line)
@@ -188,7 +184,7 @@ class InterpreterLoop(
case Interpreter.Error => None
case Interpreter.Incomplete =>
if (in.interactive && code.endsWith("\n\n")) {
- out.println("You typed two blank lines. Starting a new command.")
+ output.println("You typed two blank lines. Starting a new command.")
None
} else {
val nextLine = in.readLine(continuationPrompt)
@@ -207,7 +203,7 @@ class InterpreterLoop(
val cmd = ":load " + filename
command(cmd)
replayCommandsRev = cmd :: replayCommandsRev
- out.println()
+ output.println()
}
case _ =>
}
diff --git a/src/dotty/tools/dotc/repl/REPL.scala b/src/dotty/tools/dotc/repl/REPL.scala
index 2d6a3c742..e5ff2d3af 100644
--- a/src/dotty/tools/dotc/repl/REPL.scala
+++ b/src/dotty/tools/dotc/repl/REPL.scala
@@ -23,27 +23,37 @@ import java.io.{BufferedReader, File, FileReader, PrintWriter}
*/
class REPL extends Driver {
- /** The default input reader */
- def input(implicit ctx: Context): InteractiveReader = {
- val emacsShell = System.getProperty("env.emacs", "") != ""
- //println("emacsShell="+emacsShell) //debug
- if (ctx.settings.Xnojline.value || emacsShell) new SimpleReader()
- else InteractiveReader.createDefault()
- }
-
- /** The defult output writer */
- def output: PrintWriter = new NewLinePrintWriter(new ConsoleWriter, true)
+ lazy val config = new REPL.Config
override def newCompiler(implicit ctx: Context): Compiler =
- new repl.CompilingInterpreter(output, ctx)
+ new repl.CompilingInterpreter(config.output, ctx)
override def sourcesRequired = false
override def doCompile(compiler: Compiler, fileNames: List[String])(implicit ctx: Context): Reporter = {
if (fileNames.isEmpty)
- new InterpreterLoop(compiler, input, output).run()
+ new InterpreterLoop(compiler, config).run()
else
ctx.error(s"don't now what to do with $fileNames%, %")
ctx.reporter
}
}
+
+object REPL {
+ class Config {
+ val prompt = "scala> "
+ val continuationPrompt = " | "
+ val version = ".next (pre-alpha)"
+
+ /** The default input reader */
+ def input(implicit ctx: Context): InteractiveReader = {
+ val emacsShell = System.getProperty("env.emacs", "") != ""
+ //println("emacsShell="+emacsShell) //debug
+ if (ctx.settings.Xnojline.value || emacsShell) new SimpleReader()
+ else InteractiveReader.createDefault()
+ }
+
+ /** The default output writer */
+ def output: PrintWriter = new NewLinePrintWriter(new ConsoleWriter, true)
+ }
+}
diff --git a/test/test/TestREPL.scala b/test/test/TestREPL.scala
index a9978cdde..30dad2b64 100644
--- a/test/test/TestREPL.scala
+++ b/test/test/TestREPL.scala
@@ -5,32 +5,31 @@ import dotty.tools.dotc.core.Contexts.Context
import collection.mutable
import java.io.StringWriter
-
class TestREPL(script: String) extends REPL {
- private val prompt = "scala> "
- private val continuationPrompt = " | "
-
private val out = new StringWriter()
- override val output = new NewLinePrintWriter(out)
- override def input(implicit ctx: Context) = new InteractiveReader {
- val lines = script.lines
- def readLine(prompt: String): String = {
- val line = lines.next
- if (line.startsWith(prompt) || line.startsWith(continuationPrompt)) {
- output.println(line)
- line.drop(prompt.length)
+ override lazy val config = new REPL.Config {
+ override val output = new NewLinePrintWriter(out)
+
+ override def input(implicit ctx: Context) = new InteractiveReader {
+ val lines = script.lines
+ def readLine(prompt: String): String = {
+ val line = lines.next
+ if (line.startsWith(prompt) || line.startsWith(continuationPrompt)) {
+ output.println(line)
+ line.drop(prompt.length)
+ }
+ else readLine(prompt)
}
- else readLine(prompt)
+ val interactive = false
}
- val interactive = false
}
def check() = {
out.close()
val printed = out.toString
- val transcript = printed.drop(printed.indexOf(prompt))
+ val transcript = printed.drop(printed.indexOf(config.prompt))
if (transcript.toString != script) {
println("input differs from transcript:")
println(transcript)