diff options
author | Lex Spoon <lex@lexspoon.org> | 2006-03-13 15:25:28 +0000 |
---|---|---|
committer | Lex Spoon <lex@lexspoon.org> | 2006-03-13 15:25:28 +0000 |
commit | 045dba54660105870513b8ddc6f906afd54a6071 (patch) | |
tree | 873eb4c9910e1280b32114c39ed1a713c7041e35 | |
parent | 197e25fa59db88dfb9d391c758bc28dc7ad91448 (diff) | |
download | scala-045dba54660105870513b8ddc6f906afd54a6071.tar.gz scala-045dba54660105870513b8ddc6f906afd54a6071.tar.bz2 scala-045dba54660105870513b8ddc6f906afd54a6071.zip |
allow reading and writing to arbitrary streams,...
allow reading and writing to arbitrary streams, not just Console
-rw-r--r-- | src/compiler/scala/tools/nsc/MainInterpreter.scala | 50 |
1 files changed, 31 insertions, 19 deletions
diff --git a/src/compiler/scala/tools/nsc/MainInterpreter.scala b/src/compiler/scala/tools/nsc/MainInterpreter.scala index 77b7306cc4..cbec6c1479 100644 --- a/src/compiler/scala/tools/nsc/MainInterpreter.scala +++ b/src/compiler/scala/tools/nsc/MainInterpreter.scala @@ -5,35 +5,38 @@ // $Id$ package scala.tools.nsc -import java.io.{BufferedReader, FileReader, IOException, InputStreamReader} +import java.io.{BufferedReader, FileReader, IOException, InputStreamReader, PrintWriter} import scala.tools.nsc.util.{Position} import scala.tools.nsc.reporters.{Reporter, ConsoleReporter} -/** The main class for the new scala interpreter. - */ -object MainInterpreter { - val reporter = new ConsoleReporter() +/** The main loop of the Scala interpreter. After instantiation, clients + * should call the main() method + */ +class InterpreterLoop(in: BufferedReader, out: PrintWriter) { + def this() = this(new BufferedReader(new InputStreamReader(System.in)), + new PrintWriter(System.out)) + + val reporter = new ConsoleReporter(in, out) var interpreter: Interpreter = _ /** print a friendly help message */ def printHelp = { - Console.println("This is an interpreter for Scala.") - Console.println("Type in expressions to have them evaluated.") - Console.println("Type :quit to exit the interpreter.") - Console.println("Type :compile followed by a filename to compile a complete Scala file.") - Console.println("Type :load followed by a filename to load a sequence of interpreter commands.") - Console.println("Type :help to repeat this message later.") + out.println("This is an interpreter for Scala.") + out.println("Type in expressions to have them evaluated.") + out.println("Type :quit to exit the interpreter.") + out.println("Type :compile followed by a filename to compile a complete Scala file.") + out.println("Type :load followed by a filename to load a sequence of interpreter commands.") + out.println("Type :help to repeat this message later.") } /** The main read-eval-print loop for the interpereter. It calls command() for each line of input, and stops when command() returns false */ - def repl: Unit = { - val in = new BufferedReader(new InputStreamReader(System.in)) - + def repl(): Unit = { while(true) { - Console.print("\nscala> ") + out.print("\nscala> ") + out.flush var line = in.readLine() if(line == null) return () // assumes null means EOF @@ -61,7 +64,7 @@ object MainInterpreter { new FileReader(filename) } catch { case _:IOException => - Console.println("Error opening file: " + filename) + out.println("Error opening file: " + filename) null } if (fileIn == null) return () @@ -82,7 +85,7 @@ object MainInterpreter { def withFile(command: String)(action: String => Unit): Unit = { val spaceIdx = command.indexOf(' ') if (spaceIdx <= 0) { - Console.println("That command requires a filename to be specified.") + out.println("That command requires a filename to be specified.") return () } val filename = command.substring(spaceIdx).trim @@ -95,7 +98,7 @@ object MainInterpreter { case ":quit" => return false case _ if line.startsWith(":compile") => withFile(line)(f => interpreter.compile(f)) case _ if line.startsWith(":load") => withFile(line)(f => interpretAllFrom(f)) - case _ => Console.println("Unknown command. Type :help for help.") + case _ => out.println("Unknown command. Type :help for help.") } else if(line.startsWith("#!/")) // skip the first line of Unix scripts () @@ -125,7 +128,7 @@ object MainInterpreter { interpreter.beQuiet command.files match { case List(filename) => interpretAllFrom(filename) - case _ => Console.println( + case _ => out.println( "Sorry, arguments to interpreter scripts are not currently supported.") } } else { @@ -138,3 +141,12 @@ object MainInterpreter { } } + + +/** A wrapper that provides a command-line interface */ +object MainInterpreter { + def main(args: Array[String]): Unit = { + (new InterpreterLoop(new BufferedReader(new InputStreamReader(System.in)), + new PrintWriter(new java.io.FileWriter("out")))).main(args) + } +} |