-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypechecker.java
More file actions
515 lines (423 loc) · 12.6 KB
/
Typechecker.java
File metadata and controls
515 lines (423 loc) · 12.6 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
import visitor.GJDepthFirst;
import syntaxtree.*;
public class Typechecker extends GJDepthFirst<String, String>{
private Taburu taburu;
public Typechecker(Taburu symboru){
this.taburu = symboru;
}
/**
* f0 -> "class"
* f1 -> Identifier()
* f2 -> "{"
* f3 -> "public"
* f4 -> "static"
* f5 -> "void"
* f6 -> "main"
* f7 -> "("
* f8 -> "String"
* f9 -> "["
* f10 -> "]"
* f11 -> Identifier()
* f12 -> ")"
* f13 -> "{"
* f14 -> ( VarDeclaration() )*
* f15 -> ( Statement() )*
* f16 -> "}"
* f17 -> "}"
*/
public String visit(MainClass n, String argu) throws Exception {
String class_name = n.f1.accept(this, ":businessasusual:");
// checking the statements of main is enough
n.f15.accept(this, "main:imamethodof:"+class_name);
return null;
}
/**
* f0 -> "class"
* f1 -> Identifier()
* f2 -> "{"
* f3 -> ( VarDeclaration() )*
* f4 -> ( MethodDeclaration() )*
* f5 -> "}"
*/
public String visit(ClassDeclaration n, String argu) throws Exception {
String class_name = n.f1.accept(this, ":businessasusual:");
// pass the class_name as well
n.f4.accept(this, class_name);
return null;
}
/**
* f0 -> "class"
* f1 -> Identifier()
* f2 -> "extends"
* f3 -> Identifier()
* f4 -> "{"
* f5 -> ( VarDeclaration() )*
* f6 -> ( MethodDeclaration() )*
* f7 -> "}"
*/
public String visit(ClassExtendsDeclaration n, String argu) throws Exception {
String class_name = n.f1.accept(this, ":businessasusual:");
// since superclass has been collected simply check method decls
n.f6.accept(this, class_name);
return null;
}
/**
* f0 -> "public"
* f1 -> Type()
* f2 -> Identifier()
* f3 -> "("
* f4 -> ( FormalParameterList() )?
* f5 -> ")"
* f6 -> "{"
* f7 -> ( VarDeclaration() )*
* f8 -> ( Statement() )*
* f9 -> "return"
* f10 -> Expression()
* f11 -> ";"
* f12 -> "}"
*/
public String visit(MethodDeclaration n, String argu) throws Exception {
// to check the return type is correct
String type_L = n.f1.accept(this, null);
String my_name = n.f2.accept(this, ":businessasusual:");
String type_R = n.f10.accept(this, my_name + ":imamethodof:"+ argu);
// check method statements
n.f8.accept(this, my_name + ":imamethodof:" + argu);
// throws Exception internally if there's an error
return taburu.whats_our_type(type_L, type_R);
}
/**
* f0 -> Identifier()
* f1 -> "="
* f2 -> Expression()
* f3 -> ";"
*/
public String visit(AssignmentStatement n, String argu) throws Exception {
String type_L = n.f0.accept(this, argu);
String type_R = n.f2.accept(this, argu);
// whats_our_type returns their type or exception if they mismatch
return taburu.whats_our_type(type_L, type_R);
}
/**
* f0 -> Identifier()
* f1 -> "["
* f2 -> Expression()
* f3 -> "]"
* f4 -> "="
* f5 -> Expression()
* f6 -> ";"
*/
public String visit(ArrayAssignmentStatement n, String argu) throws Exception {
String array_type = n.f0.accept(this, argu);
String array_index = n.f2.accept(this, argu);
String assgnmt_type = n.f5.accept(this, argu);
// check that index is an int
if (!array_index.equals("int"))
throw new Exception("Index of array has to be an integer.");
// integer array case
if (array_type.equals("int[]")&&assgnmt_type.equals("int")) {
return "int[]";
}
// boolean array
if (array_type.equals("boolean[]")&&assgnmt_type.equals("boolean")) {
return "boolean[]";
}
// wrong type if we reached this point
throw new Exception("Wrong array assignment types.");
}
/**[]
* f0 -> "if"
* f1 -> "("
* f2 -> Expression()
* f3 -> ")"
* f4 -> Statement()
* f5 -> "else"
* f6 -> Statement()
*/
public String visit(IfStatement n, String argu) throws Exception {
String _ret= n.f2.accept(this, argu);
if (_ret.equals("boolean")) {
n.f4.accept(this, argu);
n.f6.accept(this, argu);
return _ret;
}
throw new Exception("Condition isn't boolean.");
}
/**
* f0 -> "while"
* f1 -> "("
* f2 -> Expression()
* f3 -> ")"
* f4 -> Statement()
*/
public String visit(WhileStatement n, String argu) throws Exception {
String _ret= n.f2.accept(this, argu);
// check that condition is boolean
if (_ret.equals("boolean")) {
n.f4.accept(this, argu);
return _ret;
}
throw new Exception("Condition isn't boolean.");
}
/**
* f0 -> "System.out.println"
* f1 -> "("
* f2 -> Expression()
* f3 -> ")"
* f4 -> ";"
*/
public String visit(PrintStatement n, String argu) throws Exception {
String _ret = n.f2.accept(this, argu);
// this is based on the project's outline that print accepts ints only
if (_ret.equals("int")) return _ret;
throw new Exception("Println accepts only integers.");
}
/**
* f0 -> Expression()
* f1 -> ExpressionTail()
*/
public String visit(ExpressionList n, String argu) throws Exception {
String expr = n.f0.accept(this, argu);
if (n.f1!=null) expr+= n.f1.accept(this, argu);
return expr;
}
/**
* f0 -> ( ExpressionTerm() )*
*/
public String visit(ExpressionTail n, String argu) throws Exception {
String expr = "";
for (Node nodo : n.f0.nodes)
expr += "," +nodo.accept(this, argu);
return expr;
}
/**
* f0 -> ","
* f1 -> Expression()
*/
public String visit(ExpressionTerm n, String argu) throws Exception {
return n.f1.accept(this, argu);
}
public String visit(BooleanArrayType n, String argu) {
return "boolean[]";
}
public String visit(IntegerArrayType n, String argu) {
return "int[]";
}
public String visit(BooleanType n, String argu) {
return "boolean";
}
public String visit(IntegerType n, String argu) {
return "int";
}
/**
* f0 -> Clause()
* f1 -> "&&"
* f2 -> Clause()
*/
public String visit(AndExpression n, String argu) throws Exception {
String clause_L = n.f0.accept(this, argu);
String clause_R = n.f2.accept(this, argu);
if (clause_L.equals("boolean")&&clause_R.equals("boolean"))
return "boolean";
throw new Exception("And expression accepts boolean clauses.");
}
/**
* f0 -> PrimaryExpression()
* f1 -> "<"
* f2 -> PrimaryExpression()
*/
public String visit(CompareExpression n, String argu) throws Exception {
String prima_L = n.f0.accept(this, argu);
String prima_R = n.f2.accept(this, argu);
if (prima_L.equals("int")&&prima_R.equals("int"))
return "boolean";
throw new Exception("Comparison isn't between integers.");
}
/**
* f0 -> PrimaryExpression()
* f1 -> "+"
* f2 -> PrimaryExpression()
*/
public String visit(PlusExpression n, String argu) throws Exception {
String prima_L = n.f0.accept(this, argu);
String prima_R = n.f2.accept(this, argu);
if (prima_L.equals("int")&&prima_R.equals("int"))
return "int";
throw new Exception("Addition isn't between integers.");
}
/**
* f0 -> PrimaryExpression()
* f1 -> "-"
* f2 -> PrimaryExpression()
*/
public String visit(MinusExpression n, String argu) throws Exception {
String prima_L = n.f0.accept(this, argu);
String prima_R = n.f2.accept(this, argu);
if (prima_L.equals("int")&&prima_R.equals("int"))
return "int";
throw new Exception("Subtraction isn't between integers.");
}
/**
* f0 -> PrimaryExpression()
* f1 -> "*"
* f2 -> PrimaryExpression()
*/
public String visit(TimesExpression n, String argu) throws Exception {
String prima_L = n.f0.accept(this, argu);
String prima_R = n.f2.accept(this, argu);
if (prima_L.equals("int")&&prima_R.equals("int"))
return "int";
throw new Exception("Multiplication isn't between integers.");
}
/**
* f0 -> PrimaryExpression()
* f1 -> "["
* f2 -> PrimaryExpression()
* f3 -> "]"
*/
public String visit(ArrayLookup n, String argu) throws Exception {
String prima_L = n.f0.accept(this, argu);
String prima_R = n.f2.accept(this, argu);
if (!prima_R.equals("int"))
throw new Exception("Array index isn't an integer.");
if (prima_L.equals("int[]"))
return "int";
if (prima_L.equals("boolean[]"))
return "boolean";
throw new Exception("Wrong type, expected array.");
}
/**
* f0 -> PrimaryExpression()
* f1 -> "."
* f2 -> "length"
*/
public String visit(ArrayLength n, String argu) throws Exception {
String _ret = n.f0.accept(this, argu);
if (_ret.equals("int[]"))
return "int";
if (_ret.equals("boolean[]"))
return "int";
throw new Exception("Wrong type, expected array.");
}
/**
* f0 -> PrimaryExpression()
* f1 -> "."
* f2 -> Identifier()
* f3 -> "("
* f4 -> ( ExpressionList() )?
* f5 -> ")"
*/
public String visit(MessageSend n, String argu) throws Exception {
String object = n.f0.accept(this, argu);
// business as usual to get the actual string
String method = n.f2.accept(this, ":businessasusual:");
String[] arguments = n.f4.present()? n.f4.accept(this, argu).split(",") : new String[0];
// return type if they're correct otherwise throws exception
return taburu.call_me(method, object, arguments);
}
/**
* f0 -> IntegerLiteral()
* | TrueLiteral()
* | FalseLiteral()
* | Identifier()
* | ThisExpression()
* | ArrayAllocationExpression()
* | AllocationExpression()
* | BracketExpression()
*/
// to handle the above cases:
/**
* f0 -> "INTEGER_LITERAL"
*/
public String visit(IntegerLiteral n, String argu) throws Exception {
return "int";
}
/**
* f0 -> "true"
*/
public String visit(TrueLiteral n, String argu) throws Exception {
return "boolean";
}
/**
* f0 -> "false"
*/
public String visit(FalseLiteral n, String argu) throws Exception {
return "boolean";
}
/**
* f0 -> IDENTIFIER
*/
public String visit(Identifier n, String argu) throws Exception {
String identifier =n.f0.toString();
// so that it works by returning the string
if (argu==null||argu.equals(":businessasusual:"))
return identifier;
// else return the type with find_me function or throw exception
return taburu.find_me(identifier, argu);
}
/**
* f0 -> "this"
*/
public String visit(ThisExpression n, String argu) throws Exception {
String[] bettersafethansorry = argu.split(":imamethodof:");
return bettersafethansorry[1];
}
/**
* f0 -> "new"
* f1 -> "boolean"
* f2 -> "["
* f3 -> Expression()
* f4 -> "]"
*/
public String visit(BooleanArrayAllocationExpression n, String argu) throws Exception {
String _ret = n.f3.accept(this, argu);
if (_ret.equals("int")) return "boolean[]";
throw new Exception("Index of array isn't int.");
}
/**
* f0 -> "new"
* f1 -> "int"
* f2 -> "["
* f3 -> Expression()
* f4 -> "]"
*/
public String visit(IntegerArrayAllocationExpression n, String argu) throws Exception {
// apla elegxos an to expression einai ontos int
String _ret = n.f3.accept(this, argu);
if (_ret.equals("int")) return "int[]";
throw new Exception("Index of array isn't int.");
}
/**
* f0 -> "new"
* f1 -> Identifier()
* f2 -> "("
* f3 -> ")"
*/
public String visit(AllocationExpression n, String argu) throws Exception {
String _ret = n.f1.accept(this, argu);
if(taburu.get_class(_ret)!=null){
return _ret;
}
throw new Exception("This class doesn't exist.");
}
/**
* f0 -> "!"
* f1 -> Clause()
*/
public String visit(NotExpression n, String argu) throws Exception {
String _ret = n.f1.accept(this, argu);
if (_ret.equals("boolean")) {
return "boolean";
}
throw new Exception("Expected boolean.");
}
/**
* f0 -> "("
* f1 -> Expression()
* f2 -> ")"
*/
public String visit(BracketExpression n, String argu) throws Exception {
String _ret = n.f1.accept(this, argu);
return _ret;
}
}