Le 13/09/2015 08:56, Marcus Denker a écrit :

On 12 Sep 2015, at 19:23, Peter Uhnák <[email protected]
<mailto:[email protected]>> wrote:

    | rewriter |
    rewriter := RBParseTreeRewriter new
    replace: ' `#aLiteral `{:node | node value isCharacter}'
            with: '`#aLiteral aMessage'; yourself.
    rewriter executeTree: (RBParser parseExpression: 'Array with: $a
    asciiValue with: $b with: #c with: d').
    rewriter tree formattedCode


This is cool! And so simple!


This can be combined with the compiler transform plugins of Opal:

-> make subclass of OCCompilerASTPlugin
-> put your code in a method called #transform

now when you override #compiler on the class side of a class like this:

compiler
^super compiler addPlugin: ASTPluginMeaningOfLife.

the plugin will be active for the hierarchy, which means that all code
compiled will be transformed.

There is a simple example ASTPluginMeaningOfLife in the image.

Cool. Intriguing.

I'm sure I can use it to:
- patch problematic code on the fly without registering an override
- change the semantics of some smalltalk statement (so that a := becomes self a: or anything else)

Would it be possible to couple it with Clement work to have some ability to pre-compile some code in specific classes (strength reduction, once literals) where we know that this is valuable?

It would also be nice to have a way not to write bytecode by hand, like, say, in the slots example of a few days back.

Oh, ok, for the literal stuff, I think I know how to do it:

In a method:
- replace target expression with symbol literal.
- evaluate target expression in isolation, store value.
- compile method with symbol literal.
- in compiled method, replace symbol literal with value stored above.

Would that work?

Thierry


Marcus



Reply via email to