How to invoke the Scala compiler programmatically?

2019-01-14 07:32发布

I want my Scala code to take a Scala class as input, compile and execute that class. How can I programmatically invoke a Scala compiler? I will be using the latest Scala version, i.e. 2.10.

2条回答
在下西门庆
2楼-- · 2019-01-14 08:08

The class to be compiled and run (in file test.scala)

class Test {

   println ("Hello World!")

}

// compileAndRun.scala (in same directory)

import scala.tools.nsc._
import java.io._

val g = new Global(new Settings()) 

val run = new g.Run  

run.compile(List("test.scala"))  // invoke compiler. it creates Test.class.

val classLoader = new java.net.URLClassLoader(
    Array(new File(".").toURI.toURL),  // Using current directory.
    this.getClass.getClassLoader)

val clazz = classLoader.loadClass("Test") // load class 

clazz.newInstance  // create an instance, which will print Hello World.
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-14 08:19

ToolBox

I think the proper way of invoking the Scala compiler is doing it via Reflection API documented in Overview. Specifically, Tree Creation via parse on ToolBoxes section in 'Symbols, Trees, and Types' talks about parsing String into Tree using ToolBox. You can then invoke eval() etc.

scala.tools.nsc.Global

But as Shyamendra Solanki wrote, in reality you can drive scalac's Global to get more done. I've written CompilerMatcher so I can compile generated code with sample code to do integration tests for example.

scala.tools.ncs.IMain

You can invoke the REPL IMain to evaluate the code (this is also available in the above CompilerMatcher if you want something that works with Scala 2.10):

  val main = new IMain(s) {
    def lastReq = prevRequestList.last
  }
  main.compileSources(files.map(toSourceFile(_)): _*)
  code map { c => main.interpret(c) match {
    case IR.Error => sys.error("Error interpreting %s" format (c))
    case _ => 
  }}
  val holder = allCatch opt {
    main.lastReq.lineRep.call("$result")
  }

This was demonstrated in Embedding the Scala Interpreter post by Josh Suereth back in 2009.

查看更多
登录 后发表回答