compiler plugin - insert code after typer

48 vues
Accéder directement au premier message non lu

Matías Toro Ipinza

non lue,
8 mai 2014, 19:33:5908/05/2014
à scala-l...@googlegroups.com
Hello,
I'm new to writing a compiler plugin and I need to insert some simple code into the AST. 

For simplicity lets suppose I wan't to insert 'println("2")' before some arbitrary tree

So far I've managed to make it to work this way:

def transform(tree: Tree) = {
    /* some selection code for the tree */

    val newTree = reify{println("2")}.tree

    val newTreeTyped = typer.typed(Block(List(x), tree))

    super.transform(newTreeTyped)
}


But the problem is that the real code that I wan't to insert is dynamic. For example, the message to print can be variable.

For this reason (without using splice) I wan't to create the tree manually.

I've replace the reify code with this:

val newTree = Apply(Ident(newTermName("println")), List(Literal(Constant(2))))

Wich works if I use reflection, but if I compile this plugin, it give me the following error:

Exception in thread "main" scala.reflect.internal.Types$TypeError: not found: object println
at scala.tools.nsc.typechecker.Contexts$Context.issue(Contexts.scala:401)
at scala.tools.nsc.typechecker.Infer$Inferencer.issue(Infer.scala:312)
at scala.tools.nsc.typechecker.Typers$Typer$$anonfun$normalTypedApply$1$1.apply(Typers.scala:4613)
at scala.tools.nsc.typechecker.Typers$Typer$$anonfun$normalTypedApply$1$1.apply(Typers.scala:4613)
at scala.tools.nsc.typechecker.Typers$Typer.onError$3(Typers.scala:4571)
at scala.tools.nsc.typechecker.Typers$Typer.normalTypedApply$1(Typers.scala:4613)
at scala.tools.nsc.typechecker.Typers$Typer.typedApply$1(Typers.scala:4625)
at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5530)
at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5608)
at scala.tools.nsc.typechecker.Typers$Typer.scala$tools$nsc$typechecker$Typers$Typer$$typedStat$1(Typers.scala:2927)
at scala.tools.nsc.typechecker.Typers$Typer$$anonfun$60.apply(Typers.scala:3031)
at scala.tools.nsc.typechecker.Typers$Typer$$anonfun$60.apply(Typers.scala:3031)
at scala.collection.immutable.List.loop$1(List.scala:170)
       .....


The other way that has worked for me is like this:

val newTree = Apply(Select(Ident(reify{scala.Predef}.tree.symbol), newTermName("println")), List(Literal(Constant("2"))))

But the problem here is the same problem with the first solution, I can't make the content of reify dynamic

What is the way to do it using only strings?

Thanks in advance for your help!


Répondre à tous
Répondre à l'auteur
Transférer
0 nouveau message