-
Notifications
You must be signed in to change notification settings - Fork 14
/
Q.pm
672 lines (576 loc) · 19.1 KB
/
Q.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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
use _007::Val;
class X::Control::Return is Exception {
has $.frame;
has $.value;
}
class X::Subscript::TooLarge is Exception {
has $.value;
has $.length;
method message() { "Subscript ($.value) too large (array length $.length)" }
}
class X::Subscript::NonInteger is Exception {
}
class X::Subscript::NonString is Exception {
}
class X::ParameterMismatch is Exception {
has $.type;
has $.paramcount;
has $.argcount;
method message {
"$.type with $.paramcount parameters called with $.argcount arguments"
}
}
role Q {
method worthy-attributes {
sub aname($attr) { $attr.name.substr(2) }
sub avalue($attr) { $attr.get_value(self) }
sub worthy($attr) {
avalue($attr) !~~ Hash # avoids showing static-lexpad
&& (aname($attr) ne "type" || avalue($attr) ne "")
}
return self.^attributes.grep(&worthy);
}
method Str {
sub pretty($_) {
when Array {
return .elems == 0
?? "[]"
!! .elems == 1 && .[0].Str.lines == 1
?? "[{.[0].Str}]"
!! "[\n{.map({.Str}).join(",\n").indent(4)}\n]"
}
when Str { return .perl }
default { return .Str }
}
sub aname($attr) { $attr.name.substr(2) }
sub avalue($attr) { $attr.get_value(self) }
my @attrs = @.worthy-attributes;
if @attrs == 1 {
return "{self.^name} {pretty(avalue(@attrs[0]))}";
}
sub keyvalue($attr) { aname($attr) ~ ": " ~ pretty(avalue($attr)) }
if @attrs == 2 && aname(@attrs[1]) eq "expr" { # prefix or postfix
@attrs .= reverse; # because it looks nicer to have expr first
}
my $contents = @attrs.map(&keyvalue).join(",\n").indent(4);
return "{self.^name} \{\n$contents\n\}";
}
}
role Q::Expr does Q {
}
role Q::Literal does Q::Expr {
}
role Q::Literal::None does Q::Literal {
method new() { self.bless }
method eval($) { Val::None.new }
method interpolate($) { self }
}
role Q::Literal::Int does Q::Literal {
has $.value;
method new(Int $value) { self.bless(:$value) }
method eval($) { Val::Int.new(:$.value) }
method interpolate($) { self }
}
role Q::Literal::Str does Q::Literal {
has $.value;
method new(Str $value) { self.bless(:$value) }
method eval($) {
my $value = $.value.subst(q[\"], q["], :g).subst(q[\\\\], q[\\], :g);
Val::Str.new(:$value);
}
method interpolate($) { self }
}
role Q::Literal::Array does Q::Literal {
has @.elements;
method new(*@elements) {
self.bless(:@elements)
}
method eval($runtime) {
Val::Array.new(:elements(@.elements>>.eval($runtime)));
}
method interpolate($runtime) {
self.new(@.elements».interpolate($runtime));
}
}
role Q::Block does Q {
has $.parameterlist;
has $.statementlist;
has %.static-lexpad;
method new($parameterlist, $statementlist) { self.bless(:$parameterlist, :$statementlist) }
method eval($runtime) {
my $outer-frame = $runtime.current-frame;
Val::Block.new(
:$.parameterlist,
:$.statementlist,
:%.static-lexpad,
:$outer-frame
);
}
method interpolate($runtime) {
self.new(
$.parameterlist.interpolate($runtime),
$.statementlist.interpolate($runtime));
# XXX: but what about the static lexpad? we kind of lose it here, don't we?
# what does that *mean* in practice? can we come up with an example where
# it matters? if the static lexpad happens to contain a value which is a
# Q node, do we continue into *it*, interpolating it, too?
}
}
role Q::Identifier does Q::Expr {
has $.name;
method new(Str $name) { self.bless(:$name) }
method eval($runtime) {
return $runtime.get-var($.name);
}
method interpolate($) { self }
}
role Q::Unquote does Q {
has $.expr;
method new($expr) { self.bless(:$expr) }
method eval($runtime) {
die "Should never hit an unquote at runtime"; # XXX: turn into X::
}
method interpolate($runtime) {
my $q = $.expr.eval($runtime);
die "Expression inside unquote did not evaluate to a Q" # XXX: turn into X::
unless $q ~~ Q;
return $q;
}
}
role Q::Prefix does Q::Expr {
has $.expr;
has $.type = "";
method new($expr) { self.bless(:$expr) }
method eval($runtime) { ... }
method interpolate($runtime) {
self.new($.expr.interpolate($runtime));
}
}
role Q::Prefix::Minus does Q::Prefix {
method type { "[-]" }
method eval($runtime) {
my $expr = $.expr.eval($runtime);
die X::TypeCheck.new(:operation<->, :got($expr), :expected(Val::Int))
unless $expr ~~ Val::Int;
return Val::Int.new(:value(-$expr.value));
}
}
role Q::Prefix::Custom[$type] does Q::Prefix {
method type { "[$type]" }
method eval($runtime) {
my $e = $.expr.eval($runtime);
my $c = $runtime.get-var("prefix:<$type>");
return $runtime.call($c, [$e]);
}
}
role Q::Infix does Q::Expr {
has $.lhs;
has $.rhs;
has $.type = "";
method new($lhs, $rhs) { self.bless(:$lhs, :$rhs) }
method eval($runtime) { ... }
method interpolate($runtime) {
self.new($.lhs.interpolate($runtime), $.rhs.interpolate($runtime));
}
}
role Q::Infix::Addition does Q::Infix {
method type { "[+]" }
method eval($runtime) {
my $lhs = $.lhs.eval($runtime);
die X::TypeCheck.new(:operation<+>, :got($lhs), :expected(Val::Int))
unless $lhs ~~ Val::Int;
my $rhs = $.rhs.eval($runtime);
die X::TypeCheck.new(:operation<+>, :got($rhs), :expected(Val::Int))
unless $rhs ~~ Val::Int;
return Val::Int.new(:value(
$lhs.value + $rhs.value
));
}
}
role Q::Infix::Concat does Q::Infix {
method type { "[~]" }
method eval($runtime) {
my $lhs = $.lhs.eval($runtime);
die X::TypeCheck.new(:operation<~>, :got($lhs), :expected(Val::Str))
unless $lhs ~~ Val::Str;
my $rhs = $.rhs.eval($runtime);
die X::TypeCheck.new(:operation<~>, :got($rhs), :expected(Val::Str))
unless $rhs ~~ Val::Str;
return Val::Str.new(:value(
$lhs.value ~ $rhs.value
));
}
}
role Q::Infix::Assignment does Q::Infix {
method type { "[=]" }
method eval($runtime) {
die "Needs to be an identifier on the left" # XXX: Turn this into an X::
unless $.lhs ~~ Q::Identifier;
my $value = $.rhs.eval($runtime);
$runtime.put-var($.lhs.name, $value);
return $value;
}
}
role Q::Infix::Eq does Q::Infix {
method type { "[==]" }
method eval($runtime) {
multi equal-value(Val $, Val $) { False }
multi equal-value(Val::None, Val::None) { True }
multi equal-value(Val::Int $l, Val::Int $r) { $l.value == $r.value }
multi equal-value(Val::Str $l, Val::Str $r) { $l.value eq $r.value }
multi equal-value(Val::Array $l, Val::Array $r) {
sub equal-at-index($i) {
equal-value($l.elements[$i], $r.elements[$i]);
}
[&&] $l.elements == $r.elements,
|(^$l.elements).map(&equal-at-index);
}
multi equal-value(Val::Block $l, Val::Block $r) {
$l.name eq $r.name
&& equal-value($l.parameterlist, $r.parameterlist)
&& equal-value($l.statementlist, $r.statementlist)
}
multi equal-value(Q $l, Q $r) {
sub same-avalue($attr) {
equal-value($attr.get_value($l), $attr.get_value($r));
}
[&&] $l.WHAT === $r.WHAT,
|$l.worthy-attributes.map(&same-avalue);
}
multi equal-value(@l, @r) { # arrays occur in the internals of Qtrees
sub equal-at-index($i) { equal-value(@l[$i], @r[$i]) }
@l == @r && |(^@l).map(&equal-at-index);
}
multi equal-value(Str $l, Str $r) { $l eq $r } # strings do too
my $l = $.lhs.eval($runtime);
my $r = $.rhs.eval($runtime);
# converting Bool->Int because the implemented language doesn't have Bool
my $equal = +equal-value($l, $r);
return Val::Int.new(:value($equal));
}
}
role Q::Infix::Custom[$type] does Q::Infix {
method type { "[$type]" }
method eval($runtime) {
my $l = $.lhs.eval($runtime);
my $r = $.rhs.eval($runtime);
my $c = $runtime.get-var("infix:<$type>");
return $runtime.call($c, [$l, $r]);
}
}
role Q::Postfix does Q::Expr {
has $.expr;
has $.type = "";
method new($expr) { self.bless(:$expr) }
method eval($runtime) { ... }
}
role Q::Postfix::Index does Q::Postfix {
has $.index;
method new($expr, $index) { self.bless(:$expr, :$index) }
method eval($runtime) {
given $.expr.eval($runtime) {
when Val::Array {
my $index = $.index.eval($runtime);
die X::Subscript::NonInteger.new
if $index !~~ Val::Int;
die X::Subscript::TooLarge.new(:value($index.value), :length(.elements))
if $index.value >= .elements;
die X::Subscript::Negative.new(:$index, :type([]))
if $index.value < 0;
return .elements[$index.value];
}
when Q {
my $property = $.index.eval($runtime);
die X::Subscript::NonString.new
if $property !~~ Val::Str;
my $propname = $property.value;
return $runtime.property($_, $propname);
}
die X::TypeCheck.new(:operation<indexing>, :got($_), :expected(Val::Array));
}
}
method interpolate($runtime) {
self.new($.expr.interpolate($runtime), $.index.interpolate($runtime));
}
}
role Q::Postfix::Call does Q::Postfix {
has $.argumentlist;
method new($expr, $argumentlist) { self.bless(:$expr, :$argumentlist) }
method eval($runtime) {
my $c = $.expr.eval($runtime);
die "macro is called at runtime"
if $c ~~ Val::Macro;
die "Trying to invoke a {$c.^name.subst(/^'Val::'/, '')}" # XXX: make this into an X::
unless $c ~~ Val::Block;
my @args = $.argumentlist».eval($runtime);
return $runtime.call($c, @args);
}
method interpolate($runtime) {
self.new($.expr.interpolate($runtime), $.argumentlist.interpolate($runtime));
}
}
role Q::Postfix::Property does Q::Postfix {
has $.ident;
method new($expr, $ident) { self.bless(:$expr, :$ident) }
method eval($runtime) {
my $obj = $.expr.eval($runtime);
my $propname = $.ident.name;
return $runtime.property($obj, $propname);
}
method interpolate($runtime) {
self.new($.expr.interpolate($runtime), $.ident.interpolate($runtime));
}
}
role Q::Postfix::Custom[$type] does Q::Postfix {
method type { "[$type]" }
method eval($runtime) {
my $e = $.expr.eval($runtime);
my $c = $runtime.get-var("postfix:<$type>");
return $runtime.call($c, [$e]);
}
method interpolate($runtime) {
self.new($.expr.interpolate($runtime));
}
}
role Q::ParameterList does Q {
has @.parameters handles <elems Numeric Real list>;
method new(*@parameters) { self.bless(:@parameters) }
method interpolate($runtime) {
self.new(@.parameters».interpolate($runtime));
}
}
role Q::ArgumentList does Q {
has @.arguments handles <elems Numeric Real list>;
method new(*@arguments) { self.bless(:@arguments) }
method interpolate($runtime) {
self.new(@.arguments».interpolate($runtime));
}
}
role Q::Statement does Q {
}
role Q::Statement::My does Q::Statement {
has $.ident;
has $.expr;
method new($ident, $expr = Empty) { self.bless(:$ident, :$expr) }
method run($runtime) {
return
unless $.expr;
my $value = $.expr.eval($runtime);
$runtime.put-var($.ident.name, $value);
}
method interpolate($runtime) {
self.new($.ident.interpolate($runtime),
$.expr === Empty ?? Empty !! $.expr.interpolate($runtime));
}
}
role Q::Statement::Constant does Q::Statement {
has $.ident;
has $.expr;
method new($ident, $expr = Empty) { self.bless(:$ident, :$expr) }
method run($runtime) {
# value has already been assigned
}
method interpolate($runtime) {
self.new($.ident.interpolate($runtime),
$.expr === Empty ?? Empty !! $.expr.interpolate($runtime)); # XXX: and here
}
}
role Q::Statement::Expr does Q::Statement {
has $.expr;
method new($expr) { self.bless(:$expr) }
method run($runtime) {
$.expr.eval($runtime);
}
method interpolate($runtime) {
self.new($.expr.interpolate($runtime));
}
}
role Q::Statement::If does Q::Statement {
has $.expr;
has $.block;
method new($expr, Q::Block $block) { self.bless(:$expr, :$block) }
method run($runtime) {
my $expr = $.expr.eval($runtime);
if $expr.truthy {
my $c = $.block.eval($runtime);
$runtime.enter($c);
my $paramcount = $c.parameterlist.elems;
die X::ParameterMismatch.new(
:type("If statement"), :$paramcount, :argcount("0 or 1"))
if $paramcount > 1;
for @($c.parameterlist) Z $expr -> ($param, $arg) {
$runtime.declare-var($param.name, $arg);
}
$.block.statementlist.run($runtime);
$runtime.leave;
}
}
method interpolate($runtime) {
self.new($.expr.interpolate($runtime), $.block.interpolate($runtime));
}
}
role Q::Statement::Block does Q::Statement {
has $.block;
method new(Q::Block $block) { self.bless(:$block) }
method run($runtime) {
$runtime.enter($.block.eval($runtime));
$.block.statementlist.run($runtime);
$runtime.leave;
}
method interpolate($runtime) {
self.new($.block.interpolate($runtime));
}
}
role Q::CompUnit does Q::Statement::Block {
}
role Q::Statement::For does Q::Statement {
has $.expr;
has $.block;
method new($expr, Q::Block $block) { self.bless(:$expr, :$block) }
method run($runtime) {
multi split_elements(@array, 1) { return @array }
multi split_elements(@array, Int $n) {
my $list = @array.list;
my @split;
while True {
my @new = $list.splice(0, $n);
last unless @new;
@split.push: @new.item;
}
@split;
}
my $c = $.block.eval($runtime);
my $count = $c.parameterlist.elems;
my $array = $.expr.eval($runtime);
die X::TypeCheck.new(:operation("for loop"), :got($array), :expected(Val::Array))
unless $array ~~ Val::Array;
if $count == 0 {
for $array.elements {
$runtime.enter($c);
$.block.statementlist.run($runtime);
$runtime.leave;
}
}
else {
for split_elements($array.elements, $count) -> $arg {
$runtime.enter($c);
for @($c.parameterlist) Z $arg.list -> ($param, $real_arg) {
$runtime.declare-var($param.name, $real_arg);
}
$.block.statementlist.run($runtime);
$runtime.leave;
}
}
}
method interpolate($runtime) {
self.new($.expr.interpolate($runtime), $.block.interpolate($runtime));
}
}
role Q::Statement::While does Q::Statement {
has $.expr;
has $.block;
method new($expr, Q::Block $block) { self.bless(:$expr, :$block) }
method run($runtime) {
while (my $expr = $.expr.eval($runtime)).truthy {
my $c = $.block.eval($runtime);
$runtime.enter($c);
my $paramcount = $c.parameterlist.elems;
die X::ParameterMismatch.new(
:type("While loop"), :$paramcount, :argcount("0 or 1"))
if $paramcount > 1;
for @($c.parameterlist) Z $expr -> ($param, $arg) {
$runtime.declare-var($param.name, $arg);
}
$.block.statementlist.run($runtime);
$runtime.leave;
}
}
method interpolate($runtime) {
self.new($.expr.interpolate($runtime), $.block.interpolate($runtime));
}
}
role Q::Statement::Return does Q::Statement {
has $.expr;
sub NONE { role { method eval($) { Val::None.new }; method Str { "(no return value)" } } }
method new($expr = NONE) { self.bless(:$expr) }
method run($runtime) {
my $frame = $runtime.get-var("--RETURN-TO--");
die X::Control::Return.new(:value($.expr.eval($runtime)), :$frame);
}
method interpolate($runtime) {
self.new($.expr.interpolate($runtime));
}
}
role Q::Statement::Sub does Q::Statement {
has $.ident;
has $.block;
method new($ident, $block) {
self.bless(:$ident, :$block);
}
method run($runtime) {
}
method interpolate($runtime) {
self.new($.ident.interpolate($runtime),
$.block.interpolate($runtime));
}
}
role Q::Statement::Macro does Q::Statement {
has $.ident;
has $.block;
method new($ident, $block) {
self.bless(:$ident, :$block);
}
method run($runtime) {
}
method interpolate($runtime) {
self.new($.ident.interpolate($runtime),
$.block.interpolate($runtime));
}
}
role Q::Statement::BEGIN does Q::Statement {
has $.block;
method new(Q::Block $block) { self.bless(:$block) }
method run($runtime) {
# a BEGIN block does not run at runtime
}
method interpolate($runtime) {
self.new($.block.interpolate($runtime));
}
}
role Q::StatementList does Q {
has @.statements handles <elems Numeric Real list>;
method new(*@statements) { self.bless(:@statements) }
method run($runtime) {
for @.statements -> $statement {
$statement.run($runtime);
}
}
method interpolate($runtime) {
self.new(@.statements».interpolate($runtime));
}
}
role Q::Trait does Q {
has $.ident;
has $.expr;
method new($ident, $expr) {
self.bless(:$ident, :$expr);
}
method interpolate($runtime) {
self.new($.ident.interpolate($runtime), $.expr.interpolate($runtime));
}
}
role Q::Quasi does Q::Expr {
has $.statementlist;
method new($statementlist) { self.bless(:$statementlist) }
method eval($runtime) {
my $statementlist = $.statementlist.interpolate($runtime);
return Q::Block.new(Q::ParameterList.new, $statementlist);
}
method interpolate($runtime) {
self.new($.statementlist.interpolate($runtime));
# XXX: the fact that we keep interpolating inside of the quasi means
# that unquotes encountered inside of this inner quasi will be
# interpolated in the context of the outer quasi. is this correct?
# can we come up with a case where it matters?
}
}