Help me make sense of this (AST manipulation)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Help me make sense of this (AST manipulation)

Victor RENE
| f g h |
f := '[ :a | ^a + 1 ].'. " Message pattern expected "
g := '| b |'. " '|' expected "
h := '| b ||'. " Success?!? "
OpalCompiler compile: f.
OpalCompiler compile: g.
OpalCompiler compile: h.

Basically I went from writing a function to clean up StringMorphs on the World...
 
cleanupStringMorph
  "Clean up string morph in world."
  | x y |
  x := World submorphs.
  y := [ x select: [ :each | (each isMemberOf: StringMorph) ] ].
  y value do: [ :each | each delete ].
  Smalltalk garbageCollect.
 
...to wondering how to generalize the idea (pass the class as an argument)...
...but that would be too easy, so why not manipulate the AST programatically...
I read https://marcusdenker.de/publications/Bera13a-OpalIWST.pdf but the compiler
thinks I am a fool :-(
 
My guess is I need to know what are the method calls from text to AST to bytecode.
Any idea?

Victor RENE
Software engineer,
Game designer, Writer


tel: +33 6 26 83 61 76
 
Reply | Threaded
Open this post in threaded view
|

Re: Help me make sense of this (AST manipulation)

Marcus Denker-4

> On 03 May 2016, at 13:58, Victor RENE <[hidden email]> wrote:
>
> | f g h |
> f := '[ :a | ^a + 1 ].'. " Message pattern expected "
> g := '| b |'. " '|' expected "
> h := '| b ||'. " Success?!? "
> OpalCompiler compile: f.
> OpalCompiler compile: g.
> OpalCompiler compile: h.
>

#compile: expects a complete method… it creates a compiledMethod object.

To parse the AST of an expression with the OpalCompiler facade, this is for now:

OpalCompiler new
        source:  '[ :a | ^a + 1 ].';
        parseExpression

Or you can use the parser directly:

RBParser parseExpression:  '[ :a | ^a + 1 ].’

(I am not yet happy with the compiler API… but we can only really change it after we get rid of the old one.
The current design is mostly driven by backward compatibility).

> Basically I went from writing a function to clean up StringMorphs on the World...
>  
> cleanupStringMorph
>   "Clean up string morph in world."
>   | x y |
>   x := World submorphs.
>   y := [ x select: [ :each | (each isMemberOf: StringMorph) ] ].
>   y value do: [ :each | each delete ].
>   Smalltalk garbageCollect.
>  
> ...to wondering how to generalize the idea (pass the class as an argument)...
> ...but that would be too easy, so why not manipulate the AST programatically…

Yes, possible, but one should only do it if another solution is not possible.

> I read https://marcusdenker.de/publications/Bera13a-OpalIWST.pdf but the compiler
> thinks I am a fool :-(
>  
> My guess is I need to know what are the method calls from text to AST to bytecode.
> Any idea?
>

Bytecode is created for methods. So that means you first need to make sure to build
a RBMethodNode.

expression := RBParser parseExpression:  '^[ :a | a + 1 ].'.

method := RBMethodNode
        selector: #myMethod
        body: expression asSequenceNode.

compiledMethod := method generate.

(compiledMethod valueWithReceiver: nil arguments: #()) value: 2.


        Marcus