mercredi 13 mai 2015

How to add symbols into a parsed AST?

(This is loosely related to Scala script in 2.11 and Generating a class from string and instantiating it in Scala 2.10).

In the code below I have a code parsed runtime using Toolbox into a corresponding AST. How can add symbol definitions (prefix in the code below) to the AST so that those symbols can be used in the expression tree?

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

object Main extends App {

  val cm = runtimeMirror(getClass.getClassLoader)
  val tb = cm.mkToolBox()

  val expr = tb.parse("println(i)")

  val build = internal.reificationSupport

  val prefix = build.setInfo(build.newFreeTerm("i", 2), typeOf[Int])

  // TODO: add prefix before expr by some AST manipulation

  tb.eval(expr)
}





Aucun commentaire:

Enregistrer un commentaire