How to write a Scala macro that evaluates to a Tree similar to "reify" -
i want write macro captures program snippet , makes available tree @ runtime. basically, want functionality of reify embed in different syntax. want call apply on workload companion object, supply code , store tree of supplied code in member of newly created workload object.
val wl = workload { // code ... } wl.tree // tree of 'some code' unfortunately, can not forward reify because macro. guess, have write own macro similar reify. have no idea how return tree macro , source of reify mentions hardwired implementation.
how can accomplished?
update
i wrote small example underline point
import scala.reflect.runtime.universe._ object macrofun { import scala.reflect.macros.blackbox.context import scala.language.experimental.macros def getsometree: expr[unit] = macro gettreeimpl def gettreeimpl(c: context): c.expr[expr[unit]] = { import c.universe._ val expr = reify { println("hello world!") } ??? } } the thing missing, way turn expr c.expr[expr[unit]]
here how did in end. make sure call reify on runtime universe expr not depend on context.
class erplmacro(val c: context) { def lift[t:c.weaktypetag](exp: c.expr[t]) = { import c.universe._ q"""erplruntime.compile(scala.reflect.runtime.universe.reify($exp))""" } } object erplruntime { def compile[t](ctxelems: (scala.symbol,any)*)(exp: expr[t]): unit = // ... } trait erplapi { def compile[t](exp: t): unit = macro erplmacro.lift[t] }
Comments
Post a Comment