-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTypeChecker.java
More file actions
299 lines (235 loc) · 8.84 KB
/
TypeChecker.java
File metadata and controls
299 lines (235 loc) · 8.84 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
/**
* Created by maximilian on 14.01.14.
* Checks out the (body-) type of the (hoe/)code
*/
/*import com.sun.java_cup.internal.runtime.virtual_parse_stack;
import com.sun.org.apache.regexp.internal.recompile;
import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;*/
import pascal.analysis.DepthFirstAdapter;
import pascal.node.*;
import java.util.HashMap;
public class TypeChecker extends DepthFirstAdapter {
private HashMap<String,String> symbols = new HashMap<String, String>();
private String eoast;
public HashMap<String , String> getSymbols(){
return symbols;
}
//put all declarations into the HashMap
@Override
public void caseADeclarationsAst(ADeclarationsAst node){
String []variable = node.getIdentifier().toString().toLowerCase().split(" ");
String type = node.getType().toString().toLowerCase().replaceAll("\\s+", "");
for(String tempVar : variable){
if(!symbols.containsKey(variable)){
symbols.put(tempVar, type);
}
else {
System.out.println("ERROR: Already declared "+tempVar+ " as "+symbols.get(variable));
System.exit(1);
}
}
}
//if number is in ast write it in eoast
public void caseANumberAst(ANumberAst node){
this.eoast = "integer";
}
//same for boolean
public void caseATrueAst(ATrueAst node){
this.eoast = "boolean";
}
public void caseAFalseAst(AFalseAst node){
this.eoast = "boolean";
}
//check if variable has been declared and throw an error if not
@Override
public void caseAIdentifierAst(AIdentifierAst node)
{
String identifier = node.getIdentifier().toString().toLowerCase().replaceAll("\\s+","");
if(!symbols.containsKey(identifier)){
System.out.println("ERROR: Identifier '"+ identifier+ "' has been declared, but not initialized");
System.exit(1);
}
this.eoast=symbols.get(identifier);
}
@Override
public void caseAAssignmentAst(AAssignmentAst node){
String identifier = node.getIdentifier().toString().toLowerCase().replaceAll("\\s+","");
String type = symbols.get(identifier);
if (!symbols.containsKey(identifier)){
System.out.println ("ERROR: Undeclared Variable " + identifier);
System.exit(1);
}
String expr = node.getAst().getClass().getSimpleName();
node.getAst().apply(this); //working its way through the AST
//check for true and false
if (expr.equals("ATrueAst") || expr.equals("AFalseAst")){
if(!type.equals("boolean")){
System.out.println("ERROR: Wrong type for " + identifier );
System.exit(1);
}
}
//in case of Number expr, is type Integer?
if (expr.equals("ANumberAst")){
if(!type.equals("integer")){
System.out.println("Expr: "+expr + " Type: " + type);
System.out.println("ERROR: Number was not assigned to Integer");
System.exit(1);
}
}
//check for arithmetic operations
if(expr.equals("APlusAst") || expr.equals("AMinusAst") || expr.equals("AMultAst") || expr.equals("ADivAst") || expr.equals("AUnMinusAst") || expr.equals("UnPlusAst")){
if (!type.equals("integer")){
System.out.println("ERROR: Integer was expected for " + identifier);
System.exit(1);
}
}
//same for boolean
if(expr.equals("AEqualAst") || expr.equals("AUnequalAst") || expr.equals("AXorAst") || expr.equals("AAndAst") || expr.equals("AOrAst") || expr.equals("ANotAst") || expr.equals("AComparisonAst")){
if(!type.equals("boolean")){
System.out.println("ERROR Boolean was expected for " + identifier);
System.exit(1);
}
}
}
//check all arithemtic types *all of them...*
//lets start with a big plus
@Override
public void caseAPlusAst(APlusAst node){
node.getLeft().apply(this);
String left = this.eoast;
node.getRight().apply(this);
String right = this.eoast;
if (!left.equals(right)){
System.out.println("ERROR: '+' expects Integer type");
System.exit(1);
}
}
@Override
public void caseAMinusAst(AMinusAst node){
node.getLeft().apply(this);
String left = this.eoast;
node.getRight().apply(this);
String right = this.eoast;
if (!left.equals(right)){
System.out.println("ERROR: '-' expects Integer type");
System.exit(1);
}
}
@Override
public void caseAMultAst(AMultAst node){
node.getLeft().apply(this);
String left = this.eoast;
node.getRight().apply(this);
String right = this.eoast;
if (!left.equals(right)){
System.out.println("ERROR: '*' expects Integer type");
System.exit(1);
}
}
@Override
public void caseADivAst(ADivAst node){
node.getLeft().apply(this);
String left = this.eoast;
node.getRight().apply(this);
String right = this.eoast;
if (!left.equals(right)){
System.out.println("ERROR: '/' expects Integer type");
System.exit(1);
}
}
@Override
public void caseAModAst(AModAst node){
node.getLeft().apply(this);
String left = this.eoast;
node.getRight().apply(this);
String right = this.eoast;
if (!left.equals(right)){
System.out.println("ERROR: 'mod' expects Integer type");
System.exit(1);
}
}
@Override
public void caseAUnPlusAst(AUnPlusAst node){
node.getAst().apply(this);
if(!eoast.equals("integer") && !node.getAst().getClass().getSimpleName().equals("AIdentifierAst") && !node.getAst().getClass().getSimpleName().equals("ANumberAst")){
System.out.println("ERROR: Unary Plus expects Integer");
System.exit(1);
}
}
@Override
public void caseAUnMinusAst(AUnMinusAst node){
node.getAst().apply(this);
if(!eoast.equals("integer") && !node.getAst().getClass().getSimpleName().equals("AIdentifierAst") && !node.getAst().getClass().getSimpleName().equals("ANumberAst")){
System.out.println("ERROR: Unary Minus expects Integer");
System.exit(1);
}
}
//same "thing" for booleans
//main thing first
@Override
public void caseAComparisonAst(AComparisonAst node){
node.getLeft().apply(this);
String left = this.eoast;
node.getRight().apply(this);
String right = this.eoast;
eoast = "boolean";
if(!left.equals(right)){
System.out.println("ERROR: wrong type for comparison");
System.exit(1);
}
}
@Override
public void caseAOrAst(AOrAst node){
node.getLeft().apply(this);
String left = this.eoast;
node.getRight().apply(this);
String right = this.eoast;
if (!left.equals(right)){
System.out.println("ERROR: Or expects Boolean");
System.exit(1);
}
}
@Override
public void caseAAndAst(AAndAst node){
node.getLeft().apply(this);
String left = this.eoast;
node.getRight().apply(this);
String right = this.eoast;
if (!left.equals(right)){
System.out.println("ERROR: And expects Boolean");
System.exit(1);
}
}
@Override
public void caseAXorAst(AXorAst node){
node.getLeft().apply(this);
String left = this.eoast;
node.getRight().apply(this);
String right = this.eoast;
if (!left.equals(right)){
System.out.println("ERROR: Xor expects Boolean");
System.exit(1);
}
}
@Override
public void caseANotAst(ANotAst node){
node.getAst().apply(this);
if (!eoast.equals("boolean")){
System.out.println("ERROR: Not expects Boolean");
System.exit(1);
}
}
//check if break is only used when a while was before. Maybe this is not the job of a type checker, but i think it is usefull
@Override
public void caseABreakAst(ABreakAst node){
Node parent = node.parent();
String parentName = parent.getClass().getSimpleName().replaceAll("\\s+","");
while (!parentName.equals("AProgramStartAst")){
if (parentName.equals("AOpenWhileStatementAst") || parentName.equals("AClosedWhileStatementAst"))return ;
parent = parent.parent();
parentName = parent.getClass().getSimpleName().replaceAll("\\s+","");
}
System.out.println("ERROR: 'break' was used without 'while' statement");
System.exit(1);
}
}