summaryrefslogtreecommitdiff
path: root/sources
diff options
context:
space:
mode:
authorBurak Emir <emir@epfl.ch>2005-11-18 14:58:59 +0000
committerBurak Emir <emir@epfl.ch>2005-11-18 14:58:59 +0000
commitf7ba3e8bbea873a783f0b2ea0255256686d965e8 (patch)
treeb51f39a2e741a8d3827f83465b7f3299c54cb1ef /sources
parent59eea769bbd4b5779b4df5e7c6751b7302dd753c (diff)
downloadscala-f7ba3e8bbea873a783f0b2ea0255256686d965e8.tar.gz
scala-f7ba3e8bbea873a783f0b2ea0255256686d965e8.tar.bz2
scala-f7ba3e8bbea873a783f0b2ea0255256686d965e8.zip
cleaned up main
Diffstat (limited to 'sources')
-rw-r--r--sources/bin/nscala.unix.tmpl2
-rw-r--r--sources/scala/tools/nsc/EvalLoop.scala19
-rwxr-xr-xsources/scala/tools/nsc/Main.scala92
-rw-r--r--sources/scala/tools/nsc/MainInterpreter.scala74
-rw-r--r--sources/scala/tools/nsc/MainTokenMetric.scala62
-rw-r--r--sources/scala/tools/nsc/Settings.scala4
6 files changed, 171 insertions, 82 deletions
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)");