/
Yapsi.pm
595 lines (555 loc) · 23.3 KB
/
Yapsi.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
use v6;
my $VERSION = '2010.10';
my $_PROGRAM; # RAKUDO: Part of workaround required because of [perl #76894]
# At any given time, @blockstack contains the chain of blocks we're in,
# from the outermost (the main program body), to the innermost. Each block
# is stored as a hash, with the keys :name :vars, though :vars is only
# filled in at the time of the action method at the end of the parsing of
# the block.
my @blockstack;
grammar Yapsi::Perl6::Grammar {
regex TOP { ^
{ push @blockstack, { :name(unique-block()) } }
<statementlist> <.ws> $ }
regex statementlist { <statement> ** <eat_terminator> }
token statement { <statement_control> || <expression> || '' }
# RAKUDO: <?after '}'> NYRI [perl #76894]
regex eat_terminator { <?{ $/.CURSOR.pos > 0
&& $_PROGRAM.substr($/.CURSOR.pos - 1, 1) eq '}'
}> \n
|| <.ws> ';' }
token expression { <assignment> || <binding> || <variable> || <literal>
|| <declaration> || <block>
|| <saycall> || <increment> || <decrement> }
token statement_control { <statement_control_if>
|| <statement_control_while_until>
|| <statement_control_unless> }
rule statement_control_if { 'if' <expression> <block>
[ 'else' <else=.block> ]? }
rule statement_control_while_until { $<keyword>=[ 'while' | 'until' ] <expression> <block> }
rule statement_control_unless { 'unless' <expression> <block> }
token lvalue { <declaration> || <variable> || <increment> }
token value { <variable> || <literal> || <declaration> || <saycall>
|| <increment> }
token variable { '$' \w+ }
token literal { \d+ }
rule declaration { $<declarator>=['my'|'our'] <variable> }
rule assignment { <lvalue> '=' <expression> }
rule binding { <lvalue> ':=' <expression> }
rule saycall { 'say' <expression> } # very temporary solution
rule increment { '++' <value> }
rule decrement { '--' <value> }
token block { <.ws> '{'
{ push @blockstack, { :name(unique-block()) } }
<.ws> <statementlist> <.ws> '}' }
}
my $block-number = 0; # Can be done with 'state' when Rakudo has it
sub unique-block() {
'B' ~ $block-number++;
}
sub traverse-top-down(Match $m, :$key = "TOP", :&action, :@skip) {
action($m, $key);
for %($m).keys -> $key {
next if $key eq any @skip;
given $m{$key} {
when Match { traverse-top-down($_, :$key, :&action, :@skip) }
when Array { traverse-top-down($_, :$key, :&action, :@skip)
for .list }
default { die "Unknown thing $_.WHAT() in parse tree!" }
}
}
}
sub traverse-bottom-up(Match $m, :$key = "TOP", :&action, :@skip) {
unless $key eq any @skip {
for %($m).keys -> $key {
given $m{$key} {
when Match { traverse-bottom-up($_, :$key, :&action, :@skip) }
when Array { traverse-bottom-up($_, :$key, :&action, :@skip)
for .list }
default { die "Unknown thing $_.WHAT() in parse tree!" }
}
}
}
action($m, $key);
}
my %block-parents;
class Yapsi::Perl6::Actions {
# At any given time, %vars maps each 'active' (currently lexically visible)
# variables to the name of the block containing its outermost declaration.
my %vars;
# The reason we temporarily store 'declaration?' in %vars is that in a
# bottom-up model such as the one the actions employ, <variable> fires
# before <declaration>. Basically, there needs to be a way to say "this
# may be an undeclared variable, or maybe a variable that is being
# declared". The two possible cases of the former are caught in the
# .variable and .block methods.
method declaration($/) {
if %vars{~$<variable>} eq 'declaration?' {
%vars{~$<variable>} = @blockstack[*-1]<name>;
}
}
method variable($/) {
die qq[Variable "$/" used but not declared]
if %vars{~$/} eq 'declaration?';
unless %vars.exists(~$/) {
%vars{~$/} = 'declaration?';
}
}
method TOP($/) {
self.block($/);
}
# As opposed to %vars with its parse-global reach, @vars only has meaning
# within the .block method. It enumerates all of the variables declared
# directly in the current block. We need to clone the Array each time we
# store it, because we keep re-using the variable.
my @vars;
my &find-declarations = sub ($m, $key) {
if $key eq "declaration" {
push @vars, { :name(~$m<variable>),
:our($m<declarator> eq 'our') };
}
};
method block($/) {
@vars = ();
traverse-top-down($/, :skip['block'], :action(&find-declarations));
my $block = pop @blockstack;
my $name = $block<name>;
$block<vars> = @vars.clone;
make $block;
if @blockstack {
%block-parents{$name} = @blockstack[*-1];
}
# RAKUDO: Can't assign to a hash when the hash is part of the rhs
# [perl #77586]
my %workaround-vars = grep { .value ne $name }, %vars;
%vars = %workaround-vars;
if first { .value eq 'declaration?' }, %vars -> $p {
die qq[Variable "$p.key()" used but not declared];
}
}
}
class Yapsi::Compiler {
has @.warnings;
method compile($program) {
@!warnings = ();
$_PROGRAM = $program; # RAKUDO: Required because of [perl #76894]
die "Could not parse"
unless Yapsi::Perl6::Grammar.parse(
$program, :actions(Yapsi::Perl6::Actions));
my @sic = "This is SIC v$VERSION";
my $INDENT = ' ';
my %package-variables;
my $*l = 0; # unique label counter
traverse-top-down($/, :action(-> $m, $key {
if $key eq 'TOP'|'block'|'else' {
push @sic, '';
push @sic, "block '$m.ast<name>':";
for $m.ast<vars>.list -> $var {
push @sic, " `var '$var<name>'"
~ ($var<our> ?? ' :our' !! '');
}
my @blocksic;
my $*c = 0; # unique register counter
my @skip = 'block', 'statement_control_if',
'statement_control_while_until',
'statement_control_unless';
my &sicify = -> $/, $key {
if $m !=== $/ && $key eq 'block' {
my $register = self.unique-register;
my $block-name = $/.ast<name>;
push @blocksic,
"$register = closure-from-block '$block-name'",
"call $register";
}
elsif $key eq 'statement_control_if' {
traverse-bottom-up(
$<expression>,
:key<expression>,
:@skip,
:action(&sicify)
);
my ($register, $) = $<expression>.ast.list;
my $block-name = $<block>.ast<name>;
my $after-if = self.unique-label;
push @blocksic, "jf $register, $after-if";
$register = self.unique-register;
push @blocksic,
"$register = closure-from-block '$block-name'",
"call $register";
my $after-else;
if $<else> {
$after-else = self.unique-label;
push @blocksic, "jmp $after-else";
}
push @blocksic, "`label $after-if";
if $<else> {
$block-name = $<else>[0].ast<name>;
$register = self.unique-register;
push @blocksic,
"$register = closure-from-block '$block-name'",
"call $register",
"`label $after-else";
}
}
elsif $key eq 'statement_control_unless' {
traverse-bottom-up(
$<expression>,
:key<expression>,
:@skip,
:action(&sicify)
);
my ($register, $) = $<expression>.ast.list;
my $block-name = $<block>.ast<name>;
my $after-unless = self.unique-label;
push @blocksic, "jt $register, $after-unless";
$register = self.unique-register;
push @blocksic,
"$register = closure-from-block '$block-name'",
"call $register";
push @blocksic, "`label $after-unless";
}
elsif $key eq 'statement_control_while_until' {
my $before-while = self.unique-label;
my $after-while = self.unique-label;
push @blocksic, "`label $before-while";
traverse-bottom-up(
$<expression>,
:key<expression>,
:@skip,
:action(&sicify)
);
my ($register, $) = $<expression>.ast.list;
given $<keyword> {
when / while / {
push @blocksic, "jf $register, $after-while";
}
when / until / {
push @blocksic, "jt $register, $after-while";
}
}
my $block-name = $<block>.ast<name>;
$register = self.unique-register;
push @blocksic,
"$register = closure-from-block '$block-name'",
"call $register",
"jmp $before-while",
"`label $after-while";
}
elsif $key eq 'variable' {
my $register = self.unique-register;
my $current_block = $m.ast;
my $level = 0;
my $slot = -1;
while True {
my @vars = $current_block<vars>.list;
for ^@vars -> $i {
if ~$/ eq @vars[$i]<name> {
$slot = $i;
# RAKUDO: Could use a 'last LOOP' here
last;
}
}
last if $slot != -1;
--$level;
$current_block
= %block-parents{$current_block<name>};
die "Variable '$/' not declared"
unless defined $current_block;
}
my $locator = "[$level, $slot]";
push @blocksic, "$register = fetch $locator";
make [$register, $locator];
}
elsif $key eq 'assignment' {
my ($register, $) = $<expression>.ast.list;
my ($, $locator) = $<lvalue>.ast.list;
push @blocksic, "store $locator, $register";
make [$register, $locator];
}
elsif $key eq 'binding' {
my ($, $leftloc) = $<lvalue>.ast.list;
my ($register, $rightloc) = $<expression>.ast.list;
push @blocksic, "bind $leftloc, $rightloc";
make [$register, $leftloc];
}
elsif $key eq 'value' {
for <variable literal declaration saycall
increment decrement> -> $e {
if $/{$e} {
make $/{$e}.ast;
}
}
}
elsif $key eq 'lvalue' {
for <variable declaration increment decrement> -> $e {
if $/{$e} {
make $/{$e}.ast;
}
}
}
elsif $key eq 'expression' {
for <variable literal declaration assignment binding
saycall increment decrement> -> $e {
if $/{$e} {
make $/{$e}.ast;
}
}
}
elsif $key eq 'literal' {
my $register = self.unique-register;
my $literal = ~$/;
push @blocksic, "$register = $literal";
make [$register, '<constant>'];
}
elsif $key eq 'declaration' {
if $<declarator> eq 'our' {
++%package-variables{~$<variable>};
}
make $<variable>.ast;
}
elsif $key eq 'increment' {
my ($register, $locator) = $<value>.ast.list;
die "Can't increment a constant"
if $locator eq '<constant>';
push @blocksic, "inc $register",
"store $locator, $register";
make [$register, $locator];
}
elsif $key eq 'decrement' {
my ($register, $locator) = $<value>.ast.list;
die "Can't increment a constant"
if $locator eq '<constant>';
push @blocksic, "dec $register",
"store $locator, $register";
make [$register, $locator];
}
elsif $key eq 'saycall' {
my ($register, $) = $<expression>.ast.list;
my $result = self.unique-register;
push @blocksic, "say $register",
"$result = 1";
make $result;
}
};
traverse-bottom-up($m, :@skip, :action(&sicify));
for renumber declutter @blocksic {
push @sic, $INDENT ~ $_;
}
}
}));
if %package-variables {
push @sic, '';
push @sic, "block 'GLOBAL':";
for %package-variables.keys -> $var {
push @sic, " `var '$var'";
}
}
return @sic;
}
method unique-register {
return '$' ~ $*c++;
}
method unique-label {
return 'L' ~ $*l++;
}
sub declutter(@instructions) {
my @decluttered;
for @instructions.kv -> $i, $line {
# RAKUDO: !~~ doesn't bind $/
if not $line ~~ / ^ ('$' \d+) ' =' / {
push @decluttered, $line;
}
else {
my $varname = ~$0;
my Bool $usages-later = False;
for $i+1 ..^ +@instructions -> $j {
# XXX: This heuristic fails when we reach many-digit
# reguster names, since it gives false positives
# for all prefixes
++$usages-later
if defined index(@instructions[$j], $varname);
}
if $usages-later {
push @decluttered, $line;
}
}
}
return @decluttered;
}
sub renumber(@instructions) {
my $number = 0;
my %mapping;
# RAKUDO: $/ doesn't work in .subst closures
my $hack;
return @instructions.map: {
.subst( :global, / ('$' \d+) { $hack = ~$0 } /, {
my $varname = $hack;
if !%mapping.exists($varname) {
%mapping{$varname} = '$' ~ $number++;
}
%mapping{$varname}
} );
};
}
}
class Value {
has $.payload;
method store($v) { die "Can't assign to a readonly value" }
}
class Container {
has Value $!value;
method store(Value $v) { $!value = $v }
method fetch() { $!value }
method payload() { $!value.defined ?? $!value.payload !! "Any()" }
}
class Lexpad {
has @.slots;
has %.names;
has Lexpad $.outer;
method Str {
"lexpad[" ~ %.names.sort(*.value)>>.key.join(", ") ~ "]";
}
}
class Closure {
has $.block;
has Lexpad $.outer;
}
sub find-block(@sic, $name) {
for @sic.kv -> $n, $line {
return $n
if $line ~~ / ^ 'block '\'(<-[']>+)\'':' $ / && $0 eq $name;
}
die "Didn't find block $name";
}
sub find-label(@sic, $name) {
for @sic.kv -> $n, $line {
return $n
if $line ~~ / ^ ' `label '(\S+) $ / && $0 eq $name;
}
die "Didn't find label $name";
}
subset Yapsi::IO where { .can('say') }
class Yapsi::Runtime {
has Yapsi::IO $!io = $*OUT;
method run(@sic) {
# RAKUDO: Need to use 'not' here rather than '!~~' [perl #76892]
if not @sic[0] ~~ /^ 'This is SIC v'(\d\d\d\d\.\d\d) $/ {
die "Incompatible SIC version line";
}
elsif ~$0 ne $VERSION {
die "SIC is $0 but this is $VERSION -- cannot run";
}
my @registers-stack = [];
my @ip-stack;
sub reg() { @registers-stack[@registers-stack - 1] }
sub n-up-from($lexpad is copy, $levels) {
$lexpad.=outer for ^$levels;
die "Went too far and ended up nowhere"
unless defined $lexpad;
$lexpad;
}
my $global-lexpad;
sub new-lexpad-from(@sic, $line is copy, Lexpad $outer?) {
my (@vars, @slots);
# RAKUDO: Some Any()s seem to end up in the @sic array. Hence the
# need for prefix:<~>. Would be interesting to learn where
# this happens.
while ~@sic[++$line]
~~ / ' `' (\S*) :s \'(<-[']>+)\' ( ':'\w+)* / {
given $0 {
when "var" {
push @vars, ~$1;
my $is-our-variable = ?( ':our' eq any $2>>.Str );
my $container = $is-our-variable
?? (.slots[.names{~$1}] given $global-lexpad)
!! Container.new;
push @slots, $container;
}
default { die "Unknown directive $0"; }
}
}
return Lexpad.new(:@slots, :names((hash @vars.kv).invert), :$outer);
}
try {
$global-lexpad
= new-lexpad-from(@sic, find-block(@sic, 'GLOBAL'));
}
my $current-lexpad = new-lexpad-from(@sic, 2);
my $ip = 3;
while @registers-stack {
while @sic[$ip++] -> $line {
given $line.substr(4) {
when / ^ '`' / {}
when / ^ '$'(\d+) ' = ' (\d+) $ / { reg[+$0] = +$1 }
when / ^ 'store ['[(0)||'-'(\d+)]', '(\d+)'], $'(\d+) $ / {
my ($levels, $slot, $register) = +$0, +$1, +$2;
my $lexpad = n-up-from($current-lexpad, $levels);
$lexpad.slots[$slot].store(
Value.new( :payload(reg[$register]) )
);
}
when / ^ '$'(\d+)' = fetch '
'['[(0)||'-'(\d+)]', '(\d+)']' $ / {
my ($register, $levels, $slot) = +$0, +$1, +$2;
my $lexpad = n-up-from($current-lexpad, $levels);
reg[$register] = $lexpad.slots[$slot].payload();
}
when / ^ 'bind ['[(0)||'-'(\d+)]', '(\d+)'], '
'['[(0)||'-'(\d+)]', '(\d+)']' $ / {
my ($var1-levels, $var1-slot) = +$0, +$1;
my $var1-lexpad
= n-up-from($current-lexpad, $var1-levels);
my ($var2-levels, $var2-slot) = +$2, +$3;
my $var2-lexpad
= n-up-from($current-lexpad, $var2-levels);
$var1-lexpad.slots[$var1-slot]
= $var2-lexpad.slots[$var2-slot];
}
when / ^ 'inc $'(\d+) $ / {
reg[+$0] = reg[+$0] eq 'Any()' ?? 1 !! reg[+$0] + 1;
}
when / ^ 'dec $'(\d+) $ / {
reg[+$0] = reg[+$0] eq 'Any()' ?? 1 !! reg[+$0] - 1;
}
when / ^ 'jf $'(\d+)', '(\S+) $ / {
if reg[+$0] == 0 {
$ip = find-label(@sic, ~$1);
}
}
when / ^ 'jt $'(\d+)', '(\S+) $ / {
if reg[+$0] != 0 {
$ip = find-label(@sic, ~$1);
}
}
when / ^ 'jmp '(\S+) $ / {
$ip = find-label(@sic, ~$0);
}
when / ^ '$'(\d+)' = closure-from-block '
\'(<-[']>+)\' $ / {
reg[+$0] = Closure.new(:block(~$1),
:outer($current-lexpad));
}
when / ^ 'call $'(\d+) $ / {
die "Trying to call a non-closure"
if (my $closure = reg[+$0]) !~~ Closure;
push @registers-stack, [];
push @ip-stack, $ip;
$ip = find-block(@sic, $closure.block);
$current-lexpad
= new-lexpad-from(@sic, $ip, $closure.outer);
++$ip;
}
when / ^ 'say $'(\d+) $ / {
$!io.say(reg[+$0]);
}
default {
die "Unknown instruction: ", $_;
}
}
}
pop @registers-stack;
$ip = pop @ip-stack;
$current-lexpad.=outer;
}
}
}