summaryrefslogtreecommitdiff
path: root/sources/scala/tools/nsc/Main.scala
blob: b3b314a7839bfba70e3c286e822a03f27618147b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* NSC -- new scala compiler
 * Copyright 2005 LAMP/EPFL
 * @author  Martin Odersky
 */
// $Id$
package scala.tools.nsc;

import java.io._;
import scala.tools.util.{Position, Reporter, ConsoleReporter}

/** The main class for NSC, a compiler for the programming
 *  language Scala.
 */
object Main {

  val PRODUCT: String =
    System.getProperty("scala.product", "scalac");
  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 interactive(compiler: Global): unit = {
    val in = new BufferedReader(new InputStreamReader(System.in));
    System.out.print(prompt);
    var line = in.readLine();
    while (line.length() > 0) {
      val args = List.fromString(line, ' ');
      val command = new CompilerCommand(args, error, true);
      compiler.compile(command.files);
      System.out.print(prompt);
      line = in.readLine();
    }
  }

  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)
      reporter.info(null, command.usageMsg, true)
    else {
      try {
	val compiler = new Global(command.settings, reporter);
	if (command.settings.interactive.value)
	  interactive(compiler);
	else if (command.files.isEmpty)
	  reporter.info(null, command.usageMsg, true)
	else
	  compiler.compile(command.files);
      } 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);
  }

}