aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorodersky <odersky@gmail.com>2016-03-31 11:20:48 +0200
committerodersky <odersky@gmail.com>2016-03-31 11:20:48 +0200
commit5bd08d437c1365bd8a81cb1b6b9801b443fde96b (patch)
treee2be21f89de19da9b7ce2ea2391c7cc300f4c515 /src
parent8cafcb9455103c34b6ce4344b58ca472a3a1f034 (diff)
parentef8c1968b2ea407c5b2ddca2fef00eb922e81f8e (diff)
downloaddotty-5bd08d437c1365bd8a81cb1b6b9801b443fde96b.tar.gz
dotty-5bd08d437c1365bd8a81cb1b6b9801b443fde96b.tar.bz2
dotty-5bd08d437c1365bd8a81cb1b6b9801b443fde96b.zip
Merge pull request #1182 from dotty-staging/repl-fixes
Repl fixes and tests
Diffstat (limited to 'src')
-rw-r--r--src/dotty/tools/dotc/Driver.scala2
-rw-r--r--src/dotty/tools/dotc/Resident.scala1
-rw-r--r--src/dotty/tools/dotc/core/Contexts.scala2
-rw-r--r--src/dotty/tools/dotc/core/Definitions.scala2
-rw-r--r--src/dotty/tools/dotc/repl/CompilingInterpreter.scala24
-rw-r--r--src/dotty/tools/dotc/repl/InterpreterLoop.scala52
-rw-r--r--src/dotty/tools/dotc/repl/REPL.scala34
7 files changed, 55 insertions, 62 deletions
diff --git a/src/dotty/tools/dotc/Driver.scala b/src/dotty/tools/dotc/Driver.scala
index 887274fa8..2e78854c1 100644
--- a/src/dotty/tools/dotc/Driver.scala
+++ b/src/dotty/tools/dotc/Driver.scala
@@ -15,8 +15,6 @@ import scala.util.control.NonFatal
*/
abstract class Driver extends DotClass {
- val prompt = "\ndotc> "
-
protected def newCompiler(implicit ctx: Context): Compiler
protected def emptyReporter: Reporter = new StoreReporter(null)
diff --git a/src/dotty/tools/dotc/Resident.scala b/src/dotty/tools/dotc/Resident.scala
index 18bb2ff4f..e1b62e4d0 100644
--- a/src/dotty/tools/dotc/Resident.scala
+++ b/src/dotty/tools/dotc/Resident.scala
@@ -31,6 +31,7 @@ class Resident extends Driver {
private val quit = ":q"
private val reset = ":reset"
+ private val prompt = "dotc> "
private def getLine() = {
Console.print(prompt)
diff --git a/src/dotty/tools/dotc/core/Contexts.scala b/src/dotty/tools/dotc/core/Contexts.scala
index 2fc958a49..fd0cff94e 100644
--- a/src/dotty/tools/dotc/core/Contexts.scala
+++ b/src/dotty/tools/dotc/core/Contexts.scala
@@ -544,7 +544,7 @@ object Contexts {
*/
def initialize()(implicit ctx: Context): Unit = {
_platform = newPlatform
- definitions.init
+ definitions.init()
}
def squashed(p: Phase): Phase = {
diff --git a/src/dotty/tools/dotc/core/Definitions.scala b/src/dotty/tools/dotc/core/Definitions.scala
index 6f8a8f837..d8c882d5c 100644
--- a/src/dotty/tools/dotc/core/Definitions.scala
+++ b/src/dotty/tools/dotc/core/Definitions.scala
@@ -798,7 +798,7 @@ class Definitions {
private[this] var _isInitialized = false
private def isInitialized = _isInitialized
- def init(implicit ctx: Context) = {
+ def init()(implicit ctx: Context) = {
this.ctx = ctx
if (!_isInitialized) {
// force initialization of every symbol that is synthesized or hijacked by the compiler
diff --git a/src/dotty/tools/dotc/repl/CompilingInterpreter.scala b/src/dotty/tools/dotc/repl/CompilingInterpreter.scala
index 7d1da1419..bc898488d 100644
--- a/src/dotty/tools/dotc/repl/CompilingInterpreter.scala
+++ b/src/dotty/tools/dotc/repl/CompilingInterpreter.scala
@@ -60,6 +60,8 @@ class CompilingInterpreter(out: PrintWriter, ictx: Context) extends Compiler wit
import ast.untpd._
import CompilingInterpreter._
+ ictx.base.initialize()(ictx)
+
/** directory to save .class files to */
val virtualDirectory =
if (ictx.settings.d.isDefault(ictx)) new VirtualDirectory("(memory)", None)
@@ -175,7 +177,7 @@ class CompilingInterpreter(out: PrintWriter, ictx: Context) extends Compiler wit
// if (prevRequests.isEmpty)
// new Run(this) // initialize the compiler // (not sure this is needed)
// parse
- parse(indentCode(line)) match {
+ parse(line) match {
case None => Interpreter.Incomplete
case Some(Nil) => Interpreter.Error // parse error or empty input
case Some(tree :: Nil) if tree.isTerm && !tree.isInstanceOf[Assign] =>
@@ -271,7 +273,7 @@ class CompilingInterpreter(out: PrintWriter, ictx: Context) extends Compiler wit
// header for the wrapper object
code.println("object " + objectName + " {")
code.print(importsPreamble)
- code.println(indentCode(toCompute))
+ code.println(toCompute)
handlers.foreach(_.extraCodeToEvaluate(this,code))
code.println(importsTrailer)
//end the wrapper object
@@ -477,7 +479,7 @@ class CompilingInterpreter(out: PrintWriter, ictx: Context) extends Compiler wit
addWrapper()
if (handler.statement.isInstanceOf[Import])
- preamble.append(handler.statement.toString + ";\n")
+ preamble.append(handler.statement.show + ";\n")
// give wildcard imports a import wrapper all to their own
if (handler.importsWildcard)
@@ -645,7 +647,7 @@ class CompilingInterpreter(out: PrintWriter, ictx: Context) extends Compiler wit
private class ImportHandler(imp: Import) extends StatementHandler(imp) {
override def resultExtractionCode(req: Request, code: PrintWriter): Unit = {
- code.println("+ \"" + imp.toString + "\\n\"")
+ code.println("+ \"" + imp.show + "\\n\"")
}
def isWildcardSelector(tree: Tree) = tree match {
@@ -734,20 +736,6 @@ class CompilingInterpreter(out: PrintWriter, ictx: Context) extends Compiler wit
/** Clean up a string for output */
private def clean(str: String)(implicit ctx: Context) =
truncPrintString(stripWrapperGunk(str))
-
- /** Indent some code by the width of the scala> prompt.
- * This way, compiler error messages read better.
- */
- def indentCode(code: String) = {
- val spaces = " "
-
- stringFrom(str =>
- for (line <- code.lines) {
- str.print(spaces)
- str.print(line + "\n")
- str.flush()
- })
- }
}
/** Utility methods for the Interpreter. */
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)
+ }
+}