-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.java.orig
2393 lines (2057 loc) · 66 KB
/
ast.java.orig
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
import java.io.*;
import java.util.*;
// **********************************************************************
// The ASTnode class defines the nodes of the abstract-syntax tree that
// represents a C-- program.
//
// Internal nodes of the tree contain pointers to children, organized
// either in a list (for nodes that may have a variable number of
// children) or as a fixed set of fields.
//
// The nodes for literals and ids contain line and character number
// information; for string literals and identifiers, they also contain a
// string; for integer literals, they also contain an integer value.
//
// Here are all the different kinds of AST nodes and what kinds of children
// they have. All of these kinds of AST nodes are subclasses of "ASTnode".
// Indentation indicates further subclassing:
//
// Subclass Kids
// -------- ----
// ProgramNode DeclListNode
// DeclListNode linked list of DeclNode
// DeclNode:
// VarDeclNode TypeNode, IdNode, int
// FnDeclNode TypeNode, IdNode, FormalsListNode, FnBodyNode
// FormalDeclNode TypeNode, IdNode
// StructDeclNode IdNode, DeclListNode
//
// FormalsListNode linked list of FormalDeclNode
// FnBodyNode DeclListNode, StmtListNode
// StmtListNode linked list of StmtNode
// ExpListNode linked list of ExpNode
//
// TypeNode:
// IntNode -- none --
// BoolNode -- none --
// VoidNode -- none --
// StructNode IdNode
//
// StmtNode:
// AssignStmtNode AssignNode
// PostIncStmtNode ExpNode
// PostDecStmtNode ExpNode
// ReadStmtNode ExpNode
// WriteStmtNode ExpNode
// IfStmtNode ExpNode, DeclListNode, StmtListNode
// IfElseStmtNode ExpNode, DeclListNode, StmtListNode,
// DeclListNode, StmtListNode
// WhileStmtNode ExpNode, DeclListNode, StmtListNode
// RepeatStmtNode ExpNode, DeclListNode, StmtListNode
// CallStmtNode CallExpNode
// ReturnStmtNode ExpNode
//
// ExpNode:
// IntLitNode -- none --
// StrLitNode -- none --
// TrueNode -- none --
// FalseNode -- none --
// IdNode -- none --
// DotAccessNode ExpNode, IdNode
// AssignNode ExpNode, ExpNode
// CallExpNode IdNode, ExpListNode
// UnaryExpNode ExpNode
// UnaryMinusNode
// NotNode
// BinaryExpNode ExpNode ExpNode
// PlusNode
// MinusNode
// TimesNode
// DivideNode
// AndNode
// OrNode
// EqualsNode
// NotEqualsNode
// LessNode
// GreaterNode
// LessEqNode
// GreaterEqNode
//
// Here are the different kinds of AST nodes again, organized according to
// whether they are leaves, internal nodes with linked lists of kids, or
// internal nodes with a fixed number of kids:
//
// (1) Leaf nodes:
// IntNode, BoolNode, VoidNode, IntLitNode, StrLitNode,
// TrueNode, FalseNode, IdNode
//
// (2) Internal nodes with (possibly empty) linked lists of children:
// DeclListNode, FormalsListNode, StmtListNode, ExpListNode
//
// (3) Internal nodes with fixed numbers of kids:
// ProgramNode, VarDeclNode, FnDeclNode, FormalDeclNode,
// StructDeclNode, FnBodyNode, StructNode, AssignStmtNode,
// PostIncStmtNode, PostDecStmtNode, ReadStmtNode, WriteStmtNode
// IfStmtNode, IfElseStmtNode, WhileStmtNode, CallStmtNode
// ReturnStmtNode, DotAccessNode, AssignExpNode, CallExpNode,
// UnaryExpNode, BinaryExpNode, UnaryMinusNode, NotNode,
// PlusNode, MinusNode, TimesNode, DivideNode,
// AndNode, OrNode, EqualsNode, NotEqualsNode,
// LessNode, GreaterNode, LessEqNode, GreaterEqNode
//
// **********************************************************************
// **********************************************************************
// ASTnode class (base class for all other kinds of nodes)
// **********************************************************************
abstract class ASTnode {
// every subclass must provide an unparse operation
abstract public void unparse(PrintWriter p, int indent);
// this method can be used by the unparse methods to do indenting
protected void doIndent(PrintWriter p, int indent) {
for (int k=0; k<indent; k++) p.print(" ");
}
}
// **********************************************************************
// ProgramNode, DeclListNode, FormalsListNode, FnBodyNode,
// StmtListNode, ExpListNode
// **********************************************************************
class ProgramNode extends ASTnode {
public ProgramNode(DeclListNode L) {
myDeclList = L;
}
/**
* nameAnalysis
* Creates an empty symbol table for the outermost scope, then processes
* all of the globals, struct defintions, and functions in the program.
*/
public void nameAnalysis() {
SymTable symTab = new SymTable();
myDeclList.nameAnalysis(symTab);
<<<<<<< HEAD
if((Sym s=symTab.lookupGlobal("main"))==null || ! s instanceOf FnSym )ErrMsg.fatal(0,0,"No main function");
=======
>>>>>>> eed1fc9eec8b3e061b1cf6aeb78fec7ef3d84598
}
/**
* typeCheck
*/
public void typeCheck() {
myDeclList.typeCheck();
}
public void unparse(PrintWriter p, int indent) {
myDeclList.unparse(p, indent);
}
// 1 kid
private DeclListNode myDeclList;
}
class DeclListNode extends ASTnode {
public DeclListNode(List<DeclNode> S) {
myDecls = S;
}
/**
* nameAnalysis
* Given a symbol table symTab, process all of the decls in the list.
*/
public void nameAnalysis(SymTable symTab) {
nameAnalysis(symTab, symTab);
}
/**
* nameAnalysis
* Given a symbol table symTab and a global symbol table globalTab
* (for processing struct names in variable decls), process all of the
* decls in the list.
*/
public void nameAnalysis(SymTable symTab, SymTable globalTab) {
for (DeclNode node : myDecls) {
if (node instanceof VarDeclNode) {
((VarDeclNode)node).nameAnalysis(symTab, globalTab);
} else {
node.nameAnalysis(symTab);
}
}
}
/**
* typeCheck
*/
public void typeCheck() {
for (DeclNode node : myDecls) {
node.typeCheck();
}
}
public void unparse(PrintWriter p, int indent) {
Iterator it = myDecls.iterator();
try {
while (it.hasNext()) {
((DeclNode)it.next()).unparse(p, indent);
}
} catch (NoSuchElementException ex) {
System.err.println("unexpected NoSuchElementException in DeclListNode.print");
System.exit(-1);
}
}
// list of kids (DeclNodes)
private List<DeclNode> myDecls;
}
class FormalsListNode extends ASTnode {
public FormalsListNode(List<FormalDeclNode> S) {
myFormals = S;
}
/**
* nameAnalysis
* Given a symbol table symTab, do:
* for each formal decl in the list
* process the formal decl
* if there was no error, add type of formal decl to list
*/
public List<Type> nameAnalysis(SymTable symTab) {
List<Type> typeList = new LinkedList<Type>();
<<<<<<< HEAD
int offset=0;
for (FormalDeclNode node : myFormals) {
Sym sym = node.nameAnalysis(symTab);
if (sym != null) {
sym.offset= offset;
offset+=4;
=======
for (FormalDeclNode node : myFormals) {
Sym sym = node.nameAnalysis(symTab);
if (sym != null) {
>>>>>>> eed1fc9eec8b3e061b1cf6aeb78fec7ef3d84598
typeList.add(sym.getType());
}
}
return typeList;
}
/**
* Return the number of formals in this list.
*/
public int length() {
return myFormals.size();
}
public void unparse(PrintWriter p, int indent) {
Iterator<FormalDeclNode> it = myFormals.iterator();
if (it.hasNext()) { // if there is at least one element
it.next().unparse(p, indent);
while (it.hasNext()) { // print the rest of the list
p.print(", ");
it.next().unparse(p, indent);
}
}
}
// list of kids (FormalDeclNodes)
private List<FormalDeclNode> myFormals;
}
class FnBodyNode extends ASTnode {
public FnBodyNode(DeclListNode declList, StmtListNode stmtList) {
myDeclList = declList;
myStmtList = stmtList;
}
/**
* nameAnalysis
* Given a symbol table symTab, do:
* - process the declaration list
* - process the statement list
*/
public void nameAnalysis(SymTable symTab) {
myDeclList.nameAnalysis(symTab);
myStmtList.nameAnalysis(symTab);
}
/**
* typeCheck
*/
public void typeCheck(Type retType) {
myStmtList.typeCheck(retType);
}
public void unparse(PrintWriter p, int indent) {
myDeclList.unparse(p, indent);
myStmtList.unparse(p, indent);
}
// 2 kids
private DeclListNode myDeclList;
private StmtListNode myStmtList;
}
class StmtListNode extends ASTnode {
public StmtListNode(List<StmtNode> S) {
myStmts = S;
}
/**
* nameAnalysis
* Given a symbol table symTab, process each statement in the list.
*/
public void nameAnalysis(SymTable symTab) {
for (StmtNode node : myStmts) {
node.nameAnalysis(symTab);
}
}
/**
* typeCheck
*/
public void typeCheck(Type retType) {
for(StmtNode node : myStmts) {
node.typeCheck(retType);
}
}
public void unparse(PrintWriter p, int indent) {
Iterator<StmtNode> it = myStmts.iterator();
while (it.hasNext()) {
it.next().unparse(p, indent);
}
}
// list of kids (StmtNodes)
private List<StmtNode> myStmts;
}
class ExpListNode extends ASTnode {
public ExpListNode(List<ExpNode> S) {
myExps = S;
}
public int size() {
return myExps.size();
}
/**
* nameAnalysis
* Given a symbol table symTab, process each exp in the list.
*/
public void nameAnalysis(SymTable symTab) {
for (ExpNode node : myExps) {
node.nameAnalysis(symTab);
}
}
/**
* typeCheck
*/
public void typeCheck(List<Type> typeList) {
int k = 0;
try {
for (ExpNode node : myExps) {
Type actualType = node.typeCheck(); // actual type of arg
if (!actualType.isErrorType()) { // if this is not an error
Type formalType = typeList.get(k); // get the formal type
if (!formalType.equals(actualType)) {
ErrMsg.fatal(node.lineNum(), node.charNum(),
"Type of actual does not match type of formal");
}
}
k++;
}
} catch (NoSuchElementException e) {
System.err.println("unexpected NoSuchElementException in ExpListNode.typeCheck");
System.exit(-1);
}
}
public void unparse(PrintWriter p, int indent) {
Iterator<ExpNode> it = myExps.iterator();
if (it.hasNext()) { // if there is at least one element
it.next().unparse(p, indent);
while (it.hasNext()) { // print the rest of the list
p.print(", ");
it.next().unparse(p, indent);
}
}
}
// list of kids (ExpNodes)
private List<ExpNode> myExps;
}
// **********************************************************************
// DeclNode and its subclasses
// **********************************************************************
abstract class DeclNode extends ASTnode {
/**
* Note: a formal decl needs to return a sym
*/
abstract public Sym nameAnalysis(SymTable symTab);
// default version of typeCheck for non-function decls
public void typeCheck() { }
}
class VarDeclNode extends DeclNode {
public VarDeclNode(TypeNode type, IdNode id, int size) {
myType = type;
myId = id;
mySize = size;
}
/**
* nameAnalysis (overloaded)
* Given a symbol table symTab, do:
* if this name is declared void, then error
* else if the declaration is of a struct type,
* lookup type name (globally)
* if type name doesn't exist, then error
* if no errors so far,
* if name has already been declared in this scope, then error
* else add name to local symbol table
*
* symTab is local symbol table (say, for struct field decls)
* globalTab is global symbol table (for struct type names)
* symTab and globalTab can be the same
*/
public Sym nameAnalysis(SymTable symTab) {
return nameAnalysis(symTab, symTab);
}
public Sym nameAnalysis(SymTable symTab, SymTable globalTab) {
boolean badDecl = false;
String name = myId.name();
Sym sym = null;
IdNode structId = null;
if (myType instanceof VoidNode) { // check for void type
ErrMsg.fatal(myId.lineNum(), myId.charNum(),
"Non-function declared void");
badDecl = true;
}
else if (myType instanceof StructNode) {
structId = ((StructNode)myType).idNode();
sym = globalTab.lookupGlobal(structId.name());
// if the name for the struct type is not found,
// or is not a struct type
if (sym == null || !(sym instanceof StructDefSym)) {
ErrMsg.fatal(structId.lineNum(), structId.charNum(),
"Invalid name of struct type");
badDecl = true;
}
else {
structId.link(sym);
}
}
if (symTab.lookupLocal(name) != null) {
ErrMsg.fatal(myId.lineNum(), myId.charNum(),
"Multiply declared identifier");
badDecl = true;
}
if (!badDecl) { // insert into symbol table
try {
if (myType instanceof StructNode) {
sym = new StructSym(structId);
}
else {
sym = new Sym(myType.type());
}
symTab.addDecl(name, sym);
myId.link(sym);
} catch (DuplicateSymException ex) {
System.err.println("Unexpected DuplicateSymException " +
" in VarDeclNode.nameAnalysis");
System.exit(-1);
} catch (EmptySymTableException ex) {
System.err.println("Unexpected EmptySymTableException " +
" in VarDeclNode.nameAnalysis");
System.exit(-1);
} catch (WrongArgumentException ex) {
System.err.println("Unexpected WrongArgumentException " +
" in VarDeclNode.nameAnalysis");
System.exit(-1);
}
}
return sym;
}
public void unparse(PrintWriter p, int indent) {
doIndent(p, indent);
myType.unparse(p, 0);
p.print(" ");
p.print(myId.name());
p.println(";");
}
// 3 kids
private TypeNode myType;
private IdNode myId;
private int mySize; // use value NOT_STRUCT if this is not a struct type
public static int NOT_STRUCT = -1;
}
class FnDeclNode extends DeclNode {
public FnDeclNode(TypeNode type,
IdNode id,
FormalsListNode formalList,
FnBodyNode body) {
myType = type;
myId = id;
myFormalsList = formalList;
myBody = body;
}
/**
* nameAnalysis
* Given a symbol table symTab, do:
* if this name has already been declared in this scope, then error
* else add name to local symbol table
* in any case, do the following:
* enter new scope
* process the formals
* if this function is not multiply declared,
* update symbol table entry with types of formals
* process the body of the function
* exit scope
*/
public Sym nameAnalysis(SymTable symTab) {
String name = myId.name();
FnSym sym = null;
if (symTab.lookupLocal(name) != null) {
ErrMsg.fatal(myId.lineNum(), myId.charNum(),
"Multiply declared identifier");
}
else { // add function name to local symbol table
try {
sym = new FnSym(myType.type(), myFormalsList.length());
symTab.addDecl(name, sym);
myId.link(sym);
} catch (DuplicateSymException ex) {
System.err.println("Unexpected DuplicateSymException " +
" in FnDeclNode.nameAnalysis");
System.exit(-1);
} catch (EmptySymTableException ex) {
System.err.println("Unexpected EmptySymTableException " +
" in FnDeclNode.nameAnalysis");
System.exit(-1);
} catch (WrongArgumentException ex) {
System.err.println("Unexpected WrongArgumentException " +
" in FnDeclNode.nameAnalysis");
System.exit(-1);
}
}
symTab.addScope(); // add a new scope for locals and params
// process the formals
List<Type> typeList = myFormalsList.nameAnalysis(symTab);
if (sym != null) {
sym.addFormals(typeList);
}
myBody.nameAnalysis(symTab); // process the function body
try {
symTab.removeScope(); // exit scope
} catch (EmptySymTableException ex) {
System.err.println("Unexpected EmptySymTableException " +
" in FnDeclNode.nameAnalysis");
System.exit(-1);
}
return null;
}
/**
* typeCheck
*/
public void typeCheck() {
myBody.typeCheck(myType.type());
}
public void unparse(PrintWriter p, int indent) {
doIndent(p, indent);
myType.unparse(p, 0);
p.print(" ");
p.print(myId.name());
p.print("(");
myFormalsList.unparse(p, 0);
p.println(") {");
myBody.unparse(p, indent+4);
p.println("}\n");
}
// 4 kids
private TypeNode myType;
private IdNode myId;
private FormalsListNode myFormalsList;
private FnBodyNode myBody;
}
class FormalDeclNode extends DeclNode {
public FormalDeclNode(TypeNode type, IdNode id) {
myType = type;
myId = id;
}
/**
* nameAnalysis
* Given a symbol table symTab, do:
* if this formal is declared void, then error
* else if this formal is already in the local symble table,
* then issue multiply declared error message and return null
* else add a new entry to the symbol table and return that Sym
*/
public Sym nameAnalysis(SymTable symTab) {
String name = myId.name();
boolean badDecl = false;
Sym sym = null;
if (myType instanceof VoidNode) {
ErrMsg.fatal(myId.lineNum(), myId.charNum(),
"Non-function declared void");
badDecl = true;
}
if (symTab.lookupLocal(name) != null) {
ErrMsg.fatal(myId.lineNum(), myId.charNum(),
"Multiply declared identifier");
badDecl = true;
}
if (!badDecl) { // insert into symbol table
try {
sym = new Sym(myType.type());
symTab.addDecl(name, sym);
myId.link(sym);
} catch (DuplicateSymException ex) {
System.err.println("Unexpected DuplicateSymException " +
" in VarDeclNode.nameAnalysis");
System.exit(-1);
} catch (EmptySymTableException ex) {
System.err.println("Unexpected EmptySymTableException " +
" in VarDeclNode.nameAnalysis");
System.exit(-1);
} catch (WrongArgumentException ex) {
System.err.println("Unexpected WrongArgumentException " +
" in VarDeclNode.nameAnalysis");
System.exit(-1);
}
}
return sym;
}
public void unparse(PrintWriter p, int indent) {
myType.unparse(p, 0);
p.print(" ");
p.print(myId.name());
}
// 2 kids
private TypeNode myType;
private IdNode myId;
}
class StructDeclNode extends DeclNode {
public StructDeclNode(IdNode id, DeclListNode declList) {
myId = id;
myDeclList = declList;
}
/**
* nameAnalysis
* Given a symbol table symTab, do:
* if this name is already in the symbol table,
* then multiply declared error (don't add to symbol table)
* create a new symbol table for this struct definition
* process the decl list
* if no errors
* add a new entry to symbol table for this struct
*/
public Sym nameAnalysis(SymTable symTab) {
String name = myId.name();
boolean badDecl = false;
if (symTab.lookupLocal(name) != null) {
ErrMsg.fatal(myId.lineNum(), myId.charNum(),
"Multiply declared identifier");
badDecl = true;
}
SymTable structSymTab = new SymTable();
// process the fields of the struct
myDeclList.nameAnalysis(structSymTab, symTab);
if (!badDecl) {
try { // add entry to symbol table
StructDefSym sym = new StructDefSym(structSymTab);
symTab.addDecl(name, sym);
myId.link(sym);
} catch (DuplicateSymException ex) {
System.err.println("Unexpected DuplicateSymException " +
" in StructDeclNode.nameAnalysis");
System.exit(-1);
} catch (EmptySymTableException ex) {
System.err.println("Unexpected EmptySymTableException " +
" in StructDeclNode.nameAnalysis");
System.exit(-1);
} catch (WrongArgumentException ex) {
System.err.println("Unexpected WrongArgumentException " +
" in StructDeclNode.nameAnalysis");
System.exit(-1);
}
}
return null;
}
public void unparse(PrintWriter p, int indent) {
doIndent(p, indent);
p.print("struct ");
p.print(myId.name());
p.println("{");
myDeclList.unparse(p, indent+4);
doIndent(p, indent);
p.println("};\n");
}
// 2 kids
private IdNode myId;
private DeclListNode myDeclList;
}
// **********************************************************************
// TypeNode and its Subclasses
// **********************************************************************
abstract class TypeNode extends ASTnode {
/* all subclasses must provide a type method */
abstract public Type type();
}
class IntNode extends TypeNode {
public IntNode() {
}
/**
* type
*/
public Type type() {
return new IntType();
}
public void unparse(PrintWriter p, int indent) {
p.print("int");
}
}
class BoolNode extends TypeNode {
public BoolNode() {
}
/**
* type
*/
public Type type() {
return new BoolType();
}
public void unparse(PrintWriter p, int indent) {
p.print("bool");
}
}
class VoidNode extends TypeNode {
public VoidNode() {
}
/**
* type
*/
public Type type() {
return new VoidType();
}
public void unparse(PrintWriter p, int indent) {
p.print("void");
}
}
class StructNode extends TypeNode {
public StructNode(IdNode id) {
myId = id;
}
public IdNode idNode() {
return myId;
}
/**
* type
*/
public Type type() {
return new StructType(myId);
}
public void unparse(PrintWriter p, int indent) {
p.print("struct ");
p.print(myId.name());
}
// 1 kid
private IdNode myId;
}
// **********************************************************************
// StmtNode and its subclasses
// **********************************************************************
abstract class StmtNode extends ASTnode {
abstract public void nameAnalysis(SymTable symTab);
abstract public void typeCheck(Type retType);
}
class AssignStmtNode extends StmtNode {
public AssignStmtNode(AssignNode assign) {
myAssign = assign;
}
/**
* nameAnalysis
* Given a symbol table symTab, perform name analysis on this node's child
*/
public void nameAnalysis(SymTable symTab) {
myAssign.nameAnalysis(symTab);
}
/**
* typeCheck
*/
public void typeCheck(Type retType) {
myAssign.typeCheck();
}
public void unparse(PrintWriter p, int indent) {
doIndent(p, indent);
myAssign.unparse(p, -1); // no parentheses
p.println(";");
}
// 1 kid
private AssignNode myAssign;
}
class PostIncStmtNode extends StmtNode {
public PostIncStmtNode(ExpNode exp) {
myExp = exp;
}
/**
* nameAnalysis
* Given a symbol table symTab, perform name analysis on this node's child
*/
public void nameAnalysis(SymTable symTab) {
myExp.nameAnalysis(symTab);
}
/**
* typeCheck
*/
public void typeCheck(Type retType) {
Type type = myExp.typeCheck();
if (!type.isErrorType() && !type.isIntType()) {
ErrMsg.fatal(myExp.lineNum(), myExp.charNum(),
"Arithmetic operator applied to non-numeric operand");
}
}
public void unparse(PrintWriter p, int indent) {
doIndent(p, indent);
myExp.unparse(p, 0);
p.println("++;");
}
// 1 kid
private ExpNode myExp;
}
class PostDecStmtNode extends StmtNode {
public PostDecStmtNode(ExpNode exp) {
myExp = exp;
}
/**
* nameAnalysis
* Given a symbol table symTab, perform name analysis on this node's child
*/
public void nameAnalysis(SymTable symTab) {
myExp.nameAnalysis(symTab);
}
/**
* typeCheck
*/
public void typeCheck(Type retType) {
Type type = myExp.typeCheck();
if (!type.isErrorType() && !type.isIntType()) {
ErrMsg.fatal(myExp.lineNum(), myExp.charNum(),
"Arithmetic operator applied to non-numeric operand");
}
}
public void unparse(PrintWriter p, int indent) {
doIndent(p, indent);
myExp.unparse(p, 0);
p.println("--;");
}
// 1 kid
private ExpNode myExp;
}
class ReadStmtNode extends StmtNode {
public ReadStmtNode(ExpNode e) {
myExp = e;
}
/**
* nameAnalysis
* Given a symbol table symTab, perform name analysis on this node's child
*/
public void nameAnalysis(SymTable symTab) {
myExp.nameAnalysis(symTab);
}
/**
* typeCheck
*/
public void typeCheck(Type retType) {
Type type = myExp.typeCheck();
if (type.isFnType()) {
ErrMsg.fatal(myExp.lineNum(), myExp.charNum(),
"Attempt to read a function");
}
if (type.isStructDefType()) {
ErrMsg.fatal(myExp.lineNum(), myExp.charNum(),
"Attempt to read a struct name");
}
if (type.isStructType()) {
ErrMsg.fatal(myExp.lineNum(), myExp.charNum(),
"Attempt to read a struct variable");
}
}
public void unparse(PrintWriter p, int indent) {
doIndent(p, indent);
p.print("cin >> ");
myExp.unparse(p, 0);
p.println(";");
}
// 1 kid (actually can only be an IdNode or an ArrayExpNode)
private ExpNode myExp;
}
class WriteStmtNode extends StmtNode {
public WriteStmtNode(ExpNode exp) {
myExp = exp;
}
/**
* nameAnalysis
* Given a symbol table symTab, perform name analysis on this node's child
*/
public void nameAnalysis(SymTable symTab) {
myExp.nameAnalysis(symTab);
}
/**
* typeCheck
*/
public void typeCheck(Type retType) {
Type type = myExp.typeCheck();