From f7ba3e8bbea873a783f0b2ea0255256686d965e8 Mon Sep 17 00:00:00 2001 From: Burak Emir Date: Fri, 18 Nov 2005 14:58:59 +0000 Subject: cleaned up main --- build.xml | 10 +++ config/build.default.properties | 2 + sources/bin/nscala.unix.tmpl | 2 + sources/scala/tools/nsc/EvalLoop.scala | 19 ++++++ sources/scala/tools/nsc/Main.scala | 92 ++++----------------------- sources/scala/tools/nsc/MainInterpreter.scala | 74 +++++++++++++++++++++ sources/scala/tools/nsc/MainTokenMetric.scala | 62 ++++++++++++++++++ sources/scala/tools/nsc/Settings.scala | 4 +- 8 files changed, 183 insertions(+), 82 deletions(-) create mode 100644 sources/scala/tools/nsc/EvalLoop.scala create mode 100644 sources/scala/tools/nsc/MainInterpreter.scala create mode 100644 sources/scala/tools/nsc/MainTokenMetric.scala diff --git a/build.xml b/build.xml index a81a7631a0..57980f0d03 100644 --- a/build.xml +++ b/build.xml @@ -342,7 +342,9 @@ + + @@ -350,15 +352,23 @@ link="${distrib.unix.scala.dir}/bin/${scala.exec.name}"/> + + + + diff --git a/config/build.default.properties b/config/build.default.properties index 1b7618a21f..61c222aa36 100644 --- a/config/build.default.properties +++ b/config/build.default.properties @@ -118,6 +118,8 @@ osc-nstools.jar.name=osc-nstools.jar # The name of the Scala executables scala.exec.name=ns scalac.exec.name=nsc +scalai.exec.name=nsi scalap.exec.name=nsp +scalatok.exec.name=nsctok ############################################################################## diff --git a/sources/bin/nscala.unix.tmpl b/sources/bin/nscala.unix.tmpl index 3440b84e2b..f45fa7d450 100644 --- a/sources/bin/nscala.unix.tmpl +++ b/sources/bin/nscala.unix.tmpl @@ -54,7 +54,9 @@ main= case "$SCRIPT" in @SCALA@ ) ;; @SCALAC@ ) main=scala.tools.nsc.Main;; + @SCALAI@ ) main=scala.tools.nsc.MainInterpreter;; @SCALAP@ ) main=scala.tools.scalap.Main;; + @SCALATOK@ ) main=scala.tools.nsc.MainTokenMetric;; * ) abort "Don't know what to do for $SCRIPT.";; esac; diff --git a/sources/scala/tools/nsc/EvalLoop.scala b/sources/scala/tools/nsc/EvalLoop.scala new file mode 100644 index 0000000000..240547491a --- /dev/null +++ b/sources/scala/tools/nsc/EvalLoop.scala @@ -0,0 +1,19 @@ +package scala.tools.nsc; + +import java.io._; + +trait EvalLoop { + + def prompt: String; + + def loop(action: (String) => Unit): unit = { + val in = new BufferedReader(new InputStreamReader(System.in)); + System.out.print(prompt); + var line = in.readLine(); + while (line != null && line.length() > 0) { + action(line); + System.out.print(prompt); + line = in.readLine(); + } + } +} diff --git a/sources/scala/tools/nsc/Main.scala b/sources/scala/tools/nsc/Main.scala index f8649fd9ca..016590fd36 100755 --- a/sources/scala/tools/nsc/Main.scala +++ b/sources/scala/tools/nsc/Main.scala @@ -5,14 +5,13 @@ // $Id$ package scala.tools.nsc; -import java.io._; import scala.tools.nsc.util.{Position}; import scala.tools.nsc.reporters.{Reporter, ConsoleReporter}; /** The main class for NSC, a compiler for the programming * language Scala. */ -object Main { +object Main extends Object with EvalLoop { val PRODUCT: String = System.getProperty("scala.product", "scalac"); @@ -29,30 +28,7 @@ object Main { def errors() = reporter.errors(); - private def loop(action: (String) => Unit): unit = { - val in = new BufferedReader(new InputStreamReader(System.in)); - System.out.print(prompt); - var line = in.readLine(); - while (line != null && line.length() > 0) { - action(line); - System.out.print(prompt); - line = in.readLine(); - } - } - def resident(compiler: Global): unit = { -/* - val in = new BufferedReader(new InputStreamReader(System.in)); - System.out.print(prompt); - var line = in.readLine(); - while (line != null && line.length() > 0) { - val args = List.fromString(line, ' '); - val command = new CompilerCommand(args, error, true); - (new compiler.Run) compile command.files; - System.out.print(prompt); - line = in.readLine(); - } -*/ loop(line => { val args = List.fromString(line, ' '); val command = new CompilerCommand(args, error, true); @@ -60,46 +36,6 @@ object Main { }) } - def interpret(gCompiler: Global): unit = { - val interpreter = new Interpreter { - val compiler: gCompiler.type = gCompiler - }; - loop(line => try { - interpreter.interpret(line.trim(), reporter) - } catch { - case e: Exception => { - reporter.info(null,e.getMessage(),true); - //e.printStackTrace(); - } - } - ) - } - - def tokenMetric(compiler: Global, fnames: List[String]): unit = { - import compiler.CompilationUnit; - import compiler.syntaxAnalyzer.Scanner; - import ast.parser.Tokens.EOF; - var totale = 0; - for (val source <- fnames) { - val s = new Scanner(new CompilationUnit(compiler.getSourceFile(source))); - s.nextToken; - var i = 0; - while(s.token != EOF) { - i = i + 1; - s.nextToken() - } - var j = 0 ; while(j + Math.log(i)/ Math.log(10) < 7) { - j = j+1; - Console.print(' '); - } - Console.print(i.toString()); - Console.print(" "); - Console.println(source.toString()); - totale = totale + i; - } - Console.println(totale.toString()+" total"); - } - def process(args: Array[String]): unit = { reporter = new ConsoleReporter(); val command = new CompilerCommand(List.fromArray(args), error, false); @@ -110,22 +46,18 @@ object Main { reporter.info(null, command.usageMsg, true) else { try { - val compiler = new Global(command.settings, reporter); - if (command.settings.tokenMetric.value) - tokenMetric(compiler, command.files); - else if (command.settings.resident.value) - resident(compiler); - else if (command.settings.interpret.value) - interpret(compiler); - else if (command.files.isEmpty) - reporter.info(null, command.usageMsg, true) - else - (new compiler.Run) compile command.files; + val compiler = new Global(command.settings, reporter); + if (command.settings.resident.value) + resident(compiler); + else if (command.files.isEmpty) + reporter.info(null, command.usageMsg, true) + else + (new compiler.Run) compile command.files; } catch { - case ex @ FatalError(msg) => - if (command.settings.debug.value) - ex.printStackTrace(); - reporter.error(null, "fatal error: " + msg); + case ex @ FatalError(msg) => + if (command.settings.debug.value) + ex.printStackTrace(); + reporter.error(null, "fatal error: " + msg); } reporter.printSummary() } diff --git a/sources/scala/tools/nsc/MainInterpreter.scala b/sources/scala/tools/nsc/MainInterpreter.scala new file mode 100644 index 0000000000..af6e487f26 --- /dev/null +++ b/sources/scala/tools/nsc/MainInterpreter.scala @@ -0,0 +1,74 @@ +/* NSC -- new scala compiler + * Copyright 2005 LAMP/EPFL + * @author emir + */ +// $Id$ +package scala.tools.nsc; + +import java.io._; +import scala.tools.nsc.util.{Position}; +import scala.tools.nsc.reporters.{Reporter, ConsoleReporter}; + +/** The main class for the new scala interpreter. + */ +object MainInterpreter extends Object with EvalLoop { + // lots of stuff duplicated from Main + val PRODUCT: String = + System.getProperty("scala.product", "scalaint"); + val VERSION: String = + System.getProperty("scala.version", "unknown version"); + val versionMsg = PRODUCT + " " + VERSION + " -- (c) 2002-05 LAMP/EPFL"; + val prompt = "\nnsc> "; + + private var reporter: ConsoleReporter = _; + + def error(msg: String): unit = + reporter.error(new Position(PRODUCT), + msg + "\n " + PRODUCT + " -help gives more information"); + + def errors() = reporter.errors(); + + def interpret(gCompiler: Global): unit = { + val interpreter = new Interpreter { + val compiler: gCompiler.type = gCompiler + }; + loop(line => try { + interpreter.interpret(line.trim(), reporter) + } catch { + case e: Exception => { + reporter.info(null,e.getMessage(),true); + //e.printStackTrace(); + } + } + ) + } + + def process(args: Array[String]): unit = { + reporter = new ConsoleReporter(); + val command = new CompilerCommand(List.fromArray(args), error, false); + reporter.prompt(command.settings.prompt.value); + if (command.settings.version.value) + reporter.info(null, versionMsg, true) + else if (command.settings.help.value) // 2do replace with InterpCommand + reporter.info(null, command.usageMsg, true) + + else { + try { + val compiler = new Global(command.settings, reporter); + interpret(compiler); + } catch { + case ex @ FatalError(msg) => + if (command.settings.debug.value) + ex.printStackTrace(); + reporter.error(null, "fatal error: " + msg); + } + reporter.printSummary() + } + } + + def main(args: Array[String]): unit = { + process(args); + System.exit(if (reporter.errors() > 0) 1 else 0); + } + +} diff --git a/sources/scala/tools/nsc/MainTokenMetric.scala b/sources/scala/tools/nsc/MainTokenMetric.scala new file mode 100644 index 0000000000..e4fe60b1ad --- /dev/null +++ b/sources/scala/tools/nsc/MainTokenMetric.scala @@ -0,0 +1,62 @@ +/* NSC -- new scala compiler + * Copyright 2005 LAMP/EPFL + * @author Martin Odersky + */ +// $Id$ +package scala.tools.nsc; + +import java.io._; +import scala.tools.nsc.reporters.{Reporter, ConsoleReporter}; + +/** The main class for NSC, a compiler for the programming + * language Scala. + */ +object MainTokenMetric { + + private var reporter: ConsoleReporter = _; + + def tokenMetric(compiler: Global, fnames: List[String]): unit = { + import compiler.CompilationUnit; + import compiler.syntaxAnalyzer.Scanner; + import ast.parser.Tokens.EOF; + var totale = 0; + for (val source <- fnames) { + val s = new Scanner(new CompilationUnit(compiler.getSourceFile(source))); + s.nextToken; + var i = 0; + while(s.token != EOF) { + i = i + 1; + s.nextToken() + } + var j = 0 ; while(j + Math.log(i)/ Math.log(10) < 7) { + j = j+1; + Console.print(' '); + } + Console.print(i.toString()); + Console.print(" "); + Console.println(source.toString()); + totale = totale + i; + } + Console.println(totale.toString()+" total"); + } + + def process(args: Array[String]): unit = { + val command = new CompilerCommand(List.fromArray(args), error, false); + reporter = new ConsoleReporter(); + try { + val compiler = new Global(command.settings, reporter); + tokenMetric(compiler, command.files); + } catch { + case ex @ FatalError(msg) => + if (command.settings.debug.value) + ex.printStackTrace(); + reporter.error(null, "fatal error: " + msg); + } + } + + def main(args: Array[String]): unit = { + process(args); + System.exit(if (reporter.errors() > 0) 1 else 0); + } + +} diff --git a/sources/scala/tools/nsc/Settings.scala b/sources/scala/tools/nsc/Settings.scala index c71f291dbe..0143525a67 100644 --- a/sources/scala/tools/nsc/Settings.scala +++ b/sources/scala/tools/nsc/Settings.scala @@ -29,9 +29,9 @@ class Settings(error: String => unit) { val debug = BooleanSetting("-debug", "Output debugging messages"); val statistics = BooleanSetting("-statistics", "Print compiler statistics"); val explaintypes = BooleanSetting("-explaintypes", "Explain type errors in more detail"); - val interpret = BooleanSetting("-interpret", "Run interpreter"); + //val interpret = BooleanSetting("-interpret", "Run interpreter"); val resident = BooleanSetting("-resident", "Compiler stays resident, files to compile are read from standard input"); - val tokenMetric = BooleanSetting("-tokenMetric", "only count tokens"); + //val tokenMetric = BooleanSetting("-tokenMetric", "only count tokens"); val uniqid = BooleanSetting("-uniqid", "Print identifiers with unique names (debugging option)"); val printtypes = BooleanSetting("-printtypes", "Print tree types (debugging option)"); val prompt = BooleanSetting("-prompt", "Display a prompt after each error (debugging option)"); -- cgit v1.2.3