18

请给出一个代码示例,说明如何以编程方式创建嵌入式 Scala REPL 解释器,该解释器适用于 Scala 2.10。(我花了几个小时梳理各种代码碎片以获得工作解释器后添加了这个问答)

4

2 回答 2

23

示例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)
}

一些笔记

  • 我选择显示 JLineReader(默认)而不是SimpleReader因为它工作得更好,可以正确处理箭头键、删除等。JLine 确实添加了一个 jar 依赖项。
  • 该示例显示了如何将值绑定到 repl(e上面的变量)。
  • 当我省略settings.Yreplsync.value = true时,REPL 挂起并且没用。
  • 根据我的测试,如果将两者usejavacpembeddedDefaults设置组合在一起,则会导致错误

I find this easiest to test via SBT; a sample 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"
)

Sample SBT session:

> 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>
于 2013-09-05T05:35:31.780 回答
4

Based on Ben's excellent answer, below is a helper class to ease starting the interpreter. Usage:

Repl.run(("e", "Double", 2.71828), ("pi", "Double", 3.1415))

It automatically detects when you're running from SBT and accommodates.

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.")
  }

}
于 2014-02-26T10:51:45.230 回答