-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSemanticAnalyzer.java
More file actions
912 lines (686 loc) · 23.1 KB
/
Copy pathSemanticAnalyzer.java
File metadata and controls
912 lines (686 loc) · 23.1 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
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
/* Author: Edric Orense
* File: SemanticAnalyzer.java
* Purpose: Goes through a SimpleJava file and checks for semantic errors
*/
import java.util.Vector;
public class SemanticAnalyzer implements ASTVisitor {
private TypeEnvironment typeEnv;
private VariableEnvironment varEnv;
private FunctionEnvironment funcEnv;
private String returnTypeStat;
private Label startLabel, endLabel;
private MachineDependent mSize;
private final int wordSize = mSize.WORDSIZE;
private int offset;
private AATBuildTree buildTree;
public SemanticAnalyzer(){
typeEnv = new TypeEnvironment();
varEnv = new VariableEnvironment();
funcEnv = new FunctionEnvironment();
funcEnv.addBuiltinFunctions();
buildTree = new AATBuildTree();
}
public Object VisitArrayVariable(ASTArrayVariable array) {
Type base, index;
TypeClass typeClass = null, baseTypeClass, indexTypeClass;
ArrayType arrayType;
baseTypeClass = (TypeClass) array.base().Accept(this);
indexTypeClass = (TypeClass) array.index().Accept(this);
base = baseTypeClass.type();
index = indexTypeClass.type();
if(base instanceof ArrayType){
arrayType = (ArrayType) base;
base = (Type) arrayType.type();
}else{
CompError.message(array.line(),
"Base variable must be of type array.");
}
if(!index.equals(IntegerType.instance())){
CompError.message(array.index().line(),
"Index of array must be of type int.");
}
AATExpression aatArray = buildTree.arrayVariable(baseTypeClass.tree(), indexTypeClass.tree(), wordSize);
typeClass = new TypeClass(base, aatArray);
return typeClass;
}
public Object VisitAssignmentStatement(ASTAssignmentStatement assign) {
TypeClass leftClass, rightClass;
Type left, right;
leftClass = (TypeClass) assign.variable().Accept(this);
rightClass = (TypeClass) assign.value().Accept(this);
left = leftClass.type();
right = rightClass.type();
if(!left.equals(right)){
CompError.message(assign.line(),
"Type mismatch error.");
}
return buildTree.assignmentStatement(leftClass.tree(), rightClass.tree());
}
public Object VisitBaseVariable(ASTBaseVariable base) {
VariableEntry varEnt;
Type type;
TypeClass typeClass = null;
varEnt = varEnv.find(base.name());
if(varEnt == null){
CompError.message(base.line()
, "Variable \"" + base.name() + "\" is not yet defined.");
type = IntegerType.instance();
}else{
type = varEnt.type();
typeClass = new TypeClass(type, buildTree.baseVariable(varEnt.offset()));
}
return typeClass;
}
public Object VisitBooleanLiteral(ASTBooleanLiteral boolliteral) {
int boolVal = 0;
if(boolliteral.value()){
boolVal = 1;
}
return new TypeClass(BooleanType.instance(), buildTree.constantExpression(boolVal));
}
public Object VisitClass(ASTClass classs) {
Type t;
VariableEnvironment instanceVars;
instanceVars = (VariableEnvironment) classs.variabledefs().Accept(this);
t = new ClassType(instanceVars);
typeEnv.insert(classs.name(), t);
return null;
}
public Object VisitClasses(ASTClasses classes) {
for(int i = 0; i < classes.size(); i++){
classes.elementAt(i).Accept(this);
}
return null;
}
public Object VisitClassVariable(ASTClassVariable classvariable) {
Type base, returnType;
ClassType classType;
TypeClass typeClass = null, baseTypeClass;
returnType = BooleanType.instance();
baseTypeClass = (TypeClass)classvariable.base().Accept(this);
base = baseTypeClass.type();
if(base instanceof ClassType){
classType = (ClassType) base;
if(classType.variables().find(classvariable.variable()) == null){
CompError.message(classvariable.line(),
classvariable.variable() + " is not a class member");
}else{
returnType = classType.variables().find(classvariable.variable()).type();
AATExpression classVarTree = buildTree.classVariable(baseTypeClass.tree(), classType.variables().find(classvariable.variable()).offset());
typeClass = new TypeClass(returnType, classVarTree);
}
}else{
CompError.message(classvariable.line(),
"Base variable must be a class type.");
}
return typeClass;
}
public Object VisitDoWhileStatement(ASTDoWhileStatement dowhile) {
Type test;
TypeClass testClass;
testClass = (TypeClass) dowhile.test().Accept(this);
test = testClass.type();
if(!test.equals(BooleanType.instance())){
CompError.message(dowhile.line()
, "Do while test must be of type boolean.");
}
AATStatement body = (AATStatement) dowhile.body().Accept(this);
return buildTree.dowhileStatement(testClass.tree(), body);
}
public Object VisitEmptyStatement(ASTEmptyStatement empty) {
return buildTree.emptyStatement();
}
public Object VisitForStatement(ASTForStatement forstmt) {
Type test;
AATStatement init = buildTree.emptyStatement(), inc = buildTree.emptyStatement(), body = buildTree.emptyStatement();
TypeClass testClass;
if(forstmt.initialize() != null){
init = (AATStatement) forstmt.initialize().Accept(this);
}
if(forstmt.increment() != null){
inc = (AATStatement) forstmt.increment().Accept(this);
}
testClass = (TypeClass) forstmt.test().Accept(this);
test = testClass.type();
if(!test.equals(BooleanType.instance())){
CompError.message(forstmt.line()
, "For test must be of type boolean.");
}
body = (AATStatement) forstmt.body().Accept(this);
return buildTree.forStatement(init, testClass.tree(), inc, body);
}
public Object VisitFormal(ASTFormal formal) {
return null;
}
public Object VisitFormals(ASTFormals formals) {
Vector v = new Vector(formals.size());
Vector v1 = new Vector(formals.size()); //keep type data
Vector v2 = new Vector(formals.size()); //keep ASTFormal
Type t;
int j = 0;
String key, keyPref;
for(int i = 0; i < formals.size(); i++){
t = typeEnv.find(formals.elementAt(i).type());
v2.addElement(formals.elementAt(i));
if(t == null){
CompError.message(formals.elementAt(i).line(),
"Type \"" + formals.elementAt(i).type() + "\" is not yet defined.");
t = IntegerType.instance();
}
if(formals.elementAt(i).arraydimension() != 0){
keyPref = t.toString();
for(j = 1; j <= formals.elementAt(i).arraydimension(); j++){
key = keyPref + Integer.toString(j) + "dim";
if (typeEnv.find(key) == null) {
t = new ArrayType(t);
typeEnv.insert(key, t);
}else{
t = typeEnv.find(key);
}
}
}
v1.addElement(t);
}
v.addElement(v1);
v.addElement(v2);
return v;
}
public Object VisitFunction(ASTFunction function) {
Vector v= null, v1 = null, v2 = null;
FunctionEntry funcEnt = funcEnv.find(function.name());
Type t;
varEnv.beginScope();
returnTypeStat = function.type();
t = typeEnv.find(function.type());
if(function.formals() != null){
v = (Vector) function.formals().Accept(this);
v1 = (Vector) v.elementAt(0);
v = (Vector) v.elementAt(1);
}
if(t == null){
CompError.message(function.line(),
"Type: " + function.type() + " is not yet defined.");
t = IntegerType.instance();
}
if(funcEnt == null){
startLabel = new Label(function.name());
endLabel = new Label(function.name());
funcEnt = new FunctionEntry(t, v1, startLabel, endLabel);
funcEnv.insert(function.name(), funcEnt);
}else{
startLabel = funcEnt.startlabel();
endLabel = funcEnt.endlabel();
if(!funcEnt.result().equals(t)){
CompError.message(function.line(), "Return type mismatch with function prototype.");
}
v2 = (Vector) funcEnt.formals().elementAt(1);
if(!compareFormals(v, v2)){
CompError.message(function.line(), "Function \"" + function.name() + "\" has arguments that do not match its prototype.");
}
}
addFormals(v);
AATStatement funcBody = (AATStatement) function.body().Accept(this);
int frameSize = varEnv.size();
varEnv.endScope();
return buildTree.functionDefinition(funcBody, frameSize, startLabel, endLabel);
}
private void addFormals(Vector formals){
varEnv.beginScope();
offset = -4;
ASTFormal currVar;
VariableEntry varEnt;
Type t;
String key, keyPref;
if (formals == null) {
return;
}
for(int i = 0; i < formals.size(); i++){
currVar = (ASTFormal) formals.elementAt(i);
t = typeEnv.find(currVar.type());
if(currVar.arraydimension() != 0){
keyPref = t.toString();
key = keyPref + Integer.toString(currVar.arraydimension()) + "dim";
t = typeEnv.find(key);
}
varEnt = new VariableEntry(t, offset);
varEnv.insert(currVar.name(), varEnt);
offset -= wordSize;
}
}
private boolean compareFormals(Vector a, Vector b){
ASTFormal elemA, elemB;
if(a == b){
return true;
}else if(a == null || b == null){
return false;
}
if(a.size() != b.size()){
return false;
}
if(a.size() < 1 && a.elementAt(1) instanceof Vector){
a = (Vector) a.elementAt(1);
}
if(b.size() < 1 && b.elementAt(1) instanceof Vector){
b = (Vector) b.elementAt(1);
}
for(int i = 0; i < a.size(); i++) {
elemA = (ASTFormal) a.elementAt(i);
elemB = (ASTFormal) b.elementAt(i);
if(!elemB.type().equals(elemA.type())
|| elemB.arraydimension() != elemA.arraydimension()){
return false;
}
}
return true;
}
public Object VisitFunctionCallExpression(
ASTFunctionCallExpression functioncall) {
FunctionEntry funcCall;
Vector v1, v2 = null, v3 = new Vector();
Type returnType;
if(functioncall.size() == 0){
v1 = null;
}else{
v1 = new Vector(functioncall.size());
v3 = new Vector(functioncall.size());
for (int i = 0; i < functioncall.size(); i++) {
TypeClass tc = (TypeClass) functioncall.elementAt(i).Accept(this);
v1.addElement(tc.type());
v3.addElement(tc.tree());
}
}
funcCall = funcEnv.find(functioncall.name());
if(funcCall == null){
CompError.message(functioncall.line(),
"Function " + functioncall.name() +" is not yet defined.");
returnType = IntegerType.instance();
}else{
if(funcCall.formals() != null){
if(funcCall.formals().size() == 0){
v2 = null;
}else{
if(funcCall.formals().elementAt(0) instanceof Vector){
v2 = (Vector) funcCall.formals().elementAt(0);
}else{
v2 = funcCall.formals();
}
}
}
returnType = funcCall.result();
if(v1 != v2 && v2 != null && v1 != null){
if (v1.size() != v2.size()) {
CompError.message(functioncall.line(),
"Argument count does not match");
}else{
for(int i = 0; i < functioncall.size(); i++){
if(!v1.elementAt(i).equals(v2.elementAt(i))){
CompError.message(functioncall.line(),
"Argument number " + (i + 1) + " does not match");
}
}
}
}
if((v1 == null || v2 == null) && v2 != v1){
CompError.message(functioncall.line(),
"Argument count does not match");
}
}
TypeClass typeClass = new TypeClass(returnType, buildTree.callExpression(v3, funcEnv.find(functioncall.name()).startlabel()));
return typeClass;
}
public Object VisitFunctionCallStatement(
ASTFunctionCallStatement functioncall) {
FunctionEntry funcCall;
Vector v1, v2 = null, v3 = null;
if(functioncall.size() == 0){
v1 = null;
v3 = new Vector();
}else{
v1 = new Vector(functioncall.size());
v3 = new Vector(functioncall.size());
for (int i = 0; i < functioncall.size(); i++) {
TypeClass tc = (TypeClass) functioncall.elementAt(i).Accept(this);
v1.addElement(tc.type());
v3.addElement(tc.tree());
}
}
funcCall = funcEnv.find(functioncall.name());
if(funcCall == null){
CompError.message(functioncall.line(),
"Function " + functioncall.name() +" is not yet defined.");
}else{
if(funcCall.result() != VoidType.instance()){
CompError.message(functioncall.line(),
"Function \"" + functioncall.name() + "\" is not a procedure.");
}
if(funcCall.formals() != null){
if(funcCall.formals().size() == 0){
v2 = null;
}else{
if(funcCall.formals().elementAt(0) instanceof Vector){
v2 = (Vector) funcCall.formals().elementAt(0);
}else{
v2 = funcCall.formals();
}
}
}
if(v1 != v2 && v2 != null && v1 != null){
if(v2.elementAt(0) instanceof Vector){
v2 = (Vector) v2.elementAt(0);
}
if(v1.elementAt(0) instanceof Vector){
v1 = (Vector) v1.elementAt(0);
}
if (v1.size() != v2.size()) {
CompError.message(functioncall.line(),
"Argument count does not match");
}else{
for(int i = 0; i < functioncall.size(); i++){
if(!v1.elementAt(i).equals(v2.elementAt(i))){
CompError.message(functioncall.line(),
"Argument number " + (i + 1) + " does not match");
}
}
}
}
if((v1 == null || v2 == null) && v2 != v1){
CompError.message(functioncall.line(),
"Argument count does not match");
}
}
return buildTree.callStatement(v3, funcEnv.find(functioncall.name()).startlabel());
}
public Object VisitIfStatement(ASTIfStatement ifsmt) {
Type test;
TypeClass testClass;
testClass = (TypeClass) ifsmt.test().Accept(this);
test = testClass.type();
if(!test.equals(BooleanType.instance())){
CompError.message(ifsmt.line()
, "If test must be of type boolean.");
}
AATStatement thenBody = (AATStatement) ifsmt.thenstatement().Accept(this);
AATStatement elseBody = buildTree.emptyStatement();
if(ifsmt.elsestatement() != null){
elseBody = (AATStatement) ifsmt.elsestatement().Accept(this);
}
return buildTree.ifStatement(testClass.tree(), thenBody, elseBody);
}
public Object VisitIntegerLiteral(ASTIntegerLiteral literal) {
TypeClass typeClass = new TypeClass(IntegerType.instance(), buildTree.constantExpression(literal.value()));
return typeClass;
}
public Object VisitInstanceVariableDef(ASTInstanceVariableDef variabledef) {
VariableEntry varEnt;
Type t = typeEnv.find(variabledef.type());
String key, keyPref;
int i;
if(t != null){
if(variabledef.arraydimension() != 0){
keyPref = t.toString();
for(i = 1; i <= variabledef.arraydimension(); i++){
key = keyPref + Integer.toString(i) + "dim";
if (typeEnv.find(key) == null) {
t = new ArrayType(t);
typeEnv.insert(key, t);
}else{
t = typeEnv.find(key);
}
}
}
varEnt = new VariableEntry(t, offset);
}else{
varEnt = new VariableEntry(IntegerType.instance());
CompError.message(variabledef.line(), "Type is not yet defined");
}
offset += wordSize;
return varEnt;
}
public Object VisitInstanceVariableDefs(ASTInstanceVariableDefs variabledefs) {
VariableEnvironment instanceVars = new VariableEnvironment();
VariableEntry varEnt;
String key;
offset = 0;
for(int i = 0; i < variabledefs.size(); i++){
varEnt = (VariableEntry) variabledefs.elementAt(i).Accept(this);
key = variabledefs.elementAt(i).name();
instanceVars.insert(key, varEnt);
}
return instanceVars;
}
public Object VisitNewArrayExpression(ASTNewArrayExpression newarray) {
Type elements, t;
TypeClass elementsClass;
String key, keyPref;
t = typeEnv.find(newarray.type());
elementsClass = (TypeClass) newarray.elements().Accept(this);
elements = elementsClass.type();
if(!elements.equals(IntegerType.instance())){
CompError.message(newarray.line(),
"Number of elements in array must be of type int.");
}
if(t != null){
if(newarray.arraydimension() != 0){
keyPref = t.toString();
key = keyPref + Integer.toString(newarray.arraydimension()) + "dim";
t = typeEnv.find(key);
if(t == null){
CompError.message(newarray.line(),
newarray.type() + " with dimensions " + newarray.arraydimension() + " is not yet defined.");
}
}
}else{
CompError.message(newarray.line(), "Type: " + newarray.type() + " is not yet defined");
}
AATExpression allocateSize = buildTree.operatorExpression(elementsClass.tree(), buildTree.constantExpression(wordSize), AATOperator.MULTIPLY);
return new TypeClass(t, buildTree.allocate(allocateSize));
}
public Object VisitNewClassExpression(ASTNewClassExpression newclass) {
Type type;
ClassType classType;
TypeClass typeClass;
int classSize;
AATExpression allocateSize;
type = typeEnv.find(newclass.type());
if(type == null){
CompError.message(newclass.line(),
"Class " + newclass.type() + " is not yet defined.");
type = IntegerType.instance();
classSize = wordSize;
allocateSize = buildTree.constantExpression(classSize);
}else{
classType = (ClassType) type;
classSize = classType.variables().size() * wordSize;
allocateSize = buildTree.constantExpression(classSize);
}
return new TypeClass(type, buildTree.allocate(allocateSize));
}
public Object VisitOperatorExpression(ASTOperatorExpression opexpr) {
Type left, right, returnType;
TypeClass leftClass, rightClass, typeClass;
leftClass = (TypeClass) opexpr.left().Accept(this);
rightClass = (TypeClass) opexpr.right().Accept(this);
left = leftClass.type();
right = rightClass.type();
if(opexpr.operator() == 5 ||
opexpr.operator() == 6){
if(!left.equals(BooleanType.instance())){
CompError.message(opexpr.left().line(),
"Left side of expression using \"&& or ||\" must be a boolean.");
}
if(!right.equals(BooleanType.instance())){
CompError.message(opexpr.right().line(),
"Right side of expression using \"&& or ||\" must be a boolean.");
}
returnType = BooleanType.instance();
}else{
if(!left.equals(IntegerType.instance())){
CompError.message(opexpr.left().line(),
"Left side of expression using \"+, -, *, /, <, >, >=, <= , != or ==\" must be an int.");
}
if(!right.equals(IntegerType.instance())){
CompError.message(opexpr.right().line(),
"Right side of expression using \"+, -, *, /, <, >, >=, <= , != or ==\" must be an int");
}
if(opexpr.operator() >= 1 && opexpr.operator() <= 4){
returnType = IntegerType.instance();
}else{
returnType = BooleanType.instance();
}
}
return new TypeClass(returnType, buildTree.operatorExpression(leftClass.tree(), rightClass.tree(), opexpr.operator()));
}
public Object VisitProgram(ASTProgram program) {
AATStatement prog = buildTree.emptyStatement();
if(program.classes() != null)
program.classes().Accept(this);
if(program.functiondefinitions() != null){
prog = (AATStatement) program.functiondefinitions().Accept(this);
}
return prog;
}
public Object VisitFunctionDefinitions(
ASTFunctionDefinitions functiondefinitions) {
AATStatement aatStat = buildTree.emptyStatement();
Vector v = new Vector();
for (int i = 0; i < functiondefinitions.size() ; i++) {
v.addElement(functiondefinitions.elementAt(i).Accept(this));
}
AATStatement seq = (AATStatement) v.elementAt(v.size() - 1);
for(int j = v.size() - 2; j >= 0; j--){
seq = buildTree.sequentialStatement((AATStatement)v.elementAt(j), seq);
}
return seq;
}
public Object VisitPrototype(ASTPrototype prototype) {
Type result = typeEnv.find(prototype.type());
FunctionEntry funcEnt;
Vector formals = null;
Label startLabel, endLabel;
if(prototype.formals() != null){
formals = (Vector)prototype.formals().Accept(this);
}
if(result == null){
CompError.message(prototype.line(),
"Type: " + prototype.type() + " is not yet defined.");
result = IntegerType.instance();
}
startLabel = new Label(prototype.name() + "START");
endLabel = new Label(prototype.name() + "END");
funcEnt = new FunctionEntry(result, formals, startLabel, endLabel);
if(funcEnv.find(prototype.name()) == null){
funcEnv.insert(prototype.name(), funcEnt);
}else{
CompError.message(prototype.line(), "Function prototype name: " + prototype.name() + " is already in use.");
}
return buildTree.emptyStatement();
}
public Object VisitReturnStatement(ASTReturnStatement ret) {
Type t, returnType;
TypeClass typeClass = null;
if(ret.value() == null){
t = VoidType.instance();
typeClass = new TypeClass(t, buildTree.constantExpression(0));
}else{
typeClass = (TypeClass) ret.value().Accept(this);
t = typeClass.type();
}
returnType = typeEnv.find(returnTypeStat);
if(returnType == null){
returnType = VoidType.instance();
}
if(!returnType.equals(t)){
CompError.message(ret.line(),
"Return must be of type " + returnTypeStat);
}
return buildTree.returnStatement(typeClass.tree(), endLabel);
}
public Object VisitStatements(ASTStatements statements) {
//varEnv.beginScope();
offset = 0;
Vector v = new Vector();
for(int i = 0; i < statements.size(); i++){
v.addElement(statements.elementAt(i).Accept(this));
}
AATStatement seq = (AATStatement) v.elementAt(v.size() - 1);
for(int j = v.size() - 2; j >= 0; j--){
seq = buildTree.sequentialStatement((AATStatement)v.elementAt(j), seq);
}
//varEnv.endScope();
return seq;
}
public Object VisitUnaryOperatorExpression(
ASTUnaryOperatorExpression operator) {
Type type;
TypeClass typeClass;
typeClass = (TypeClass) operator.operand().Accept(this);
type = typeClass.type();
if(!type.equals(BooleanType.instance())){
CompError.message(operator.line(),
"Operand for \"!\" must be a boolean.");
type = BooleanType.instance();
}
AATExpression unaryOp = buildTree.operatorExpression(typeClass.tree(), buildTree.constantExpression(1), AATOperator.MINUS);
return new TypeClass(type, unaryOp);
}
public Object VisitVariableDefStatement(ASTVariableDefStatement vardef) {
Type type, init;
String key, keyPref;
VariableEntry varEnt;
TypeClass initClass = null;
AATExpression base;
AATStatement varInit;
type = typeEnv.find(vardef.type());
if(type == null){
CompError.message(vardef.line(),
"Type \"" + vardef.type() + "\" is not yet defined.");
type = IntegerType.instance();
}
keyPref = type.toString();
if(vardef.arraydimension() != 0){
for(int j = 1; j <= vardef.arraydimension(); j++){
key = keyPref + Integer.toString(j) + "dim";
if (typeEnv.find(key) == null) {
type = new ArrayType(type);
typeEnv.insert(key, type);
}else{
type = typeEnv.find(key);
}
}
}
base = buildTree.baseVariable(offset);
if(vardef.init() != null){
initClass = (TypeClass) vardef.init().Accept(this);
init = initClass.type();
if(!init.equals(type)){
CompError.message(vardef.line(),
"Type mismatch error.");
}
varInit = buildTree.assignmentStatement(base, initClass.tree());
}else{
varInit = buildTree.emptyStatement();
}
varEnt = new VariableEntry(type, offset);
offset += wordSize;
varEnv.insert(vardef.name(), varEnt);
return varInit;
}
public Object VisitVariableExpression(
ASTVariableExpression variableexpression) {
TypeClass type;
type = (TypeClass) variableexpression.variable().Accept(this);
return type;
}
public Object VisitWhileStatement(ASTWhileStatement whilestatement) {
TypeClass testClass;
Type test;
testClass = (TypeClass) whilestatement.test().Accept(this);
test = testClass.type();
if(!test.equals(BooleanType.instance())){
CompError.message(whilestatement.line()
, "While test must be of type boolean.");
}
AATStatement body = (AATStatement) whilestatement.body().Accept(this);
return buildTree.whileStatement(testClass.tree(), body);
}
}