About RB AST in 3.8a: Visitor at work
Marcus Denker
denker at iam.unibe.ch
Fri Sep 24 10:13:54 UTC 2004
Hi,
The RB AST is in 3.8a unstable. In a second step, I added the
ParseTreeSearcher and the RBParser/RBScanner.
The AST implementation is much nicer then the one we had in Squeak
before: It encodes the parent of the treenode
and provides a Visitor implementation (RBProgramNodeVisitor).
Here is a short Tutorial.
We want to parse an expression:
tree := RBParser parseExpression: '3 + 4'
Now we have the AST (Abstrakt syntax tree). Have a look at it with the
ObjectExplorerer:
tree explore
We can easily walk across the tree using the RBProgramNodeVisitor:
RBProgramNodeVisitor new visitNode: tree.
Of course, nothing happens, as all the visitor-methods are only stubs
in this class.
So you need to subclass that to do anything usefull.
As an example, we would like to walk the tree and get all Literals back.
So we make a subclass:
RBProgramNodeVisitor subclass: #TestVisitor
instanceVariableNames: 'literals'
classVariableNames: ''
poolDictionaries: ''
category: 'Compiler-AST-Visitors'
initialize
literals := Set new.
literals
^literals
acceptLiteralNode: aLiteralNode
literals add: aLiteralNode value.
(TestVisitor new visitNode: tree) literals
and we get back: a Set(3 4). Nice, simple and most important: No need
to
clutter the AST classes, the designs get much more modular.
The plan is to move over to this AST step by step. So if you are doing
anything using
the current AST, consider to move over to the RB AST. It will make your
live much simpler.
Marcus
More information about the Squeak-dev
mailing list
|