-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstructor.pm
2485 lines (1898 loc) · 64.8 KB
/
Constructor.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
# Copyright (c) 2015, Mitchell Cooper
package Ferret::Lexer::Constructor;
use warnings;
use strict;
use 5.010;
use Scalar::Util qw(blessed);
use F;
use Ferret::Lexer::Current;
use Ferret::Lexer::Rules;
use Ferret::Lexer::Scope;
use Ferret::Lexer::Verifier;
our ($current, $error);
sub construct {
my $main_node = shift;
my $err;
# CONSTRUCTOR
# ===========
# separate into lines.
my @tokens = @_;
my (@lines, %positions);
foreach my $tok (@tokens) {
my $pos = $tok->[2];
$positions{$pos} = $tok;
push @{ $lines[ int $pos ] ||= [] }, $tok;
}
$current = Ferret::Lexer::Current->new(
main_node => $main_node,
file => $main_node->file_name || 'unknown',
node => $main_node,
last_el => $main_node,
token_lines => \@lines,
token_pos => \%positions,
upcoming => \@tokens,
done_toks => [],
elements => []
);
while (my ($label, $value, $position) = @{ shift @tokens || [] }) {
return $err if $err = handle_label($label, $value, $position);
# update last_el.
my $old_el = $current->{last_el} || 0;
my $last_el = ($current->node->children)[-1] || $current->node;
$current->{last_el} = $last_el;
# pending comment.
if ($last_el != $old_el && $current->{doc_comment}) {
$last_el->{doc_comment} = delete $current->{doc_comment};
}
}
# EOF
return $err if $err = c_eof($current, $main_node);
# ENFORCER
# ========
Ferret::Lexer::RuleFunctions::final_check($main_node);
return $err if $err = check_error();
# VERIFIER
# ========
Ferret::Lexer::Verifier::verify($main_node);
return $err if $err = check_error();
return;
}
sub check_error {
my $err = $error;
undef $error;
return $err;
}
sub handle_label {
my ($label, $value, $position) = @_;
my $redo = sub { handle_label($label, $value, $position) };
$position //= 0;
my $err;
# check for error.
return $err if $err = check_error();
# current info.
@$current{ qw(label value line position next_tok unknown_el) } = (
$label, $value,
int $position, $position,
$current->{upcoming}[0],
undef
);
# check token rules.
Ferret::Lexer::RuleFunctions::token_check($label, $current, $value);
return $err if $err = check_error();
# call the handler for all.
my $started_instr = c_any($label, $current, $value);
# redo.
return $redo->() if $current->should_redo;
# call a handler if one exists.
if (my $code = __PACKAGE__->can("c_$label")) {
my $el = $code->($current, $value);
# check error.
return $err if $err = check_error();
if (blessed $el) {
return $el if $el->isa('F::Error');
push @{ $current->{elements} }, $el;
}
# started an instruction.
$el->{started_instr} = 1 if $started_instr;
# redo.
return $redo->() if $current->should_redo;
# all is good.
}
# nothing to handle it. throw in an unknown element.
else {
# Rule Token[0]:
# Parent must be of type NONE (pseudotype representing no type).
$current->node->adopt($current->unknown_el);
}
push @{ $current->{done_toks} }, [ $label, $value ];
# final error check.
return $err if $err = check_error();
return;
}
###############################
### DOCUMENT-LEVEL KEYWORDS ###
################################################################################
# package declaration.
sub c_PKG_DEC {
my ($c, $value) = @_;
# add includes to current package
c_spaces($c->document) if $c->document;
# terminate current class.
$c->close_node_maybe('Document');
# create a document.
my $pkg = F::new('Document', package => $value->{name});
$c->set_document($pkg);
# Rule Package[0]:
# Must be a direct child of a Document.
# set as current node.
# will be terminated by another package declaration, 'end', or end of file.
$c->adopt_and_set_node($pkg);
return $pkg;
}
# class declaration.
sub c_CLASS_DEC {
my ($c, $value) = @_;
# terminate current class.
$c->close_node_maybe('Class');
# create class.
my $class = F::new('Class', %$value);
$c->set_class($class);
# Rule Class[0]:
# Must be a direct child of a Document or Package.
# set as current node.
# will be terminated by another class declaration, 'end', or end of file.
$c->adopt_and_set_node($class);
# Rule Class[1]:
# Direct children must be of one of the following types:
# Function
# Method
# Instruction
# Type
# Rule Class[2]:
# If a direct child is an Instruction, its statement must satisfy one
# of the following conditions:
#
# Child is a 'load' statement.
# Child is an 'alias' statement.
# Child is a lexical variable assignment.
#
return $class;
}
# end of a class or document.
sub c_KEYWORD_END {
my ($c, $value) = @_;
# Rule KEYWORD_END[0]:
# Upper nodes must contain a Class or Package.
# Rule KEYWORD_END[1]:
# The current 'end_cap' (document or class to capture 'end') must exist.
# Rule KEYWORD_END[2]:
# The current node must be a Class or Package.
# end the class.
my $class_or_pkg = $c->end_cap;
if ($c->class && $class_or_pkg == $c->class) {
$c->close_class;
}
# end the document.
elsif ($c->document && $class_or_pkg == $c->document) {
$c->close_document;
}
# close it.
$c->close_end_cap;
$c->close_node;
return;
}
# namespace load statement.
sub c_KEYWORD_LOAD {
my ($c, $value) = @_;
# Rule Load[0]:
# Must be a direct child of an Instruction.
# Rule Load[1]:
# Parent must be a direct child of a Class or Document.
# Rule Load[2]:
# Direct children must be of type Bareword.
# Rule Load[3]:
# Number of direct children must be exactly one (1).
my $load = F::new('Load');
$c->adopt_and_set_node($load);
}
#################
### OPERATORS ###
################################################################################
##################################################
### BRACKETS, PARENTHESES, ANGLES (DELIMITERS) ###
##################################################
# start of a closure (opening curly bracket).
sub c_CLOSURE_S {
my ($c, $value, $is_colon) = @_;
# Rule CLOSURE_S[0]:
# The current 'clos_cap' must exist.
my $closure = $c->get_closure;
# if it's a call, the closure is a function being passed.
if ($closure->type eq 'Call') {
my $call = $closure;
$call->{call_capturing_closure} = 1;
# create a function.
my $func = F::new('Function', anonymous => 1, call_closure => 1);
$func->body->{call_closure} = 1;
# make it an argument to the call.
$call->arg_list->new_item->adopt($func);
$closure = $func->body;
}
# a closure can terminate these.
$c->close_nodes(qw(
Negation Operation Assignment LocalDeclaration
WantNeed WantNeedType WantNeedValue
));
# a closure can terminate a generated expression.
# for instance, inside $something {}. the expression $something ends there.
my $node = $c->node;
if ($node->{generated_expression}) {
$c->close_node;
}
# remember which closure this is inside; if any.
# then, set this node as the current closure.
$c->set_closure($closure);
$c->set_node($closure);
return;
}
# end of a closure (closing curly bracket).
sub c_CLOSURE_E {
my ($c, $value, $is_semi) = @_;
# Rule CLOSURE_E[0]:
# The current 'closure' must exist.
# DISABLED
# Rule CLOSURE_E[1]:
# The current 'node' must be equal to the current 'closure'.
# simulate a semicolon on the last instruction of the closure body.
$c->simulate('OP_SEMI')
if $c->instruction && $c->node != $c->closure;
# close the closure and the node.
my $closure = $c->closure;
$c->close_closure;
$c->close_node($closure->is_type('Body') ? 2 : 1);
# this is a closure-capturing function call.
if ($closure->{call_closure}) {
# terminate the call.
my $upper_call = $c->node->first_self_or_parent('Call');
if ($upper_call->{call_capturing_closure}) {
$c->close_node_until($upper_call->parent);
}
}
# this section handles expression closures within instructions
#
# if the closure we just terminated is a function body,
# check if it is anonymous. if so, possibly inject a semicolon.
# however, don't do this for 'on' functions.
#
# or if it's a gather body, we may need to inject a semicolon.
#
my $p = $closure->parent;
my $is_gather = $closure->type eq 'GatherBody';
my $is_anon_func =
$closure->type eq 'FunctionBody' &&
$p->type eq 'Function' &&
$p->anonymous;
undef $is_anon_func if $p->parent->type eq 'On';
# do not allow anon funcs in void context
# if ($is_anon_func && !$c->instruction) {
# return $c->unexpected([
# 'to close anonymous function',
# 'Anonymous function is not permitted in void context'
# ]);
# }
# terminate anon funcs and gathers
if (!$is_semi and $is_anon_func || $is_gather) {
# if the closing curly bracket is the last thing on the line,
# inject a semicolon.
my $close_pos = $p->{close_pos};
my $is_close = $c->{token_pos}{$close_pos}[0] eq 'CLOSURE_E';
my $last_on_line = $c->{token_lines}[ int $close_pos ][-1];
$c->simulate('OP_SEMI')
if $is_close && $close_pos == $last_on_line->[2];
$c->close_node if $closure->type eq 'ForBody'
&& $p->{is_gatherfor};
}
# gather for: this is a special case --
#
# if the closure we just terminated is a 'for' body,
# check if the for's owner is a gather body. then, see
# if the gather is marked as a 'gatherfor' keyword.
#
# if those conditions are met, close another two nodes,
# which is the gather and its body.
#
# finally, simulate a semicolon. this is because it would
# not have been done above since it was a ForBody not a GatherBody.
# this will close assignments, return statements, etc.
#
$c->close_node(2) and $c->simulate('OP_SEMI')
if $closure->type eq 'ForBody' && $p->{is_gatherfor};
# continue: this is a special case --
#
# if the closure we just terminated is a 'continue' body,
# terminate the for as well.
$c->close_node if $closure->type eq 'ContinueBody';
# catch: this is a very special case --
#
# if the closure we just terminated is a 'catch' body,
# we need to also terminate the parent instruction.
#
# note that a semicolon has already been processed at
# this point, so we don't simulate one here.
#
$c->close_node if $closure->type eq 'CatchBody';
return;
}
# tests whether a statement followed by a colon could be a one-line closure.
sub could_be_one_liner {
my $c = shift;
# clos_cap gotta be there.
return unless $c->clos_cap;
# if we're inside a generated expression, all good.
# this could be problematic if a generated expression had an anonymous
# function or other sort of closure in it, but that would be ridiculous.
my $el = $c->node;
do {
return 1 if $el->{generated_expression};
} while $el = $el->parent;
# if the last token is any of these, good.
my %is_reasonable = map { $_ => 1 } qw(
KEYWORD_ELSE
KEYWORD_DEFER
);
my $l_label = $c->{done_toks}[-1] ? $c->{done_toks}[-1][0] : '';
return $is_reasonable{$l_label};
}
# opening parenthesis.
sub c_PAREN_S {
my ($c, $value) = @_;
# Rule List[0]:
# Direct children must be of type ListItem.
# Rule ListItem[0]:
# Direct children must be of type Pair or some sort of Expression.
# Rule ListItem[1]:
# Must directly follow a ListItem, if following anything.
# Rule ListItem[2]:
# Must come directly before a ListItem, if before anything.
# Rule ListItem[3]:
# Number of direct children must be exactly one (1).
# Rule for ListItem implemented in F/List.pm:
# Pairs and non-pairs cannot be mixed in a list unless it is the
# argument list of a call.
my $list = $c->start_list('PAREN_E');
return $list;
}
# closing parenthesis.
sub c_PAREN_E {
my ($c, $value) = @_;
# Rule PAREN_E[0]:
# The current 'list' must exist.
# this must be the expected list terminator.
my $t = $c->list->{list_terminator};
my $p = F::pretty_token($t);
return $c->unexpected("to close list (instead of $p)")
if $t ne 'PAREN_E';
# closes these things.
$c->close_nodes(qw(
Negation Operation Pair NamedPair Detail
Assignment LocalDeclaration WantNeed WantNeedType WantNeedValue
));
# close the list itself.
#
# the current node becomes
# the current node (list item)'s parent (list)'s parent
#
return $c->unexpected if $c->node->parent != $c->list;
$c->close_node(2);
# function call.
#
# as a special case, close function calls here as well, since they are
# terminated by the end of their argument list.
#
$c->close_node_maybe('Call') || $c->close_node_maybe('InterfaceMethod');
# finally, the current list becomes the next list up in the tree.
return $c->close_list;
}
# opening bracket.
sub c_BRACKET_S {
my ($c, $value) = @_;
# Rules for List:
# See c_PAREN_S().
my $list = $c->start_list('BRACKET_E');
$list->{collection} = 1; # define as a value list or hash.
$list->{array} = 1; # default
return $list;
}
# closing bracket.
sub c_BRACKET_E {
my ($c, $value) = @_;
# Rule BRACKET_E[0]:
# The current 'list' must exist.
# this must be the expected list terminator.
my $t = $c->list->{list_terminator};
my $p = F::pretty_token($t);
return $c->unexpected("to close list (instead of $p)")
if $t ne 'BRACKET_E';
# closes these things.
$c->close_nodes(
qw(Negation Operation Pair NamedPair Assignment
LocalDeclaration WantNeed WantNeedType WantNeedValue
));
# close the list itself.
#
# the current node becomes
# the current node (list item)'s parent (list)'s parent
#
return $c->unexpected if $c->node->parent != $c->list;
$c->close_node(2);
# index.
#
# as a special case, close indices here as well, since they are
# terminated by the end of their argument list.
#
my $closed_index = $c->close_node_maybe('Index');
# property.
#
# additionally, the termination of the index can close a
# non-bareword property, such as in 2.["even"]
#
# only do it though if the property is marked as indexed
# and if we have not closed an index above.
#
$c->close_node_maybe('Property')
if !$closed_index && $c->node->{is_index};
# finally, the current list becomes the next list up in the tree.
return $c->close_list;
}
# opening angle.
sub c_ANGLE_S {
my $c = shift;
# it is guaranteed by the tokenizer that the opening angle is
# immediately preceded by a bareword or class declaration.
my $last_el = $c->last_el;
my $tc = F::new('TypedClass', ready_for_another => 1);
# Rule TypedClass[0]:
# Direct children must be of type Bareword or Maybe.
# Rule TypedClass[1]:
# Number of children must be no less than two (2).
# it's a class declaration.
if ($last_el->type eq 'Class') {
# remember that it belongs to this class.
# on close, it will be abandoned,
# but the class will remember its generics.
$tc->set_tc_class($last_el);
# set the typed class as the current node.
# DO NOT adopt it. classes cannot contain them.
# TypedClass will always return the class as
# its ->parent, but it will not be a true child.
$c->set_node($tc);
}
# for everything else, adopt the previous element.
else {
$tc->adopt($last_el);
$tc->{ready_for_another}++;
$c->adopt_and_set_node($tc);
}
return $tc;
}
# closing angle.
sub c_ANGLE_E {
my $c = shift;
# it is guaranteed by the tokenizer that this closing angle has
# an opening angle to correspond with.
# Rule ANGLE_E[0]:
# The current node must be of type TypedClass.
$c->close_node;
return;
}
#########################
### CALLS AND INDICES ###
#########################
# handle_call() is used for calls as well as indices.
sub handle_call {
my ($c, $value, $has_list, $is_index) = @_;
# Rule Call[0]:
# First child must be of type Expression.
# Rule Call[1]:
# Second child must be of type List.
# Rule Call[2]:
# Number of children must be exactly one (1).
# Rules for InterfaceMethod:
# See c_KEYWORD_CAN().
# if it's a 'can', this is an interface method requirement.
my $is_req = ($c->node->{req_type} || '') eq 'can';
# determine list terminator and call package.
my $terminator = $is_index ? 'BRACKET_E' : 'PAREN_E';
my $package = $is_index ? 'Index' :
$is_req ? 'InterfaceMethod' : 'Call';
# a call can only come after an expression.
my $last_el = $c->last_el;
return $c->unexpected if !$last_el || !$last_el->is_type('Expression');
# create a function call, adopting the last element.
my $call = $c->node->adopt(F::new($package));
$call->adopt($last_el);
# handle the list, then adopt it.
if ($has_list) {
# Rules for List:
# See c_PAREN_S().
my $list = $c->start_list($terminator);
$list->{is_callidx} = 1;
$list->{is_index} = $is_index;
$list->{collection} = 1; # pretend to be array/hash for checks.
$call->adopt($list);
}
# if it's not an index or method requirement,
# it might be a closure-capturing call.
if (!$is_index && !$is_req) {
# found a more likely thing to capture a closure -
# something that's not a call like an 'if' or 'on'
my $more_likely = $c->clos_cap
if $c->clos_cap && $c->clos_cap->type ne 'Call';
# clos_cap is just a call or nothing;
# use this call as the new clos_cap.
$c->capture_closure_with($call) if !$more_likely;
}
return $call;
}
# opening bracket for an index.
sub c_BRACKET_IDX {
my ($c, $value) = @_;
return handle_call($c, $value, 1, 1);
}
# opening parenthesis for a call.
sub c_PAREN_CALL {
my ($c, $value) = @_;
return handle_call($c, $value, 1);
}
# zero-argument call operator.
sub c_OP_CALL {
my ($c, $value) = @_;
return handle_call($c, $value);
}
# detailed return values.
sub c_KEYWORD_DETAIL {
my ($c, $value) = @_;
my $det = F::new('Detail');
return $c->adopt_and_set_node($det);
}
###################
### ASSIGNMENTS ###
###################
# standard assignment operator.
sub c_OP_ASSIGN {
my ($c, $value) = @_;
# if we're in a WantNeed, this is the value expression.
$c->close_node_maybe('WantNeedType');
if ($c->node->type eq 'WantNeed') {
my $wn = $c->node;
# Rules for WantNeedValue:
# See c_KEYWORD_WANT().
# if the argument already has a value expression, that's an issue.
if ($wn->value_exp) {
return $c->unexpected([
"inside $$wn{arg_type}",
'This argument declaration already has a fallback value '.
'expression'
]);
}
my $exp = $wn->create_value_exp;
$c->adopt_and_set_node($exp);
return $exp;
}
# Rule Assignment[0]:
# Direct parent must be of one of the following types:
# Instruction
# IfParameter
# Alias
# SharedDeclaration
# LocalDeclaration
# Rule Assignment[1]:
# Number of direct children must be exactly two (2).
# Rule Assignment[2]:
# The first direct child must be some sort of Assignable expression.
# Rule Assignment[3]:
# The second direct child must be an Expression of sorts.
# Rule Assignment[4]:
# When the direct parent is of type Alias, only direct children of type
# Bareword are permitted.
# Rule Assignment[5]:
# When the direct parent is of type Alias, voids Assignment[2].
# Rule Assignment[6]:
# When the direct parent is of type Alias, voids Assignment[3].
# remember the last element as the left side of the assignment.
my $a = F::new('Assignment');
$c->adopt_and_set_node($a);
$a->adopt($c->last_el);
return $a;
}
# lazy assignment operator.
sub c_OP_LASSIGN {
my ($c, $value) = @_;
# Rules for Assignment:
# See c_OP_ASSIGN().
# remember the last element as the left side of the assignment.
my $a = F::new('Assignment', lazy => 1);
$c->adopt_and_set_node($a);
$a->adopt($c->last_el);
return $a;
}
*c_OP_ADD_A = *c_OP_SUB_A =
*c_OP_MUL_A = *c_OP_DIV_A = *c_altering_assignment;
sub c_altering_assignment {
my ($c, $value) = @_;
# determine the operation type
my $op = lc $c->label;
$op =~ s/^op_(.+)_a/$1/;
$op = '_sub' if $op eq 'sub';
# Rules for Assignment:
# See c_OP_ASSIGN().
# remember the last element as the left side of the assignment.
my $a = F::new('Assignment', operation => $op);
$c->adopt_and_set_node($a);
$a->adopt($c->last_el);
}
##################
### OPERATIONS ###
##################
*c_OP_ADD = *c_OP_SUB =
*c_OP_MUL = *c_OP_DIV =
*c_OP_POW = *c_OP_MOD =
*c_OP_AND = *c_OP_OR =
*c_OP_SIM = *c_OP_NSIM =
*c_OP_EQUAL = *c_OP_NEQUAL =
*c_OP_EQUAL_I = *c_OP_NEQUAL_I =
*c_OP_LESS = *c_OP_LESS_E =
*c_OP_GR8R = *c_OP_GR8R_E =
*c_OP_RANGE = *c_operator;
# used for all operators managed by the Operation node.
sub c_operator {
my ($c, $value) = @_;
# we're only interested in the previous element at the same level.
my $last_el = $c->last_el;
undef $last_el if $last_el && $last_el == $c->node;
# do not capture the left side of an assignment.
undef $last_el if $last_el && $c->node->type eq 'Assignment'
&& $last_el == $c->node->assign_to;
# if it's addition or subtraction, it might be a sign.
my %signs = (OP_ADD => 1, OP_SUB => 1);
if ($last_el && !$last_el->is_type('Expression') && $signs{ $c->label }) {
undef $last_el;
}
# it has to be an expression
return $c->expected(
'an expression',
'at left of '.F::pretty_token($c->label)
) if $last_el && !$last_el->is_type('Expression');
# FIXES NEGATING THE ENTIRE OPERATION
if ($c->node->type eq 'Negation') {
$last_el = $c->node;
$c->close_node;
}
# if the current node is an operation, just add another thing.
my $operator = F::new('Operator', token => $c->label);
if ($c->node->type eq 'Operation') {
$c->node->adopt($operator);
return $operator;
}
# adopt the last element as the left side of the operation.
my $op = F::new('Operation');
$c->adopt_and_set_node($op);
$op->adopt($last_el) if $last_el;
$op->adopt($operator);
return $op;
}
#######################
### MISC. OPERATORS ###
#######################
# the comma. separates list items and whatnot.
sub c_OP_COMMA {
my ($c, $value) = @_;
# we're in a typed class.
if ($c->node->type eq 'TypedClass') {
my $tc = $c->node;
$tc->{ready_for_another}++;
return $tc;
}
# we're in a want/need. this starts another.
$c->close_nodes(qw(WantNeedType WantNeedValue));
if ($c->node->type eq 'WantNeed') {
my $old_wn = $c->node;
# fake a semicolon to terminate the instruction
# wrapping the previous WantNeed.
$c->simulate('OP_SEMI');
# create new want/need.
my $wn = F::new('WantNeed', arg_type => $old_wn->{arg_type});
# wrap it with an instruction.
my $instr = F::new('Instruction');
$c->set_instruction($instr);
$c->adopt_and_set_node($instr);
return $c->adopt_and_set_node($wn);
}
# we're in a var. this starts another.
if ($c->node->type eq 'LocalDeclaration') {
# fake a semicolon to terminate the instruction
# wrapping the previous var.
$c->simulate('OP_SEMI');
# create new var.
my $local = F::new('LocalDeclaration');
# wrap it with an instruction.
my $instr = F::new('Instruction');
$c->set_instruction($instr);
$c->adopt_and_set_node($instr);
return $c->adopt_and_set_node($local);
}
# we're in a list.
if ($c->list) {
# Rules for ListItem:
# See c_PAREN_S().
# close these things and then make sure the current node is the
# list item.
$c->close_nodes(qw(
Negation Operation Pair NamedPair Detail
Assignment LocalDeclaration WantNeed WantNeedType WantNeedValue
));
return $c->unexpected if $c->node->parent != $c->list;
# set the current node to a new list item.
$c->set_node($c->list->new_item);
return $c->node;
}
# we're inside an OnParameter, so this comma could separate from a
# symbol callback name.
if (my $exp = $c->node->first_self_or_parent('OnParameter')) {
my $e = $c->next_token_must_be(
'VAR_SYM',
"Following a comma within 'on' parameter ".
"must be a symbol callback name"
);
return $e if $e;
$exp->{cb_method} = 'set_cb_name';
$c->set_node($exp);
return;
}
return $c->unexpected('outside of list');
}
# the semicolon. terminates an instruction.
sub c_OP_SEMI {
my ($c, $automatic) = @_;
# if something is waiting to capture a closure,
# maybe that's what this is, a single-statement closure.
if (could_be_one_liner($c)) {
# simulate a {
$c->simulate('CLOSURE_S', 1);
# remember that the coming intruction will terminate closure.
$c->instruction_opens_closure;
return;
}
# otherwise, there has to be an instruction.
if (!$c->instruction) {
return $c->unexpected([
undef,
'Attempted to terminate an instruction, but no instruction is '.
'open'
]);
}