-
Notifications
You must be signed in to change notification settings - Fork 15
/
Actions.pm
1314 lines (1104 loc) · 36.1 KB
/
Actions.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
package Niecza::Actions;
use 5.010;
use strict;
use warnings;
use Op;
use RxOp;
use Body;
use Unit;
use Sig;
use Try::Tiny;
our $AUTOLOAD;
my %carped;
sub AUTOLOAD {
my ($cl, $M) = @_;
if ($AUTOLOAD =~ /^Niecza::Actions::(.*)__S_\d\d\d(.*)$/) {
my ($cat, $spec) = ($1, $2);
my $m = "${cat}__S_$spec";
my $a = "${cat}__S_ANY";
if ($cl->can($m) || !$cl->can($a)) {
return $cl->$m($M);
} else {
return $cl->$a($M, $spec);
}
}
$M->sorry("Action method $AUTOLOAD not yet implemented") unless $carped{$AUTOLOAD}++;
}
sub ws { }
sub vws { }
sub unv { }
sub begid { }
sub comment { }
sub comment__S_Sharp { }
sub spacey { }
sub unspacey { }
sub nofun { }
sub curlycheck { }
sub pod_comment { }
sub infixstopper { }
sub decint { my ($cl, $M) = @_;
$M->{_ast} = eval $M->Str; # XXX use a real string parser
}
sub integer { my ($cl, $M) = @_;
$M->{_ast} =
($M->{decint} // $M->{octint} // $M->{hexint} // $M->{binint})->{_ast};
}
sub number { my ($cl, $M) = @_;
my $child = $M->{integer} // $M->{dec_number} // $M->{rad_number};
$M->{_ast} = $child ? $child->{_ast} :
($M->Str eq 'NaN') ? (1e99999/1e99999) : (1e99999);
}
# Value :: Op
sub value { }
sub value__S_number { my ($cl, $M) = @_;
# TODO: Implement the rest of the numeric hierarchy once MMD exists
$M->{_ast} = Op::Num->new(value => $M->{number}{_ast});
}
sub value__S_quote { my ($cl, $M) = @_;
$M->{_ast} = $M->{quote}{_ast};
}
sub ident { my ($cl, $M) = @_;
$M->{_ast} = $M->Str;
}
sub identifier { my ($cl, $M) = @_;
$M->{_ast} = $M->Str;
}
# Either String Op
sub morename { my ($cl, $M) = @_;
$M->{_ast} = $M->{identifier}[0] ? $M->{identifier}[0]{_ast} :
$M->{EXPR}[0]{_ast};
}
# { dc: Bool, names: [Either String Op] }
sub name { my ($cl, $M) = @_;
my @names = map { $_->{_ast} } @{ $M->{morename} };
unshift @names, $M->{identifier}{_ast} if $M->{identifier};
$M->{_ast} = { dc => !($M->{identifier}), names => \@names };
}
sub longname {} # look at the children yourself
sub deflongname {}
sub mangle_longname { my ($cl, $M, $single) = @_;
if ($M->{name}{_ast}{dc}) {
$M->sorry('Leading double colons not yet supported');
return "";
}
if ($single && @{ $M->{name}{_ast}{names} } > 1) {
$M->sorry("Multipart names not yet supported for $single");
return "";
}
my @ns = @{ $M->{name}{_ast}{names} };
my $n = pop @ns;
for my $cp (@{ $M->{colonpair} }) {
my $k = $cp->{k};
if (ref $cp->{v}) {
$n .= ":" . $cp->{k};
$n .= $cp->{v}{qpvalue} // do {
$M->sorry("Invalid colonpair used as name extension");
"";
}
} else {
# STD seems to think term:name is term:sym<name>. Needs speccy
# clarification.
$M->sorry("Boolean colonpairs as name extensions NYI");
}
}
$single ? $n : ($n, @ns);
}
sub subshortname { my ($cl, $M) = @_;
if (@{ $M->{colonpair} }) {
$M->sorry("Colonpair subshortnames NYI");
return;
}
$M->{_ast} = $M->{desigilname}{_ast};
}
sub sublongname { my ($cl, $M) = @_;
if (@{ $M->{sigterm} }) {
$M->sorry("Sigterm sublongnames NYI");
return;
}
$M->{_ast} = $M->{subshortname}{_ast};
}
sub desigilname { my ($cl, $M) = @_;
if ($M->{variable}) {
$M->sorry("Truncated contextualizer syntax NYI");
return;
}
$M->{_ast} = [ $cl->mangle_longname($M->{longname}) ];
}
sub stopper { }
# quote :: Op
sub quote {}
sub quote__S_Double_Double { my ($cl, $M) = @_;
$M->{_ast} = $M->{nibble}{_ast};
}
sub quote__S_Single_Single { my ($cl, $M) = @_;
$M->{_ast} = $M->{nibble}{_ast};
}
sub quote__S_qq { my ($cl, $M) = @_;
$M->{_ast} = $M->{quibble}{_ast};
}
sub quote__S_q { my ($cl, $M) = @_;
$M->{_ast} = $M->{quibble}{_ast};
}
sub quote__S_Q { my ($cl, $M) = @_;
$M->{_ast} = $M->{quibble}{nibble}{_ast};
}
sub quote__S_Slash_Slash { my ($cl, $M) = @_;
$M->{_ast} = $M->{nibble}{_ast};
}
# :: RxOp
sub atom { my ($cl, $M) = @_;
if ($M->{metachar}) {
$M->{_ast} = $M->{metachar}{_ast};
} else {
$M->{_ast} = RxOp::String->new(text => $M->Str);
}
}
sub quantified_atom { my ($cl, $M) = @_; # :: RxOp
my $atom = $M->{atom}{_ast};
my $ns = $M->{normspace}[0];
my $q = $M->{quantifier}[0] ? $M->{quantifier}[0]{_ast} : undef;
if (!$q) {
$M->{_ast} = $atom;
} elsif ($q->{simple}) {
$M->{_ast} = RxOp::Quantifier->new(type => $q->{simple},
zyg => [$atom]);
} else {
$M->sorry("Unhandled quantifier " . $M->{quantifier}[0]->Str);
}
}
# :: Context hash interpreted by quantified_atom
sub quantifier {}
sub quantifier__S_Star { my ($cl, $M) = @_;
$M->{_ast} = { simple => '*' };
}
sub quantifier__S_Plus { my ($cl, $M) = @_;
$M->{_ast} = { simple => '+' };
}
sub quantifier__S_Question { my ($cl, $M) = @_;
$M->{_ast} = { simple => '?' };
}
sub quantmod { my ($cl, $M) = @_;
if ($M->Str ne '') {
$M->sorry('Quantmods NYI');
return;
}
}
sub quant_atom_list { my ($cl, $M) = @_;
$M->{_ast} = RxOp::Sequence->new(zyg =>
[ map { $_->{_ast} } @{ $M->{quantified_atom} } ]);
}
sub nibbler { my ($cl, $M) = @_;
if ($M->isa('STD::Regex')) {
my $slot = $cl->gensym;
# TODO should be a real pass.
$M->{_ast} = Op::CallMethod->new(name => 'bless',
receiver => Op::Lexical->new(name => 'Regex'),
positionals => [
RxOp::Export->new(zyg => [$M->{EXPR}{_ast}])->closure ]);
} elsif ($M->isa('Niecza::Grammar::CgOp')) {
# XXX We don't interpret the code, so we can't tell if it's actually
# using variables, but still, it probably is.
for my $k (keys %$::CURLEX) {
$::CURLEX->{$k}{used} = 1 if $k =~ /^[\@\%\&\$]\w/;
}
$M->{_ast} = Op::CgOp->new(op => $M->{cgexp}{_ast});
} else {
# garden variety nibbler
my $str = "";
for my $n (@{ $M->{nibbles} }) {
if ($n->isa('Str')) {
$str .= $n->{TEXT};
} else {
$M->sorry("Non-literal contents of strings NYI");
}
}
$M->{_ast} = Op::StringLiteral->new(text => $str);
}
}
sub circumfix { }
sub circumfix__S_Lt_Gt { my ($cl, $M) = @_;
my $sl = $M->{nibble}{_ast};
if (!$sl->isa('Op::StringLiteral') || ($sl->text =~ /\S\s\S/)) {
$M->sorry("Word splitting NYI");
return;
}
my ($t) = $sl->text =~ /^\s*(.*?)\s*$/;
$M->{_ast} = Op::StringLiteral->new(text => $t);
$M->{qpvalue} = '<' . $t . '>';
}
sub circumfix__S_LtLt_GtGt { goto &circumfix__S_Lt_Gt }
sub circumfix__S_Paren_Thesis { my ($cl, $M) = @_;
$M->{_ast} = Op::StatementList->new(children =>
[ map { $_ ? ($_->paren) : () } @{ $M->{semilist}{_ast} } ]);
}
sub circumfix__S_Cur_Ly { my ($cl, $M) = @_;
$M->{_ast} = $cl->block_to_immediate('bare', $M->{pblock}{_ast});
}
sub infixish { my ($cl, $M) = @_;
$M->sorry("Metaoperators NYI") if $M->{infix_postfix_meta_operator}[0];
$M->sorry("Adverbs NYI") if $M->{colonpair};
}
sub INFIX { my ($cl, $M) = @_;
my ($l,$s,$r) = ($M->{left}{_ast}, $M->{infix}{sym}, $M->{right}{_ast});
if ($s eq ':=') { #XXX macro
$M->{_ast} = Op::Bind->new(lhs => $l, rhs => $r, readonly => 0);
return;
}
if ($s eq ',') {
#XXX STD bug causes , in setting to be parsed as left assoc
my @r;
push @r, ($l->isa('Op::CallSub') && $l->splittable_parcel) ?
@{ $l->positionals } : ($l);
push @r, ($r->isa('Op::CallSub') && $r->splittable_parcel) ?
@{ $r->positionals } : ($r);
$M->{_ast} = Op::CallSub->new(
invocant => Op::Lexical->new(name => '&infix:<,>'),
positionals => \@r, splittable_parcel => 1);
return;
}
$M->{_ast} = Op::CallSub->new(
invocant => Op::Lexical->new(name => "&infix:<$s>"),
positionals => [ $l, $r ]);
if ($s eq '=' && $l->isa('Op::Lexical') && $l->state_decl) {
# Assignments (and assign metaops, but we don't do that yet) to has
# and state declarators are rewritten into an appropriate phaser
my $cv = $cl->gensym;
$M->{_ast} = Op::StatementList->new(children => [
Op::Start->new(condvar => $cv, body => $M->{_ast}),
Op::Lexical->new(name => $l->name)]);
}
}
sub CHAIN { my ($cl, $M) = @_;
if (@{ $M->{chain} } != 3) {
$M->sorry('Chaining not yet implemented');
return;
}
$M->{_ast} = Op::CallSub->new(
invocant => Op::Lexical->new(name => '&infix:<' . $M->{chain}[1]{sym} . '>'),
positionals => [ $M->{chain}[0]{_ast}, $M->{chain}[2]{_ast} ]);
}
my %loose2tight = (
'&&' => '&&', '||' => '||', '//' => '//', 'andthen' => 'andthen',
'orelse' => '//', 'and' => '&&', 'or' => '||',
);
sub LIST { my ($cl, $M) = @_;
# STD guarantees that all elements of delims have the same sym
# the last item may have an ast of undef due to nulltermish
my $op = $M->{delims}[0]{sym};
my @pos = grep { defined } map { $_->{_ast} } @{ $M->{list} };
if ($loose2tight{$op}) {
$M->{_ast} = Op::ShortCircuit->new(kind => $loose2tight{$op},
args => \@pos);
} else {
$M->{_ast} = Op::CallSub->new(
invocant => Op::Lexical->new(name => "&infix:<$op>"),
positionals => \@pos,
splittable_parcel => ($op eq ','));
}
}
sub POSTFIX { my ($cl, $M) = @_;
my $op = $M->{_ast};
if ($op->{postfix}) {
$M->{_ast} = Op::CallSub->new(
invocant => Op::Lexical->new(name => "&postfix:<" . $op->{postfix} . ">"),
positionals => [ $M->{arg}{_ast} ]);
} elsif ($op->{name} && $op->{name} =~ /^(?:HOW|WHAT)$/) {
if ($op->{args}) {
$M->sorry("Interrogative operator " . $op->{name} .
" does not take arguments");
return;
}
$M->{_ast} = Op::Interrogative->new(
receiver => $M->{arg}{_ast},
name => $op->{name});
} elsif ($op->{metamethod}) {
$M->{_ast} = Op::CallMetaMethod->new(
receiver => $M->{arg}{_ast},
name => $op->{metamethod},
positionals => $op->{args} // []);
} elsif ($op->{name}) {
$M->{_ast} = Op::CallMethod->new(
receiver => $M->{arg}{_ast},
name => $op->{name},
positionals => $op->{args} // []);
} elsif ($op->{postcall}) {
if (@{ $op->{postcall} } > 1) {
$M->sorry("Slicels NYI");
return;
}
$M->{_ast} = Op::CallSub->new(
invocant => $M->{arg}{_ast},
positionals => ($op->{postcall}[0] // []));
} else {
$M->sorry("Unhandled postop type");
}
}
sub PREFIX { my ($cl, $M) = @_;
$M->{_ast} = Op::CallSub->new(
invocant => Op::Lexical->new(name => '&prefix:<' . $M->{sym} . '>'),
positionals => [ $M->{arg}{_ast} ]);
}
# infix et al just parse the operator itself
sub infix { }
sub infix__S_ANY { }
sub prefix { }
sub prefix__S_ANY { }
sub postfix { }
sub postfix__S_ANY { }
sub postcircumfix { }
sub postcircumfix__S_Paren_Thesis { my ($cl, $M) = @_;
$M->{_ast} = { postcall => $M->{semiarglist}{_ast} };
}
sub postop { my ($cl, $M) = @_;
$M->{_ast} = $M->{postcircumfix} ? $M->{postcircumfix}{_ast} :
{ postfix => $M->{sym} };
}
sub POST { my ($cl, $M) = @_;
$M->{_ast} = $M->{dotty}{_ast} if $M->{dotty};
$M->{_ast} = $M->{privop}{_ast} if $M->{privop};
$M->{_ast} = $M->{postop}{_ast} if $M->{postop};
}
sub PRE { }
sub methodop { my ($cl, $M) = @_;
my %r;
$r{name} = $cl->mangle_longname($M->{longname}, "method call") if $M->{longname};
$r{quote} = $M->{quote}{_ast} if $M->{quote};
$r{ref} = $M->{variable}{_ast}{term} if $M->{variable};
$r{args} = $M->{args}[0]{_ast}[0] if $M->{args}[0];
$r{args} = $M->{arglist}[0]{_ast} if $M->{arglist}[0];
$M->{_ast} = \%r;
}
sub dottyop { my ($cl, $M) = @_;
if ($M->{colonpair}) {
$M->sorry("Colonpair dotties NYI");
return;
}
$M->{_ast} = $M->{methodop}{_ast} if $M->{methodop};
$M->{_ast} = { postfix => $M->{postop}{_ast} } if $M->{postop};
}
sub privop { my ($cl, $M) = @_;
$M->{_ast} = { %{ $M->{methodop}{_ast} }, private => 1 };
}
sub dotty { }
sub dotty__S_Dot { my ($cl, $M) = @_;
$M->{_ast} = $M->{dottyop}{_ast};
}
sub dotty__S_DotStar { my ($cl, $M) = @_;
if ($M->{sym} eq '.^' && $M->{dottyop}{_ast}{name}) {
$M->{_ast} = { metamethod => $M->{dottyop}{_ast}{name},
args => $M->{dottyop}{_ast}{args} };
} else {
$M->sorry('NYI dottyop form ' . $M->{sym});
}
}
sub coloncircumfix { my ($cl, $M) = @_;
$M->{_ast} = $M->{circumfix}{_ast};
$M->{qpvalue} = $M->{circumfix}{qpvalue};
}
sub colonpair { }
# term :: Op
sub term { }
sub term__S_value { my ($cl, $M) = @_;
$M->{_ast} = $M->{value}{_ast};
}
sub term__S_identifier { my ($cl, $M) = @_;
my $id = $M->{identifier}{_ast};
my $sal = $M->{args}{_ast} // []; # TODO: support zero-D slicels
if (@$sal > 1) {
$M->sorry("Slicel lists are NYI");
return;
}
if ($M->is_name($M->{identifier}->Str)) {
$M->{_ast} = Op::Lexical->new(name => $id);
return;
}
my $args = $sal->[0] // [];
$M->{_ast} = Op::CallSub->new(
invocant => Op::Lexical->new(name => '&' . $id),
positionals => $args);
}
sub term__S_self { my ($cl, $M) = @_;
$M->{_ast} = Op::Lexical->new(name => 'self');
}
sub term__S_circumfix { my ($cl, $M) = @_;
$M->{_ast} = $M->{circumfix}{_ast};
}
sub term__S_scope_declarator { my ($cl, $M) = @_;
$M->{_ast} = $M->{scope_declarator}{_ast};
}
sub term__S_multi_declarator { my ($cl, $M) = @_;
$M->{_ast} = $M->{multi_declarator}{_ast};
}
sub term__S_package_declarator { my ($cl, $M) = @_;
$M->{_ast} = $M->{package_declarator}{_ast};
}
sub term__S_routine_declarator { my ($cl, $M) = @_;
$M->{_ast} = $M->{routine_declarator}{_ast};
}
sub term__S_regex_declarator { my ($cl, $M) = @_;
$M->{_ast} = $M->{regex_declarator}{_ast};
}
sub term__S_type_declarator { my ($cl, $M) = @_;
$M->{_ast} = $M->{type_declarator}{_ast};
}
sub term__S_dotty { my ($cl, $M) = @_;
$M->{_ast} = $M->{dotty}{_ast};
}
sub term__S_capterm { my ($cl, $M) = @_;
$M->{_ast} = $M->{capterm}{_ast};
}
sub term__S_sigterm { my ($cl, $M) = @_;
$M->{_ast} = $M->{sigterm}{_ast};
}
sub term__S_statement_prefix { my ($cl, $M) = @_;
$M->{_ast} = $M->{statement_prefix}{_ast};
}
sub term__S_variable { my ($cl, $M) = @_;
$M->{_ast} = $M->{variable}{_ast}{term};
}
sub term__S_DotDotDot { my ($cl, $M) = @_;
$M->{_ast} = Op::Yada->new(kind => '...');
}
sub term__S_BangBangBang { my ($cl, $M) = @_;
$M->{_ast} = Op::Yada->new(kind => '!!!');
}
sub term__S_QuestionQuestionQuestion { my ($cl, $M) = @_;
$M->{_ast} = Op::Yada->new(kind => '???');
}
sub variable { my ($cl, $M) = @_;
my $sigil = $M->{sigil} ? $M->{sigil}->Str : substr($M->Str, 0, 1);
my $twigil = $M->{twigil}[0] ? $M->{twigil}[0]{sym} : '';
my ($name, @rest);
if ($M->{desigilname}) {
($name, @rest) = @{ $M->{desigilname}{_ast} };
} elsif ($M->{sublongname}) {
($name, @rest) = @{ $M->{sublongname}{_ast} };
} else {
$M->sorry("Non-simple variables NYI");
return;
}
my $sl = $sigil . $twigil . $name;
if ($twigil && @rest) {
$M->sorry("Cannot use a twigil on a qualified name");
return;
}
given ($twigil) {
when ('!') {
$M->{_ast} = {
term => Op::GetSlot->new(name => $name,
object => Op::Lexical->new(name => 'self')),
};
}
when ('') {
if (@rest) {
$M->{_ast} = {
term => Op::PackageVar->new(
path => \@rest, name => $sl, slot => $cl->gensym),
};
} else {
$M->{_ast} = {
term => Op::Lexical->new(name => $sl),
decl_slot => $sl,
};
}
}
default {
$M->sorry("Unhandled twigil $twigil");
}
}
}
sub param_sep {}
# :: Sig::Target
sub param_var { my ($cl, $M) = @_;
if ($M->{signature}) {
$M->sorry('Sub-signatures NYI');
return;
}
my $twigil = $M->{twigil}[0] ? $M->{twigil}[0]->Str : '';
my $sigil = $M->{sigil}->Str;
if ($twigil || ($sigil ne '$' && $sigil ne '@')) {
$M->sorry('Non bare scalar targets NYI');
return;
}
$M->{_ast} = Sig::Target->new(list => ($sigil eq '@'), slot =>
$M->{name}[0] ? ($sigil . $M->{name}[0]->Str) : undef);
}
# :: Sig::Parameter
sub parameter { my ($cl, $M) = @_;
if (@{ $M->{type_constraint} } > 0) {
$M->sorry('Parameter type constraints NYI');
return;
}
if (@{ $M->{trait} } > 0) {
$M->sorry('Parameter traits NYI');
return;
}
if (@{ $M->{post_constraint} } > 0) {
$M->sorry('Parameter post constraints NYI');
return;
}
if ($M->{default_value}[0]) {
$M->sorry('Default values NYI');
return;
}
if ($M->{named_param}) {
$M->sorry('Named parameters NYI');
return;
}
if ($M->{quant} ne '*' && ($M->{quant} ne '' || $M->{kind} ne '!')) {
$M->sorry('Exotic parameters NYI');
return;
}
$M->{_ast} = Sig::Parameter->new(target => $M->{param_var}{_ast},
slurpy => ($M->{quant} eq '*'));
}
# signatures exist in several syntactic contexts so just make an object for now
sub signature { my ($cl, $M) = @_;
if ($M->{type_constraint}[0]) {
$M->sorry("Return type constraints NYI");
return;
}
if ($M->{param_var}) {
$M->sorry('\| signatures NYI');
return;
}
for (@{ $M->{param_sep} }) {
if ($_->Str !~ /,/) {
$M->sorry('Parameter separator ' . $_->Str . ' NYI');
return;
}
}
$M->{_ast} = Sig->new(params =>
[map { $_->{_ast} } @{ $M->{parameter} }]);
}
sub multisig { my ($cl, $M) = @_;
if (@{ $M->{signature} } != 1) {
$M->sorry("Multiple signatures NYI");
return;
}
$M->{_ast} = $M->{signature}[0]{_ast};
}
sub cgopname { my ($cl, $M) = @_;
$M->{_ast} = $M->Str;
}
sub cgexp { }
sub cgexp__S_name { my ($cl, $M) = @_;
$M->{_ast} = $M->{cgopname}{_ast};
}
sub cgexp__S_decint { my ($cl, $M) = @_;
$M->{_ast} = $M->{decint}{_ast};
}
sub cgexp__S_quote { my ($cl, $M) = @_;
if (!$M->{quote}{_ast}->isa('Op::StringLiteral')) {
$M->sorry("Strings used in CgOp code must be compile time constants");
}
$M->{_ast} = $M->{quote}{_ast}->text;
}
my %opshortcut = (
'@', [ 'fetch' ],
'l', [ 'scopedlex' ],
'w', [ 'wrap' ],
'ns', [ 'newscalar' ],
'nsw', [ 'newrwscalar' ],
'==', [ 'compare', '==' ], '!=', [ 'compare', '!=' ],
'>=', [ 'compare', '>=' ], '<=', [ 'compare', '<=' ],
'<', [ 'compare', '<' ], '>', [ 'compare', '>' ],
'+', [ 'arith', '+' ], '-', [ 'arith', '-' ],
'*', [ 'arith', '*' ], '/', [ 'arith', '/' ],
);
sub cgexp__S_op { my ($cl, $M) = @_;
no strict 'refs';
my $l = $M->{cgopname}{_ast};
my ($op, @p) = @{ $opshortcut{$l} // [ $l ] };
$M->{_ast} = &{"CgOp::$op"}(@p, map { $_->{_ast} } @{ $M->{cgexp} });
}
sub apostrophe {}
sub quibble {}
sub tribble {}
sub babble {}
sub quotepair {}
# We can't do much at blockoid reduce time because the context is unknown.
# Roles and subs need somewhat different code gen
sub blockoid { my ($cl, $M) = @_;
# XXX horrible cheat, but my data structures aren't up to the task of
# $::UNIT being a class body &c.
if ($M->Str eq '{YOU_ARE_HERE}') {
$M->{_ast} = Op::YouAreHere->new(unitname => $::UNITNAME);
} else {
$M->{_ast} = $M->{statementlist}{_ast};
}
}
sub sigil {}
sub sigil__S_Amp {}
sub sigil__S_Dollar {}
sub sigil__S_At {}
sub sigil__S_Percent {}
sub twigil {}
sub twigil__S_Equal {}
sub twigil__S_Bang {}
sub twigil__S_Dot {}
sub twigil__S_Tilde {}
sub twigil__S_Star {}
sub twigil__S_Question {}
sub twigil__S_Caret {}
sub twigil__S_Colon {}
sub terminator {}
sub terminator__S_Thesis {}
sub terminator__S_Semi {}
sub terminator__S_Ket {}
sub terminator__S_Ly {}
sub terminator__S_then {}
sub terminator__S_again {}
sub terminator__S_repeat {}
sub terminator__S_while {}
sub terminator__S_else {}
sub stdstopper {}
sub unitstopper {}
sub eat_terminator {}
sub scoped { my ($cl, $M) = @_;
$M->{_ast} = ($M->{declarator} // $M->{regex_declarator} //
$M->{package_declarator} // $M->{multi_declarator})->{_ast};
}
# :: Op (but adds decls)
sub declarator { my ($cl, $M) = @_;
if ($M->{signature}) {
$M->sorry("Signature declarations NYI");
return;
}
$M->{_ast} = $M->{variable_declarator} ? $M->{variable_declarator}{_ast} :
$M->{routine_declarator} ? $M->{routine_declarator}{_ast} :
$M->{regex_declarator} ? $M->{regex_declarator}{_ast} :
$M->{type_declarator}{_ast};
}
sub scope_declarator { my ($cl, $M) = @_;
$M->{_ast} = $M->{scoped}{_ast};
}
sub scope_declarator__S_my {}
sub scope_declarator__S_our {}
sub scope_declarator__S_augment {}
sub scope_declarator__S_supercede {}
sub scope_declarator__S_has {}
sub scope_declarator__S_state {}
sub scope_declarator__S_anon {}
sub variable_declarator { my ($cl, $M) = @_;
if ($M->{trait}[0] || $M->{post_constraint}[0] || $M->{shape}[0]) {
$M->sorry("Traits, postconstraints, and shapes on variable declarators NYI");
return;
}
my $name = $M->{variable}{_ast}{decl_slot};
my $slot = $name;
if (!$slot) {
$M->sorry("Cannot apply a declarator to a non-simple variable");
return;
}
my $scope = $::SCOPE // 'my';
if ($scope eq 'augment' || $scope eq 'supercede') {
$M->sorry("Illogical scope $scope for simple variable");
return;
}
if ($scope eq 'has') {
$M->sorry("Unsupported scope $scope for simple variable");
return;
}
if ($scope eq 'anon') {
$slot = $cl->gensym;
}
if ($scope eq 'state') {
$M->{_ast} = Op::Lexical->new(name => $slot, state_decl => 1,
state_backing => $cl->gensym, declaring => 1,
list => scalar ($M->{variable}->Str =~ /^\@/));
} elsif ($scope eq 'our') {
$M->{_ast} = Op::PackageVar->new(name => $slot, slot => $slot,
path => [ 'OUR' ]);
} else {
$M->{_ast} = Op::Lexical->new(name => $slot, declaring => 1,
list => scalar ($M->{variable}->Str =~ /^\@/));
}
}
sub type_declarator {}
sub type_declarator__S_constant { my ($cl, $M) = @_;
my $scope = $::SCOPE // 'my';
if (!$M->{identifier} && !$M->{variable}) {
$M->sorry("Anonymous constants NYI"); #wtf?
return;
}
my $slot = ($M->{identifier} // $M->{variable})->Str;
# This is a cheat. Constants should be, well, constant, and we should be
# using the phaser rewrite mechanism to get the initializer here. XXX
# terms need to use a context hash.
$M->{_ast} = Op::Lexical->new(name => $slot, declaring => 1);
}
sub package_declarator {}
sub package_declarator__S_class { my ($cl, $M) = @_;
$M->{_ast} = $M->{package_def}{_ast};
}
sub package_declarator__S_grammar { my ($cl, $M) = @_;
$M->{_ast} = $M->{package_def}{_ast};
}
sub package_declarator__S_package { my ($cl, $M) = @_;
$M->{_ast} = $M->{package_def}{_ast};
}
sub package_declarator__S_module { my ($cl, $M) = @_;
$M->{_ast} = $M->{package_def}{_ast};
}
sub package_declarator__S_knowhow { my ($cl, $M) = @_;
$M->{_ast} = $M->{package_def}{_ast};
}
sub package_declarator__S_role { my ($cl, $M) = @_;
$M->{_ast} = $M->{package_def}{_ast};
}
sub package_declarator__S_slang { my ($cl, $M) = @_;
$M->{_ast} = $M->{package_def}{_ast};
}
sub package_declarator__S_also { my ($cl, $M) = @_;
$M->{_ast} = Op::StatementList->new(children =>
$cl->process_package_traits($M, undef, @{ $M->{trait} }));
}
sub process_package_traits { my ($cl, $M, $export, @tr) = @_;
my @r;
for (@tr) {
if (exists $_->{_ast}{name}) {
push @r, Op::Super->new(name => $_->{_ast}{name});
} elsif ($_->{_ast}{export}) {
if ($export) {
push @$export, @{ $_->{_ast}{export} };
} else {
$M->sorry('Cannot mark a class as exported outside the declarator');
}
} else {
$M->sorry("Non-superclass traits for packageoids NYI");
}
}
@r;
}
sub termish {}
sub nulltermish { my ($cl, $M) = @_; # for 1,2,3,
# XXX this is insane
$M->{term}{_ast} = $M->{term}{term}{_ast} if $M->{term};
}
sub EXPR {}
sub arglist { my ($cl, $M) = @_;
$M->sorry("Invocant handling is NYI") if $::INVOCANT_IS;
my $x = $M->{EXPR}{_ast};
if (!defined $x) {
$M->{_ast} = [];
} elsif ($x && $x->isa('Op::CallSub') && $x->splittable_parcel) {
$M->{_ast} = $x->positionals;
} else {
$M->{_ast} = [$x];
}
}
sub semiarglist { my ($cl, $M) = @_;
$M->{_ast} = [ map { $_->{_ast} } @{ $M->{arglist} } ];
}
sub args { my ($cl, $M) = @_;
if ($M->{moreargs} || $M->{semiarglist} && $M->{arglist}[0]) {
$M->sorry("Interaction between semiargs and args is not understood");
return;
}
$M->{_ast} = $M->{semiarglist} ? $M->{semiarglist}{_ast} :
$M->{arglist}[0] ? [ $M->{arglist}[0]{_ast} ] : undef;
}
sub statement { my ($cl, $M) = @_;
if ($M->{label} || $M->{statement_mod_cond}[0] || $M->{statement_mod_loop}[0]) {
$M->sorry("Control is NYI");
return;
}
$M->{_ast} = $M->{statement_control} ? $M->{statement_control}{_ast} :
$M->{EXPR} ? $M->{EXPR}{_ast} : undef;
}
sub statementlist { my ($cl, $M) = @_;
$M->{_ast} = Op::StatementList->new(children =>
[ grep { defined $_ } map { $_->{_ast} } @{ $M->{statement} } ]);
}
sub semilist { my ($cl, $M) = @_;
$M->{_ast} = [ map { $_->{_ast} } @{ $M->{statement} } ];
}
sub module_name { }
sub module_name__S_normal { my ($cl, $M) = @_;
# name-extension stuff is just ignored on module names for now
$M->{_ast} = {
name => $M->{longname}{name}->Str,
args => $M->{arglist}[0] ? $M->{arglist}[0]{_ast} : undef };
}
sub statement_control { }
sub statement_control__S_if { my ($cl, $M) = @_;
my $else = $M->{else}[0] ?
$cl->block_to_immediate('cond', $M->{else}[0]{_ast}) : undef;
my @elsif;
for (reverse @{ $M->{elsif} }) {
$else = Op::Conditional->new(check => $_->{_ast}[0],
true => $cl->block_to_immediate('cond', $_->{_ast}[1]),
false => $else);
}
$M->{_ast} = Op::Conditional->new(check => $M->{xblock}{_ast}[0],
true => $cl->block_to_immediate('cond', $M->{xblock}{_ast}[1]),
false => $else);
}
sub statement_control__S_while { my ($cl, $M) = @_;
$M->{_ast} = Op::WhileLoop->new(check => $M->{xblock}{_ast}[0],