-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTypeCheckVisitor.java
More file actions
526 lines (480 loc) · 18.2 KB
/
TypeCheckVisitor.java
File metadata and controls
526 lines (480 loc) · 18.2 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
import java.util.HashMap;
import java.util.Map;
public class TypeCheckVisitor implements Visitor {
private Environment<String, Type> variableEnv = new Environment<>();
private Environment<String, FunctionDeclaration> functionEnv = new Environment<>();
private static String mainFunctionName = "main";
@Override
public Type visit(Program p) throws SemanticException {
boolean mainChecked = false;
Function mainFunc = null;
Map<String, Function> funcMap = new HashMap<String, Function>();
int funcCount = p.getFunctionCount();
if (funcCount < 1) {
throw new SemanticException("Line [0, 0]: A program must contain at least one function");
}
for (int i = 0; i < funcCount; i++) {
Function f = p.getFunction(i);
functionEnv.add(f.decl.id.name, f.decl);
}
for (int i = 0; i < funcCount; i++)
{
Function f = p.getFunction(i);
String funcSig = f.functionNameSignature();
if (funcMap.get(funcSig) != null) {
// 2.1.3
throw new SemanticException("Line [%d, %d]: There are two functions have the same name: %s",
f.decl.type.line,
f.decl.type.pos,
f.decl.id.name
);
} else {
funcMap.put(funcSig, f);
}
if (f.decl.id.name.equals(TypeCheckVisitor.mainFunctionName)) {
mainChecked = true;
mainFunc = f;
}
f.accept(this);
}
// 2.1.2
if (mainChecked == false) {
throw new SemanticException("Line [0, 0]: There must be at least one function main");
}
// 2.1.2
if (mainFunc == null) {
throw new SemanticException("Line [0, 0]: There must be at least one function main");
}
// 2.1.2 a
if (mainFunc.decl.params != null && mainFunc.decl.params.getFormalParameterCount() != 0) {
throw new SemanticException("Line [%d, %d]: Main function must take no parameters",
mainFunc.decl.type.line,
mainFunc.decl.type.pos
);
}
// 2.1.2 b
if (!(mainFunc.decl.type instanceof VoidType)) {
throw new SemanticException("Line [%d, %d]: Main function must have a return type of `void`",
mainFunc.decl.type.line,
mainFunc.decl.type.pos
);
}
return null;
}
@Override
public Type visit(Function f) throws SemanticException {
variableEnv.beginScope();
Type returnType = (Type)f.decl.accept(this);
f.body.containingFunction = f;
f.body.accept(this);
variableEnv.endScope();
return returnType;
}
@Override
public Type visit(FunctionDeclaration d) throws SemanticException {
if (d.params != null) {
d.params.accept(this);
}
return d.type;
}
@Override
public Type visit(FormalParameterList l) throws SemanticException {
Map<String, FormalParameter> paramMap = new HashMap<String, FormalParameter>();
int listSize = l.getFormalParameterCount();
for (int i=0; i < listSize; i++)
{
FormalParameter p = l.getFormalParameter(i);
// 2.2.1
if (variableEnv.lookupInScope(p.id.name) != null) {
throw new SemanticException("Line [%d, %d]: No two parameters of a function may have the same name: %s",
p.type.line,
p.type.pos,
p.id.name
);
} else {
paramMap.put(p.id.name, p);
}
// 2.2.3
if (p.type instanceof VoidType) {
throw new SemanticException("Line [%d, %d]: No parameter may have a type of void: %s",
p.type.line,
p.type.pos,
p.id.name
);
}
p.accept(this);
}
return null;
}
@Override
public Type visit(FormalParameter p) throws SemanticException {
variableEnv.add(p.id.name, p.type);
return p.type;
}
@Override
public Type visit(FunctionBody f) throws SemanticException {
Map<String, VariableDeclaration> localVarMap = new HashMap<String, VariableDeclaration>();
for (int i=0; i< f.getVariableDeclarationCount() ; i++) {
VariableDeclaration v = f.getVariableDeclaration(i);
// 2.2.2
if (variableEnv.lookupInScope(v.id.name) != null) {
throw new SemanticException("Line [%d, %d]: No two local variables declared in a function may have the same name: %s",
v.type.line,
v.type.pos,
v.id.name
);
} else {
localVarMap.put(v.id.name, v);
}
// 2.2.4
if (v.type instanceof VoidType) {
throw new SemanticException("Line [%d, %d]: No local variable may have a type of void: %s",
v.type.line,
v.type.pos,
v.id.name
);
}
v.accept(this);
}
for (int i=0; i< f.getStatementCount() ; i++)
{
Statement s = f.getStatement(i);
if (s instanceof ReturnStatement) {
Type retType = (Type)s.accept(this);
if (!(retType.isSubType(f.containingFunction.decl.type))) {
throw new SemanticException("Line [%d, %d]: The type of an expression used in a return statement must be a subtype of the return type of the containing function",
retType.line,
retType.pos
);
}
} else {
s.accept(this);
}
}
return null;
}
@Override
public Type visit(VariableDeclaration v) throws SemanticException {
variableEnv.add(v.id.name, v.type);
return v.type;
}
@Override
public Type visit(Block b) throws SemanticException {
for (int i=0; i< b.getStatementCount(); i++)
{
Statement s = b.getStatement(i);
s.accept(this);
}
return null;
}
@Override
public Type visit(WhileStatement w) throws SemanticException {
Type exprType = (Type)w.expr.accept(this);
// 3.1.2
if (!(exprType instanceof BooleanType)) {
throw new SemanticException("Line [%d, %d]: An expression used as an while-statement condition must have type boolean",
exprType.line,
exprType.pos
);
}
w.block.accept(this);
return null;
}
@Override
public Type visit(IfStatement i) throws SemanticException {
Type exprType = (Type)i.e.accept(this);
// 3.1.1
if (!(exprType instanceof BooleanType)) {
throw new SemanticException("Line [%d, %d]: An expression used as an if-statement condition must have type boolean",
exprType.line,
exprType.pos
);
}
i.ifBlock.accept(this);
if (i.elseBlock != null) {
i.elseBlock.accept(this);
}
return null;
}
@Override
public Type visit(PrintStatement p) throws SemanticException {
Type t = (Type)p.e.accept(this);
// 3.1.3
if (t instanceof VoidType) {
throw new SemanticException("Line [%d, %d]: An expression used in a print-statement must have type in {int, float, char, string, boolean}",
t.line,
t.pos
);
}
return null;
}
@Override
public Type visit(PrintLnStatement p) throws SemanticException {
Type t = (Type)p.e.accept(this);
// 3.1.4
if (t instanceof VoidType) {
throw new SemanticException("Line [%d, %d]: An expression used in a println-statement must have type in {int, float, char, string, boolean}",
t.line,
t.pos
);
}
return null;
}
@Override
public Type visit(AssignmentStatement a) throws SemanticException {
Type varType = (Type)a.id.accept(this);
Type exprType = (Type)a.e.accept(this);
if (!(exprType.isSubType(varType))) {
throw new SemanticException("Line [%d, %d]: The type %s of an expression used in an assignment statment must be a subtype of the variable type %s",
exprType.line,
exprType.pos,
exprType,
varType
);
}
return null;
}
@Override
public Type visit(ArrayAssignment a) throws SemanticException {
Type arrayType = ((ArrayType)a.id.accept(this)).type;
Type indexType = (Type)a.e1.accept(this);
Type exprType = (Type)a.e2.accept(this);
if (!(indexType instanceof IntegerType)) {
throw new SemanticException("Line [%d, %d]: An array index expression must have type of int",
indexType.line,
indexType.pos
);
}
if (!(exprType.isSubType(arrayType))) {
throw new SemanticException("Line [%d, %d]: The type %s of an expression used in an array element assignment must be a subtype of the array type %s",
exprType.line,
exprType.pos,
exprType,
arrayType
);
}
return null;
}
@Override
public Type visit(ExpressionStatement e) throws SemanticException {
e.e.accept(this);
return null;
}
@Override
public Type visit(ReturnStatement r) throws SemanticException {
if (r.e != null) {
Type retType = (Type)r.e.accept(this);
return retType;
}
return new VoidType();
}
@Override
public Type visit(EmptyStatement s) throws SemanticException {
return null;
}
@Override
public Type visit(PlusExpression p) throws SemanticException {
Type typeA = (Type)p.e1.accept(this);
Type typeB = (Type)p.e2.accept(this);
if (typeA instanceof BooleanType || typeA instanceof VoidType || typeA instanceof ArrayType) {
throw new SemanticException("Line [%d, %d]: Binary operator '+' cannot be applied to operands of type %s", typeA.line, typeA.pos, typeA);
}
if (typeB instanceof BooleanType || typeB instanceof VoidType || typeB instanceof ArrayType) {
throw new SemanticException("Line [%d, %d]: Binary operator '+' cannot be applied to operands of type %s", typeB.line, typeB.pos, typeB);
}
if (!typeA.equals(typeB)) {
throw new SemanticException("Line [%d, %d]: Binary operator '+' cannot be applied to operands of type %s and %s", typeA.line, typeA.pos, typeA, typeB);
}
return typeA;
}
@Override
public Type visit(SubtractExpression p) throws SemanticException {
Type typeA = (Type)p.e1.accept(this);
Type typeB = (Type)p.e2.accept(this);
if (typeA instanceof StringType ||
typeA instanceof BooleanType ||
typeA instanceof VoidType ||
typeA instanceof ArrayType) {
throw new SemanticException("Line [%d, %d]: Binary operator '-' cannot be applied to operands of type %s", typeA.line, typeA.pos, typeA);
}
if (typeB instanceof StringType ||
typeB instanceof BooleanType ||
typeB instanceof VoidType ||
typeB instanceof ArrayType) {
throw new SemanticException("Line [%d, %d]: Binary operator '-' cannot be applied to operands of type %s", typeB.line, typeB.pos, typeB);
}
if (!typeA.equals(typeB)) {
throw new SemanticException("Line [%d, %d]: Binary operator '-' cannot be applied to operands of type %s and %s", typeA.line, typeA.pos, typeA, typeB);
}
return typeA;
}
@Override
public Type visit(MultExpression m) throws SemanticException {
Type typeA = (Type)m.e1.accept(this);
Type typeB = (Type)m.e2.accept(this);
if (!(typeA instanceof IntegerType) &&
!(typeA instanceof FloatType)) {
throw new SemanticException("Line [%d, %d]: Binary operator '*' cannot be applied to operands of type %s", typeA.line, typeA.pos, typeA);
}
if (!(typeB instanceof IntegerType) &&
!(typeB instanceof FloatType)) {
throw new SemanticException("Line [%d, %d]: Binary operator '*' cannot be applied to operands of type %s", typeA.line, typeA.pos, typeA);
}
if (!typeA.equals(typeB)) {
throw new SemanticException("Line [%d, %d]: Binary operator '*' cannot be applied to operands of type %s and %s", typeA.line, typeA.pos, typeA, typeB);
}
return typeA;
}
@Override
public Type visit(EqualityExpression e) throws SemanticException {
Type typeA = (Type)e.e1.accept(this);
Type typeB = (Type)e.e2.accept(this);
if (typeA instanceof VoidType || typeB instanceof VoidType) {
throw new SemanticException("Line [%d, %d]: Binary operator '==' cannot be applied to operands of type %s", typeA.line, typeA.pos, typeA);
}
if (!typeA.equals(typeB)) {
throw new SemanticException("Line [%d, %d]:Binary operator '==' cannot be applied to operands of type %s and %s",
typeA.line,
typeA.pos,
typeA,
typeB
);
}
return new BooleanType();
}
@Override
public Type visit(LessThanExpression l) throws SemanticException {
Type typeA = (Type)l.e1.accept(this);
Type typeB = (Type)l.e2.accept(this);
if (!typeA.equals(typeB)) {
throw new SemanticException("Line [%d, %d]: Binary operator '<' cannot be applied to operands of type %s and %s",
typeA.line, typeA.pos, typeA, typeB
);
}
return new BooleanType();
}
@Override
public Type visit(ArrayReference a) throws SemanticException {
Type indexType = (Type)a.e.accept(this);
if (!(indexType instanceof IntegerType)) {
throw new SemanticException("Line [%d, %d]: An array index expression must have type of int",
indexType.line,
indexType.pos
);
}
return ((ArrayType)a.id.accept(this)).type;
}
@Override
public Type visit(FunctionCall f) throws SemanticException {
if (f.exprList != null) {
f.exprList.accept(this);
}
FunctionDeclaration decl = functionEnv.lookup(f.id.id.name);
if (decl == null) {
return null;
}
if (decl.params != null) {
int defParamCount = decl.params.getFormalParameterCount();
int invocationParamCount = f.exprList.getExpressionCount();
if (defParamCount != invocationParamCount) {
// 3.2.3 a
throw new SemanticException("Line [%d, %d]: The number of arguments in the invocation must match the number of parameters %d in the function %s",
f.id.id.line,
f.id.id.pos,
defParamCount,
f.id.id.name
);
}
for (int i = 0; i < defParamCount; i++) {
Type defType = decl.params.getFormalParameter(i).type;
Type invoType = (Type)f.exprList.getExpression(i).accept(this);
if (!(invoType.isSubType(defType))) {
// 3.2.3 b
throw new SemanticException("Line [%d, %d]: Type of arguments must be convetible to type of declared parameters",
invoType.line,
invoType.pos
);
}
}
}
return decl.type;
}
@Override
public Type visit(IdentifierValue iv) throws SemanticException {
Type varType = variableEnv.lookupInScope(iv.id.name);
if (varType == null) {
throw new SemanticException("Line [%d, %d]: Local variable must be defined before being used: %s",
iv.id.line,
iv.id.pos,
iv.id.name
);
}
return varType;
}
@Override
public Type visit(IntegerLiteral il) throws SemanticException {
return new IntegerType();
}
@Override
public Type visit(FloatLiteral fl) throws SemanticException {
return new FloatType();
}
@Override
public Type visit(StringLiteral sl) throws SemanticException {
return new StringType();
}
@Override
public Type visit(CharacterLiteral cl) throws SemanticException {
return new CharType();
}
@Override
public Type visit(BooleanLiteral bl) throws SemanticException {
return new BooleanType();
}
@Override
public Type visit(ParenExpression p) throws SemanticException {
return (Type)p.e.accept(this);
}
@Override
public Type visit(ExpressionList l) throws SemanticException {
int listSize = l.getExpressionCount();
for (int i=0; i < listSize; i++)
{
Expression e = l.getExpression(i);
e.accept(this);
}
return null;
}
@Override
public Type visit(IntegerType type) throws SemanticException {
return type;
}
@Override
public Type visit(FloatType type) throws SemanticException {
return type;
}
@Override
public Type visit(StringType type) throws SemanticException {
return type;
}
@Override
public Type visit(CharType type) throws SemanticException {
return type;
}
@Override
public Type visit(VoidType type) throws SemanticException {
return type;
}
@Override
public Type visit(ArrayType type) throws SemanticException {
return type;
}
@Override
public Type visit(BooleanType type) throws SemanticException {
return type;
}
@Override
public Type visit(Identifier id) throws SemanticException {
return null;
}
}