-
Notifications
You must be signed in to change notification settings - Fork 14
/
Q.pm
1086 lines (927 loc) · 27.8 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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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"
}
}
class X::Property::NotFound is Exception {
has $.propname;
has $.type;
method message {
"Property '$.propname' not found on object of type $.type"
}
}
class X::Associativity::Conflict is Exception {
method message { "The operator already has a defined associativity" }
}
class X::Regex::InvalidMatchType is Exception {
method message { "A regex can only match strings" }
}
class X::_007::RuntimeException is Exception {
has $.msg;
method message {
$.msg.Str;
}
}
sub aname($attr) { $attr.name.substr(2) }
sub avalue($attr, $obj) { $attr.get_value($obj) }
role Q {
method Str {
my %*stringification-seen;
Helper::Str(self);
}
method quoted-Str {
self.Str
}
method truthy {
True
}
method attributes {
sub find($aname) { self.^attributes.first({ $aname eq aname($_) }) }
self.can("attribute-order")
?? self.attribute-order.map({ find($_) })
!! self.^attributes;
}
}
### ### Q::Expr
###
### An expression; something that can be evaluated to a value.
###
role Q::Expr does Q {
method eval($runtime) { ... }
}
### ### Q::Term
###
### A term; a unit of parsing describing a value or an identifier. Along with
### operators, what makes up expressions.
###
role Q::Term does Q::Expr {
}
### ### Q::Literal
###
### A literal; a constant value written out explicitly in the program, such as
### `None`, `True`, `5`, or `"James Bond"`.
###
### Compound values such as arrays and objects are considered terms but not
### literals.
###
role Q::Literal does Q::Term {
}
### ### Q::Literal::None
###
### The `None` literal.
###
class Q::Literal::None does Q::Literal {
method eval($) { NONE }
}
### ### Q::Literal::Bool
###
### A boolean literal; either `True` or `False`.
###
class Q::Literal::Bool does Q::Literal {
has Val::Bool $.value;
method eval($) { $.value }
}
### ### Q::Literal::Int
###
### An integer literal; a non-negative number.
###
### Negative numbers are not themselves considered integer literals: something
### like `-5` is parsed as a `prefix:<->` containing a literal `5`.
###
class Q::Literal::Int does Q::Literal {
has Val::Int $.value;
method eval($) { $.value }
}
### ### Q::Literal::Str
###
### A string literal.
###
class Q::Literal::Str does Q::Literal {
has Val::Str $.value;
method eval($) { $.value }
}
### ### Q::Identifier
###
### An identifier; a name which identifies a storage location in the program.
###
### Identifiers are subject to *scoping*: the same name can point to different
### storage locations because they belong to different scopes.
###
class Q::Identifier does Q::Term {
has Val::Str $.name;
has $.frame = NONE;
method attribute-order { <name> }
method eval($runtime) {
return $runtime.get-var($.name.value, $.frame);
}
method put-value($value, $runtime) {
$runtime.put-var(self, $value);
}
}
### ### Q::Term::Regex
###
### A regular expression (*regex*).
###
class Q::Term::Regex does Q::Term {
has Val::Str $.contents;
method eval($runtime) {
Val::Regex.new(:$.contents);
}
}
### ### Q::Term::Array
###
### An array. Array terms consist of zero or more *elements*, each of which
### can be an arbitrary expression.
###
class Q::Term::Array does Q::Term {
has Val::Array $.elements;
method eval($runtime) {
Val::Array.new(:elements($.elements.elements.map(*.eval($runtime))));
}
}
### ### Q::Term::Object
###
### An object. Object terms consist of an optional *type*, and a property list
### with zero or more key/value pairs.
###
class Q::Term::Object does Q::Term {
has Q::Identifier $.type;
has $.propertylist;
method eval($runtime) {
return $runtime.get-var($.type.name.value, $.type.frame).create(
$.propertylist.properties.elements.map({.key.value => .value.eval($runtime)})
);
}
}
### ### Q::Property
###
### An object property. Properties have a key and a value.
###
class Q::Property does Q {
has Val::Str $.key;
has $.value;
}
### ### Q::PropertyList
###
### A property list in an object. Property lists have zero or more key/value
### pairs. Keys in objects are considered unordered, but a property list has
### a specified order: the order the properties occur in the program text.
###
class Q::PropertyList does Q {
# RAKUDO: Can simplify this to `.=` once [RT #126975] is fixed
has Val::Array $.properties = Val::Array.new;
}
### ### Q::Declaration
###
### A declaration; something that introduces a name.
###
role Q::Declaration {
method is-assignable { False }
}
### ### Q::Trait
###
### A trait; a piece of metadata for a routine. A trait consists of an
### identifier and an expression.
###
class Q::Trait does Q {
has $.identifier;
has $.expr;
method attribute-order { <identifier expr> }
}
### ### Q::TraitList
###
### A list of zero or more traits. Each routine has a traitlist.
###
class Q::TraitList does Q {
# RAKUDO: Can simplify this to `.=` once [RT #126975] is fixed
has Val::Array $.traits = Val::Array.new;
method attribute-order { <traits> }
}
### ### Q::Term::Sub
###
### A subroutine.
###
class Q::Term::Sub does Q::Term does Q::Declaration {
has $.identifier;
has $.traitlist = Q::TraitList.new;
has $.block;
method attribute-order { <identifier traitlist block> }
method eval($runtime) {
my $name = $.identifier ~~ Val::NoneType
?? Val::Str.new(:value(""))
!! $.identifier.name;
return Val::Sub.new(
:$name,
:parameterlist($.block.parameterlist),
:statementlist($.block.statementlist),
:static-lexpad($.block.static-lexpad),
:outer-frame($runtime.current-frame),
);
}
}
### ### Q::Block
###
### A block. Blocks are used in a number of places: by routines, by
### block statements, by other compound statements (such as `if` statements)
### and by `quasi` terms and sub terms. Blocks are not, however, terms
### in their own regard.
###
### A block has a parameter list and a statement list, each of which can
### be empty.
###
class Q::Block does Q {
has $.parameterlist;
has $.statementlist;
has Val::Object $.static-lexpad is rw = Val::Object.new;
method attribute-order { <parameterlist statementlist> }
}
### ### Q::Prefix
###
### A prefix operator; an operator that occurs before a term, like the
### `-` in `-5`.
###
class Q::Prefix does Q::Expr {
has $.identifier;
has $.operand;
method attribute-order { <identifier operand> }
method eval($runtime) {
my $e = $.operand.eval($runtime);
my $c = $.identifier.eval($runtime);
return $runtime.call($c, [$e]);
}
}
### ### Q::Prefix::Str
###
### A stringification operator.
###
class Q::Prefix::Str is Q::Prefix {}
### ### Q::Prefix::Plus
###
### A numification operator.
###
class Q::Prefix::Plus is Q::Prefix {}
### ### Q::Prefix::Minus
###
### A numeric negation operator.
###
class Q::Prefix::Minus is Q::Prefix {}
### ### Q::Prefix::Not
###
### A boolean negation operator.
###
class Q::Prefix::Not is Q::Prefix {}
### ### Q::Prefix::Upto
###
### An "upto" operator; applied to a number `n` it produces an array
### of values `[0, 1, ... , n-1]`.
###
class Q::Prefix::Upto is Q::Prefix {}
### ### Q::Infix
###
### An infix operator; something like the `+` in `2 + 2` that occurs between
### two terms.
###
class Q::Infix does Q::Expr {
has $.identifier;
has $.lhs;
has $.rhs;
method attribute-order { <identifier lhs rhs> }
method eval($runtime) {
my $l = $.lhs.eval($runtime);
my $r = $.rhs.eval($runtime);
my $c = $.identifier.eval($runtime);
return $runtime.call($c, [$l, $r]);
}
}
### ### Q::Infix::Addition
###
### A numeric addition operator.
###
class Q::Infix::Addition is Q::Infix {}
### ### Q::Infix::Addition
###
### A numeric subtraction operator.
###
class Q::Infix::Subtraction is Q::Infix {}
### ### Q::Infix::Multiplication
###
### A numeric multiplication operator.
###
class Q::Infix::Multiplication is Q::Infix {}
### ### Q::Infix::Modulo
###
### A numeric modulo operator; produces the *remainder* left from an integer
### division between two numbers. For example, `456 % 100` is `56` because the
### remainder from dividing `456` by `100` is `56`.
###
class Q::Infix::Modulo is Q::Infix {}
### ### Q::Infix::Divisibility
###
### A divisibility test operator. Returns `True` exactly when the remainder
### operator would return `0`.
###
class Q::Infix::Divisibility is Q::Infix {}
### ### Q::Infix::Concat
###
### A string concatenation operator. Returns a single string that is the
### result of sequentially putting two strings together.
###
class Q::Infix::Concat is Q::Infix {}
### ### Q::Infix::Replicate
###
### A string replication operator. Returns a string which consists of `n`
### copies of a string.
###
class Q::Infix::Replicate is Q::Infix {}
### ### Q::Infix::ArrayReplicate
###
### An array replication operator. Returns an array which consists of
### the original array's elements, repeated `n` times.
###
class Q::Infix::ArrayReplicate is Q::Infix {}
### ### Q::Infix::Cons
###
### A "cons" operator. Given a value and an array, returns a new
### array with the value added as the first element.
###
class Q::Infix::Cons is Q::Infix {}
### ### Q::Infix::Assignment
###
### An assignment operator. Puts a value in a storage location.
###
class Q::Infix::Assignment is Q::Infix {
method eval($runtime) {
my $value = $.rhs.eval($runtime);
$.lhs.put-value($value, $runtime);
return $value;
}
}
### ### Q::Infix::Eq
###
### A string equality test operator.
###
class Q::Infix::Eq is Q::Infix {}
### ### Q::Infix::Ne
###
### A string inequality test operator.
###
class Q::Infix::Ne is Q::Infix {}
### ### Q::Infix::Gt
###
### A string greater-than test operator.
###
class Q::Infix::Gt is Q::Infix {}
### ### Q::Infix::Lt
###
### A string less-than test operator.
###
class Q::Infix::Lt is Q::Infix {}
### ### Q::Infix::Ge
###
### A string greater-than-or-equal test operator.
###
class Q::Infix::Ge is Q::Infix {}
### ### Q::Infix::Le
###
### A string less-than-or-equal test operator.
###
class Q::Infix::Le is Q::Infix {}
### ### Q::Infix::Or
###
### A short-circuiting disjunction operator; evaluates its right-hand
### side only if the left-hand side is falsy.
###
class Q::Infix::Or is Q::Infix {
method eval($runtime) {
my $l = $.lhs.eval($runtime);
return $l.truthy
?? $l
!! $.rhs.eval($runtime);
}
}
### ### Q::Infix::DefinedOr
###
### A short-circuiting "defined-or" operator. Evaluates its
### right-hand side only if the left-hand side is `None`.
###
class Q::Infix::DefinedOr is Q::Infix {
method eval($runtime) {
my $l = $.lhs.eval($runtime);
return $l !~~ Val::NoneType
?? $l
!! $.rhs.eval($runtime);
}
}
### ### Q::Infix::And
###
### A short-circuiting "and" operator. Evaluates its
### right-hand side only if the left-hand side is truthy.
###
class Q::Infix::And is Q::Infix {
method eval($runtime) {
my $l = $.lhs.eval($runtime);
return !$l.truthy
?? $l
!! $.rhs.eval($runtime);
}
}
### ### Q::Infix::TypeMatch
###
### A type match operator. Checks if a value on the left-hand side has
### the type on the right-hand side, including subtypes.
###
class Q::Infix::TypeMatch is Q::Infix {}
### ### Q::Infix::TypeNonMatch
###
### A negative type match operator. Returns `True` exactly in the cases
### a type match would return `False`.
###
class Q::Infix::TypeNonMatch is Q::Infix {}
### ### Q::Postfix
###
### A postfix operator; something like the `[0]` in `agents[0]` that occurs
### after a term.
###
class Q::Postfix does Q::Expr {
has $.identifier;
has $.operand;
method attribute-order { <identifier operand> }
method eval($runtime) {
my $e = $.operand.eval($runtime);
my $c = $.identifier.eval($runtime);
return $runtime.call($c, [$e]);
}
}
### ### Q::Postfix::Index
###
### An indexing operator; returns an array element or object property.
### Arrays expect integer indices and objects expect string property names.
###
class Q::Postfix::Index is Q::Postfix {
has $.index;
method attribute-order { <identifier operand index> }
method eval($runtime) {
given $.operand.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 Val::Object | Val::Sub | 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 put-value($value, $runtime) {
given $.operand.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;
.elements[$index.value] = $value;
}
when Val::Object | Q {
my $property = $.index.eval($runtime);
die X::Subscript::NonString.new
if $property !~~ Val::Str;
my $propname = $property.value;
$runtime.put-property($_, $propname, $value);
}
die X::TypeCheck.new(:operation<indexing>, :got($_), :expected(Val::Array));
}
}
}
### ### Q::Postfix::Call
###
### An invocation operator; calls a routine.
###
class Q::Postfix::Call is Q::Postfix {
has $.argumentlist;
method attribute-order { <identifier operand argumentlist> }
method eval($runtime) {
my $c = $.operand.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::Sub;
my @arguments = $.argumentlist.arguments.elements.map(*.eval($runtime));
return $runtime.call($c, @arguments);
}
}
### ### Q::Postfix::Property
###
### An object property operator; fetches a property out of an object.
###
class Q::Postfix::Property is Q::Postfix {
has $.property;
method attribute-order { <identifier operand property> }
method eval($runtime) {
my $obj = $.operand.eval($runtime);
my $propname = $.property.name.value;
$runtime.property($obj, $propname);
}
method put-value($value, $runtime) {
given $.operand.eval($runtime) {
when Val::Object | Q {
my $propname = $.property.name.value;
$runtime.put-property($_, $propname, $value);
}
die "We don't handle this case yet"; # XXX: think more about this case
}
}
}
### ### Q::Unquote
###
### An unquote; allows Qtree fragments to be inserted into places in a quasi.
###
class Q::Unquote does Q {
has $.qtype;
has $.expr;
method eval($runtime) {
die "Should never hit an unquote at runtime"; # XXX: turn into X::
}
}
### ### Q::Unquote::Prefix
###
### An unquote which is a prefix operator.
###
class Q::Unquote::Prefix is Q::Unquote {
has $.operand;
}
### ### Q::Unquote::Infix
###
### An unquote which is an infix operator.
###
class Q::Unquote::Infix is Q::Unquote {
has $.lhs;
has $.rhs;
}
### ### Q::Term::Quasi
###
### A quasi; a piece of 007 code which evaluates to that code's Qtree
### representation. A way to "quote" code in a program instead of running
### it directly in place. Used together with macros.
###
### The term "quasi" comes from the fact that inside the quoted code there
### can be parametric holes ("unquotes") where Qtree fragments can be
### inserted. Quasiquotation is the practice of combining literal code
### fragments with such parametric holes.
###
class Q::Term::Quasi does Q::Term {
has $.qtype;
has $.contents;
method attribute-order { <qtype contents> }
method eval($runtime) {
sub interpolate($thing) {
return $thing.new(:elements($thing.elements.map(&interpolate)))
if $thing ~~ Val::Array;
return $thing.new(:properties(%($thing.properties.map(.key => interpolate(.value)))))
if $thing ~~ Val::Object;
return $thing
if $thing ~~ Val;
return $thing.new(:name($thing.name), :frame($runtime.current-frame))
if $thing ~~ Q::Identifier;
if $thing ~~ Q::Unquote::Prefix {
my $prefix = $thing.expr.eval($runtime);
die X::TypeCheck.new(:operation("interpolating an unquote"), :got($prefix), :expected(Q::Prefix))
unless $prefix ~~ Q::Prefix;
return $prefix.new(:identifier($prefix.identifier), :operand($thing.operand));
}
elsif $thing ~~ Q::Unquote::Infix {
my $infix = $thing.expr.eval($runtime);
die X::TypeCheck.new(:operation("interpolating an unquote"), :got($infix), :expected(Q::Infix))
unless $infix ~~ Q::Infix;
return $infix.new(:identifier($infix.identifier), :lhs($thing.lhs), :rhs($thing.rhs));
}
if $thing ~~ Q::Unquote {
my $ast = $thing.expr.eval($runtime);
die "Expression inside unquote did not evaluate to a Q" # XXX: turn into X::
unless $ast ~~ Q;
return $ast;
}
my %attributes = $thing.attributes.map: -> $attr {
aname($attr) => interpolate(avalue($attr, $thing))
};
$thing.new(|%attributes);
}
if $.qtype.value eq "Q::Unquote" && $.contents ~~ Q::Unquote {
return $.contents;
}
return interpolate($.contents);
}
}
### ### Q::Parameter
###
### A parameter. Any identifier that's declared as the input to a block
### is a parameter, including subs, macros, and `if` statements.
###
class Q::Parameter does Q does Q::Declaration {
has $.identifier;
method is-assignable { True }
}
### ### Q::ParameterList
###
### A list of zero or more parameters.
###
class Q::ParameterList does Q {
# RAKUDO: Can simplify this to `.=` once [RT #126975] is fixed
has Val::Array $.parameters = Val::Array.new;
}
### ### Q::ArgumentList
###
### A list of zero or more arguments.
###
class Q::ArgumentList does Q {
# RAKUDO: Can simplify this to `.=` once [RT #126975] is fixed
has Val::Array $.arguments = Val::Array.new;
}
### ### Q::Statement
###
### A statement.
###
role Q::Statement does Q {
}
### ### Q::Statement::My
###
### A `my` variable declaration statement.
###
class Q::Statement::My does Q::Statement does Q::Declaration {
has $.identifier;
has $.expr = NONE;
method attribute-order { <identifier expr> }
method is-assignable { True }
method run($runtime) {
return
unless $.expr !~~ Val::NoneType;
my $value = $.expr.eval($runtime);
$.identifier.put-value($value, $runtime);
}
}
### ### Q::Statement::Constant
###
### A `constant` declaration statement.
###
class Q::Statement::Constant does Q::Statement does Q::Declaration {
has $.identifier;
has $.expr;
method attribute-order { <identifier expr> }
method run($runtime) {
# value has already been assigned
}
}
### ### Q::Statement::Expr
###
### A statement consisting of an expression.
###
class Q::Statement::Expr does Q::Statement {
has $.expr;
method run($runtime) {
$.expr.eval($runtime);
}
}
### ### Q::Statement::If
###
### An `if` statement.
###
class Q::Statement::If does Q::Statement {
has $.expr;
has $.block;
has $.else = NONE;
method attribute-order { <expr block else> }
method run($runtime) {
my $expr = $.expr.eval($runtime);
if $expr.truthy {
my $paramcount = $.block.parameterlist.elems;
die X::ParameterMismatch.new(
:type("If statement"), :$paramcount, :argcount("0 or 1"))
if $paramcount > 1;
$runtime.enter($runtime.current-frame, $.block.static-lexpad, $.block.statementlist);
for @($.block.parameterlist.parameters.elements) Z $expr -> ($param, $arg) {
$runtime.declare-var($param.identifier, $arg);
}
$.block.statementlist.run($runtime);
$runtime.leave;
}
else {
given $.else {
when Q::Statement::If {
$.else.run($runtime)
}
when Q::Block {
$runtime.enter($runtime.current-frame, $.else.static-lexpad, $.else.statementlist);
$.else.statementlist.run($runtime);
$runtime.leave;
}
}
}
}
}
### ### Q::Statement::Block
###
### A block statement.
###
class Q::Statement::Block does Q::Statement {
has $.block;
method run($runtime) {
$runtime.enter($runtime.current-frame, $.block.static-lexpad, $.block.statementlist);
$.block.statementlist.run($runtime);
$runtime.leave;
}
}
### ### Q::CompUnit
###
### A block-level statement representing a whole compilation unit.
### We can read "compilation unit" here as meaning "file".
###
class Q::CompUnit is Q::Statement::Block {
}
### ### Q::Statement::For
###
### A `for` loop statement.
###
class Q::Statement::For does Q::Statement {
has $.expr;
has $.block;
method attribute-order { <expr block> }
method run($runtime) {
my $count = $.block.parameterlist.parameters.elements.elems;
die X::ParameterMismatch.new(
:type("For loop"), :paramcount($count), :argcount("0 or 1"))
if $count > 1;
my $array = $.expr.eval($runtime);
die X::TypeCheck.new(:operation("for loop"), :got($array), :expected(Val::Array))
unless $array ~~ Val::Array;
for $array.elements -> $arg {
$runtime.enter($runtime.current-frame, $.block.static-lexpad, $.block.statementlist);
if $count == 1 {
$runtime.declare-var($.block.parameterlist.parameters.elements[0].identifier, $arg.list[0]);
}
$.block.statementlist.run($runtime);
$runtime.leave;
}
}
}
### ### Q::Statement::While
###
### A `while` loop statement.
###
class Q::Statement::While does Q::Statement {
has $.expr;
has $.block;
method attribute-order { <expr block> }
method run($runtime) {
while (my $expr = $.expr.eval($runtime)).truthy {
my $paramcount = $.block.parameterlist.parameters.elements.elems;
die X::ParameterMismatch.new(
:type("While loop"), :$paramcount, :argcount("0 or 1"))
if $paramcount > 1;
$runtime.enter($runtime.current-frame, $.block.static-lexpad, $.block.statementlist);
for @($.block.parameterlist.parameters.elements) Z $expr -> ($param, $arg) {
$runtime.declare-var($param.identifier, $arg);
}
$.block.statementlist.run($runtime);
$runtime.leave;
}
}
}
### ### Q::Statement::Return
###
### A `return` statement.
###
class Q::Statement::Return does Q::Statement {
has $.expr = NONE;
method run($runtime) {
my $value = $.expr ~~ Val::NoneType ?? $.expr !! $.expr.eval($runtime);
my $frame = $runtime.get-var("--RETURN-TO--");
die X::Control::Return.new(:$value, :$frame);
}
}
### ### Q::Statement::Throw
###
### A `throw` statement.
###
class Q::Statement::Throw does Q::Statement {
has $.expr = NONE;
method run($runtime) {
my $value = $.expr ~~ Val::NoneType
?? Val::Exception.new(:message(Val::Str.new(:value("Died"))))
!! $.expr.eval($runtime);
die X::TypeCheck.new(:got($value), :excpected(Val::Exception))
if $value !~~ Val::Exception;
die X::_007::RuntimeException.new(:msg($value.message.value));
}
}
### ### Q::Statement::Sub
###
### A subroutine declaration statement.
###
class Q::Statement::Sub does Q::Statement does Q::Declaration {
has $.identifier;
has $.traitlist = Q::TraitList.new;
has Q::Block $.block;
method attribute-order { <identifier traitlist block> }