diff options
Diffstat (limited to 'src/compiler')
-rw-r--r-- | src/compiler/scala/tools/nsc/CompilerCommand.scala | 32 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/Interpreter.scala | 20 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/InterpreterCommand.scala | 8 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/MainInterpreter.scala | 54 |
4 files changed, 74 insertions, 40 deletions
diff --git a/src/compiler/scala/tools/nsc/CompilerCommand.scala b/src/compiler/scala/tools/nsc/CompilerCommand.scala index 71707dc3c5..ddf3165ac3 100644 --- a/src/compiler/scala/tools/nsc/CompilerCommand.scala +++ b/src/compiler/scala/tools/nsc/CompilerCommand.scala @@ -16,6 +16,12 @@ class CompilerCommand(arguments: List[String], error: String => unit, interactiv /** The applicable settings */ val settings: Settings = new Settings(error); + /** The name of the command */ + val cmdName = "scalac"; + + /** The file extension of files that the compiler can process */ + val fileEnding = ".scala"; + /** A message explaining usage and options */ def usageMsg: String = { val helpSyntaxColumnWidth: int = @@ -29,12 +35,12 @@ class CompilerCommand(arguments: List[String], error: String => unit, interactiv } settings.allSettings .map(setting => - format(setting.helpSyntax) + " " + setting.helpDescription) + format(setting.helpSyntax) + " " + setting.helpDescription) .mkString( - "Usage: scalac <options | source files>\n" + - "where possible options include: \n ", - "\n ", - "\n"); + "Usage: " + cmdName + " <options | source files>\n" + + "where possible options include: \n ", + "\n ", + "\n"); } // initialization @@ -44,17 +50,17 @@ class CompilerCommand(arguments: List[String], error: String => unit, interactiv val args0 = args; if (args.head.startsWith("-")) { if (interactive) { - error("no options can be given in interactive mode"); - ok = false + error("no options can be given in interactive mode"); + ok = false } else { - for (val setting <- settings.allSettings) + for (val setting <- settings.allSettings) args = setting.tryToSet(args); - if (args eq args0) { - error("unknown option: '" + args.head + "'"); - ok = false - } + if (args eq args0) { + error("unknown option: '" + args.head + "'"); + ok = false + } } - } else if (args.head.endsWith(".scala")) { + } else if (args.head.endsWith(fileEnding)) { fs = args.head :: fs; args = args.tail } else { diff --git a/src/compiler/scala/tools/nsc/Interpreter.scala b/src/compiler/scala/tools/nsc/Interpreter.scala index 204788e74d..b7b8f05b54 100644 --- a/src/compiler/scala/tools/nsc/Interpreter.scala +++ b/src/compiler/scala/tools/nsc/Interpreter.scala @@ -21,7 +21,7 @@ import symtab.Flags line of Scala code at the request of the user. The overall approach is based on compiling the requested code and then - using a Java classloader and using Java reflection to run the code + using a Java classloader and Java reflection to run the code and access its results. In more detail, a single compiler instance is used @@ -57,6 +57,12 @@ class Interpreter(val compiler: Global, output: (String => Unit)) { private def reporter = compiler.reporter + /** whether to print out result lines */ + private var printResults: Boolean = true + + /** be quiet; do not print out the results of each submitted command */ + def beQuiet = { printResults = false } + /** directory to save .class files to */ private val classfilePath = File.createTempFile("scalaint", "") classfilePath.delete // the file is created as a file; make it a directory @@ -184,12 +190,14 @@ class Interpreter(val compiler: Global, output: (String => Unit)) { val interpreterResultString = req.loadAndRun - // print the result - output(interpreterResultString) + if(printResults) { + // print the result + output(interpreterResultString) - // print out types of functions; they are not printed in the - // request printout - output(req.defTypesSummary) + // print out types of functions; they are not printed in the + // request printout + output(req.defTypesSummary) + } // book-keeping prevRequests += req diff --git a/src/compiler/scala/tools/nsc/InterpreterCommand.scala b/src/compiler/scala/tools/nsc/InterpreterCommand.scala new file mode 100644 index 0000000000..d25202dd3b --- /dev/null +++ b/src/compiler/scala/tools/nsc/InterpreterCommand.scala @@ -0,0 +1,8 @@ +package scala.tools.nsc + +/** a command line for the interpreter */ +class InterpreterCommand(arguments: List[String], error: String => unit) +extends CompilerCommand(arguments, error, false) { + override val cmdName = "scalaint" + override val fileEnding = ".scalaint" +} diff --git a/src/compiler/scala/tools/nsc/MainInterpreter.scala b/src/compiler/scala/tools/nsc/MainInterpreter.scala index 13518b51c1..d0b95cda8f 100644 --- a/src/compiler/scala/tools/nsc/MainInterpreter.scala +++ b/src/compiler/scala/tools/nsc/MainInterpreter.scala @@ -26,10 +26,10 @@ object MainInterpreter { Console.println("Type :help to repeat this message later.") } - /** A simple, generic read-eval-print loop with a pluggable eval-print function. - Loop reading lines of input and invoking the eval-print function. - Stop looping when eval-print returns false. */ - def repl(evpr: String => Boolean): Unit = { + /** 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)) while(true) { @@ -38,7 +38,7 @@ object MainInterpreter { if(line == null) return () // assumes null means EOF - val keepGoing = evpr(line) + val keepGoing = command(line) if(!keepGoing) return () // the evpr function said to stop } @@ -76,6 +76,7 @@ object MainInterpreter { } } + /** run one command submitted by the user */ def command(line: String): Boolean = { def withFile(command: String)(action: String => Unit): Unit = { @@ -96,33 +97,44 @@ object MainInterpreter { case _ if line.startsWith(":load") => withFile(line)(f => interpretAllFrom(f)) case _ => Console.println("Unknown command. Type :help for help.") } + else if(line.startsWith("#!/")) // skip the first line of Unix scripts + () + else if(line.startsWith("scalaint ")) // skip the second line of Unix scripts + () else interpretOne(line) true } - /** the main interpreter loop */ - def interpretLoop(compiler: Global): unit = { - interpreter = new Interpreter(compiler, str => Console.print(str)) - repl(command) - interpreter.close - } - /** process the command-line arguments and do as they request */ - def process(args: Array[String]): Unit = { - val command = new CompilerCommand(List.fromArray(args), error, false) + /** process command-line arguments and do as they request */ + def main(args: Array[String]): unit = { + val command = new InterpreterCommand(List.fromArray(args), error) + reporter.prompt = command.settings.prompt.value if (command.settings.help.value) { reporter.info(null, command.usageMsg, true) + return () } - else { - printHelp - interpretLoop(new Global(command.settings, reporter)) - } - } - def main(args: Array[String]): Unit = { - process(args) + val compiler = new Global(command.settings, reporter) + interpreter = new Interpreter(compiler, str=>Console.print(str)) + + try { + if(!command.files.isEmpty) { + interpreter.beQuiet + command.files match { + case List(filename) => interpretAllFrom(filename) + case _ => Console.println( + "Sorry, arguments to interpreter scripts are not currently supported.") + } + } else { + printHelp + repl + } + } finally { + interpreter.close + } } } |