0

我试图通过 nsc(新的 scala 编译器)的代码。我有点困惑Main.scala。它的实现如下:

/* NSC -- new Scala compiler
 * Copyright 2005-2013 LAMP/EPFL
 * @author  Martin Odersky
 */
package scala.tools
package nsc

import scala.language.postfixOps

/** The main class for NSC, a compiler for the programming
 *  language Scala.
 */
class MainClass extends Driver with EvalLoop {
  def resident(compiler: Global): Unit = loop { line =>
    val command = new CompilerCommand(line split "\\s+" toList, new Settings(scalacError))
    compiler.reporter.reset()
    new compiler.Run() compile command.files
  }

  override def newCompiler(): Global = Global(settings, reporter)
  override def doCompile(compiler: Global) {
    if (settings.resident) resident(compiler)
    else super.doCompile(compiler)
  }
}

object Main extends MainClass { }

我的第一个问题是,Main编译器进程如何调用?当我按照以下方式称呼某些东西时:

scalac [ <options> ] <source files>

在某个地方,newCompiler并且doCompile正在被调用,有人可以帮助我跟踪它是如何被调用的以及编译器是如何被初始化的吗?

任何指针将不胜感激。

谢谢

4

1 回答 1

1

MainClassextendsDriver具有以下main方法:

def main(args: Array[String]) {
  process(args)
  sys.exit(if (reporter.hasErrors) 1 else 0)
}

同时, object MainextendsMainClass意味着有一个Main.class文件包含一个public static void main(String[] args)转发器方法,该方法实际上调用了 object 上的上述非静态方法Main。有关如何在 Scala 中编译的更多详细信息,请参见例如这个问题。object

这意味着在运行 scala 编译器时scala.tools.nsc.Main可以将其用作主类scalac(这是在脚本中的某处硬编码的)。

newCompiler并由doCompile调用process

于 2015-03-30T04:47:37.883 回答