请给出一个代码示例,展示如何以编程方式创建一个嵌入式Scala REPL解释器,并使其在Scala 2.10中运行。(我在挖掘各种代码片段几个小时后才得到一个可用的解释器,因此添加了这个问题和答案)
示例 Repl.scala
:
import scala.tools.nsc.interpreter._
import scala.tools.nsc.Settings
object Repl extends App {
def repl = new ILoop {
override def loop(): Unit = {
intp.bind("e", "Double", 2.71828)
super.loop()
}
}
val settings = new Settings
settings.Yreplsync.value = true
//use when launching normally outside SBT
settings.usejavacp.value = true
//an alternative to 'usejavacp' setting, when launching from within SBT
//settings.embeddedDefaults[Repl.type]
repl.process(settings)
}
一些注释
SimpleReader
, 因为它能更好地运作, 正确处理箭头键、删除等操作。但使用JLine需要添加一个jar依赖。e
)。settings.Yreplsync.value = true
, repl会挂起,变得毫无用处。usejavacp
和embeddedDefaults
设置结合在一起,会导致出现错误。我发现最简单的测试是通过SBT进行; 一个示例build.sbt
:
name := "Repl"
organization := "ExamplesRUs"
scalaVersion := "2.10.2"
libraryDependencies ++= Seq(
"org.scala-lang" % "scala-compiler" % "2.10.2",
"org.scala-lang" % "jline" % "2.10.2"
)
示例 SBT 会话:
> run-main Repl
[info] Running Repl
Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_37).
Type in expressions to have them evaluated.
Type :help for more information.
e: Double = 2.71828
scala> 2 * e
res1: Double = 5.43656
scala>
Repl.run(("e", "Double", 2.71828), ("pi", "Double", 3.1415))
它可以自动检测您是否正在使用SBT,并做出相应调整。
Repl.scala:
import scala.tools.nsc.interpreter.ILoop
import scala.tools.nsc.Settings
import java.io.CharArrayWriter
import java.io.PrintWriter
object Repl {
def run(params: (String, String, Any)*) {
def repl = new ILoop {
override def loop(): Unit = {
params.foreach(p => intp.bind(p._1, p._2, p._3))
super.loop()
}
}
val settings = new Settings
settings.Yreplsync.value = true
// Different settings needed when running from SBT or normally
if (isRunFromSBT) {
settings.embeddedDefaults[Repl.type]
} else {
settings.usejavacp.value = true
}
repl.process(settings)
}
def isRunFromSBT = {
val c = new CharArrayWriter()
new Exception().printStackTrace(new PrintWriter(c))
c.toString().contains("at sbt.")
}
}