forked from pmichaud/pmtcl
/
Actions.pm
290 lines (248 loc) · 8.74 KB
/
Actions.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
class Partcl::Actions is HLL::Actions;
method TOP($/) { make $<TOP_eval>.ast; }
## TOP_eval and TOP_expr create a PAST::Block that uses the
## lexical scope given by the caller's %LEXPAD.
method TOP_eval($/) { make eval_block($<body>.ast); }
method TOP_expr($/) { make eval_block($<EXPR>.ast); }
sub eval_block($past) {
## This is the runtime equivalent of
## register lexpad := DYNAMIC::<%LEXPAD>;
## The body of the code to be evaluated
my $lexpad_init :=
PAST::Var.new( :name<lexpad>, :scope<register>, :isdecl,
:viviself( PAST::Op.new(:pirop('find_dynamic_lex Ps'), '%LEXPAD'))
);
if ! pir::isnull(pir::find_dynamic_lex('@*PARTCL_COMPILER_NAMESPACE')) {
PAST::Block.new( PAST::Stmts.new( $lexpad_init ), $past, :hll<tcl>,
:namespace(@*PARTCL_COMPILER_NAMESPACE)
);
} else {
PAST::Block.new( PAST::Stmts.new( $lexpad_init ), $past, :hll<tcl>);
}
}
## TOP_proc creates a PAST::Block that initializes a
## new lexical scope in %LEXPAD.
method TOP_proc($/) { make lex_block($<body>.ast); }
sub lex_block($past) {
## This is the runtime equivalent of
## register lexpad :=
## my %LEXPAD := TclLexPad.newpad(DYNAMIC::<%LEXPAD>);
my $lexpad_init :=
PAST::Var.new( :name<lexpad>, :scope<register>, :isdecl,
:viviself(
PAST::Var.new( :name<%LEXPAD>, :scope<lexical>, :isdecl,
:viviself(
PAST::Op.new(
:pasttype<callmethod>, :name<newpad>,
PAST::Var.new( :name<TclLexPad>, :scope<package>, :namespace<> ),
PAST::Op.new(:pirop('find_dynamic_lex Ps'), '%LEXPAD')
)
)
)
)
);
if ! pir::isnull(pir::find_dynamic_lex('@*PARTCL_COMPILER_NAMESPACE')) {
PAST::Block.new( PAST::Stmts.new( $lexpad_init ), $past, :hll<tcl>,
:namespace(@*PARTCL_COMPILER_NAMESPACE)
);
} else {
PAST::Block.new( PAST::Stmts.new( $lexpad_init ), $past, :hll<tcl>);
}
}
method body($/) { make $<script>.ast; }
method script($/) {
my $past := PAST::Stmts.new( :node($/) );
if $<command> {
for $<command> { $past.push($_.ast); }
}
else { $past.push(''); }
make $past;
}
method command($/) {
my $past := PAST::Op.new( :name('invoke'), :node($/) );
my $i := 0;
my $n := +$<word>;
while $i < $n {
$past.push($<word>[$i].ast);
$i++;
}
make $past;
}
method word:sym<{*}>($/) {
make PAST::Op.new( :name<EXPAND>, $<word>.ast, :flat);
}
method word:sym<{ }>($/) { make $<braced_word>.ast; }
method word:sym<" ">($/) { make $<quoted_word>.ast; }
method word:sym<bare>($/) { make concat_atoms($<bare_atom>); }
method braced_word($/) { make concat_atoms($<braced_atom>); }
method braced_atom:sym<{ }>($/) { make '{' ~ $<braced_word>.ast ~ '}'; }
method braced_atom:sym<backnl>($/) { make ' '; }
method braced_atom:sym<back{>($/) { make "\\" ~ '{'; }
method braced_atom:sym<back}>($/) { make "\\" ~ '}'; }
method braced_atom:sym<backd>($/) { make "\\" ~ "\\"; }
method braced_atom:sym<back>($/) { make "\\"; }
method braced_atom:sym<chr>($/) { make ~$/; }
method quoted_word($/) { make concat_atoms($<quoted_atom>); }
method quoted_atom:sym<[ ]>($/) { make $<script>.ast; }
method quoted_atom:sym<var>($/) { make $<variable>.ast; }
method quoted_atom:sym<$>($/) { make '$'; }
method quoted_atom:sym<\\>($/) { make $<backslash>.ast; }
method quoted_atom:sym<chr>($/) { make ~$/; }
method bare_atom:sym<[ ]>($/) { make $<script>.ast; }
method bare_atom:sym<var>($/) { make $<variable>.ast; }
method bare_atom:sym<$>($/) { make '$'; }
method bare_atom:sym<\\>($/) { make $<backslash>.ast; }
method bare_atom:sym<chr>($/) { make ~$/; }
method backslash:sym<bel>($/) { make "\x07"; }
method backslash:sym<bs>($/) { make "\x08"; }
method backslash:sym<ff>($/) { make "\x0c"; }
method backslash:sym<lf>($/) { make "\x0a"; }
method backslash:sym<cr>($/) { make "\x0d"; }
method backslash:sym<ht>($/) { make "\x09"; }
method backslash:sym<vt>($/) { make "\x0b"; }
method backslash:sym<chr>($/) { make ~$<chr>; }
method backslash:sym<backnl>($/) { make ' '; }
method backslash:sym<backx>($/) {
my $len := pir::length(~$<x>);
my $substr_len := ($len >= 2) ?? -2 !! -$len;
make pir::chr(HLL::Actions::string_to_int(pir::substr(~$<x>, $substr_len), 16));
}
method backslash:sym<backo>($/) {
make pir::chr(HLL::Actions::string_to_int(~$<o>, 8));
}
method backslash:sym<backu>($/) {
make pir::chr(HLL::Actions::string_to_int(~$<u>, 16));
}
method list($/) {
my @list := pir::new('TclList');
for $<EXPR> {
@list.push: $_.ast;
}
make @list;
}
method list_word($/) { make concat_atoms($<list_atom>); }
method list_atom:sym<\\>($/) { make $<backslash>.ast; }
method list_atom:sym<chr>($/) { make ~$/; }
sub concat_atoms(@atoms) {
my @parts;
my $lastlit := '';
for @atoms {
my $ast := $_.ast;
if !PAST::Node.ACCEPTS($ast) {
$lastlit := $lastlit ~ $ast;
}
else {
if $lastlit gt '' { @parts.push($lastlit); }
@parts.push($ast);
$lastlit := '';
}
}
if $lastlit gt '' { @parts.push($lastlit); }
my $past := @parts ?? @parts.shift !! '';
while @parts {
$past := PAST::Op.new( $past, @parts.shift, :pirop<concat>);
}
$past;
}
## The beginning of each variable block sets up the C<lexpad> register
## to point to the current lexical scope -- this simply
## looks up the variable name in that lexpad and returns
## the corresponding value.
method variable:sym<normal>($/) {
my $variable;
if $<global> {
$variable := PAST::Var.new( :scope<keyed>,
PAST::Var.new( :name<%GLOBALS>, :scope<package>, :namespace([]) ),
~$<identifier>
);
} else {
$variable := PAST::Var.new( :scope<keyed>,
PAST::Var.new( :name<lexpad>, :scope<register> ),
~$<identifier>
);
}
# Array access
if $<key> {
make PAST::Op.new( :pasttype<if>,
PAST::Op.new( :pirop<iseq__iss>,
PAST::Op.new( :pirop<typeof__sP>, $variable),
PAST::Val.new( :value<TclArray>)
),
PAST::Var.new( :scope<keyed>,
$variable,
~$<key>[0]
),
PAST::Op.new( :pasttype<call>, :name<error>,
"can't read \"$<identifier>({$<key>[0]})\": variable isn't array"
)
)
}
else {
# Scalar
make PAST::Op.new( :pasttype<unless>,
PAST::Op.new( :pirop<isnull>, $variable),
PAST::Op.new( :pasttype<unless>,
PAST::Op.new( :pirop<iseq__iss>,
PAST::Op.new( :pirop<typeof__sP>, $variable),
PAST::Val.new( :value<TclArray>)
),
$variable,
PAST::Op.new( :pasttype<call>, :name<error>,
"can't read \"$<identifier>\": variable is array"
)
),
PAST::Op.new( :pasttype<call>, :name<error>,
"can't read \"$<identifier>\": no such variable"
)
);
}
}
method variable:sym<escaped>($/) {
make PAST::Var.new( :scope<keyed>,
PAST::Var.new( :name<lexpad>, :scope<register> ),
~$<identifier>,
:node($/)
);
}
method integer($/) {
if $<sign> eq '-' {
make -1 * $<int>.ast;
} else {
make $<int>.ast;
}
}
method int:sym<oct>($/) { make HLL::Actions::string_to_int(~$<digits>, 8) }
method int:sym<dec>($/) { make HLL::Actions::string_to_int(~$<digits>, 10) }
method int:sym<hex>($/) { make HLL::Actions::string_to_int(~$<digits>, 16) }
method term:sym<true>($/) { make $/.Str }
method term:sym<false>($/) { make $/.Str }
method term:sym<variable>($/) { make $<variable>.ast; }
method term:sym<integer>($/) { make $<integer>.ast; }
method term:sym<( )>($/) { make $<EXPR>.ast; }
method term:sym<[ ]>($/) { make $<script>.ast; }
method term:sym<" ">($/) { make concat_atoms($<quoted_atom>); }
=begin head1 index
Return a two element list; the first element is either 1 (from 0) or
2 (from end), the second is the relative position.
=end head1 index
method index:sym<int>($/) {
my $val := $<a>.ast;
if $<op> {
if ~$<op>[0] eq '+' {
$val := $val + $<b>[0].ast;
} else {
$val := $val - $<b>[0].ast;
}
}
make (1, $val);
}
method index:sym<end>($/) {
make (2, 0);
}
method index:sym<end+>($/) {
make (2, $<a>.ast);
}
method index:sym<end->($/) {
make (2, -$<a>.ast);
}
# vim: expandtab shiftwidth=4 ft=perl6: