aboutsummaryrefslogblamecommitdiff
path: root/src/dotty/tools/dotc/Driver.scala
blob: 13604d9cc5a97bd0b019d3b61c06742abbf54fac (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16















                                                                                               
                        












                                                                 
                  
                                                                                                             










                                                                                       
package dotty.tools.dotc

import config.CompilerCommand
import core.Contexts.{Context, ContextBase}
import core.DotClass

abstract class Driver extends DotClass {

  val prompt = "\ndotc>"

  protected def newCompiler(): Compiler

  protected def doCompile(compiler: Compiler, fileNames: List[String])(implicit ctx: Context) =
    if (fileNames.nonEmpty) {
      val run = compiler.newRun
      run.compile(fileNames)
      run.printSummary()
    }

  protected def initCtx = (new ContextBase).initialCtx

  def process(args: Array[String]): Boolean = {
    val summary = CompilerCommand.distill(args)(initCtx)
    implicit val ctx = initCtx.fresh.withSettings(summary.sstate)
    val fileNames = CompilerCommand.checkUsage(summary)
    try {
      doCompile(newCompiler(), fileNames)
      !ctx.reporter.hasErrors
    } catch {
      case ex: Throwable =>
        ex match {
          case ex: FatalError  => ctx.error(ex.getMessage); false // signals that we should fail compilation.
          case _               => throw ex // unexpected error, tell the outside world.
        }
    }
  }

  def main(args: Array[String]): Unit =
    sys.exit(if (process(args)) 1 else 0)
}

class FatalError(msg: String) extends Exception