-
Notifications
You must be signed in to change notification settings - Fork 0
/
orchestra.jison
1771 lines (1585 loc) · 52 KB
/
orchestra.jison
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
%start orchestra
%%
identifier
: IDENTIFIER
{ $$ = new Identifier(@$, {string: $1}); };
global_value_identifier
: GLOBAL_VALUE_IDENTIFIER
{ $$ = new Identifier(@$, {string: $1}); };
opcode
: OPCODE
{ $$ = new Identifier(@$, {string: $1}); };
void_opcode
: VOID_OPCODE
{ $$ = new Identifier(@$, {string: $1}); };
decimal_integer
: DECIMAL_INTEGER
{ $$ = new NumberLiteral(@$, {string: $1}); };
constant
: decimal_integer
| NUMBER
{ $$ = new NumberLiteral(@$, {string: $1}); }
| STRING
{ $$ = new StringLiteral(@$, {string: $1}); }
;
primary_expression
: identifier
| global_value_identifier
| constant
| '(' conditional_expression ')'
{
$$ = $conditional_expression;
}
| error
{
yy.addError({
severity: 'error',
location: {
position: yy.lexer.rangeFromPosition(@$.first_line, @$.first_column)
},
excerpt: 'Expected expression'
});
}
;
array_member
: identifier '[' conditional_expression ']'
{
$$ = new ArrayMember(@$, {children: [$identifier, $conditional_expression]});
}
| array_member '[' conditional_expression ']'
{
$$ = new ArrayMember(@$, {children: [$array_member, $conditional_expression]});
}
;
postfix_expression
: primary_expression
| array_member
| opcode '(' opcode_inputs ')'
{
$$ = new OpcodeExpression(@$, {children: [
$opcode,
new InputArgumentList(@opcode_inputs, {children: $opcode_inputs})
]});
}
| opcode OPCODE_OUTPUT_TYPE_ANNOTATION '(' opcode_inputs ')'
{
$$ = new OpcodeExpression(@$, {outputTypeAnnotation: $OPCODE_OUTPUT_TYPE_ANNOTATION, children: [
$opcode,
new InputArgumentList(@opcode_inputs, {children: $opcode_inputs})],
});
}
;
unary_operator
: '+' { $$ = new UnaryPlus(@$, {string: $1}); }
| '-' { $$ = new UnaryMinus(@$, {string: $1}); }
| '~' { $$ = new BitwiseComplement(@$, {string: $1}); }
| '!' { $$ = new Not(@$, {string: $1}); }
;
unary_expression
: postfix_expression
| unary_operator unary_expression
{
$$ = new UnaryOperation(@$, {children: [$unary_operator, $unary_expression]});
}
;
multiplicative_operator
: '*' { $$ = new Multiplication(@$, {string: $1}); }
| '/' { $$ = new Division(@$, {string: $1}); }
| '^' { $$ = new Power(@$, {string: $1}); }
| '%' { $$ = new Modulus(@$, {string: $1}); }
;
multiplicative_expression
: unary_expression
| multiplicative_expression multiplicative_operator unary_expression
{
$$ = new BinaryOperation(@$, {children: [$multiplicative_expression, $multiplicative_operator, $unary_expression]});
}
;
additive_operator
: '+' { $$ = new Plus(@$, {string: $1}); }
| '-' { $$ = new Minus(@$, {string: $1}); }
;
additive_expression
: multiplicative_expression
| additive_expression additive_operator multiplicative_expression
{
$$ = new BinaryOperation(@$, {children: [$additive_expression, $additive_operator, $multiplicative_expression]});
}
;
shift_operator
: '<<' { $$ = new LeftShift(@$, {string: $1}); }
| '>>' { $$ = new RightShift(@$, {string: $1}); }
;
shift_expression
: additive_expression
| shift_expression shift_operator additive_expression
{
$$ = new BinaryOperation(@$, {children: [$shift_expression, $shift_operator, $additive_expression]});
}
;
relational_operator
: '<' { $$ = new LessThan(@$, {string: $1}); }
| '>' { $$ = new GreaterThan(@$, {string: $1}); }
| '<=' { $$ = new LessThanOrEqual(@$, {string: $1}); }
| '>=' { $$ = new GreaterThanOrEqual(@$, {string: $1}); }
;
relational_expression
: shift_expression
| relational_expression relational_operator shift_expression
{
$$ = new BinaryOperation(@$, {children: [$relational_expression, $relational_operator, $shift_expression]});
}
;
equality_operator
: '==' { $$ = new Equal(@$, {string: $1}); }
| '!=' { $$ = new NotEqual(@$, {string: $1}); }
;
equality_expression
: relational_expression
| equality_expression equality_operator relational_expression
{
$$ = new BinaryOperation(@$, {children: [$equality_expression, $equality_operator, $relational_expression]});
}
;
and_expression
: equality_expression
| and_expression '&'[AND] equality_expression
{
$$ = new BinaryOperation(@$, {children: [$and_expression, new BitwiseAND(@AND, {string: $AND}), $equality_expression]});
}
;
exclusive_or_expression
: and_expression
| exclusive_or_expression '#'[XOR] and_expression
{
$$ = new BinaryOperation(@$, {children: [$exclusive_or_expression, new BitwiseXOR(@XOR, {string: $XOR}), $and_expression]});
}
;
inclusive_or_expression
: exclusive_or_expression
| inclusive_or_expression '|'[OR] exclusive_or_expression
{
$$ = new BinaryOperation(@$, {children: [$inclusive_or_expression, new BitwiseOR(@OR, {string: $OR}), $exclusive_or_expression]});
}
;
logical_and_expression
: inclusive_or_expression
| logical_and_expression '&&'[and] inclusive_or_expression
{
$$ = new BinaryOperation(@$, {children: [$logical_and_expression, new And(@and, {string: $and}), $inclusive_or_expression]});
}
;
logical_or_expression
: logical_and_expression
| logical_or_expression '||'[or] logical_and_expression
{
$$ = new BinaryOperation(@$, {children: [$logical_or_expression, new Or(@or, {string: $or}), $logical_and_expression]});
}
;
conditional_expression
: logical_or_expression
| logical_or_expression '?' conditional_expression ':' conditional_expression
{
$$ = new ConditionalExpression(@$, {children: [$logical_or_expression, $conditional_expression1, $conditional_expression2]});
}
;
labeled_statement
: LABEL statement
{
$$ = new LabeledStatement(@$, {children: [new Label(@LABEL, {string: $LABEL.slice(0, -2).trim()}), $statement]});
}
| LABEL EOF
{
$$ = new LabeledStatement(@$, {children: [new Label(@LABEL, {string: $LABEL.slice(0, -2).trim()})]});
}
;
compound_assignment_operator
: '+=' { $$ = new Plus(@$); }
| '-=' { $$ = new Minus(@$); }
| '*=' { $$ = new Multiplication(@$); }
| '/=' { $$ = new Division(@$); }
;
array_declarator
: identifier '[' ']'
{
$$ = new ArrayDeclarator(@$, {children: [$identifier]});
}
| array_declarator '['[left_square_bracket] ']'[right_square_bracket]
{
$$ = new ArrayDeclarator(@$, {children: [$array_declarator.children[0]]});
yy.addError({
severity: 'warning',
location: {
position: yy.lexer.rangeFromLocation({
first_line: @left_square_bracket.first_line,
first_column: @left_square_bracket.first_column,
last_line: @right_square_bracket.last_line,
last_column: @right_square_bracket.last_column
})
},
excerpt: `Multiple ${yy.lexer.quote('[]')} are not needed to declare multidimensional arrays`
});
}
;
declarator
: identifier
| array_declarator
| array_member
;
opcode_outputs
: declarator
{
$$ = [$declarator];
}
| opcode_outputs ',' declarator
{
$$ = $opcode_outputs;
$$.push($declarator);
}
;
opcode_inputs
: conditional_expression
{
$$ = [$conditional_expression];
}
| opcode_inputs ',' conditional_expression
{
$$ = $opcode_inputs;
$$.push($conditional_expression);
}
;
opcode_expression
: opcode
{
$$ = new OpcodeExpression(@$, {children: [$opcode]});
}
| opcode opcode_inputs
{
$$ = new OpcodeExpression(@$, {children: [
$opcode,
new InputArgumentList(@opcode_inputs, {children: $opcode_inputs})
]});
}
;
assignment_statement
: opcode_outputs '=' opcode_inputs NEWLINE
{
$$ = new Assignment(@$, {children: [
new OutputArgumentList(@opcode_outputs, {children: $opcode_outputs}),
new InputArgumentList(@opcode_inputs, {children: $opcode_inputs})
]});
}
| identifier compound_assignment_operator conditional_expression NEWLINE
{
$$ = new CompoundAssignment(@$, {children: [$identifier, $compound_assignment_operator, $conditional_expression]});
}
| opcode_outputs opcode_expression NEWLINE
{
$$ = new OpcodeStatement(@$, {children: [
new OutputArgumentList(@opcode_outputs, {children: $opcode_outputs}),
$opcode_expression
]});
}
;
void_opcode_statement
: void_opcode NEWLINE
{
$$ = new VoidOpcodeStatement(@$, {children: [
new OpcodeExpression({
first_line: @void_opcode.first_line,
first_column: @void_opcode.first_column,
last_line: @void_opcode.last_line,
last_column: @void_opcode.last_column
}, {children: [
$void_opcode
]})
]});
}
| void_opcode WHITESPACE opcode_inputs NEWLINE
{
$$ = new VoidOpcodeStatement(@$, {children: [
new OpcodeExpression({
first_line: @void_opcode.first_line,
first_column: @void_opcode.first_column,
last_line: @opcode_inputs.last_line,
last_column: @opcode_inputs.last_column
}, {children: [
$void_opcode,
new InputArgumentList(@opcode_inputs, {children: $opcode_inputs})
]})
]});
}
| void_opcode '(' opcode_inputs ')'[right_parenthesis] NEWLINE
{
$$ = new VoidOpcodeStatement(@$, {children: [
new OpcodeExpression({
first_line: @void_opcode.first_line,
first_column: @void_opcode.first_column,
last_line: @right_parenthesis.last_line,
last_column: @right_parenthesis.last_column
}, {children: [
$void_opcode,
new InputArgumentList(@opcode_inputs, {children: $opcode_inputs})
]})
]});
}
;
goto_statement
: GOTO identifier NEWLINE
{
$$ = new Goto(@$, {children: [$identifier]});
}
| GOTO decimal_integer NEWLINE
{
$$ = new Goto(@$, {children: [$decimal_integer]});
}
| GOTO error NEWLINE
{
yyclearin;
yyerrok;
yy.addError({
severity: 'error',
location: {
position: yy.lexer.rangeFromPosition(@GOTO.last_line, @GOTO.last_column)
},
excerpt: 'Expected newline'
});
}
;
then_statement
: THEN NEWLINE statements
{
$$ = new Then(@$, {children: $statements});
}
| THEN NEWLINE
{
$$ = new Then(@$);
}
| THEN error NEWLINE
{
yyclearin;
yyerrok;
yy.addError({
severity: 'error',
location: {
position: yy.lexer.rangeFromPosition(@THEN.last_line, @THEN.last_column)
},
excerpt: 'Expected newline'
});
}
;
elseif_statement
: ELSEIF equality_expression then_statement
{
$$ = new If(@$, {children: [$equality_expression, $then_statement]});
}
;
// The lack of a NEWLINE after ELSE is intentional and matches Csound.
else
: ELSE statements
{
$$ = new Else(@$, {children: $statements});
}
;
elseif
: elseif_statement
| elseif elseif_statement
{
$$ = $elseif;
$$.children.push(new Else(@elseif_statement, {children: [$elseif_statement]}));
}
| elseif else
{
$$ = $elseif;
$$.children.push($else);
}
;
if_statement
: IF logical_or_expression goto_statement
{
$$ = new If(@$, {children: [$logical_or_expression, $goto_statement]});
}
| IF logical_or_expression then_statement ENDIF NEWLINE
{
$$ = new If(@$, {children: [$logical_or_expression, $then_statement]});
}
| IF logical_or_expression error
{
yyclearin;
yyerrok;
yy.addError({
severity: 'error',
location: {
position: yy.lexer.rangeFromPosition(@IF.first_line, @IF.first_column)
},
excerpt: 'Invalid if-statement'
});
}
| IF logical_or_expression then_statement else ENDIF NEWLINE
{
$$ = new If(@$, {children: [$logical_or_expression, $then_statement, $else]});
}
| IF logical_or_expression then_statement elseif ENDIF NEWLINE
{
$$ = new If(@$, {children: [$logical_or_expression, $then_statement, new Else(@elseif, {children: [$elseif]})]});
}
;
// The lack of a NEWLINE after DO and OD is intentional and matches Csound.
do
: DO
{
$$ = new Do(@$);
}
| DO statements
{
$$ = new Do(@$, {children: $statements});
}
;
loop_statement
: WHILE equality_expression do OD
{
$$ = new While(@$, {children: [$equality_expression, $do]});
}
| UNTIL equality_expression do OD
{
$$ = new Until(@$, {children: [$equality_expression, $do]});
}
;
statement
: labeled_statement
| void_opcode_statement
| assignment_statement
| if_statement
| loop_statement
| goto_statement
| NEWLINE
{
$$ = new Empty(@$);
}
| error NEWLINE
{
yyclearin;
yyerrok;
yy.addError({
severity: 'error',
location: {
position: yy.lexer.rangeFromPosition(@$.first_line, @$.first_column)
},
excerpt: 'Invalid statement'
});
}
;
statements
: statement
{
$$ = [$statement];
}
| statements statement
{
$$ = $statements;
$$.push($statement);
}
;
instrument_number_or_name
: decimal_integer
| identifier
| '+'[plus] identifier
{
$$ = new UnaryOperation(@$, {children: [new UnaryPlus(@plus), $identifier]});
}
;
instrument_numbers_and_names
: instrument_number_or_name
{
$$ = [$instrument_number_or_name];
}
| instrument_numbers_and_names ',' instrument_number_or_name
{
$$ = $instrument_numbers_and_names;
$$.push($instrument_number_or_name);
}
;
instrument_number_and_name_list
: instrument_numbers_and_names
{
$$ = new InstrumentNumberAndNameList(@$, {children: $instrument_numbers_and_names});
}
;
instrument
: INSTR instrument_number_and_name_list NEWLINE statements ENDIN NEWLINE
{
$$ = new Instrument(@$, {children: [$instrument_number_and_name_list, ...$statements]});
}
| INSTR instrument_number_and_name_list NEWLINE ENDIN NEWLINE
{
$$ = new Instrument(@$, {children: [$instrument_number_and_name_list]});
}
;
opcode_output_type_signature
: OPCODE_OUTPUT_TYPE_SIGNATURE
{
$$ = new OpcodeOutputTypeSignature(@$, {string: $OPCODE_OUTPUT_TYPE_SIGNATURE});
}
;
opcode_input_type_signature
: OPCODE_INPUT_TYPE_SIGNATURE
{
$$ = new OpcodeInputTypeSignature(@$, {string: $OPCODE_INPUT_TYPE_SIGNATURE});
}
;
opcode_definition
: OPCODE identifier ',' opcode_output_type_signature ',' opcode_input_type_signature NEWLINE statements ENDOP NEWLINE
{
$$ = new Opcode(@$, {children: [$identifier, $opcode_output_type_signature, $opcode_input_type_signature, ...$statements]});
}
| OPCODE identifier ',' opcode_output_type_signature ',' opcode_input_type_signature NEWLINE ENDOP NEWLINE
{
$$ = new Opcode(@$, {children: [$identifier, $opcode_output_type_signature, $opcode_input_type_signature]});
}
;
orchestra_statement
: global_value_identifier '=' decimal_integer NEWLINE
{
$$ = new Assignment(@$, {children: [
new OutputArgumentList(@global_value_identifier, {children: [$global_value_identifier]}),
new InputArgumentList(@decimal_integer, {children: [$decimal_integer]})
]});
}
| statement
| instrument
| opcode_definition
;
orchestra_statements
: orchestra_statement
{
$$ = [$orchestra_statement];
}
| orchestra_statements orchestra_statement
{
$$ = $orchestra_statements;
$$.push($orchestra_statement);
}
;
orchestra
: orchestra_statements
{
$$ = new Orchestra(@$, {children: $orchestra_statements});
$$.analyzeSemantics(yy);
return $$;
}
;
%%
const matchedInputTypeSignaturesSymbol = Symbol('matchedInputTypeSignaturesSymbol');
const optionalInputArgumentAnalyzersByMatchedInputTypeSignaturesSymbol = Symbol('optionalInputArgumentAnalyzersByMatchedInputTypeSignatures');
class ASTNode {
constructor(rangeOrLocation, properties) {
this.range = Array.isArray(rangeOrLocation) ? rangeOrLocation : parser.lexer.rangeFromLocation(rangeOrLocation);
Object.assign(this, properties);
}
get errorLocation () { return {position: this.range}; }
get description() { return 'expression'; }
get inputArgumentDescription() { return 'input arguments'; }
canBeType(type, arraySuffixPredicate) {
// Expressions will have matchedInputTypeSignatures defined.
const matchedInputTypeSignatures = this[matchedInputTypeSignaturesSymbol];
if (matchedInputTypeSignatures) {
for (const inputTypeSignature of matchedInputTypeSignatures) {
for (const outputTypeSignature of this.outputTypeSignaturesByInputTypeSignature[inputTypeSignature]) {
let regex = /([ikaSfwbBs.])((?:\[\])*)$/;
let result = regex.exec(outputTypeSignature);
if (result) {
let expectedTypes;
const outputTypeCharacter = result[1];
switch (outputTypeCharacter) {
case 'i': // i-time scalar
case 'k': // k-rate scalar
case 'a': // a-rate vector
case 'S': // string
case 'f': // frequency-domain variable, used by phase vocoder opcodes
case 'w': // frequency-domain variable, used by spectrum and related opcodes
case 'b': // i-time Boolean
case 'B': // k-rate Boolean
expectedTypes = [outputTypeCharacter];
break;
case 's': // k-rate scalar or a-rate vector
expectedTypes = ['k', 'a'];
break;
case '.': // any type
expectedTypes = ['i', 'k', 'a', 'S', 'f', 'w'];
break;
}
for (const expectedType of expectedTypes) {
if (type.charAt(0) === expectedType && (!arraySuffixPredicate || arraySuffixPredicate(expectedType + result[2])))
return true;
}
}
}
}
return false;
}
// Variables will have a type property unless they’re undefined. If
// this.type is undefined, there should already be an error about an
// undefined variable; return true so that there isn’t another error about
// an unmatched type.
if (this.type === undefined)
return true;
return type.charAt(0) === this.type.charAt(0) && (!arraySuffixPredicate || arraySuffixPredicate(this.type));
}
analyzeLabels(yy) {
if (this.children) {
for (const child of this.children) {
if (child.analyzeLabels instanceof Function)
child.analyzeLabels(yy);
}
}
}
analyzeSemantics(yy) {
if (this.children) {
for (const child of this.children) {
if (child.analyzeSemantics instanceof Function)
child.analyzeSemantics(yy);
}
}
}
analyzeSemanticsOfInputArguments(yy, inputArguments, inputTypeSignatures) {
class OptionalInputArgumentAnalyzer {
constructor(defaultValue, inputArgument) {
this.defaultValue = defaultValue;
this.inputArgument = inputArgument;
}
analyze() {
return this.inputArgument instanceof NumberLiteral && this.inputArgument.value === this.defaultValue;
}
}
const matchedInputTypeSignatures = [];
this[matchedInputTypeSignaturesSymbol] = matchedInputTypeSignatures;
const optionalInputArgumentAnalyzersByMatchedInputTypeSignatures = {};
this[optionalInputArgumentAnalyzersByMatchedInputTypeSignaturesSymbol] = optionalInputArgumentAnalyzersByMatchedInputTypeSignatures;
for (const inputTypeSignature of inputTypeSignatures) {
if (matchedInputTypeSignatures.indexOf(inputTypeSignature) >= 0)
continue;
let inputArgumentIndex = 0;
let inputTypesMatchTypeSignature = true;
// Maps from characters in input type signatures to input types, from
// https://github.com/csound/csound/search?q=POLY_IN_TYPES+path%3AEngine+filename%3Acsound_standard_types.c
// First, handle required (“poly”) input arguments.
let regex = /([iaSfwblkxTUB.])((?:\[\])*)/y;
let lastIndex = regex.lastIndex;
let result;
while ((result = regex.exec(inputTypeSignature))) {
lastIndex = regex.lastIndex;
let expectedTypes;
let arraySuffixPredicate = type => type.substr(1) === result[2];
const inputTypeCharacter = result[1];
switch (inputTypeCharacter) {
case 'i': // i-time scalar
case 'a': // a-rate vector
case 'S': // string
case 'f': // frequency-domain variable, used by phase vocoder opcodes
case 'w': // frequency-domain variable, used by spectrum and related opcodes
case 'b': // i-time Boolean
case 'l': // label, used by goto opcodes
expectedTypes = [inputTypeCharacter];
break;
case 'k': // i-time or k-rate scalar
expectedTypes = ['i', 'k'];
break;
case 'x': // i-time scalar, k-rate scalar, or a-rate vector
expectedTypes = ['i', 'k', 'a'];
break;
case 'T': // i-time scalar or string
expectedTypes = ['i', 'S'];
break;
case 'U': // i-time scalar, k-rate scalar, or string
expectedTypes = ['i', 'k', 'S'];
break;
case 'B': // i-time or k-rate Boolean
expectedTypes = ['b', 'B'];
break;
case '.': // any type
// Technically 'b', 'B', and 'l' are also allowed; see
// https://github.com/csound/csound/issues/685#issuecomment-236439968.
expectedTypes = ['i', 'k', 'a', 'S', 'f', 'w'];
arraySuffixPredicate = type => type.endsWith(result[2]);
break;
}
const inputArgument = inputArguments[inputArgumentIndex];
inputTypesMatchTypeSignature = false;
if (inputArgument && inputArgument.canBeType instanceof Function) {
for (const expectedType of expectedTypes) {
if (inputArgument.canBeType(expectedType + result[2], arraySuffixPredicate)) {
inputTypesMatchTypeSignature = true;
break;
}
}
}
if (inputTypesMatchTypeSignature)
inputArgumentIndex++;
else
break;
}
if (!inputTypesMatchTypeSignature)
continue;
// Next, handle optional input arguments.
const optionalInputArgumentAnalyzers = [];
regex = /[ojvpqhOJVP?]/y;
regex.lastIndex = lastIndex;
while (inputArgumentIndex < inputArguments.length && (result = regex.exec(inputTypeSignature))) {
lastIndex = regex.lastIndex;
let expectedTypes;
let defaultValue;
switch (result[0]) {
case 'o': // i-time scalar defaulting to 0
expectedTypes = ['i'];
defaultValue = 0;
break;
case 'j': // i-time scalar defaulting to -1
expectedTypes = ['i'];
defaultValue = -1;
break;
case 'v': // i-time scalar defaulting to 0.5
expectedTypes = ['i'];
defaultValue = 0.5;
break;
case 'p': // i-time scalar defaulting to 1
expectedTypes = ['i'];
defaultValue = 1;
break;
case 'q': // i-time scalar defaulting to 10
expectedTypes = ['i'];
defaultValue = 10;
break;
case 'h': // i-time scalar defaulting to 127
expectedTypes = ['i'];
defaultValue = 127;
break;
case 'O': // i-time or k-rate scalar defaulting to 0
expectedTypes = ['i', 'k'];
defaultValue = 0;
break;
case 'J': // i-time or k-rate scalar defaulting to -1
expectedTypes = ['i', 'k'];
defaultValue = -1;
break;
case 'V': // i-time or k-rate scalar defaulting to 0.5
expectedTypes = ['i', 'k'];
defaultValue = 0.5;
break;
case 'P': // i-time or k-rate scalar defaulting to 1
expectedTypes = ['i', 'k'];
defaultValue = 1;
break;
case '?': // any (scalar) type
expectedTypes = ['i', 'k', 'a', 'S', 'f', 'w'];
break;
}
const inputArgument = inputArguments[inputArgumentIndex];
let optionalInputArgumentAnalyzer;
if (defaultValue === -1) {
optionalInputArgumentAnalyzer = {
defaultValue: defaultValue,
inputArgument: inputArgument
}
optionalInputArgumentAnalyzer.analyze = (function() {
if (this.inputArgument instanceof UnaryOperation && this.inputArgument.children[0] instanceof UnaryMinus) {
const unaryExpression = this.inputArgument.children[1];
return unaryExpression instanceof NumberLiteral && unaryExpression.value === 1;
}
}).bind(optionalInputArgumentAnalyzer);
} else {
optionalInputArgumentAnalyzer = new OptionalInputArgumentAnalyzer(defaultValue, inputArgument);
}
optionalInputArgumentAnalyzers.unshift(optionalInputArgumentAnalyzer);
inputTypesMatchTypeSignature = false;
if (inputArgument.canBeType instanceof Function) {
for (const expectedType of expectedTypes) {
if (inputArgument.canBeType(expectedType)) {
inputTypesMatchTypeSignature = true;
break;
}
}
}
if (inputTypesMatchTypeSignature)
inputArgumentIndex++;
else
break;
}
if (!inputTypesMatchTypeSignature)
continue;
const lastOptionalInputArgumentIndex = inputArgumentIndex - 1;
// Finally, handle a variable number of additional input arguments.
regex = /[mzyWMNnZ*]/y;
regex.lastIndex = lastIndex;
// There should be only one variable argument type character.
if ((result = regex.exec(inputTypeSignature))) {
let countPredicate;
let expectedTypesList;
switch (result[0]) {
case 'm': // any number of i-time scalars
countPredicate = count => true;
expectedTypesList = [['i']];
break;
case 'z': // i-time or k-rate scalars
countPredicate = count => true;
expectedTypesList = [['i', 'k']];
break;
case 'y': // a-rate vectors
countPredicate = count => true;
expectedTypesList = [['a']];
break;
case 'W': // strings
countPredicate = count => true;
expectedTypesList = [['S']];
break;
case 'M': // i-time scalars, k-rate scalars, and a-rate vectors
countPredicate = count => true;
expectedTypesList = [['i', 'k', 'a']];
break;
case 'N': // i-time scalars, k-rate scalars, a-rate vectors, and strings
countPredicate = count => true;
expectedTypesList = [['i', 'k', 'a', 'S']];
break;
case 'n': // odd number of i-time scalars, used only by tablexseg
countPredicate = count => (count % 2) === 1;
expectedTypesList = [['i']];
break;
case 'Z': // even number of alternating k-rate scalars and a-rate vectors
countPredicate = count => count > 0 && (count % 2) === 0;
expectedTypesList = [['i', 'k'], ['a']];
break;
case '*': // any types
countPredicate = count => true;
expectedTypesList = [['i', 'k', 'a', 'S', 'f', 'w']];
break;
}
if (!countPredicate(inputArguments.length - inputArgumentIndex))
continue;
let expectedTypesListIndex = 0;
for ( ; inputArgumentIndex < inputArguments.length; inputArgumentIndex++) {
const inputArgument = inputArguments[inputArgumentIndex]
inputTypesMatchTypeSignature = false;
if (inputArgument.canBeType instanceof Function) {
const expectedTypes = expectedTypesList[expectedTypesListIndex];
for (const expectedType of expectedTypes) {
if (inputArgument.canBeType(expectedType)) {
inputTypesMatchTypeSignature = true;
break;
}
}
}
if (inputTypesMatchTypeSignature)
break;
expectedTypesListIndex++;
if (expectedTypesListIndex >= expectedTypesList.length)
expectedTypesListIndex = 0;
}
}
if (!inputTypesMatchTypeSignature)
continue;
// The types of the input arguments match the input type signature.
matchedInputTypeSignatures.push(inputTypeSignature);
// If the last input argument is optional, save the optional input
// argument analyzers to use after analyzing output arguments.
if (lastOptionalInputArgumentIndex === inputArguments.length - 1)
optionalInputArgumentAnalyzersByMatchedInputTypeSignatures[inputTypeSignature] = optionalInputArgumentAnalyzers;
}
if (matchedInputTypeSignatures.length === 0) {
yy.addError({
severity: 'error',
location: this.errorLocation,
excerpt: `Types of ${this.inputArgumentDescription} do not match type signatures of ${this.description}`
});
}
}
analyzeSemanticsOfDeclarator(yy, declarator) {
let identifier;
let arrayDimension = 0;
if (declarator instanceof ArrayDeclarator) {
identifier = declarator;
do {
identifier = identifier.children[0];
arrayDimension++;
} while (identifier instanceof ArrayDeclarator);
} else if (declarator instanceof ArrayMember) {
identifier = declarator;
do {
identifier = identifier.children[0];
} while (identifier instanceof ArrayMember);
} else if (declarator instanceof Identifier) {
identifier = declarator;
} else {
return;