-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stmt.java
240 lines (214 loc) · 7.34 KB
/
Stmt.java
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
class Stmt extends SuperToken implements Token {
Expr expr;
Stmt stmt;
Stmt elseStmt;
Name name;
String ID;
int cond;
Readlist readList;
Printlist printList;
Printlinelist printlineList;
Args args;
String unaryOperator;
Fielddecls fieldDecls;
Stmts stmts;
boolean hasSemi;
public Stmt(Expr expr, Stmt stmt, Stmt ie){
this.expr = expr;
this.stmt = stmt;
this.elseStmt = ie;
cond = 1;
}
public Stmt(Expr expr, Stmt stmt){
this.expr = expr;
this.stmt = stmt;
cond = 2;
}
public Stmt(Name n, Expr expr){
this.name = n;
this.expr = expr;
cond = 3;
}
public Stmt(Readlist rl){
this.readList = rl;
cond = 4;
}
public Stmt(Printlist pl){
this.printList = pl;
cond = 5;
}
public Stmt(Printlinelist pll){
this.printlineList = pll;
cond = 6;
}
public Stmt(String id){
this.ID = id;
cond = 7;
}
public Stmt(String id, Args args){
this.ID = id;
this.args = args;
cond = 8;
}
public Stmt(){
cond = 9;
}
public Stmt(Expr expr){
this.expr = expr;
cond = 10;
}
public Stmt(Name name, String kind){
this.name = name;
this.unaryOperator = kind;
cond = 11;
}
public Stmt(Fielddecls fd, Stmts stmts, boolean hasSemi){
this.fieldDecls = fd;
this.stmts = stmts;
this.hasSemi = hasSemi;
cond = 12;
}
public String toString(int t){
String tabs = "";
for (int i = 0; i < t; ++i) {
tabs += "\t";
}
String ret = "";
switch (cond) {
case 1:
ret = tabs + "if (" + expr.toString(t) + ")\n" +
(stmt.cond == 12 ? stmt.toString(t) : tabs + "{\n" + stmt.toString(t+1) + tabs + "}\n") +
((elseStmt == null ? "" : tabs + "else\n" + ( elseStmt.cond == 12 ? elseStmt.toString(t) : tabs + "{\n" + elseStmt.toString(t+1) + tabs + "}\n")));
return ret;
case 2:
return tabs + "while (" + expr.toString(t) + ")\n" + (stmt.cond == 12 ? stmt.toString(t) : tabs + "{\n" + stmt.toString(t+1) + tabs + "}\n");
case 3:
return tabs + name.toString(t) + " = " + expr.toString(t) + ";\n";
case 4:
return tabs + "read(" + readList.toString(t) + ");\n";
case 5:
return tabs + "print(" + printList.toString(t) + ");\n";
case 6:
return tabs + "printline(" + printlineList.toString(t) + ");\n";
case 7:
return tabs + ID + "();\n";
case 8:
return tabs + ID + "(" + args.toString(t) + ");\n";
case 9:
return tabs + "return;\n";
case 10:
return tabs + "return " + expr.toString(t) + ";\n";
case 11:
return tabs + name.toString(t) + unaryOperator + ";\n";
case 12:
ret = tabs + "{\n";
ret += fieldDecls.toString(t+1);
ret += stmts.toString(t+1);
ret += tabs;
ret += "}";
ret += (hasSemi == false ? "\n" : ";\n");
return ret;
default:
return "";
}
}
public VarType typeCheck() throws Exception {
VarType stmtType;
SymbolTable.VarData var;
switch (cond){
case 1:
stmtType = expr.typeCheck();
if (!stmtType.equals(VarType.Bool) && !stmtType.equals(VarType.Int)) {
System.out.println("❌ Incompatible types: " + stmtType + " cannot be converted to Boolean");
System.exit(0);
}
typeCheckConditional(stmt);
if (elseStmt != null){
typeCheckConditional(elseStmt);
}
break;
case 2:
stmtType = expr.typeCheck();
if (!stmtType.equals(VarType.Bool) && !stmtType.equals(VarType.Int)) {
System.out.println("❌ Incompatible types: " + stmtType + " cannot be converted to Boolean");
System.exit(0);
}
typeCheckConditional(stmt);
break;
case 3:
VarType nameType = name.typeCheck();
if (!nameType.equals(expr.typeCheck())) {
System.out.println("❌ Fatal error: Incompatible types: " + expr.typeCheck() + " cannot be casted to " + nameType);
System.exit(0);
}
// TODO: fix final var from changing
var = symbolTable.findVar(name.ID);
if (var.isFinal) {
System.out.println("❌ Fatal error: Cannot reassign a value to final variable " + name.ID);
System.exit(0);
}
break;
case 4:
readList.typeCheck();
break;
case 5:
printList.typeCheck();
break;
case 6:
printlineList.typeCheck();
break;
case 7:
// check if function exists in scope
var = symbolTable.findVar(ID);
if (var == null || !var.isMethod ) {
System.out.println("❌ Fatal error: No method found by the name of " + ID);
System.exit(0);
}
break;
case 8:
var = symbolTable.findVar(ID);
if (var == null || !var.isMethod ) {
System.out.println("❌ Fatal error: No method found by the name of " + ID);
System.exit(0);
}
// check if method's arguments are compatible
args.typeCheck(ID, var.methodArgs);
break;
case 9:
// System.out.println(symbolTable.table.getFirst().);
setContainsRet(true);
setReturnType(VarType.Void);
return VarType.Void;
case 10:
// Boolean containsRet = getContainsRet();
// VarType prevReturnType = getReturnType();
VarType retType = expr.typeCheck();
setContainsRet(true);
setReturnType(retType);
return retType;
case 11:
if (!name.typeCheck().equals(VarType.Int)){
System.out.println("❌ Fatal error: bad operand type " + name.typeCheck() + " for unary operator " + unaryOperator);
System.exit(0);
}
return VarType.Int;
case 12:
typeCheckBlock();
default:
return null;
}
return null;
}
// case 12
private void typeCheckBlock() throws Exception{
symbolTable.prependScope();
fieldDecls.typeCheck();
stmts.typeCheck();
symbolTable.removeScope();
}
private void typeCheckConditional(Stmt stmt) throws Exception {
symbolTable.prependScope();
stmt.typeCheck();
symbolTable.removeScope();
}
}