-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI2PIR.pm
323 lines (237 loc) · 7.52 KB
/
I2PIR.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package main;
# This module has been generated using Parse::Eyapp::Treereg
# from file I2PIR.trg. Don't modify it.
# Change I2PIR.trg instead.
# Copyright (c) Casiano Rodriguez-Leon 2006. Universidad de La Laguna.
# You may use it and distribute it under the terms of either
# the GNU General Public License or the Artistic License,
# as specified in the Perl README file.
use strict;
use warnings;
use Carp;
use Parse::Eyapp::_TreeregexpSupport qw(until_first_match checknumchildren);
#line 6 "I2PIR.trg"
our @algebra = Parse::Eyapp::YATW->buildpatterns(fold => \&fold, wxz => \&wxz, zxw => \&zxw, neg => \&neg,);
#line 44 "I2PIR.trg"
our @translation = Parse::Eyapp::YATW->buildpatterns(t_num => \&t_num, t_var => \&t_var, t_op => \&t_op, t_neg => \&t_neg, t_assign => \&t_assign, t_list => \&t_list, t_print => \&t_print,);
our @all = ( our $fold, our $zxw, our $wxz, our $neg, our $reg_assign, our $t_num, our $t_var, our $t_op, our $t_neg, our $t_assign, our $t_print, our $t_list, ) = Parse::Eyapp::YATW->buildpatterns(fold => \&fold, zxw => \&zxw, wxz => \&wxz, neg => \&neg, reg_assign => \®_assign, t_num => \&t_num, t_var => \&t_var, t_op => \&t_op, t_neg => \&t_neg, t_assign => \&t_assign, t_print => \&t_print, t_list => \&t_list, );
#line 1 "I2PIR.trg"
# Example of support code
use List::Util qw(reduce);
my %Op = (PLUS=>'+', MINUS => '-', TIMES=>'*', DIV => '/');
sub fold {
my $fold = $_[3]; # reference to the YATW pattern object
my @NUM;
my $b;
{
my $child_index = 0;
return 0 unless ref($b = $_[$child_index]) =~ m{\bTIMES\b|\bPLUS\b|\bDIV\b|\bMINUS\b}x;
return 0 unless ref($NUM[0] = $b->child(0+$child_index)) eq 'NUM';
return 0 unless ref($NUM[1] = $b->child(1+$child_index)) eq 'NUM';
} # end block of child_index
#line 8 "I2PIR.trg"
{
my $op = $Op{ref($b)};
croak "Unexpected tree shape: ".$_[0]->str." can't find number in the expected place\n" unless exists ($NUM[0]->{attr}) && ($NUM[0]->{attr} =~ /^\d+/);
$NUM[0]->{attr} = eval "$NUM[0]->{attr} $op $NUM[1]->{attr}";
$_[0] = $NUM[0];
}
} # end of fold
sub zxw {
my $zxw = $_[3]; # reference to the YATW pattern object
my $W;
my $TIMES;
my $NUM;
{
my $child_index = 0;
return 0 unless (ref($TIMES = $_[$child_index]) eq 'TIMES');
return 0 unless ref($NUM = $TIMES->child(0+$child_index)) eq 'NUM';
return 0 unless defined($W = $TIMES->child(1+$child_index));
return 0 unless do
#line 17 "I2PIR.trg"
{$NUM->{attr} == 0};
} # end block of child_index
#line 17 "I2PIR.trg"
{ $_[0] = $NUM }
} # end of zxw
sub wxz {
my $wxz = $_[3]; # reference to the YATW pattern object
my $TIMES;
my $NUM;
my $W;
{
my $child_index = 0;
return 0 unless (ref($TIMES = $_[$child_index]) eq 'TIMES');
return 0 unless defined($W = $TIMES->child(0+$child_index));
return 0 unless ref($NUM = $TIMES->child(1+$child_index)) eq 'NUM';
return 0 unless do
#line 19 "I2PIR.trg"
{$NUM->{attr} == 0};
} # end block of child_index
#line 19 "I2PIR.trg"
{ $_[0] = $NUM }
} # end of wxz
sub neg {
my $neg = $_[3]; # reference to the YATW pattern object
my $NUM;
my $NEG;
{
my $child_index = 0;
return 0 unless (ref($NEG = $_[$child_index]) eq 'NEG');
return 0 unless ref($NUM = $NEG->child(0+$child_index)) eq 'NUM';
} # end block of child_index
#line 21 "I2PIR.trg"
{ $NUM->{attr} = -$NUM->{attr}; $_[0] = $NUM }
} # end of neg
#line 23 "I2PIR.trg"
{
my $num = 1; # closure
sub new_N_register {
return '$N'.$num++;
}
}
sub reg_assign {
my $reg_assign = $_[3]; # reference to the YATW pattern object
my $x;
{
my $child_index = 0;
return 0 unless defined($x = $_[$child_index]);
} # end block of child_index
#line 31 "I2PIR.trg"
{
if (ref($x) =~ /VAR|NUM/) {
$x->{reg} = $x->{attr};
return 1;
}
if (ref($x) =~ /ASSIGN/) {
$x->{reg} = $x->child(0)->{attr};
return 1;
}
$_[0]->{reg} = new_N_register();
}
} # end of reg_assign
sub t_num {
my $t_num = $_[3]; # reference to the YATW pattern object
my $NUM;
{
my $child_index = 0;
return 0 unless ref($NUM = $_[$child_index]) eq 'NUM';
} # end block of child_index
#line 46 "I2PIR.trg"
{ $NUM->{tr} = $NUM->{attr} }
} # end of t_num
#line 48 "I2PIR.trg"
our %s;
sub t_var {
my $t_var = $_[3]; # reference to the YATW pattern object
my $VAR;
{
my $child_index = 0;
return 0 unless ref($VAR = $_[$child_index]) eq 'VAR';
} # end block of child_index
#line 50 "I2PIR.trg"
{
croak "Unexpected tree shape: ".$_[0]->str." can't find identifier in VAR node\n" unless exists $_[0]->{attr};
$s{$_[0]->{attr}} = "num";
$_[0]->{tr} = $_[0]->{attr};
}
} # end of t_var
sub t_op {
my $t_op = $_[3]; # reference to the YATW pattern object
my $y;
my $x;
my $b;
{
my $child_index = 0;
return 0 unless ref($b = $_[$child_index]) =~ m{\bTIMES\b|\bPLUS\b|\bDIV\b|\bMINUS\b}x;
return 0 unless defined($x = $b->child(0+$child_index));
return 0 unless defined($y = $b->child(1+$child_index));
} # end block of child_index
#line 56 "I2PIR.trg"
{
my $op = $Op{ref($b)};
$b->{tr} = "$b->{reg} = $x->{reg} $op $y->{reg}";
}
} # end of t_op
sub t_neg {
my $t_neg = $_[3]; # reference to the YATW pattern object
my $NEG;
my $exp;
{
my $child_index = 0;
return 0 unless (ref($NEG = $_[$child_index]) eq 'NEG');
return 0 unless defined($exp = $NEG->child(0+$child_index));
} # end block of child_index
#line 61 "I2PIR.trg"
{ $NEG->{tr} = "$NEG->{reg} = - $exp->{reg}"; }
} # end of t_neg
sub t_assign {
my $t_assign = $_[3]; # reference to the YATW pattern object
my $v;
my $ASSIGN;
my $e;
{
my $child_index = 0;
return 0 unless (ref($ASSIGN = $_[$child_index]) eq 'ASSIGN');
return 0 unless defined($v = $ASSIGN->child(0+$child_index));
return 0 unless defined($e = $ASSIGN->child(1+$child_index));
} # end block of child_index
#line 63 "I2PIR.trg"
{
$s{$v->{attr}} = "num";
$ASSIGN->{tr} = "$v->{reg} = $e->{reg}"
}
} # end of t_assign
#line 68 "I2PIR.trg"
my $cr = '\\n';
sub t_print {
my $t_print = $_[3]; # reference to the YATW pattern object
my $PRINT;
my $var;
my $W;
{
my $child_index = 0;
return 0 unless (ref($PRINT = $_[$child_index]) eq 'PRINT');
return 0 unless defined($W = $PRINT->child(0+$child_index));
return 0 unless defined($var = $PRINT->child(1+$child_index));
} # end block of child_index
#line 69 "I2PIR.trg"
{
$s{$var->{attr}} = "num";
$PRINT->{tr} =<<"EOP";
print "$var->{attr} = "
print $var->{attr}
print "$cr"
EOP
}
} # end of t_print
#line 79 "I2PIR.trg"
# Concatenates the translations of the subtrees
sub cat_trans {
my $t = shift;
my $tr = "";
for ($t->children) {
(ref($_) =~ m{NUM|VAR|TERMINAL})
or $tr .= cat_trans($_)."\n"
}
$tr .= $t->{tr} ;
}
sub t_list {
my $t_list = $_[3]; # reference to the YATW pattern object
my $EXPS;
my @S;
{
my $child_index = 0;
return 0 unless (ref($EXPS = $_[$child_index]) eq 'EXPS');
@S = ($EXPS->children);
@S = @S[$child_index+0..$#S];
} # end block of child_index
#line 94 "I2PIR.trg"
{
$EXPS->{tr} = "";
my @tr = map { cat_trans($_) } @S;
$EXPS->{tr} =
reduce { "$a\n$b" } @tr if @tr;
}
} # end of t_list
1;