Manipulate the Scala Syntax Tree of a program

Good morning all :),

Is it possible to manipulatek i.e. insert new or alter already existing nodes in the syntax tree of a compiled source code please ?

I want to insert a function call whenever a certain function call, example .tell() is met. I.e. instead of :

ping ! StartMessage

I want the code to be altered to :
Launcher.sendStatement(ping, StartMessage)
ping ! StartMessage

Thanks a lot :slight_smile:

You’ve asked many variations of this question, and you haven’t gotten an answer because it isn’t a standard thing to do, and there’s an enormous amount of “it depends”. In traditional Scala 2, for example, it’s meaningless to talk about “the syntax tree of a compiled source”, because Scala 2 (until recently) compiles to JVM bytecode, and that’s the level you would need to instrument. In Scala 3, you could in principle modify the TASTy files, but it’s very hard to say whether that fits your problem, since you haven’t given much information.

As someone mentioned a while ago, this just plain isn’t the way that you would normally do this in Scala. Typically, you would instead write a function that wraps around ! and does the extra stuff before calling it, and call that instead, which doesn’t require any fancy features. If that’s insufficient, then you have a real research project, and the right solution is going to depend on the details of what you are trying to do – there probably aren’t any answers that are both general and correct.

6 Likes

Thanks a lot for the information you have provided me with.

I appreciate every single word you mentioned. This will help me :

  1. Will help me provide clearer and informative queries next time :

  2. Help me check other possibilities with relation to my query.

Thanks again and good day :slight_smile: