[Pharo-users] Help me make sense of this (AST manipulation)

Marcus Denker marcus.denker at inria.fr
Tue May 3 11:15:57 EDT 2016


> On 03 May 2016, at 13:58, Victor RENE <victor-rene.dev at outlook.com> 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


	










More information about the Pharo-users mailing list