/
NQP.nqp
3460 lines (3058 loc) · 120 KB
/
NQP.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
# This file automatically generated by tools/build/gen-cat.pl
# From src/NQP/World.nqp
use NQPP6QRegexMoar;
use QASTMoar;
class NQP::World is HLL::World {
# The stack of lexical pads, actually as QAST::Block objects. The
# outermost frame is at the bottom, the latest frame is on top.
has @!BLOCKS;
# Set of code objects that need to be fixed up if dynamic compilation
# takes place (that is, compiling parts of the program early during
# compile time because they're needed at a BEGIN phase). Maps per-
# compilation unit ID to a list of code objects.
has %!code_objects_to_fix_up;
# The types of those code objects.
has %!code_object_types;
# Mapping of QAST::Stmts node containing fixups, keyed by sub ID. If
# we do dynamic compilation then we do the fixups immediately and
# then clear this list.
has %!code_object_fixup_list;
# Mapping of sub IDs to SC indexes of code stubs.
has %!code_stub_sc_idx;
# Clear-up tasks.
has @!clearup_tasks;
method BUILD(*%opts) {
@!BLOCKS := nqp::list();
%!code_objects_to_fix_up := nqp::hash();
%!code_object_types := nqp::hash();
%!code_object_fixup_list := nqp::hash();
%!code_stub_sc_idx := nqp::hash();
@!clearup_tasks := nqp::list();
}
# Creates a new lexical scope and puts it on top of the stack.
method push_lexpad($/) {
# Create pad, link to outer and add to stack.
my $pad := QAST::Block.new( QAST::Stmts.new(), :node($/) );
if +@!BLOCKS {
$pad<outer> := @!BLOCKS[+@!BLOCKS - 1];
}
@!BLOCKS[+@!BLOCKS] := $pad;
$pad
}
# Pops a lexical scope off the stack.
method pop_lexpad() {
@!BLOCKS.pop()
}
# Gets the top lexpad.
method cur_lexpad() {
@!BLOCKS[+@!BLOCKS - 1]
}
# XXX This goes away really soon...after the multi refactor.
method get_legacy_block_list() {
my @x := nqp::clone(@!BLOCKS);
@x.reverse();
@x
}
# We need to load the module loader to load modules, which means we
# can't just use ...; it; we break the circularity by using the global
# HLL symbol stash.
my $loader := nqp::getcurhllsym('ModuleLoader');
# Loads the setting and emits code
method load_setting($setting_name) {
# Do nothing for the NULL setting.
if $setting_name ne 'NULL' {
# Load it immediately, so the compile time info is available.
# Once it's loaded, set it as the outer context of the code
# being compiled.
my $setting := %*COMPILING<%?OPTIONS><outer_ctx>
:= $loader.load_setting($setting_name);
# Emit fixup or loading code.
my $set_outer := QAST::Op.new(
:op('forceouterctx'),
QAST::BVal.new( :value($*UNIT) ),
QAST::Op.new(
:op('callmethod'), :name('load_setting'),
QAST::Op.new(
:op('getcurhllsym'),
QAST::SVal.new( :value('ModuleLoader') )
),
QAST::SVal.new( :value($setting_name) )
));
if self.is_precompilation_mode() {
self.add_load_dependency_task(:deserialize_past(QAST::Stmts.new(
QAST::Op.new(
:op('loadbytecode'),
QAST::VM.new(
:parrot(QAST::SVal.new( :value('ModuleLoader.pbc') )),
:jvm(QAST::SVal.new( :value('ModuleLoader.class') ))
)),
$set_outer
)));
}
else {
self.add_fixup_task(:fixup_past($set_outer));
}
return nqp::ctxlexpad($setting);
}
}
# Loads a module immediately, and also makes sure we load it
# during the deserialization.
method load_module($module_name, $cur_GLOBALish) {
# Immediate loading.
my $module := $loader.load_module($module_name, $cur_GLOBALish);
# Make sure we do the loading during deserialization.
if self.is_precompilation_mode() {
self.add_load_dependency_task(:deserialize_past(QAST::Stmts.new(
QAST::Op.new(
:op('loadbytecode'),
QAST::VM.new(
:parrot(QAST::SVal.new( :value('ModuleLoader.pbc') )),
:jvm(QAST::SVal.new( :value('ModuleLoader.class') ))
)),
QAST::Op.new(
:op('callmethod'), :name('load_module'),
QAST::Op.new(
:op('getcurhllsym'),
QAST::SVal.new( :value('ModuleLoader') )
),
QAST::SVal.new( :value($module_name) )
))));
}
return nqp::ctxlexpad($module);
}
method import($stash) {
my $target := self.cur_lexpad();
for $stash {
self.install_lexical_symbol($target, $_.key, $_.value);
}
}
# Installs a symbol into the package. Does so immediately, and
# makes sure this happens on deserialization also.
method install_package_symbol($package, @sym, $obj) {
@sym := nqp::clone(@sym);
my $name := ~@sym.pop();
# Install symbol immediately.
my $target := $package;
for @sym {
my $part := ~$_;
if nqp::existskey($target.WHO, $part) {
$target := nqp::atkey($target.WHO, $part);
}
else {
my $pkgtype := nqp::existskey(%*HOW, 'package')
?? nqp::atkey(%*HOW, 'package')
!! nqp::atkey(%*HOW, 'knowhow');
my $pkg := $pkgtype.new_type(:name($part));
$pkg.HOW.compose($pkg);
$target := nqp::bindkey($target.WHO, $part, $pkg);
}
}
($target.WHO){$name} := $obj;
}
# Installs a lexical symbol. Takes a QAST::Block object, name and
# the object to install.
method install_lexical_symbol($block, $name, $obj) {
$block.symbol($name, :scope('lexical'), :value($obj));
$block[0].push(QAST::Var.new(
:scope('lexical'), :name($name), :decl('static'), :value($obj)
));
}
# Adds a fixup to install a specified QAST::Block in a package under the
# specified name.
method install_package_routine($package, $name, $past_block) {
my $fixup := QAST::Op.new(
:op('bindkey'),
QAST::Op.new( :op('who'), QAST::WVal.new( :value($package) ) ),
QAST::SVal.new( :value(~$name) ),
QAST::BVal.new( :value($past_block) )
);
self.add_fixup_task(:deserialize_past($fixup), :fixup_past($fixup));
}
# Registers a code object, and gives it a dynamic compilation thunk.
# Makes a real code object if it's a dispatcher.
method create_code($past, $name, $is_dispatcher, :$code_type_name = 'NQPRoutine') {
# See if NQPRoutine is available to wrap this up in.
my $code_type;
my $have_code_type := 0;
try {
$code_type := self.find_sym([$code_type_name]);
$have_code_type := $*PACKAGE.HOW.name($*PACKAGE) ne $code_type_name;
}
# For code refs, we need a "stub" that we'll clone and use for the
# compile-time representation. If it ever gets invoked it'll go
# and compile the code and run it.
# XXX Lexical environment.
my $stub_code := sub (*@args, *%named) {
# Do the compilation.
$past.unshift(self.libs());
my $compiler := nqp::getcomp('nqp');
my $compiled := $compiler.compile(
QAST::CompUnit.new( :hll('nqp'), $past ),
:from<ast>, :compunit_ok(1),
:bootstrap(%*COMPILING<%?OPTIONS><bootstrap> ?? 1 !! 0));
# Fix up any code objects holding stubs with the real compiled thing.
my @allcodes := $compiler.backend.compunit_coderefs($compiled);
my $c := nqp::elems(@allcodes);
my $i := 0;
while $i < $c {
my $subid := nqp::getcodecuid(@allcodes[$i]);
if nqp::existskey(%!code_objects_to_fix_up, $subid) {
# First, go over the code objects. Update the $!do, and the
# entry in the SC. Make sure the newly compiled code is marked
# as a static code ref.
my $static := %!code_objects_to_fix_up{$subid}.shift();
nqp::bindattr($static, %!code_object_types{$subid}, '$!do', @allcodes[$i]);
nqp::bindattr($static, %!code_object_types{$subid}, '$!clone_callback', nqp::null());
for %!code_objects_to_fix_up{$subid} {
nqp::bindattr($_, %!code_object_types{$subid}, '$!do', nqp::clone(@allcodes[$i]));
nqp::bindattr($_, %!code_object_types{$subid}, '$!clone_callback', nqp::null());
}
nqp::markcodestatic(@allcodes[$i]);
self.update_root_code_ref(%!code_stub_sc_idx{$subid}, @allcodes[$i]);
# Clear up the fixup statements.
my $fixup_stmts := %!code_object_fixup_list{$subid};
$fixup_stmts.shift() while +@($fixup_stmts);
}
$i := $i + 1;
}
my $mainline := $compiler.backend.compunit_mainline($compiled);
$mainline(|@args, |%named)
};
# Create code object, if we'll need one.
my $code_obj;
if $have_code_type {
$code_obj := nqp::create($code_type);
}
# See if we already have our compile-time dummy. If not, create it.
my $fixups := QAST::Stmts.new();
my $dummy;
my $code_ref_idx;
if nqp::defined($past<compile_time_dummy>) {
$dummy := $past<compile_time_dummy>;
}
else {
# Create a fresh stub code, and set its name.
$dummy := nqp::freshcoderef($stub_code);
nqp::setcodename($dummy, $name);
# Tag it as a static code ref and add it to the root code refs set.
nqp::markcodestatic($dummy);
nqp::markcodestub($dummy);
$code_ref_idx := self.add_root_code_ref($dummy, $past);
%!code_stub_sc_idx{$past.cuid()} := $code_ref_idx;
$past<compile_time_dummy> := $dummy;
# Things with code objects may be methods in roles or multi-dispatch
# routines. We need to handle their cloning and maintain the fixup
# list.
if $have_code_type {
%!code_object_fixup_list{$past.cuid()} := $fixups;
if self.is_precompilation_mode() {
my $cb := sub ($orig, $clone, $code_obj) {
%!code_objects_to_fix_up{$past.cuid()}.push($code_obj);
nqp::bindattr($code_obj, $code_type, '$!clone_callback', nqp::null());
my $do := nqp::getattr($code_obj, $code_type, '$!do');
nqp::markcodestub($do);
}
nqp::bindattr($code_obj, $code_type, '$!clone_callback', $cb);
nqp::push(@!clearup_tasks, sub () {
nqp::bindattr($code_obj, $code_type, '$!clone_callback', nqp::null());
});
}
else {
my $cb := sub ($orig, $clone, $code_obj) {
# Emit fixup code.
self.add_object($code_obj);
$fixups.push(QAST::Op.new(
:op('setcodeobj'),
QAST::Op.new(
:op('bindattr'),
QAST::WVal.new( :value($code_obj) ),
QAST::WVal.new( :value($code_type) ),
QAST::SVal.new( :value('$!do') ),
QAST::Op.new( :op('clone'), QAST::BVal.new( :value($past) ) )
),
QAST::WVal.new( :value($code_obj) )
));
# Add to dynamic compilation fixup list.
%!code_objects_to_fix_up{$past.cuid()}.push($code_obj);
};
nqp::bindattr($code_obj, $code_type, '$!clone_callback', $cb);
nqp::push(@!clearup_tasks, sub () {
nqp::bindattr($code_obj, $code_type, '$!clone_callback', nqp::null());
});
}
}
}
# Add fixups task node; it'll get populated or cleared during the compile.
self.add_fixup_task(:fixup_past($fixups));
# Provided we have the code type, now wrap what we have up in a
# code object.
if $have_code_type {
# Create it now.
nqp::bindattr($code_obj, $code_type, '$!do', $dummy);
nqp::bindattr($code_obj, $code_type, '$!dispatchees', compilee_list())
if $is_dispatcher;
my $slot := self.add_object($code_obj);
# Add deserialization fixup task.
self.add_fixup_task(
:deserialize_past(QAST::Op.new(
:op('setcodeobj'),
QAST::BVal.new( :value($past) ),
QAST::WVal.new( :value($code_obj) )
)));
# Add fixup of the code object and the $!do attribute.
$fixups.push(QAST::Op.new(
:op('bindattr'),
QAST::WVal.new( :value($code_obj) ),
QAST::WVal.new( :value($code_type) ),
QAST::SVal.new( :value('$!do') ),
QAST::BVal.new( :value($past) )
));
$fixups.push(QAST::Op.new(
:op('setcodeobj'),
QAST::BVal.new( :value($past) ),
QAST::WVal.new( :value($code_obj) )
));
# Add it to the dynamic compilation fixup todo list.
%!code_objects_to_fix_up{$past.cuid()} := [$code_obj];
%!code_object_types{$past.cuid()} := $code_type;
$code_obj
}
else {
# For fixup, if we have no code body, we need to assign the method body
# we actually compiled into the one that went into the SC.
$fixups.push(QAST::Op.new(
:op('replacecoderef'),
QAST::Op.new(
:op('scgetcode'),
QAST::SVal.new( :value(self.handle()) ),
QAST::IVal.new( :value($code_ref_idx) )
),
QAST::BVal.new( :value($past) )
));
return $dummy;
}
}
# Creates a meta-object for a package, adds it to the root objects and
# stores an event for the action. Returns the created object.
method pkg_create_mo($how, :$name, :$repr) {
# Create the meta-object and add to root objects.
my %args;
if nqp::defined($name) { %args<name> := $name; }
if nqp::defined($repr) { %args<repr> := $repr; }
my $mo := $how.new_type(|%args);
self.add_object($mo);
# Result is just the object.
return $mo;
}
# Constructs a meta-attribute and adds it to a meta-object. Expects to
# be passed the meta-attribute type object, a set of literal named
# arguments to pass and a set of name to object mappings to pass also
# as named arguments, but where these passed objects also live in a
# serialization context. The type would be passed in this way.
method pkg_add_attribute($obj, $meta_attr, %lit_args, %obj_args) {
my $attr := $meta_attr.new(|%lit_args, |%obj_args);
$obj.HOW.add_attribute($obj, $attr);
$attr
}
# Adds a method to the meta-object.
method pkg_add_method($obj, $meta_method_name, $name, $code) {
$obj.HOW."$meta_method_name"($obj, $name, $code);
}
# Associates a signature with a code object.
method set_routine_signature($code_obj, $types, $definednesses) {
my $sig_type := self.find_sym(['NQPSignature']);
my $code_type := self.find_sym(['NQPRoutine']);
my $sig_obj := nqp::create($sig_type);
nqp::bindattr($sig_obj, $sig_type, '$!types',
compilee_list($types));
nqp::bindattr($sig_obj, $sig_type, '$!definednesses',
compilee_list($definednesses));
nqp::bindattr($code_obj, $code_type, '$!signature', $sig_obj);
}
# This handles associating the role body with a role declaration.
method pkg_set_body_block($obj, $body_past) {
# Get a code object for the body block.
my $body_code_obj := self.create_code($body_past, $body_past.name, 0);
# Set it as the body block.
$obj.HOW.set_body_block($obj, $body_code_obj);
}
# Adds a parent or role to the meta-object.
method pkg_add_parent_or_role($obj, $meta_method_name, $to_add) {
$obj.HOW."$meta_method_name"($obj, $to_add);
}
method pkg_add_parrot_vtable_handler_mapping($obj, $name, $att_name) {
$obj.HOW.add_parrot_vtable_handler_mapping($obj, $name, $att_name);
}
# Composes the package.
method pkg_compose($obj) {
$obj.HOW.compose($obj);
}
# Runs a block at BEGIN time.
method run_begin_block($past) {
# Create a wrapper that makes all outer symbols visible.
my $wrapper := QAST::Block.new(
QAST::Stmts.new(),
$past
);
my %seen;
my $i := +@!BLOCKS;
while $i > 0 {
$i := $i - 1;
my %symbols := @!BLOCKS[$i].symtable();
for %symbols {
if !%seen{$_.key} && nqp::existskey($_.value, 'value') {
try {
$wrapper[0].push(QAST::Op.new(
:op('bind'),
QAST::Var.new( :name($_.key), :scope('lexical'), :isdecl('var') ),
QAST::WVal.new( :value(($_.value)<value>) )
));
};
%seen{$_.key} := 1;
}
}
}
# Compile and run it.
my $code := self.create_code($wrapper, 'BEGIN block', 0);
my $old_global := nqp::getcurhllsym('GLOBAL');
nqp::bindcurhllsym('GLOBAL', $*GLOBALish);
$code();
nqp::bindcurhllsym('GLOBAL', $old_global);
# Need any nested blocks inside the BEGIN block to make it into the
# output code.
$wrapper.shift();
return $wrapper;
}
# Adds libraries that NQP code depends on.
method libs() {
# Need to load the NQP dynops/dympmcs, plus any extras requested.
my @loadlibs := ['nqp_group', 'nqp_ops', 'nqp_bigint_ops', 'trans_ops', 'io_ops'];
if %*COMPILING<%?OPTIONS><vmlibs> {
for nqp::split(',', %*COMPILING<%?OPTIONS><vmlibs>) {
@loadlibs.push($_);
}
}
QAST::VM.new(
loadlibs => @loadlibs,
jvm => QAST::Op.new( :op('null') ) );
}
# Adds some initial tasks.
method add_initializations() {
self.add_load_dependency_task(:deserialize_past(QAST::VM.new(
:parrot(QAST::Stmts.new(
QAST::VM.new( :pirop('nqp_dynop_setup v') ),
QAST::VM.new( :pirop('nqp_bigint_setup v') ),
QAST::Op.new(
:op('callmethod'), :name('hll_map'),
QAST::VM.new( :pirop('getinterp P') ),
QAST::VM.new( :pirop('get_class Ps'), QAST::SVal.new( :value('LexPad') ) ),
QAST::VM.new( :pirop('get_class Ps'), QAST::SVal.new( :value('NQPLexPad') ) )
))),
:jvm(QAST::Op.new( :op('null') ))
)));
}
# Does cleanups.
method cleanup() {
for @!clearup_tasks { $_() }
}
# Makes a list safe to cross the compilation boundary.
sub compilee_list(@orig?) {
my $list := nqp::create(nqp::bootarray());
if nqp::islist(@orig) {
for @orig {
nqp::push($list, $_);
}
}
$list
}
# Checks if the given name is known anywhere in the lexpad
# and with lexical scope.
method is_lexical($name) {
self.is_scope($name, 'lexical')
}
# Checks if the given name is known anywhere in the lexpad
# and with package scope.
method is_package($name) {
self.is_scope($name, 'package')
}
# Checks if a given name is known in the lexpad anywhere
# with the specified scope.
method is_scope($name, $wanted_scope) {
my $i := +@!BLOCKS;
while $i > 0 {
$i := $i - 1;
my %sym := @!BLOCKS[$i].symbol($name);
if +%sym {
return %sym<scope> eq $wanted_scope;
}
}
0;
}
# Checks if the symbol is known.
method known_sym($/, @name) {
my $known := 0;
try {
self.find_sym(@name);
$known := 1;
}
$known
}
# Finds a symbol that has a known value at compile time from the
# perspective of the current scope. Checks for lexicals, then if
# that fails tries package lookup.
method find_sym(@name) {
# Make sure it's not an empty name.
unless +@name { nqp::die("Cannot look up empty name"); }
# If it's a single-part name, look through the lexical
# scopes.
if +@name == 1 {
my $final_name := @name[0];
my $i := +@!BLOCKS;
while $i > 0 {
$i := $i - 1;
my %sym := @!BLOCKS[$i].symbol($final_name);
if +%sym {
if nqp::existskey(%sym, 'value') {
return %sym<value>;
}
else {
nqp::die("No compile-time value for $final_name");
}
}
}
}
# If it's a multi-part name, see if the containing package
# is a lexical somewhere. Otherwise we fall back to looking
# in GLOBALish.
my $result := $*GLOBALish;
if +@name >= 2 {
my $first := @name[0];
my $i := +@!BLOCKS;
while $i > 0 {
$i := $i - 1;
my %sym := @!BLOCKS[$i].symbol($first);
if +%sym {
if nqp::existskey(%sym, 'value') {
$result := %sym<value>;
@name.shift();
$i := 0;
}
else {
nqp::die("No compile-time value for $first");
}
}
}
}
# Try to chase down the parts of the name.
for @name {
if nqp::existskey($result.WHO, ~$_) {
$result := ($result.WHO){$_};
}
else {
nqp::die("Could not locate compile-time value for symbol " ~
join('::', @name));
}
}
$result;
}
}
# From src/NQP/Grammar.nqp
grammar NQP::Grammar is HLL::Grammar {
method TOP() {
# Language braids.
my %*LANG;
%*LANG<Regex> := NQP::Regex;
%*LANG<Regex-actions> := NQP::RegexActions;
%*LANG<MAIN> := NQP::Grammar;
%*LANG<MAIN-actions> := NQP::Actions;
# Package declarator to meta-package mapping. Note that there is
# one universal KnowHOW from the 6model core, and an attribute
# meta-object to go with it.
my %*HOW;
%*HOW<knowhow> := nqp::knowhow();
%*HOW<knowhow-attr> := nqp::knowhowattr();
# Serialization context builder - keeps track of objects that
# cross the compile-time/run-time boundary that are associated
# with this compilation unit.
my $file := nqp::getlexdyn('$?FILES');
my $source_id := nqp::sha1(self.target()) ~
(%*COMPILING<%?OPTIONS><stable-sc> ?? '' !! '-' ~ ~nqp::time_n());
my $*W := nqp::isnull($file) ??
NQP::World.new(:handle($source_id)) !!
NQP::World.new(:handle($source_id), :description($file));
my $*SCOPE := '';
my $*MULTINESS := '';
my $*PKGDECL := '';
my $*INVOCANT_OK := 0;
my $*RETURN_USED := 0;
my $*CONTROL_USED := 0;
my %*HANDLERS;
self.comp_unit;
}
## Lexer stuff
token identifier { <.ident> [ <[\-']> <.ident> ]* }
token name { <identifier> ['::'<identifier>]* }
token deflongname {
<identifier> <colonpair>**0..1
}
token ENDSTMT {
[
| $
| \h* $$ <.ws> <?MARKER('endstmt')>
| <.unv>? $$ <.ws> <?MARKER('endstmt')>
]?
}
token ws {
|| <?MARKED('ws')>
|| <!ww>
[ \v+
| '#' \N*
| ^^ <.pod_comment>
| \h+
]*
<?MARKER('ws')>
}
token unv {
# :dba('horizontal whitespace')
[
| ^^ <?before \h* '=' [ \w | '\\'] > <.pod_comment>
| \h* '#' \N*
| \h+
]
}
token pod_comment {
^^ \h* '='
[
| 'begin' \h+ 'END' >>
[ .*? \n \h* '=' 'end' \h+ 'END' » \N* || .* ]
| 'begin' \h+ <identifier>
[
|| .*? \n \h* '=' 'end' \h+ $<identifier> » \N*
|| <.panic: '=begin without matching =end'>
]
| 'begin' » \h*
[ $$ || '#' || <.panic: 'Unrecognized token after =begin'> ]
[
|| .*? \n \h* '=' 'end' » \N*
|| <.panic: '=begin without matching =end'>
]
| <identifier>
.*? ^^ <?before \h* [
'='
[ 'cut' »
<.panic: 'Obsolete pod format, please use =begin/=end instead'> ]?
| \n ]>
|
[ \s || <.panic: 'Illegal pod directive'> ]
\N*
]
}
## Top-level rules
token comp_unit {
:my $*IN_DECL := '';
:my $*HAS_YOU_ARE_HERE := 0;
:my $*MAIN_SUB;
:my $*UNIT := $*W.push_lexpad($/);
# Create GLOBALish - the current GLOBAL view, created fresh
# for each compilation unit so we get separate compilation.
:my $*GLOBALish := $*W.pkg_create_mo(%*HOW<knowhow>, :name('GLOBALish'));
{
$*GLOBALish.HOW.compose($*GLOBALish);
$*W.install_lexical_symbol($*UNIT, 'GLOBALish', $*GLOBALish);
}
# This is also the starting package.
:my $*PACKAGE := $*GLOBALish;
{ $*W.install_lexical_symbol($*UNIT, '$?PACKAGE', $*PACKAGE); }
# Create EXPORT::DEFAULT.
:my $*EXPORT;
{
unless %*COMPILING<%?OPTIONS><setting> eq 'NULL' {
$*EXPORT := $*W.pkg_create_mo(%*HOW<knowhow>, :name('EXPORT'));
$*EXPORT.HOW.compose($*EXPORT);
$*W.install_lexical_symbol($*UNIT, 'EXPORT', $*EXPORT);
my $DEFAULT := $*W.pkg_create_mo(%*HOW<knowhow>, :name('DEFAULT'));
$DEFAULT.HOW.compose($DEFAULT);
($*EXPORT.WHO)<DEFAULT> := $DEFAULT;
}
}
{ $*W.add_initializations(); }
<.outerctx>
<statementlist>
[ $ || <.panic: 'Confused'> ]
}
rule statementlist {
| $
| [<statement><.eat_terminator> ]*
}
token statement {
<!before <[\])}]> | $ >
[
| <statement_control>
| <EXPR> <.ws>
[
|| <?MARKED('endstmt')>
|| <statement_mod_cond> <statement_mod_loop>**0..1
|| <statement_mod_loop>
]**0..1
]
}
token eat_terminator {
|| ';'
|| <?MARKED('endstmt')>
|| <?terminator>
|| $
}
token xblock {
<EXPR> <.ws> <pblock>
}
token pblock {
| <.lambda>
<.newpad>
<signature> <.ws>
<blockoid>
| <?[{]>
<.newpad>
<blockoid>
| <.panic: 'Missing block'>
}
token lambda { '->' | '<->' }
token block {
[ <?[{]> || <.panic: 'Missing block'> ]
<.newpad>
<blockoid>
}
token blockoid {
:my %*HANDLERS;
<.finishpad>
[
|| '{YOU_ARE_HERE}' <you_are_here>
|| '{' ~ '}' <statementlist>
]
<?ENDSTMT>
}
token newpad { <?> }
token outerctx { <?> }
token finishpad { <?> }
token you_are_here { <?> }
proto token terminator { <...> }
token terminator:sym<;> { <?[;]> }
token terminator:sym<}> { <?[}]> }
## Statement control
proto token statement_control { <...> }
token statement_control:sym<use> {
<sym> \s :s <name>
}
token statement_control:sym<if> {
<sym> \s :s
<xblock>
[ 'elsif'\s <xblock> ]*
[ 'else'\s <else=.pblock> ]**0..1
}
token statement_control:sym<unless> {
<sym> \s :s
<xblock>
[ <!before 'else'> || <.panic: 'unless does not take "else", please rewrite using "if"'> ]
}
token statement_control:sym<while> {
:my $*CONTROL_USED := 0;
$<sym>=[while|until] \s :s
<xblock>
}
token statement_control:sym<repeat> {
:my $*CONTROL_USED := 0;
<sym> \s :s
[
| $<wu>=[while|until]\s <xblock>
| <pblock> $<wu>=[while|until]\s <EXPR>
]
}
token statement_control:sym<for> {
:my $*CONTROL_USED := 0;
<sym> \s :s
<xblock>
}
token statement_control:sym<CATCH> {
<sym> \s :s
<block>
}
token statement_control:sym<CONTROL> {
<sym> \s :s
<block>
}
proto token statement_prefix { <...> }
token statement_prefix:sym<BEGIN> { <sym> <blorst> }
token statement_prefix:sym<INIT> { <sym> <blorst> }
token statement_prefix:sym<try> {
<sym>
<blorst>
}
token blorst {
\s <.ws> [ <?[{]> <block> | <statement> ]
}
## Statement modifiers
proto token statement_mod_cond { <...> }
token statement_mod_cond:sym<if> { <sym> :s <cond=.EXPR> }
token statement_mod_cond:sym<unless> { <sym> :s <cond=.EXPR> }
proto token statement_mod_loop { <...> }
token statement_mod_loop:sym<while> { <sym> :s <cond=.EXPR> }
token statement_mod_loop:sym<until> { <sym> :s <cond=.EXPR> }
token statement_mod_loop:sym<for> { <sym> :s <cond=.EXPR> }
## Terms
token term:sym<fatarrow> { <fatarrow> }
token term:sym<colonpair> { <colonpair> }
token term:sym<variable> { <variable> }
token term:sym<package_declarator> { <package_declarator> }
token term:sym<scope_declarator> { <scope_declarator> }
token term:sym<routine_declarator> { <routine_declarator> }
token term:sym<multi_declarator> {
<?before 'multi'|'proto'|'only'>
<!before 'proto' <.ws> ['regex'|'token'|'rule']>
<multi_declarator>
}
token term:sym<regex_declarator> { <regex_declarator> }
token term:sym<statement_prefix> { <statement_prefix> }
token term:sym<lambda> { <?lambda> <pblock> }
token fatarrow {
<key=.identifier> \h* '=>' <.ws> <val=.EXPR('i=')>
}
token colonpair {
':'
[
| $<not>='!' <identifier>
| <identifier> <circumfix>**0..1
| <circumfix>
| <variable>
]
}
token variable {
| <sigil> <twigil>**0..1 <desigilname=.name>
| <sigil> <?[<]> <postcircumfix>
| <sigil> '(' ~ ')' <semilist>
| $<sigil>=['$'] $<desigilname>=[<[/_!]>]
}
token sigil { <[$@%&]> }
token twigil { <[*!?]> }
proto token package_declarator { <...> }
token package_declarator:sym<module> {
:my $*OUTERPACKAGE := $*PACKAGE;
:my $*PKGDECL := 'module';
<sym> <package_def>
}
token package_declarator:sym<knowhow> {
:my $*OUTERPACKAGE := $*PACKAGE;
:my $*PKGDECL := 'knowhow';
<sym> <package_def>
}
token package_declarator:sym<class> {
:my $*OUTERPACKAGE := $*PACKAGE;
:my $*PKGDECL := 'class';
<sym> <package_def>
}
token package_declarator:sym<grammar> {
:my $*OUTERPACKAGE := $*PACKAGE;
:my $*PKGDECL := 'grammar';
<sym> <package_def>
}
token package_declarator:sym<role> {
:my $*OUTERPACKAGE := $*PACKAGE;
:my $*PKGDECL := 'role';
<sym> <package_def>
}
token package_declarator:sym<native> {
:my $*OUTERPACKAGE := $*PACKAGE;
:my $*PKGDECL := 'native';
<sym> <package_def>
}
token package_declarator:sym<stub> {
:my $*OUTERPACKAGE := $*PACKAGE;
:my $*PKGDECL := 'stub';
<sym> :s <name>
'metaclass' <metaclass=.name>
'{' '...' '}'
}