-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
Actions.nqp
3866 lines (3365 loc) Β· 129 KB
/
Actions.nqp
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 NQPP6QRegex;
use NQPP5QRegex;
#-------------------------------------------------------------------------------
# The classes of the AST nodes come from the Raku setting bootstrap, so
# we need to load them from there. We also need the OperatorProperties
# class, so fetch that as well.
my $RakuAST-WHO; # RakuAST.WHO
my $OperatorProperties; # OperatorProperties class
# Logic for setting up RakuAST-WHO and OperatorProperties
sub setup-RakuAST-WHO() {
unless nqp::isconcrete($RakuAST-WHO) {
my $loader := nqp::gethllsym('Raku','ModuleLoader');
my $unit := $loader.load_module('Perl6::BOOTSTRAP::v6c',{},GLOBALish);
my $export := $unit<EXPORT>.WHO<DEFAULT>.WHO;
$RakuAST-WHO := nqp::existskey($export,'RakuAST')
?? nqp::atkey($export,'RakuAST').WHO
!! nqp::die('Cannot find RakuAST nodes');
$OperatorProperties := nqp::atkey($export,'OperatorProperties');
}
}
# Provide easy lookup of RakuAST:: classes at runtime of the
# actions. These classes can **NOT** be referenced directly
# as they may not yet be known when the grammar / actions
# run. Note that direct specification of RakuAST classes
# *will* compile, but may cause compile time issues of Raku
# code, typically resulting in error messages stating a method
# having been called on VMNull.
sub Nodify(*@todo) {
my $res := nqp::atkey($RakuAST-WHO,nqp::shift(@todo));
while @todo && !nqp::isnull($res) {
$res := nqp::atkey($res.WHO,nqp::shift(@todo));
}
nqp::ifnull(
$res,
nqp::die('No such node RakuAST::' ~ nqp::join('::',@todo))
)
}
#-------------------------------------------------------------------------------
# Role for all Action classes associated with Raku grammar slangs
role Raku::CommonActions {
# Some AST nodes need symbol resolution or attachment of position
# information as we go. This factors out that process and attaches
# the AST to the match object.
method attach($/, $node, :$as-key-origin) {
if nqp::istype($node, Nodify('ImplicitLookups')) {
$node.resolve-implicit-lookups-with($*R);
}
if nqp::istype($node, Nodify('Attaching')) {
$node.attach($*R);
}
self.SET-NODE-ORIGIN($/, $node, :$as-key-origin);
make $node;
}
method SET-NODE-ORIGIN($/, $node, :$as-key-origin) {
# XXX This is a temporary stub to avoid unimplemented nodes.
# Must be replaced with exception throwing when RakuAST is
# considered ready for this.
unless nqp::isconcrete($node) {
return
}
if nqp::istype($node, Nodify('Node')) {
unless nqp::isconcrete($node.origin) {
$node.set-origin(
Nodify('Origin').new(
:from($/.from),
:to($/.to),
:source($*ORIGIN-SOURCE)));
}
if $as-key-origin {
my $nestings := @*ORIGIN-NESTINGS;
unless nqp::istype($node, Nodify('CompUnit')) {
@*PARENT-NESTINGS.push($node)
}
$node.origin.set-nestings($nestings);
}
}
}
method key-origin($/) {
self.SET-NODE-ORIGIN($/, $/.ast, :as-key-origin);
}
method quibble($/) {
self.attach: $/, $<nibble>.ast;
}
# Grammars also need to be able to lookup RakuAST nodes. Historically
# this was done with the "r" method. Since it is apparently impossible
# to reliably export Nodify, this interface is kept alive.
method r(*@parts) { Nodify(|@parts) }
}
#-------------------------------------------------------------------------------
# The actions associated with the base Raku grammar
class Raku::Actions is HLL::Actions does Raku::CommonActions {
method OperatorProperties() { $OperatorProperties }
#-------------------------------------------------------------------------------
# Compilation unit, language version and other entry point bits
# Thread-safely produce a unique serialization context ID
my $count := -1;
my $lock := NQPLock.new;
sub next-id() { $lock.protect({ ++$count }) }
# Given a package, returns a low-level hash for its stash
sub stash-hash($package) {
my $hash := $package.WHO;
nqp::ishash($hash)
?? $hash
!! $hash.FLATTENABLE_HASH
}
# Perform all actions that are needed before any actual parsing can
# be done by a grammar.
method comp-unit-prologue($/) {
# Be ready to do Nodify lookups
setup-RakuAST-WHO();
# Be ready to report locations in the source.
$*ORIGIN-SOURCE := Nodify('Origin', 'Source').new(:orig($/.target()));
# Set up the literals builder, so we can produce and intern literal
# values.
$*LITERALS := Nodify('LiteralBuilder').new;
# Set up the base resolver
my %OPTIONS := %*OPTIONS;
my $context := %OPTIONS<outer_ctx>;
my $resolver-type := Nodify('Resolver', 'Compile');
my $RESOLVER := $*R := nqp::isconcrete($context)
?? $resolver-type.from-context(
:$context, :global(%OPTIONS<global>), :resolver($*OUTER-RESOLVER)
)
!! $resolver-type.from-setting(
:setting-name(%OPTIONS<setting> // 'CORE.d')
);
# Make debugging a *lot* easier
&*DD := $RESOLVER.setting-constant('&dd');
}
# Perform all actions related to "use vxxx" and loading appropriate
# (default) settings and configuring the compilation unit and resolver
method lang-setup($/) {
# Look up these dynamic vars only once
my $HLL-COMPILER := $*HLL-COMPILER;
my %OPTIONS := %*OPTIONS;
my $LANG := $*LANG;
my $RESOLVER := $*R;
# Some shortcuts;
my $language-revision := $HLL-COMPILER.language_revision;
my $is-EVAL := nqp::isconcrete(%OPTIONS<outer_ctx>);
my $setting-name := %OPTIONS<setting>;
# Helper sub to configure the resolver with selected language revision
my sub resolver-from-revision() {
$setting-name := 'CORE.' ~ $HLL-COMPILER.lvs.p6rev($language-revision);
$RESOLVER.set-setting(:$setting-name);
}
# Not EVALling and explicit setting requested
if $setting-name && !$is-EVAL {
# TODO This branch is for when we start compiling the CORE.
if nqp::eqat($setting-name, 'NULL.', 0) {
$*COMPILING_CORE_SETTING := 1;
if $setting-name ne 'NULL.c' {
my $loader := nqp::gethllsym('Raku', 'ModuleLoader');
$*R.set-setting(:setting-name($loader.previous_setting_name($setting-name)));
}
else {
# TODO CORE.c is being compiled. What resolver is to be used?
nqp::die("Can't compiler CORE.c yet");
}
}
# Setting name is explicitly set. Use it to determine the
# default language revision.
else {
$RESOLVER.set-setting(:setting-name($setting-name));
$language-revision :=
nqp::unbox_i($RESOLVER.setting-constant('CORE-SETTING-REV'));
$HLL-COMPILER.set_language_revision($language-revision);
}
}
# Seen a -use vxxx- statement
my $version := $<version>
?? ~$<version>
!! nqp::getenvhash()<RAKU_LANGUAGE_VERSION> || "";
if $version {
my @vparts := $HLL-COMPILER.lvs.from-public-repr($version);
my %lang-revisions := $HLL-COMPILER.language_revisions;
my @final-version;
my $modifier-deprecated;
# Globbed version needs a bit of research needs to be done first.
if nqp::index($version,'*') >= 0 || nqp::index($version,'+') >= 0 {
my $Version := $RESOLVER.setting-constant('Version');
my $ver-requested := $Version.new(
$HLL-COMPILER.lvs.from-public-repr($version, :as-str)
);
my @can-versions := $HLL-COMPILER.can_language_versions;
my $can-version;
my $i := nqp::elems(@can-versions);
# Iterate over the version candidates from higher to lower
# ones, skip these that don't match the requested version
# glob, and these without a modifier but one is required.
# Like 6.e would be a valid version in the future, but for
# now it has to be 6.e.PREVIEW.
while --$i >= 0 {
$can-version := $Version.new(@can-versions[$i]);
next unless $ver-requested.ACCEPTS($can-version);
# If version candidate
my $can-parts := $can-version.parts;
my $can-revision := nqp::unbox_i($can-parts.head);
last
unless $can-parts.elems == 1
&& nqp::existskey(%lang-revisions{$can-revision},'require');
}
if $i < 0 {
$/.typed-panic: 'X::Language::Unsupported', :$version;
}
# Are there any easier way to unbox boxable types?
my $Int := $RESOLVER.setting-constant('Int');
my $Str := $RESOLVER.setting-constant('Str');
my @can-parts := nqp::getattr($can-version, $Version, '$!parts');
for @can-parts -> $part {
@final-version.push: nqp::isint($part) || nqp::isstr($part)
?? $part
!! nqp::istype($part, $Int)
?? nqp::unbox_i($part)
!! nqp::istype($part, $Str)
?? nqp::unbox_s($part)
!! nqp::die(
"Don't know how to handle version part of '"
~ $part.HOW.name($part)
~ "' type"
);
}
}
# A non-globbed version can be used as-is, make sure it is valid
else {
my $revision := @vparts[0];
# Consider version to have a language modifier if the last
# part of is a string of non-zero length.
my $modifier := @vparts > 1 && nqp::objprimspec(@vparts[-1]) == 3
?? @vparts[-1]
!! nqp::null();
# Do we know this language version?
unless nqp::existskey(%lang-revisions, $revision)
&& (!$modifier || nqp::existskey(%lang-revisions{$revision}<mods>, $modifier))
{
$/.typed-panic: 'X::Language::Unsupported', :$version;
}
my %config := %lang-revisions{$revision};
# If version is known, is it used with a required modifier?
if nqp::existskey(%config,'require')
&& (!$modifier || %config<require> ne $modifier) {
$/.typed-panic: 'X::Language::ModRequired',
:$version, :modifier(%config<require>);
}
# We can't issue a worry immediately because the current
# resolver is temporary, so just set a flag
if $modifier && %config<mods>{$modifier}<deprecate> {
$modifier-deprecated := $modifier;
}
@final-version := @vparts;
}
$HLL-COMPILER.set_language_version(@final-version);
$language-revision := @final-version[0];
resolver-from-revision();
# Now the resolver is final, express our modifier concern!
if $modifier-deprecated {
# At this point our compiler version is final.
$/.worry:
"$modifier-deprecated modifier is deprecated for Raku v"
~ $HLL-COMPILER.language_version;
}
}
# No version seen and not in an EVAL
elsif !$is-EVAL {
resolver-from-revision();
}
# Locate an EXPORTHOW and set those mappings on our current language.
my $EXPORTHOW :=
$RESOLVER.resolve-lexical-constant('EXPORTHOW').compile-time-value;
for stash-hash($EXPORTHOW) {
$LANG.set_how($_.key, $_.value);
}
my $package-how := $LANG.how('package');
my $export-package := $package-how.new_type(name => 'EXPORT');
$export-package.HOW.compose($export-package);
$RESOLVER.set-export-package($export-package);
$*EXPORT := $export-package;
# Create a compilation unit.
my $comp-unit-name := $*ORIGIN-SOURCE.original-file ~ $/.target;
# It's an EVAL. We'll take our GLOBAL, $?PACKAGE, etc. from that.
if $is-EVAL {
$*CU := Nodify('CompUnit').new(
:comp-unit-name($comp-unit-name ~ next-id), # uniqify
:$setting-name,
:eval,
:outer-cu($*OUTER-CU),
:$language-revision
);
}
# Top-level compilation.
else {
$*CU := Nodify('CompUnit').new(
:$comp-unit-name,
:$setting-name,
:global-package-how($package-how),
:precompilation-mode(%OPTIONS<precomp>),
:$export-package,
:$language-revision
);
# Create a GLOBAL using the correct package meta-object.
my $global := $*CU.generated-global;
$RESOLVER.set-global($global);
nqp::bindhllsym('Raku','GLOBAL',$global);
}
$*LITERALS.set-resolver($RESOLVER);
}
method comp-unit($/) {
# Do dynamic lookups once
my $COMPUNIT := $*CU;
my %OPTIONS := %*OPTIONS;
my $RESOLVER := $*R;
# Put the body in place.
$COMPUNIT.replace-statement-list($<statementlist>.ast);
# Sort out sinking; the compilation unit is sunk as a whole if we are
# not in a REPL or EVAL context.
$COMPUNIT.mark-sunk() unless nqp::existskey(%OPTIONS,'outer_ctx');
$COMPUNIT.calculate-sink();
# if --(raku)doc specified, add INIT phaser that handles that
if nqp::existskey(%OPTIONS,'doc') {
$COMPUNIT.add-INIT-phaser-for-doc-handling(
'Pod', %OPTIONS<doc> || 'Text'
);
}
elsif nqp::existskey(%OPTIONS,'rakudoc') {
$COMPUNIT.add-INIT-phaser-for-doc-handling(
'RakuDoc', %OPTIONS<rakudoc> || 'Text'
);
}
# Have check time.
$COMPUNIT.check($RESOLVER);
my $exception := $RESOLVER.produce-compilation-exception;
if nqp::isconcrete($exception) {
if $RESOLVER.has-compilation-errors {
# Really has errors, so report them.
$exception.throw;
}
else {
# Only potential difficulties, just just print them.
stderr().print($exception.gist);
}
}
self.attach: $/, $COMPUNIT, :as-key-origin;
}
# Action method to load any modules specified with -M
method load-M-modules($/) {
my $M := %*OPTIONS<M>;
unless nqp::defined($M) {
return; # nothing to do here
}
# Create a RakuAST statement list with -use- statements
# of the specified module names and attach that
my $ast := Nodify('StatementList').new;
for nqp::islist($M) ?? $M !! [$M] -> $longname {
my $use := Nodify('Statement', 'Use').new(
module-name => Nodify('Name').from-identifier-parts(
|nqp::split('::', $longname)
)
);
$use.ensure-begin-performed($*R, $*CU.context);
$ast.add-statement: $use;
}
self.attach: $/, $ast;
}
#-------------------------------------------------------------------------------
# Statement level handling
# Helper method to collect statements and potentially and declarator
# docs for an actual StatementList, StatementSequence or SemiList.
method collect-statements($/, $typename) {
my $statements := Nodify($typename).new;
for $<statement> {
$_.ast.add-to-statements($statements);
}
self.attach: $/, $statements;
$statements
}
# Action methods for the various collectors of statements
method statementlist($/) {
my $statements := self.collect-statements($/, 'StatementList');
# Add any uncollected doc blocks. This can happen if there
# are no statements in a statementlist, e.g. in a rakudoc
# only file.
for $*DOC-BLOCKS-COLLECTED {
$statements.add-doc-block($_);
}
$*DOC-BLOCKS-COLLECTED := [];
}
method semilist($/) { self.collect-statements($/, 'SemiList') }
method sequence($/) { self.collect-statements($/, 'StatementSequence') }
# Action method for handling an actual statement
method statement($/) {
# Setting label on already created statement
if $<label> {
my $ast := $<statement>.ast;
$ast.add-label($<label>.ast);
make $ast;
return; # nothing left to do here
}
# Statement ID must be captured before creation of statement object
my $statement-id := $*STATEMENT-ID;
my $statement;
# Handle expression with optional condition/loop modifiers
if $<EXPR> {
my $ast := $<EXPR>.ast;
if nqp::istype($ast, Nodify('ColonPairs')) {
$ast := Nodify('ApplyListInfix').new:
:infix(Nodify('Infix').new(',')),
:operands($ast.colonpairs);
}
$statement := Nodify('Statement','Expression').new(:expression($ast));
$statement.replace-condition-modifier($<statement-mod-cond>.ast)
if $<statement-mod-cond>;
$statement.replace-loop-modifier($<statement-mod-loop>.ast)
if $<statement-mod-loop>;
}
# Handle statement control (if / for / given / when / etc.)
elsif $<statement-control> {
$statement := $<statement-control>.ast;
}
# Handle an empty statement
else {
$statement := Nodify('Statement','Empty').new;
}
# Final statement tweaks
$statement.set-trace(1) if $/.pragma('trace');
$statement.set-statement-id($statement-id);
$statement.attach-doc-blocks unless $*PARSING-DOC-BLOCK;
self.attach: $/, $statement;
}
# Action method for handling labels attached to a statement
method label($/) {
my $name := ~$<identifier>;
my $decl := Nodify('Label').new($name);
$*R.declare-lexical($decl)
?? $/.typed-panic('X::Redeclaration', :symbol($name))
!! self.attach: $/, $decl;
}
# Helper method for attaching (pointy) blocks
method attach-block($/, $signature?) {
my $block := $*BLOCK;
$block.replace-signature($signature.ast) if $signature;
$block.replace-body($<blockoid>.ast);
$block.ensure-begin-performed($*R, $*CU.context);
self.attach: $/, $block;
}
# Action methods for handling (pointy) blocks
method pointy-block($/) { self.attach-block($/, $<signature>) }
method block($/) { self.attach-block($/) }
# Action method for handling the inside of (pointy) blocks
method blockoid($/) {
self.attach: $/,
Nodify('Blockoid').new($<statementlist>.ast),
:as-key-origin;
}
# Action method for handling "unit" scoped packages
method unit-block($/) {
my $block := $*BLOCK;
# Wrap the statements into a (non-existing) blockoid
$block.replace-body(Nodify('Blockoid').new($<statementlist>.ast));
$block.ensure-begin-performed($*R, $*CU.context);
self.attach: $/, $block;
}
# Action method handling {*}
method onlystar($/) {
self.attach: $/, Nodify('OnlyStar').new;
}
# Helper method to connect any leading declarator doc that was
# collected already to the given declarand
method set-declarand($/, $it) {
# Ignoring this one
if $*IGNORE-NEXT-DECLARAND {
$*IGNORE-NEXT-DECLARAND := 0;
}
# Should handle
else {
my $from := $/.from;
my $worries := $*DECLARAND-WORRIES;
for $worries {
$_.value.typed-worry:
'X::Syntax::Doc::Declarator::MissingDeclarand'
if $_.key < $from;
nqp::deletekey($worries, $_.key);
}
$*DECLARAND := $it;
$*LAST-TRAILING-LINE := +$*ORIGIN-SOURCE.original-line($from);
if @*LEADING-DOC -> @leading {
$it.set-leading(@leading);
@*LEADING-DOC := [];
}
$*IGNORE-NEXT-DECLARAND := nqp::istype($it,Nodify('Package'));
}
$it
}
# Helper methof to steal the information of the current declarand
# into the given declarand, and make *that* the declarand. Needed
# for cases like subsets with a where, where the where block is
# seen *before* the subset, causing leading declarator doc to be
# attached to the where block, rather than to the subset.
method steal-declarand($/, $it) {
$it.set-WHY($*DECLARAND.cut-WHY);
$*DECLARAND := $it;
$*LAST-TRAILING-LINE := +$*ORIGIN-SOURCE.original-line($/.from);
}
# Action method when entering a scope (package, sub, phaser etc.)
# Assume the $*BLOCK dynamic var is appropriately localized as it
# will set that with the RakuAST:: object being created.
method enter-block-scope($/) {
my $block := $*MULTINESS
?? Nodify($*SCOPE-KIND).new(:multiness($*MULTINESS))
!! Nodify($*SCOPE-KIND).new;
$*R.enter-scope($block);
$*BLOCK := $block;
self.set-declarand($/, $block)
if nqp::istype($block,Nodify('Doc','DeclaratorTarget'));
}
# Action method when leaving a scope.
method leave-block-scope($/) {
$*R.leave-scope();
}
#-------------------------------------------------------------------------------
# Statement control
# Helper method for control statements taking a block without any
# intervening expression
method takes-none($/, $name) {
self.attach: $/, Nodify('Statement',$name).new(body => $<block>.ast);
}
# Helper method for control statements taking a source and a pointy
# block
method takes-source($/, $name) {
self.attach: $/, Nodify('Statement',$name).new(
source => $<EXPR>.ast, body => $<pointy-block>.ast
);
}
# Helper method for conditional statements taking a condition and
# a pointy block
method takes-cond($/, $name) {
self.attach: $/, Nodify('Statement',$name).new(
condition => $<EXPR>.ast, body => $<pointy-block>.ast
);
}
# Helper method for looping statements taking a condition and
# a pointy block
method takes-loop($/, $name) {
self.attach: $/, Nodify('Statement','Loop',$name).new(
condition => $<EXPR>.ast, body => $<pointy-block>.ast
);
}
# Handling of simple control statements that take a block
method statement-control:sym<default>($/) { self.takes-none($/,'Default') }
method statement-control:sym<CATCH>($/) { self.takes-none($/,'Catch') }
method statement-control:sym<CONTROL>($/) { self.takes-none($/,'Control') }
# Handling of simple control statements that take a pointy block
method statement-control:sym<for>($/) { self.takes-source($/,'For') }
method statement-control:sym<given>($/) { self.takes-source($/,'Given') }
method statement-control:sym<unless>($/) { self.takes-cond($/,'Unless') }
method statement-control:sym<when>($/) { self.takes-cond($/,'When') }
method statement-control:sym<without>($/) { self.takes-cond($/,'Without') }
# Handling of all forms of loops
method statement-control:sym<repeat>($/) {
self.takes-loop($/, 'Repeat' ~ nqp::tclc(~$<wu>))
}
method statement-control:sym<while>($/) {
self.takes-loop($/, nqp::tclc(~$<sym>))
}
# Handling of whenever
method statement-control:sym<whenever>($/) {
self.attach: $/,
Nodify('Statement', 'Whenever').new:
trigger => $<EXPR>.ast, body => $<pointy-block>.ast;
}
# Dummy control statement to set a trait on a target
method statement-control:sym<also>($/) {
if $*ALSO-TARGET -> $target {
for $<trait> {
$target.add-trait($_.ast);
}
$target.apply-traits($*R, $*CU.context, $target);
self.attach: $/, Nodify('Statement','Empty').new;
}
else {
$/.panic("Could not find target for 'also'");
}
}
# Basic if / with handling with all of the elsifs / orelses
method statement-control:sym<if>($/) {
# collect the if and all of the elsifs / orelses
my @elsifs;
my $index := 0;
for $<sym> {
@elsifs.push:
Nodify('Statement', nqp::tclc(~$_)).new:
condition => $<condition>[$index].ast,
then => $<then>[$index].ast;
++$index;
}
# the first is the main part, add others if appropriate
my $ast := @elsifs.shift;
$ast.set-elsifs(@elsifs) if @elsifs;
$ast.set-else($<else>.ast) if $<else>;
self.attach: $/, $ast;
}
# Basic loop handling
method statement-control:sym<loop>($/) {
my %parts;
%parts<setup> := $<e1>.ast if $<e1>;
%parts<condition> := $<e2>.ast if $<e2>;
%parts<increment> := $<e3>.ast if $<e3>;
%parts<body> := $<block>.ast;
self.attach: $/, Nodify('Statement', 'Loop').new(|%parts);
}
#-------------------------------------------------------------------------------
# Pragma and module loading related statements
# "no foo" can only mean a pragma at the moment
method statement-control:sym<no>($/) {
my str $name := ~$<module_name>;
my $Pragma := Nodify('Pragma');
if $Pragma.IS-PRAGMA($name) {
my $ast := $<arglist><EXPR>
?? $Pragma.new(:$name, :argument($<arglist><EXPR>.ast), :off)
!! $Pragma.new(:$name, :off);
$ast.ensure-begin-performed($*R, $*CU.context);
self.attach: $/, $ast;
}
else {
nqp::die("Don't know how to 'no " ~ $name ~ "'just yet")
}
}
method statement-control:sym<use>($/) {
my str $name := ~$<module_name>;
my $Pragma := Nodify('Pragma');
my $ast;
if $Pragma.IS-PRAGMA($name) {
$ast := $<arglist><EXPR>
?? $Pragma.new(:$name, :argument($<arglist><EXPR>.ast))
!! $Pragma.new(:$name);
$ast.ensure-begin-performed($*R, $*CU.context);
}
# proper module loading
else {
$ast := $<arglist><EXPR>
?? Nodify('Statement', 'Use').new(
:module-name($<module_name>.ast),
:argument($<arglist><EXPR>.ast)
)
!! Nodify('Statement', 'Use').new(
:module-name($<module_name>.ast)
);
$ast.ensure-begin-performed($*R, $*CU.context);
for $ast.IMPL-UNWRAP-LIST($ast.categoricals) {
$/.add-categorical(
$_.category, $_.opname, $_.canname, $_.subname, $_.declarand);
}
}
self.attach: $/, $ast;
}
method statement-control:sym<need>($/) {
my @module-names;
for $<module_name> {
@module-names.push: $_.ast;
}
my $ast := Nodify('Statement', 'Need').new(:@module-names);
$ast.ensure-begin-performed($*R, $*CU.context);
self.attach: $/, $ast;
}
method statement-control:sym<import>($/) {
my $ast := $<arglist><EXPR>
?? Nodify('Statement', 'Import').new(
:module-name($<module_name>.ast),
:argument($<arglist><EXPR>.ast)
)
!! Nodify('Statement', 'Import').new(
:module-name($<module_name>.ast)
);
$ast.IMPL-CHECK($*R, $*CU.context, 1);
for $ast.IMPL-UNWRAP-LIST($ast.categoricals) {
$/.add-categorical(
$_.category, $_.opname, $_.canname, $_.subname, $_.declarand);
}
self.attach: $/, $ast;
}
method statement-control:sym<require>($/) {
#TODO non-trivial cases, args
self.attach: $/, Nodify('Statement', 'Require').new(
module-name => $<module_name>.ast,
);
}
#-------------------------------------------------------------------------------
# Statement modifiers
# Helper method to attach an expression
method modifier-expr($/) {
self.attach: $/, $<EXPR>.ast;
}
# Helper method for setting up statement modifiers
method SM-cond($/, $name) {
self.attach: $/,
Nodify('StatementModifier', $name).new($<modifier-expr>.ast)
}
# Simple statement modifiers
method statement-mod-cond:sym<if>($/) { self.SM-cond($/, 'If') }
method statement-mod-cond:sym<unless>($/) { self.SM-cond($/, 'Unless') }
method statement-mod-cond:sym<when>($/) { self.SM-cond($/, 'When') }
method statement-mod-cond:sym<with>($/) { self.SM-cond($/, 'With') }
method statement-mod-cond:sym<without>($/) { self.SM-cond($/, 'Without') }
# Statement modifiers that set $_
method statement-mod-loop:sym<for>($/) { self.SM-cond($/, 'For') }
method statement-mod-loop:sym<given>($/) { self.SM-cond($/, 'Given') }
method statement-mod-loop:sym<until>($/) { self.SM-cond($/, 'Until') }
method statement-mod-loop:sym<while>($/) { self.SM-cond($/, 'While') }
#-------------------------------------------------------------------------------
# Phasers
# Helper method for setting up simple phasers that just take a blorst
method SP-phaser($/, $name) {
self.attach: $/,
Nodify('StatementPrefix', 'Phaser', $name).new($<blorst>.ast)
}
# Simple phasers that just take a blorst
method statement-prefix:sym<CHECK>($/) { self.SP-phaser($/, 'Check') }
method statement-prefix:sym<CLOSE>($/) { self.SP-phaser($/, 'Close') }
method statement-prefix:sym<END>($/) { self.SP-phaser($/, 'End') }
method statement-prefix:sym<ENTER>($/) { self.SP-phaser($/, 'Enter') }
method statement-prefix:sym<FIRST>($/) { self.SP-phaser($/, 'First') }
method statement-prefix:sym<INIT>($/) { self.SP-phaser($/, 'Init') }
method statement-prefix:sym<KEEP>($/) { self.SP-phaser($/, 'Keep') }
method statement-prefix:sym<LAST>($/) { self.SP-phaser($/, 'Last') }
method statement-prefix:sym<LEAVE>($/) { self.SP-phaser($/, 'Leave') }
method statement-prefix:sym<NEXT>($/) { self.SP-phaser($/, 'Next') }
method statement-prefix:sym<QUIT>($/) { self.SP-phaser($/, 'Quit') }
method statement-prefix:sym<UNDO>($/) { self.SP-phaser($/, 'Undo') }
# BEGIN phaser needs to be executed *now* and produce a value
method statement-prefix:sym<BEGIN>($/) {
my $ast :=
Nodify('StatementPrefix','Phaser','Begin').new($<blorst>.ast);
$ast.ensure-begin-performed($*R, $*CU.context);
self.attach: $/, $ast;
}
# PRE/POST phasers need a stringification of the blorst as well
method statement-prefix:sym<PRE>($/) {
self.attach: $/, Nodify(
'StatementPrefix', 'Phaser', 'Pre'
).new($<blorst>.ast, ~$<blorst>);
}
method statement-prefix:sym<POST>($/) {
self.attach: $/, Nodify(
'StatementPrefix', 'Phaser', 'Post'
).new($<blorst>.ast, ~$<blorst>);
}
# DOC phaser only works if so activated on command line
method statement-prefix:sym<DOC>($/) {
if %*OPTIONS<doc> || %*OPTIONS<rakudoc> {
my $phase := ~$<phase>;
$phase eq 'BEGIN'
?? self.statement-prefix:sym<BEGIN>($/)
!! self.SP-phaser($/, nqp::tclc($phase));
}
# not activated
else {
self.attach: $/, self.Nil
}
}
#-------------------------------------------------------------------------------
# Statement prefixes
# Helper method to normalize a blorst
method blorst($/) { self.attach: $/, $<block>.ast }
# Helper method for setting up simple prefix that just take a blorst
method SP-prefix($/, $name) {
self.attach: $/,
Nodify('StatementPrefix', $name).new($<blorst>.ast)
}
# Simple prefixes that just take a blorst
method statement-prefix:sym<do>($/) { self.SP-prefix($/, 'Do') }
method statement-prefix:sym<eager>($/) { self.SP-prefix($/, 'Eager') }
method statement-prefix:sym<gather>($/) { self.SP-prefix($/, 'Gather') }
method statement-prefix:sym<once>($/) { self.SP-prefix($/, 'Once') }
method statement-prefix:sym<quietly>($/) { self.SP-prefix($/, 'Quietly') }
method statement-prefix:sym<react>($/) { self.SP-prefix($/, 'React') }
method statement-prefix:sym<start>($/) { self.SP-prefix($/, 'Start') }
method statement-prefix:sym<supply>($/) { self.SP-prefix($/, 'Supply') }
method statement-prefix:sym<try>($/) { self.SP-prefix($/, 'Try') }
# Helper method for statement prefixes that modify for loops
method SP-looper($/, $mode) {
my $ast := $<blorst>.ast;
if nqp::istype($ast, Nodify('Statement', 'For')) {
$ast.replace-mode(nqp::lc($mode));
self.attach: $/, $ast;
}
else {
self.SP-prefix($/, $mode);
}
}
# Prefixes that work differently on for loops
method statement-prefix:sym<hyper>($/) { self.SP-looper($/, 'Hyper') }
method statement-prefix:sym<lazy>($/) { self.SP-looper($/, 'Lazy') }
method statement-prefix:sym<race>($/) { self.SP-looper($/, 'Race') }
#-------------------------------------------------------------------------------
# Expression generation
# Just a term
method EXPR($/) {
self.attach: $/, $/.ast // $<OPER>.ast;
$/ # simplies end of EXPR "token"
}
# A ternary expression
method TERNARY-EXPR($/) {
self.attach: $/, Nodify('Ternary').new:
condition => $/[0].ast,
then => $<infix><EXPR>.ast, # the way the grammar parses
else => $/[1].ast;
}
# An assignment, or infix expression
method INFIX-EXPR($/) {
if $<infix><sym> eq '=' {
my $lhs := $/[0];
if nqp::istype($lhs,Nodify('ApplyPostfix')) {
my $postfix := $lhs.postfix;
if (nqp::istype( # [foo]
$postfix,
Nodify('Postcircumfix','ArrayIndex')
) && $postfix.index.statements.elems == 1
) || nqp::istype( # <bar>
$postfix,
Nodify('Postcircumfix','LiteralHashIndex')
) {
$postfix.set-assignee($/[1]);
self.attach: $/, $lhs;
return;
}
}
}
my $infix := $/.ast;
if nqp::istype($infix, Nodify('DottyInfixish')) {
self.attach: $/, Nodify('ApplyDottyInfix').new:
infix => $infix, left => $/[0].ast, right => $/[1].ast;
}
else {
self.attach: $/, Nodify('ApplyInfix').new:
infix => $infix, left => $/[0].ast, right => $/[1].ast;
}
}
# A listy expression
method LIST-EXPR($/) {
my @operands;
for $/.list {
my $ast := $_.ast;
@operands.push($ast) if nqp::isconcrete($ast);
}
self.attach: $/, Nodify('ApplyListInfix').new:
infix => $/.ast, operands => @operands;
}
# A prefix expression
method PREFIX-EXPR($/) {
self.attach: $/, Nodify('ApplyPrefix').new:
prefix => $/.ast // Nodify('Prefix').new($<prefix><sym>),
operand => $/[0].ast;
}
# A postfix expression
method POSTFIX-EXPR($/) {
my $ast := $/.ast;
my $operand := $/[0].ast;
my $cp := $<colonpair>;
if $cp {
if $*ADVERB-AS-INFIX
&& (nqp::istype($operand, Nodify('ColonPair'))
|| nqp::istype($operand, Nodify('ColonPairs'))
) {
self.attach: $/, Nodify('ColonPairs').new($operand,$cp.ast);
}