使用反射和解释器动态解析字符串并在scala中返回函数

Dor*_*lan 5 reflection functional-programming scala type-inference twitter-util

我试图从语法上解释以字符串形式给出的代码。例如:

val myString = "def f(x:Int):Int=x+1".
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种可以从中返回实函数的方法:例如:

val myIncrementFunction = myDarkMagicFunctionThatWillBuildMyFunction(myString)
println(myIncrementFunction(3))
Run Code Online (Sandbox Code Playgroud)

将打印4

用例:我想稍后在代码中使用该解释代码中的一些简单功能。例如,他们可以提供像def fun(x:Int):Int = x + 1这样的字符串,然后使用解释器来编译/执行该代码,然后我就可以使用此fun(x ),例如在地图中。

问题在于该函数类型对我来说是未知的,这是一个大问题,因为我需要从IMain撤消。我读过有关反射,类型系统等的信息,经过一番谷歌搜索后,我到达了这一点。我也检查了twitter的util-eval,但是从他们的测试中的文档和示例中看不到太多,这是完全一样的。

如果我知道类型,我可以做类似的事情

val settings = new Settings
val imain = new IMain(settings)
val res = imain.interpret("def f(x:Int):Int=x+1; val ret=f _ ")
val myF = imain.valueOfTerm("ret").get.asInstanceOf[Function[Int,Int]]
println(myF(2))
Run Code Online (Sandbox Code Playgroud)

可以正常工作并打印3,但是我被上面提到的问题所困扰,我不知道该函数的类型,而此示例的工作仅是因为我强制转换为在定义用于测试IMain的字符串函数时使用的类型作品。

您知道任何方法可以实现此功能吗?

我是新手,所以如果我写错了,请原谅。

谢谢

Dor*_*lan 2

好的,我设法实现了我想要的功能,我仍在寻找改进此代码的方法,但此代码片段满足了我的要求。

我使用了 scala 工具箱和准引号

import scala.reflect.runtime.universe.{Quasiquote, runtimeMirror}
import scala.tools.reflect.ToolBox

object App {
    def main(args: Array[String]): Unit = {
        val mirror = runtimeMirror(getClass.getClassLoader)
        val tb = ToolBox(mirror).mkToolBox()

        val data = Array(1, 2, 3)

        println("Data before function applied on it")
        println(data.mkString(","))


        println("Please enter the map function you want:")
        val function = scala.io.StdIn.readLine()
        val functionWrapper = "object FunctionWrapper { " + function + "}"
        val functionSymbol = tb.define(tb.parse(functionWrapper).asInstanceOf[tb.u.ImplDef])

        // Map each element using user specified function
        val dataAfterFunctionApplied = data.map(x => tb.eval(q"$functionSymbol.function($x)"))

        println("Data after function applied on it")
        println(dataAfterFunctionApplied.mkString(","))
    }
}
Run Code Online (Sandbox Code Playgroud)

这是终端中的结果:

Data before function applied on it
1,2,3
Please enter the map function you want:
def function(x: Int): Int = x + 2
Data after function applied on it
3,4,5

Process finished with exit code 0
Run Code Online (Sandbox Code Playgroud)