-
Notifications
You must be signed in to change notification settings - Fork 15
/
Actions.pm
648 lines (521 loc) · 16 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
package Niecza::Actions;
use 5.010;
use strict;
use warnings;
use Op;
use Body;
use Unit;
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 comment { }
sub comment__S_Sharp { }
sub spacey { }
sub nofun { }
sub curlycheck { }
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) = @_;
$M->sorry("Num is NYI");
}
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} ? $M->{identifier}{_ast} : $M->{EXPR}{_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) = @_;
if ($M->{name}{_ast}{dc} || @{ $M->{name}{_ast}{names} } > 1) {
$M->sorry("Multipart names not yet supported");
return "";
}
my ($n) = @{ $M->{name}{_ast}{names} };
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");
}
}
$n;
}
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 nibbler { my ($cl, $M) = @_;
if ($M->isa('STD::Regex')) {
$M->{_ast} = $M->{EXPR}{_ast};
} elsif ($M->isa('Niecza::Grammar::NIL')) {
$M->{_ast} = Op::NIL->new(code => [map { @{$_->{_ast}} } @{$M->{insn}}]);
} 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/)) {
$M->sorry("Word splitting NYI");
return;
}
$M->{_ast} = $sl;
$M->{qpvalue} = '<' . $sl->text . '>';
}
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) = @_;
$M->{_ast} = Op::CallSub->new(
invocant => Op::Lexical->new(name => '&infix:<' . $M->{infix}{sym} . '>'),
positionals => [ $M->{left}{_ast}, $M->{right}{_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 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};
if (@$sal > 1) {
$M->sorry("Slicel lists are NYI");
return;
}
my $args = $sal->[0] // [];
$M->{_ast} = Op::CallSub->new(
invocant => Op::Lexical->new(name => '&' . $id),
positionals => $args);
}
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};
}
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 voidmark { my ($cl, $M) = @_;
$M->{_ast} = 1;
}
sub up { my ($cl, $M) = @_;
$M->{_ast} = length ($M->Str);
}
sub lexdecl { my ($cl, $M) = @_;
$M->{_ast} = [ map { $_->Str, $M->{clrid}->Str } @{ $M->{varid} } ];
}
# :: [row of NIL op]
sub insn {}
sub insn__S_lextypes { my ($cl, $M) = @_;
$M->{_ast} = [[ lextypes => map { @{ $_->{_ast} } } @{ $M->{lexdecl} } ]];
}
sub insn__S_clone_lex { my ($cl, $M) = @_;
$M->{_ast} = [ map { [ clone_lex => $_->Str ] } @{ $M->{varid} } ];
}
sub insn__S_copy_lex { my ($cl, $M) = @_;
$M->{_ast} = [ map { [ copy_lex => $_->Str ] } @{ $M->{varid} } ];
}
sub insn__S_string_var { my ($cl, $M) = @_;
if (!$M->{quote}{_ast}->isa('Op::StringLiteral')) {
$M->sorry("Strings used in NIL code must be compile time constants");
}
$M->{_ast} = [[ string_var => $M->{quote}{_ast}->text ]];
}
sub insn__S_clr_string { my ($cl, $M) = @_;
if (!$M->{quote}{_ast}->isa('Op::StringLiteral')) {
$M->sorry("Strings used in NIL code must be compile time constants");
}
$M->{_ast} = [[ clr_string => $M->{quote}{_ast}->text ]];
}
# the negatives here are somewhat of a cheat.
sub insn__S_label { my ($cl, $M) = @_;
$M->{_ast} = [[ labelhere => -$M->{decint}{_ast} ]];
}
sub insn__S_goto { my ($cl, $M) = @_;
$M->{_ast} = [[ goto => -$M->{decint}{_ast} ]];
}
sub insn__S_lex { my ($cl, $M) = @_;
$M->{_ast} = [[ lex => $M->{up}{_ast}, $M->{varid}->Str ]];
}
sub insn__S_lexget { my ($cl, $M) = @_;
$M->{_ast} = [[ lexget => $M->{up}{_ast}, $M->{varid}->Str ]];
}
sub insn__S_lexput { my ($cl, $M) = @_;
$M->{_ast} = [[ lexput => $M->{up}{_ast}, $M->{varid}->Str ]];
}
sub insn__S_how { my ($cl, $M) = @_;
$M->{_ast} = [[ 'how' ]];
}
sub insn__S_fetch { my ($cl, $M) = @_;
$M->{_ast} = [[ 'fetch' ]];
}
sub insn__S_dup_fetchlv { my ($cl, $M) = @_;
$M->{_ast} = [[ 'dup_fetch' ]];
}
sub insn__S_pos { my ($cl, $M) = @_;
$M->{_ast} = [[ pos => $M->{decint}{_ast} ]];
}
sub insn__S_call_method { my ($cl, $M) = @_;
$M->{_ast} = [[ call_method => !$M->{voidmark}, $M->{identifier}->Str,
$M->{decint}{_ast} ]];
}
sub insn__S_call_sub { my ($cl, $M) = @_;
$M->{_ast} = [[ call_sub => !$M->{voidmark}[0], $M->{decint}{_ast} ]];
}
sub insn__S_tail_call_sub { my ($cl, $M) = @_;
$M->{_ast} = [[ tail_call_sub => $M->{decint}{_ast} ]];
}
sub insn__S_clr_call_direct { my ($cl, $M) = @_;
$M->{_ast} = [[ clr_call_direct => $M->{clrid}->Str, $M->{decint}{_ast} ]];
}
sub insn__S_unwrap { my ($cl, $M) = @_;
$M->{_ast} = [[ clr_unwrap => $M->{clrid}->Str ]];
}
sub insn__S_new { my ($cl, $M) = @_;
$M->{_ast} = [[ clr_new => $M->{clrid}->Str, $M->{decint}{_ast} ]];
}
sub insn__S_clr_field_get { my ($cl, $M) = @_;
$M->{_ast} = [[ clr_field_get => $M->{varid}->Str ]];
}
sub insn__S_clr_field_set { my ($cl, $M) = @_;
$M->{_ast} = [[ clr_field_set => $M->{varid}->Str ]];
}
sub insn__S_clr_index_get { my ($cl, $M) = @_;
$M->{_ast} = [[ clr_index_get => ($M->{varid}[0] ? ($M->{varid}[0]->Str) : ()) ]];
}
sub insn__S_clr_index_set { my ($cl, $M) = @_;
$M->{_ast} = [[ clr_index_set => ($M->{varid}[0] ? ($M->{varid}[0]->Str) : ()) ]];
}
sub insn__S_cast { my ($cl, $M) = @_;
$M->{_ast} = [[ cast => $M->{clrid}->Str ]];
}
sub insn__S_return { my ($cl, $M) = @_;
$M->{_ast} = [[ return => $M->{0} ]];
}
sub insn__S_push_null { my ($cl, $M) = @_;
$M->{_ast} = [[ push_null => $M->{clrid}->Str ]];
}
sub clrid {}
sub varid {}
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) = @_;
$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 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};
}
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 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 termish {}
sub EXPR {}
sub arglist { my ($cl, $M) = @_;
$M->sorry("Invocant handling is NYI") if $::INVOCANT_IS;
my $x = $M->{EXPR}{_ast};
if ($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->{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->{EXPR} ? $M->{EXPR}{_ast} : undef;
}
sub statementlist { my ($cl, $M) = @_;
$M->{_ast} = Op::StatementList->new(children =>
[ grep { defined $_ } map { $_->{_ast} } @{ $M->{statement} } ]);
}
sub package_def { my ($cl, $M) = @_;
if ($M->{longname}[0] && $::SCOPE ne 'my') {
$M->sorry('Non-lexical class definitions are not yet supported');
return;
}
if (!$M->{decl}{stub}) {
$M->sorry('Non-stub class definitions are not yet supported');
return;
}
# allocate a slot
$::CURLEX->{'!slots'}{$M->{decl}{name}} = 1;
}
sub routine_declarator {}
sub routine_declarator__S_sub { my ($cl, $M) = @_;
$M->{_ast} = $M->{routine_def}{_ast};
}
my $next_anon_id = 0;
sub gensym { 'anon_' . ($next_anon_id++) }
sub sl_to_block { my ($cl, $ast, %args) = @_;
my $subname = $args{subname} // 'ANON';
Body->new(
name => $subname,
$args{bare} ? () : (
protos => ($::CURLEX->{'!preinit'} // []),
enter => ($::CURLEX->{'!enter'} // []),
lexical => ($::CURLEX->{'!slots'} // {})),
do => $ast);
}
sub block_to_closure { my ($cl, $blk, %args) = @_;
my $outer = $STD::ALL->{ $::CURLEX->{'OUTER::'}[0] };
my $outer_key = $args{outer_key} // $cl->gensym;
$outer->{'!slots'}{$outer_key} = 1 if $outer;
unless ($args{stub}) {
push @{ $outer->{'!preinit'} //= [] },
[ 0, $outer_key, $blk ] if $outer;
push @{ $outer->{'!enter'} //= [] },
Op::CloneSub->new(name => $outer_key) if $outer;
}
Op::Lexical->new(name => $outer_key);
}
# always a sub, though sometimes it's an implied sub after multi/proto/only
sub routine_def { my ($cl, $M) = @_;
if ($M->{sigil}[0] && $M->{sigil}[0]->Str eq '&*') {
$M->sorry("Contextuals NYI");
return;
}
my $dln = $M->{deflongname}[0];
if ($M->{multisig}[0]) {
$M->sorry("Signatures NYI");
return;
}
if ($M->{trait}[0]) {
$M->sorry("Sub traits NYI");
return;
}
my $scope = !$dln ? 'anon' : $::SCOPE || 'my';
if ($scope ne 'my' && $scope ne 'our' && $scope ne 'anon') {
$M->sorry("Illegal scope $scope for subroutine");
return;
}
if ($scope eq 'our') {
$M->sorry('Package subs NYI');
return;
}
my $m = $dln ? $cl->mangle_longname($dln) : undef;
$M->{_ast} = $cl->block_to_closure(
$cl->sl_to_block($M->{blockoid}{_ast}, subname => $m),
stub => $dln && $M->{decl}{stub},
outer_key => (($scope eq 'my') ? "&$m" : undef));
}
sub block { my ($cl, $M) = @_;
$M->{_ast} = $cl->sl_to_block($M->{blockoid}{_ast});
}
# returns Body of 0 args
sub blast { my ($cl, $M) = @_;
if ($M->{block}) {
$M->{_ast} = $M->{block}{_ast};
} else {
$M->{_ast} = Body->new(
name => 'ANON',
do => $M->{statement}{_ast});
}
}
sub statement_prefix {}
sub statement_prefix__S_PREMinusINIT { my ($cl, $M) = @_;
my $var = $cl->gensym;
push @{ $::CURLEX->{'!preinit'} //= [] },
[ 1, $var, $M->{blast}{_ast} ];
push @{ $::CURLEX->{'!enter'} //= [] },
Op::ShareLex->new(name => $var);
$M->{_ast} = Op::Lexical->new(name => $var);
}
sub comp_unit { my ($cl, $M) = @_;
my $body = $cl->sl_to_block($M->{statementlist}{_ast},
subname => 'mainline');
$M->{_ast} = Unit->new(mainline => $body);
}
1;