2 # Copyright (C) 2005-2006, The Perl Foundation.
7 use lib qw(t . lib ../lib ../../lib ../../../lib);
10 use Parrot::Test tests => 3;
14 tge/grammar.t - TGE::Parser tests
18 $ prove t/compilers/tge/grammar.t
22 This is a test script to try out constructing a tree grammar from a tree
23 grammar syntax file, and using the constructed grammar to transform a
24 tree of the specified type.
28 pir_output_is( <<'CODE', <<'OUT', 'test compiling anonymous and named grammars' );
31 load_bytecode 'TGE.pbc'
33 # Compile a grammar from the source
35 $P1 = new 'TGE::Compiler'
36 grammar = $P1.'compile'(<<'GRAMMAR')
37 transform min (Leaf) :language('PIR') {
38 $P1 = getattribute node, "value"
45 # Add the grammar keyword and recompile
46 grammar = $P1.'compile'(<<'GRAMMAR')
47 grammar TreeMin is TGE::Grammar;
48 transform min (Leaf) :language('PIR') {
49 $P1 = getattribute node, "value"
56 # Add a POD comment and recompile
58 source = "=head NAME\n\n TreeMin2\n\n=cut\n\n"
60 grammar TreeMin2 is TGE::Grammar;
61 transform min (Leaf) :language('PIR') {
62 $P1 = getattribute node, "value"
66 grammar = $P1.'compile'(source)
76 pir_output_is( <<'CODE', <<'OUT', 'complete example: Branch/Leaf tree grammar' );
81 load_bytecode 'TGE.pbc'
83 # Load the grammar in a string
86 grammar TreeMin is TGE::Grammar;
88 transform min (Leaf) :language('PIR') {
89 $P1 = getattribute node, "value"
93 transform min (Branch) :language('PIR') {
100 left = getattribute node, "left"
101 left_val = tree.get('min', left)
102 right = getattribute node, "right"
103 right_val = tree.get('min', right)
106 if min <= right_val goto got_min
112 # find the global minimum and propagate it back down the tree
113 transform gmin (ROOT) :language('PIR') {
116 gmin = tree.get('min', node)
120 transform gmin (Branch) :applyto('left') :language('PIR') {
122 gmin = tree.get('gmin', node)
126 transform gmin (Branch) :applyto('right') :language('PIR') {
128 gmin = tree.get('gmin', node)
132 # reconstruct the tree with every leaf replaced with the minimum
134 transform result (Leaf) :language('PIR') {
138 $P1 = tree.get('gmin', node)
139 setattribute newnode, 'value', $P1
143 transform result (Branch) :language('PIR') {
145 .local pmc left_child
146 .local pmc right_child
147 newnode = new 'Branch'
148 left_child = getattribute node, 'left'
149 right_child = getattribute node, 'right'
150 $P1 = tree.get('result', left_child)
151 $P2 = tree.get('result', right_child)
153 setattribute newnode, 'left', $P1
154 setattribute newnode, 'right', $P2
159 # Compile a grammar from the source
161 $P1 = new 'TGE::Compiler'
162 grammar = $P1.'compile'(source)
164 # Build up the tree for testing
168 # Apply the grammar to the test tree
170 AGI = grammar.apply(tree)
172 # Retrieve the value of a top level attribute
173 $P4 = AGI.get('gmin')
174 print "the global minimum attribute value is: "
181 # Rerieve the transformed tree
182 $P5 = AGI.get('result')
184 $P6 = getattribute tree, 'left'
185 $P7 = getattribute $P6, 'left'
186 $P8 = getattribute $P7, 'value'
187 print "before transform, the value of the left-most leaf is: "
191 $P6 = getattribute $P5, 'left'
192 $P7 = getattribute $P6, 'left'
193 $P8 = getattribute $P7, 'value'
194 print "after transform, the value of the left-most leaf is: "
198 $P10 = getattribute tree, 'right'
199 $P11 = getattribute $P10, 'right'
200 $P12 = getattribute $P11, 'right'
201 $P13 = getattribute $P12, 'value'
202 print "before transform, the value of the right-most leaf is: "
206 $P10 = getattribute $P5, 'right'
207 $P11 = getattribute $P10, 'right'
208 $P12 = getattribute $P11, 'right'
209 $P13 = getattribute $P12, 'value'
210 print "after transform, the value of the right-most leaf is: "
217 print "Unable to parse the tree grammar.\n"
221 # ----------------------------------
225 addattribute $P1, "value" # the value of the leaf node
227 # Create Branch class
228 newclass $P2, "Branch"
229 addattribute $P2, "left" # left child
230 addattribute $P2, "right" # right child
234 $P2 = build_Branch($P0, $P1)
237 $P4 = build_Branch($P3, $P2)
241 $P7 = build_Branch($P5, $P6)
243 $P8 = build_Branch($P7, $P4)
254 setattribute newnode, 'value', $P1
259 .param pmc left_child
260 .param pmc right_child
262 newnode = new 'Branch'
263 setattribute newnode, 'left', left_child
264 setattribute newnode, 'right', right_child
269 the global minimum attribute value is: 1 of type: Integer
270 before transform, the value of the left-most leaf is: 2
271 after transform, the value of the left-most leaf is: 1
272 before transform, the value of the right-most leaf is: 9
273 after transform, the value of the right-most leaf is: 1
277 local $TODO = "unresolved bug";
280 <<'CODE', <<'OUT', 'two rules of the same name can apply to the same node, when called with a different dummy type' );
283 load_bytecode 'TGE.pbc'
285 # Load the grammar in a string
288 grammar TreeMin is TGE::Grammar;
290 transform tiddlywinks (ROOT) :language('PIR') {
292 tree.'get'('twister', node, 'pingpong')
293 tree.'get'('twister', node, 'pongpong')
295 transform twister (pingpong) :language('PIR') {
296 say 'in first twister'
298 transform twister (pongpong) :language('PIR') {
299 say 'in second twister'
304 .local object testing
307 # Compile a grammar from the source
309 $P1 = new 'TGE::Compiler'
310 grammar = $P1.'compile'(source)
312 # Apply the grammar to the test tree
314 AGI = grammar.apply(testing)
316 # Retrieve the value of a top level attribute
317 $P4 = AGI.get('tiddlywinks')
330 Allison Randal <allison@perl.org>
336 # cperl-indent-level: 4
339 # vim: expandtab shiftwidth=4: