将String转换为可运行代码的方法有哪些?

时间:2015-06-25 15:12:17

标签: scala

我找不到如何将String转换为可运行的代码,例如:

val i = "new String('Yo')"
// conversion
println(i)

应该打印

Yo

转换后。

我在另一篇文章中找到了以下示例:

import scala.tools.nsc.interpreter.ILoop
import java.io.StringReader
import java.io.StringWriter
import java.io.PrintWriter
import java.io.BufferedReader
import scala.tools.nsc.Settings

object FuncRunner extends App {

  val line = "sin(2 * Pi * 400 * t)"

  val lines = """import scala.math._
    |var t = 1""".stripMargin

  val in = new StringReader(lines + "\n" + line + "\nval f = (t: Int) => " + line)
  val out = new StringWriter

  val settings = new Settings

  val looper = new ILoop(new BufferedReader(in), new PrintWriter(out))
  val res = looper process settings
  Console println s"[$res] $out"
}

link:How to convert a string from a text input into a function in a Scala

但似乎scala.tools不再可用了,而且我是Scala的新手,所以我无法弄清楚如何更换它。 现在可能只有其他方法可以做到。

谢谢!

4 个答案:

答案 0 :(得分:5)

您可以使用Quasiquotes(实验模块)简单地执行String中包含的代码。

import scala.reflect.runtime.universe._
import scala.reflect.runtime.currentMirror
import scala.tools.reflect.ToolBox

// TO compile and run code we will use a ToolBox api.
val toolbox = currentMirror.mkToolBox()

// write your code starting with q  and put it inside double quotes.
// NOTE : you will have to use triple quotes if you have any double quotes usage in your code.
val code1 = q"""new String("hello")"""
//compile and run your code.
val result1 = toolbox.compile(code1)()

// another example
 val code2 = q"""
 case class A(name:String,age:Int){
    def f = (name,age)
 }
 val a = new A("Your Name",22)
 a.f
 """

 val result2 = toolbox.compile(code2)()

REPL输出:

// Exiting paste mode, now interpreting.

import scala.reflect.runtime.universe._
import scala.reflect.runtime.currentMirror
import scala.tools.reflect.ToolBox
toolbox: scala.tools.reflect.ToolBox[reflect.runtime.universe.type] = scala.tools.reflect.ToolBoxFactory$ToolBoxImpl@69b34f89
code1: reflect.runtime.universe.Tree = new String("hello")
result1: Any = hello
code2: reflect.runtime.universe.Tree =
{
  case class A extends scala.Product with scala.Serializable {
    <caseaccessor> <paramaccessor> val name: String = _;
    <caseaccessor> <paramaccessor> val age: Int = _;
    def <init>(name: String, age: Int) = {
      super.<init>();
      ()
    };
    def f = scala.Tuple2(name, age)
  };
  val a = new A("Your Name", 22);
  a.f
}
result2: Any = (Your Name,22)

scala> 

要了解有关Quasiquotes的更多信息: http://docs.scala-lang.org/overviews/quasiquotes/setup.html

答案 1 :(得分:1)

scala编译器(和“解释器循环”)可用here。例如:

https://github.com/scala/scala/blob/v2.11.7/src/repl/scala/tools/nsc/interpreter/ILoop.scala

具有该类的已编译jar将位于lib\scala-compiler.jar下的scala分发中。

答案 2 :(得分:1)

I found a simple solution using the ToolBox tool :

val cm = universe.runtimeMirror(getClass.getClassLoader)

val tb = cm.mkToolBox()

val str = tb.eval(tb.parse("new String(\"Yo\")"))

println(str)

This is printing:

Yo

答案 3 :(得分:-1)

一种方法可能是获取/解析string代码,然后将其自己编写为Scala代码在某个文件中。这样,当您调用它时,它将由Scala编译器执行。

一个例子:就像Scala在使用Java一样。它使用此代码,然后使用main方法将其转换为Java。

object Xyz extends App {
 println ("Hello World!")
}