-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAstNodes.java
More file actions
881 lines (768 loc) · 28.7 KB
/
AstNodes.java
File metadata and controls
881 lines (768 loc) · 28.7 KB
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
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
abstract class AstNode extends Token implements SemanticNode {
protected static String indent(int indentLevel) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < indentLevel; i++) {
builder.append(" ");
}
return builder.toString();
}
protected static String joinLines(List<? extends AstNode> nodes, int indentLevel) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < nodes.size(); i++) {
builder.append(nodes.get(i).toString(indentLevel));
if (i + 1 < nodes.size()) {
builder.append("\n");
}
}
return builder.toString();
}
}
final class TypeHelpers {
private TypeHelpers() {
}
static TypeInfo fromPrimitive(String name) {
return TypeInfo.fromPrimitive(name);
}
static void ensureAssignable(TypeInfo target, TypeInfo source, String message) throws SemanticException {
if (!target.isAssignableFrom(source)) {
throw new SemanticException(message + ": cannot assign " + source + " to " + target);
}
}
static void ensureBoolCondition(TypeInfo type, String message) throws SemanticException {
if (!type.isBoolCoercible()) {
throw new SemanticException(message + ": condition must be bool-compatible, found " + type);
}
}
static void ensureNumeric(TypeInfo type, String message) throws SemanticException {
if (!type.isNumeric()) {
throw new SemanticException(message + ": numeric operand required, found " + type);
}
}
static void ensureBoolCompatible(TypeInfo type, String message) throws SemanticException {
if (!type.isBoolCoercible()) {
throw new SemanticException(message + ": bool or bool-coercible operand required, found " + type);
}
}
static TypeInfo numericResult(TypeInfo left, TypeInfo right) throws SemanticException {
if (!left.isNumeric() || !right.isNumeric()) {
throw new SemanticException("numeric operands required, found " + left + " and " + right);
}
if (left.getKind() == TypeInfo.Kind.FLOAT || right.getKind() == TypeInfo.Kind.FLOAT) {
return TypeInfo.FLOAT;
}
return TypeInfo.INT;
}
static TypeInfo equalityResult(TypeInfo left, TypeInfo right) throws SemanticException {
if (left.isNumeric() && right.isNumeric()) {
return TypeInfo.BOOL;
}
throw new SemanticException("numeric equality operands required, found " + left + " and " + right);
}
}
final class ClassDeclNode extends AstNode {
private final String name;
private final List<MemberDeclNode> members;
ClassDeclNode(String name, List<MemberDeclNode> members) {
this.name = name;
this.members = members == null ? Collections.emptyList() : new ArrayList<>(members);
}
@Override
public String toString(int indentLevel) {
StringBuilder builder = new StringBuilder();
builder.append("class ").append(name).append(" {\n");
for (int i = 0; i < members.size(); i++) {
builder.append(members.get(i).toString(indentLevel + 1));
if (i + 1 < members.size()) {
builder.append("\n");
}
builder.append("\n");
}
builder.append("}");
return builder.toString();
}
@Override
public TypeInfo typeCheck(SemanticContext context) throws SemanticException {
context.enterClass(name);
try {
for (MemberDeclNode member : members) {
member.register(context);
member.typeCheck(context);
}
} finally {
context.exitClass();
}
return TypeInfo.VOID;
}
}
abstract class MemberDeclNode extends AstNode {
protected final String name;
MemberDeclNode(String name) {
this.name = name;
}
abstract void register(SemanticContext context) throws SemanticException;
}
final class FieldDeclNode extends MemberDeclNode {
private final TypeInfo type;
private final boolean isFinal;
private final ExprNode initializer;
private final boolean isArray;
FieldDeclNode(String name, TypeInfo type, boolean isFinal, ExprNode initializer, boolean isArray) {
super(name);
this.type = type;
this.isFinal = isFinal;
this.initializer = initializer;
this.isArray = isArray;
}
@Override
void register(SemanticContext context) throws SemanticException {
TypeInfo declaredType = isArray ? TypeInfo.arrayOf(type) : type;
context.symbols().declare(name, SymbolTable.SymbolKind.FIELD, declaredType, isFinal, null, null);
}
@Override
public String toString(int indentLevel) {
StringBuilder builder = new StringBuilder();
builder.append(indent(indentLevel));
if (isFinal) {
builder.append("final ");
}
builder.append(type);
builder.append(isArray ? "[] " : " ");
builder.append(name);
if (initializer != null) {
builder.append(" = ").append(initializer.toString(0));
}
builder.append(";");
return builder.toString();
}
@Override
public TypeInfo typeCheck(SemanticContext context) throws SemanticException {
if (initializer != null) {
TypeInfo actual = initializer.typeCheck(context);
TypeInfo expected = isArray ? TypeInfo.arrayOf(type) : type;
TypeHelpers.ensureAssignable(expected, actual, "field initializer for " + name);
}
return TypeInfo.VOID;
}
}
final class MethodDeclNode extends MemberDeclNode {
private final TypeInfo returnType;
private final List<ParamDeclNode> params;
private final BlockStmtNode body;
private final boolean isVoid;
MethodDeclNode(TypeInfo returnType, String name, List<ParamDeclNode> params, BlockStmtNode body, boolean isVoid) {
super(name);
this.returnType = returnType;
this.params = params == null ? Collections.emptyList() : new ArrayList<>(params);
this.body = body;
this.isVoid = isVoid;
}
@Override
void register(SemanticContext context) throws SemanticException {
List<String> paramNames = new ArrayList<>();
List<TypeInfo> paramTypes = new ArrayList<>();
for (ParamDeclNode param : params) {
paramNames.add(param.name());
paramTypes.add(param.type());
}
context.symbols().declare(name, SymbolTable.SymbolKind.METHOD, isVoid ? TypeInfo.VOID : returnType, false,
paramNames, paramTypes);
}
@Override
public String toString(int indentLevel) {
StringBuilder builder = new StringBuilder();
builder.append(indent(indentLevel));
builder.append(isVoid ? "void" : returnType.toString());
builder.append(" ").append(name).append("(");
for (int i = 0; i < params.size(); i++) {
builder.append(params.get(i).toString(0));
if (i + 1 < params.size()) {
builder.append(", ");
}
}
builder.append(") ").append(body.toString(indentLevel));
return builder.toString();
}
@Override
public TypeInfo typeCheck(SemanticContext context) throws SemanticException {
context.enterMethod(name, isVoid ? TypeInfo.VOID : returnType);
try {
for (ParamDeclNode param : params) {
param.register(context);
}
body.typeCheck(context);
if (!isVoid && !body.containsReturn()) {
throw new SemanticException("missing return in method " + name);
}
} finally {
context.exitMethod();
}
return TypeInfo.VOID;
}
}
final class ParamDeclNode extends AstNode {
private final String name;
private final TypeInfo type;
private final boolean isArray;
ParamDeclNode(String name, TypeInfo type, boolean isArray) {
this.name = name;
this.type = type;
this.isArray = isArray;
}
TypeInfo type() {
return isArray ? TypeInfo.arrayOf(type) : type;
}
String name() {
return name;
}
void register(SemanticContext context) throws SemanticException {
context.symbols().declare(name, SymbolTable.SymbolKind.PARAMETER, type(), false, null, null);
}
@Override
public String toString(int indentLevel) {
return type + (isArray ? "[]" : "") + " " + name;
}
@Override
public TypeInfo typeCheck(SemanticContext context) {
return TypeInfo.VOID;
}
}
abstract class StmtNode extends AstNode {
boolean containsReturn() {
return false;
}
}
final class BlockStmtNode extends StmtNode {
private final List<VarDeclStmtNode> declarations;
private final List<StmtNode> statements;
BlockStmtNode(List<VarDeclStmtNode> declarations, List<StmtNode> statements) {
this.declarations = declarations == null ? Collections.emptyList() : new ArrayList<>(declarations);
this.statements = statements == null ? Collections.emptyList() : new ArrayList<>(statements);
}
@Override
public String toString(int indentLevel) {
StringBuilder builder = new StringBuilder();
builder.append("{\n");
for (VarDeclStmtNode declaration : declarations) {
builder.append(declaration.toString(indentLevel + 1)).append("\n");
}
for (StmtNode statement : statements) {
builder.append(statement.toString(indentLevel + 1)).append("\n");
}
builder.append(indent(indentLevel)).append("}");
return builder.toString();
}
@Override
public TypeInfo typeCheck(SemanticContext context) throws SemanticException {
context.enterBlock();
for (VarDeclStmtNode declaration : declarations) {
declaration.typeCheck(context);
}
for (StmtNode statement : statements) {
statement.typeCheck(context);
}
context.exitBlock();
return TypeInfo.VOID;
}
@Override
boolean containsReturn() {
for (StmtNode statement : statements) {
if (statement.containsReturn()) {
return true;
}
}
return false;
}
}
final class VarDeclStmtNode extends StmtNode {
private final String name;
private final TypeInfo type;
private final boolean isArray;
private final ExprNode initializer;
private final boolean isFinal;
VarDeclStmtNode(String name, TypeInfo type, boolean isArray, ExprNode initializer, boolean isFinal) {
this.name = name;
this.type = type;
this.isArray = isArray;
this.initializer = initializer;
this.isFinal = isFinal;
}
@Override
public String toString(int indentLevel) {
StringBuilder builder = new StringBuilder();
builder.append(indent(indentLevel));
if (isFinal) {
builder.append("final ");
}
builder.append(type);
builder.append(isArray ? "[] " : " ");
builder.append(name);
if (initializer != null) {
builder.append(" = ").append(initializer.toString(0));
}
builder.append(";");
return builder.toString();
}
@Override
public TypeInfo typeCheck(SemanticContext context) throws SemanticException {
TypeInfo declaredType = isArray ? TypeInfo.arrayOf(type) : type;
context.symbols().declare(name, SymbolTable.SymbolKind.LOCAL, declaredType, isFinal, null, null);
if (initializer != null) {
TypeInfo actual = initializer.typeCheck(context);
TypeHelpers.ensureAssignable(declaredType, actual, "local initializer for " + name);
}
return TypeInfo.VOID;
}
}
final class IfStmtNode extends StmtNode {
private final ExprNode condition;
private final StmtNode thenBranch;
private final StmtNode elseBranch;
IfStmtNode(ExprNode condition, StmtNode thenBranch, StmtNode elseBranch) {
this.condition = condition;
this.thenBranch = thenBranch;
this.elseBranch = elseBranch;
}
@Override
public String toString(int indentLevel) {
StringBuilder builder = new StringBuilder();
builder.append(indent(indentLevel)).append("if (").append(condition.toString(0)).append(") ");
builder.append(thenBranch.toString(indentLevel));
if (elseBranch != null) {
builder.append(" else ").append(elseBranch.toString(indentLevel));
}
return builder.toString();
}
@Override
public TypeInfo typeCheck(SemanticContext context) throws SemanticException {
TypeHelpers.ensureBoolCondition(condition.typeCheck(context), "if statement");
thenBranch.typeCheck(context);
if (elseBranch != null) {
elseBranch.typeCheck(context);
}
return TypeInfo.VOID;
}
@Override
boolean containsReturn() {
return elseBranch != null && thenBranch.containsReturn() && elseBranch.containsReturn();
}
}
final class WhileStmtNode extends StmtNode {
private final ExprNode condition;
private final StmtNode body;
WhileStmtNode(ExprNode condition, StmtNode body) {
this.condition = condition;
this.body = body;
}
@Override
public String toString(int indentLevel) {
return indent(indentLevel) + "while (" + condition.toString(0) + ") " + body.toString(indentLevel);
}
@Override
public TypeInfo typeCheck(SemanticContext context) throws SemanticException {
TypeHelpers.ensureBoolCondition(condition.typeCheck(context), "while statement");
body.typeCheck(context);
return TypeInfo.VOID;
}
}
final class AssignStmtNode extends StmtNode {
private final ExprNode target;
private final ExprNode value;
AssignStmtNode(ExprNode target, ExprNode value) {
this.target = target;
this.value = value;
}
@Override
public String toString(int indentLevel) {
return indent(indentLevel) + target.toString(0) + " = " + value.toString(0) + ";";
}
@Override
public TypeInfo typeCheck(SemanticContext context) throws SemanticException {
TypeInfo targetType = target.typeCheck(context);
TypeInfo valueType = value.typeCheck(context);
if (!target.isAssignable()) {
throw new SemanticException("assignment target is not assignable: " + target.toString(0));
}
if (target.isFinalTarget(context)) {
throw new SemanticException("cannot reassign final value: " + target.toString(0));
}
TypeHelpers.ensureAssignable(targetType, valueType, "assignment");
return TypeInfo.VOID;
}
}
final class ReadStmtNode extends StmtNode {
private final List<ExprNode> targets;
ReadStmtNode(List<ExprNode> targets) {
this.targets = targets == null ? Collections.emptyList() : new ArrayList<>(targets);
}
@Override
public String toString(int indentLevel) {
StringBuilder builder = new StringBuilder();
builder.append(indent(indentLevel)).append("read(");
for (int i = 0; i < targets.size(); i++) {
builder.append(targets.get(i).toString(0));
if (i + 1 < targets.size()) {
builder.append(", ");
}
}
builder.append(");");
return builder.toString();
}
@Override
public TypeInfo typeCheck(SemanticContext context) throws SemanticException {
for (ExprNode target : targets) {
TypeInfo type = target.typeCheck(context);
if (!target.isAssignable()) {
throw new SemanticException("read target is not assignable: " + target.toString(0));
}
if (target.isFinalTarget(context)) {
throw new SemanticException("cannot read into final value: " + target.toString(0));
}
if (type.getKind() == TypeInfo.Kind.ARRAY || type.getKind() == TypeInfo.Kind.VOID) {
throw new SemanticException("read target has invalid type: " + type);
}
}
return TypeInfo.VOID;
}
}
final class PrintStmtNode extends StmtNode {
private final List<ExprNode> values;
private final boolean line;
PrintStmtNode(List<ExprNode> values, boolean line) {
this.values = values == null ? Collections.emptyList() : new ArrayList<>(values);
this.line = line;
}
@Override
public String toString(int indentLevel) {
StringBuilder builder = new StringBuilder();
builder.append(indent(indentLevel)).append(line ? "printline(" : "print(");
for (int i = 0; i < values.size(); i++) {
builder.append(values.get(i).toString(0));
if (i + 1 < values.size()) {
builder.append(", ");
}
}
builder.append(");");
return builder.toString();
}
@Override
public TypeInfo typeCheck(SemanticContext context) throws SemanticException {
for (ExprNode value : values) {
TypeInfo type = value.typeCheck(context);
if (type.getKind() == TypeInfo.Kind.VOID || type.getKind() == TypeInfo.Kind.ARRAY) {
throw new SemanticException("print operand has invalid type: " + type);
}
}
return TypeInfo.VOID;
}
}
final class CallStmtNode extends StmtNode {
private final CallExprNode call;
CallStmtNode(String name, List<ExprNode> arguments) {
this.call = new CallExprNode(name, arguments);
}
@Override
public String toString(int indentLevel) {
return indent(indentLevel) + call.toString(0) + ";";
}
@Override
public TypeInfo typeCheck(SemanticContext context) throws SemanticException {
call.typeCheck(context);
return TypeInfo.VOID;
}
}
final class ReturnStmtNode extends StmtNode {
private final ExprNode value;
ReturnStmtNode(ExprNode value) {
this.value = value;
}
@Override
public String toString(int indentLevel) {
if (value == null) {
return indent(indentLevel) + "return;";
}
return indent(indentLevel) + "return " + value.toString(0) + ";";
}
@Override
public TypeInfo typeCheck(SemanticContext context) throws SemanticException {
TypeInfo expected = context.currentReturnType();
if (expected.getKind() == TypeInfo.Kind.VOID) {
if (value != null) {
throw new SemanticException("void method cannot return a value");
}
return TypeInfo.VOID;
}
if (value == null) {
throw new SemanticException("non-void method must return a value");
}
TypeInfo actual = value.typeCheck(context);
TypeHelpers.ensureAssignable(expected, actual, "return statement");
return TypeInfo.VOID;
}
@Override
boolean containsReturn() {
return true;
}
}
final class IncDecStmtNode extends StmtNode {
private final ExprNode target;
private final boolean increment;
IncDecStmtNode(ExprNode target, boolean increment) {
this.target = target;
this.increment = increment;
}
@Override
public String toString(int indentLevel) {
return indent(indentLevel) + target.toString(0) + (increment ? "++" : "--") + ";";
}
@Override
public TypeInfo typeCheck(SemanticContext context) throws SemanticException {
TypeInfo type = target.typeCheck(context);
if (!target.isAssignable()) {
throw new SemanticException("increment/decrement target is not assignable: " + target.toString(0));
}
if (target.isFinalTarget(context)) {
throw new SemanticException("cannot modify final value: " + target.toString(0));
}
if (!type.isNumeric()) {
String suffix = increment ? "++;" : "--;";
throw new SemanticException(context.errorPrefix() + "Cannot increment/decrement variable of type: " + type
+ " line: " + target.toString(0) + suffix);
}
return TypeInfo.VOID;
}
}
abstract class ExprNode extends AstNode {
boolean isAssignable() {
return false;
}
boolean isFinalTarget(SemanticContext context) {
return false;
}
}
final class LiteralExprNode extends ExprNode {
private final String literal;
private final TypeInfo type;
LiteralExprNode(String literal, TypeInfo type) {
this.literal = literal;
this.type = type;
}
@Override
public String toString(int indentLevel) {
return literal;
}
@Override
public TypeInfo typeCheck(SemanticContext context) {
return type;
}
}
final class NameExprNode extends ExprNode {
private final String name;
NameExprNode(String name) {
this.name = name;
}
@Override
public String toString(int indentLevel) {
return name;
}
@Override
public TypeInfo typeCheck(SemanticContext context) throws SemanticException {
SymbolTable.Symbol symbol = context.symbols().lookup(name);
if (symbol == null) {
throw new SemanticException("undeclared identifier: " + name);
}
if (symbol.kind == SymbolTable.SymbolKind.METHOD) {
throw new SemanticException("method identifier cannot be used as variable: " + name);
}
return symbol.type;
}
@Override
boolean isAssignable() {
return true;
}
@Override
boolean isFinalTarget(SemanticContext context) {
SymbolTable.Symbol symbol = context.symbols().lookup(name);
return symbol != null && symbol.isFinal;
}
}
final class IndexExprNode extends ExprNode {
private final ExprNode base;
private final ExprNode index;
IndexExprNode(ExprNode base, ExprNode index) {
this.base = base;
this.index = index;
}
@Override
public String toString(int indentLevel) {
return base.toString(0) + "[" + index.toString(0) + "]";
}
@Override
public TypeInfo typeCheck(SemanticContext context) throws SemanticException {
TypeInfo baseType = base.typeCheck(context);
TypeInfo indexType = index.typeCheck(context);
if (indexType.getKind() != TypeInfo.Kind.INT) {
throw new SemanticException("array index must be int, found " + indexType);
}
if (baseType.getKind() != TypeInfo.Kind.ARRAY) {
throw new SemanticException("indexed value is not an array: " + baseType);
}
return baseType.getElementType();
}
@Override
boolean isAssignable() {
return true;
}
@Override
boolean isFinalTarget(SemanticContext context) {
if (base instanceof NameExprNode) {
return ((NameExprNode) base).isFinalTarget(context);
}
return false;
}
}
final class CallExprNode extends ExprNode {
private final String name;
private final List<ExprNode> arguments;
CallExprNode(String name, List<ExprNode> arguments) {
this.name = name;
this.arguments = arguments == null ? Collections.emptyList() : new ArrayList<>(arguments);
}
@Override
public String toString(int indentLevel) {
StringBuilder builder = new StringBuilder();
builder.append(name).append("(");
for (int i = 0; i < arguments.size(); i++) {
builder.append(arguments.get(i).toString(0));
if (i + 1 < arguments.size()) {
builder.append(", ");
}
}
builder.append(")");
return builder.toString();
}
@Override
public TypeInfo typeCheck(SemanticContext context) throws SemanticException {
SymbolTable.Symbol symbol = context.symbols().lookupGlobal(name);
if (symbol == null || symbol.kind != SymbolTable.SymbolKind.METHOD) {
throw new SemanticException("undefined method: " + name);
}
if (symbol.parameterTypes.size() != arguments.size()) {
throw new SemanticException("argument count mismatch for " + name);
}
for (int i = 0; i < arguments.size(); i++) {
TypeInfo actual = arguments.get(i).typeCheck(context);
TypeInfo expected = symbol.parameterTypes.get(i);
TypeHelpers.ensureAssignable(expected, actual, "argument " + (i + 1) + " to " + name);
}
return symbol.type;
}
}
final class UnaryExprNode extends ExprNode {
private final String operator;
private final ExprNode operand;
private final TypeInfo castType;
UnaryExprNode(String operator, ExprNode operand) {
this(operator, operand, null);
}
UnaryExprNode(String operator, ExprNode operand, TypeInfo castType) {
this.operator = operator;
this.operand = operand;
this.castType = castType;
}
@Override
public String toString(int indentLevel) {
if (castType != null) {
return "(" + castType + ")" + operand.toString(0);
}
return operator + operand.toString(0);
}
@Override
public TypeInfo typeCheck(SemanticContext context) throws SemanticException {
TypeInfo type = operand.typeCheck(context);
if (castType != null) {
if (!castType.isScalar() || !type.isScalar()) {
throw new SemanticException("invalid cast from " + type + " to " + castType);
}
return castType;
}
if ("+".equals(operator) || "-".equals(operator)) {
TypeHelpers.ensureNumeric(type, "unary " + operator);
return type;
}
if ("~".equals(operator)) {
TypeHelpers.ensureBoolCompatible(type, "unary ~");
return TypeInfo.BOOL;
}
throw new SemanticException("unsupported unary operator: " + operator);
}
}
final class BinaryExprNode extends ExprNode {
private final ExprNode left;
private final String operator;
private final ExprNode right;
BinaryExprNode(ExprNode left, String operator, ExprNode right) {
this.left = left;
this.operator = operator;
this.right = right;
}
@Override
public String toString(int indentLevel) {
return "(" + left.toString(0) + " " + operator + " " + right.toString(0) + ")";
}
@Override
public TypeInfo typeCheck(SemanticContext context) throws SemanticException {
TypeInfo leftType = left.typeCheck(context);
TypeInfo rightType = right.typeCheck(context);
if ("&&".equals(operator) || "||".equals(operator)) {
TypeHelpers.ensureBoolCondition(leftType, "logical operator left operand");
TypeHelpers.ensureBoolCondition(rightType, "logical operator right operand");
return TypeInfo.BOOL;
}
if ("<".equals(operator) || "<=".equals(operator) || ">".equals(operator) || ">=".equals(operator)) {
TypeHelpers.ensureNumeric(leftType, "comparison left operand");
TypeHelpers.ensureNumeric(rightType, "comparison right operand");
return TypeInfo.BOOL;
}
if ("==".equals(operator) || "<>".equals(operator)) {
return TypeHelpers.equalityResult(leftType, rightType);
}
if ("+".equals(operator)) {
if ((leftType.getKind() == TypeInfo.Kind.STRING && rightType.isScalar())
|| (rightType.getKind() == TypeInfo.Kind.STRING && leftType.isScalar())) {
return TypeInfo.STRING;
}
return TypeHelpers.numericResult(leftType, rightType);
}
if ("-".equals(operator) || "*".equals(operator) || "/".equals(operator)) {
return TypeHelpers.numericResult(leftType, rightType);
}
throw new SemanticException("unsupported binary operator: " + operator);
}
}
final class TernaryExprNode extends ExprNode {
private final ExprNode condition;
private final ExprNode whenTrue;
private final ExprNode whenFalse;
TernaryExprNode(ExprNode condition, ExprNode whenTrue, ExprNode whenFalse) {
this.condition = condition;
this.whenTrue = whenTrue;
this.whenFalse = whenFalse;
}
@Override
public String toString(int indentLevel) {
return "(" + condition.toString(0) + " ? " + whenTrue.toString(0) + " : " + whenFalse.toString(0) + ")";
}
@Override
public TypeInfo typeCheck(SemanticContext context) throws SemanticException {
TypeHelpers.ensureBoolCondition(condition.typeCheck(context), "ternary condition");
TypeInfo trueType = whenTrue.typeCheck(context);
TypeInfo falseType = whenFalse.typeCheck(context);
if (trueType.isSameBase(falseType)) {
return trueType;
}
throw new SemanticException("ternary branches are incompatible: " + trueType + " and " + falseType);
}
}