-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTypeChecker.java
More file actions
493 lines (465 loc) · 18 KB
/
TypeChecker.java
File metadata and controls
493 lines (465 loc) · 18 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
package cop5556sp18;
import cop5556sp18.Scanner.Kind;
import cop5556sp18.Scanner.Token;
import cop5556sp18.Types.Type;
import cop5556sp18.AST.ASTNode;
import cop5556sp18.AST.ASTVisitor;
import cop5556sp18.AST.Block;
import cop5556sp18.AST.Declaration;
import cop5556sp18.AST.ExpressionBinary;
import cop5556sp18.AST.ExpressionBooleanLiteral;
import cop5556sp18.AST.ExpressionConditional;
import cop5556sp18.AST.ExpressionFloatLiteral;
import cop5556sp18.AST.ExpressionFunctionAppWithExpressionArg;
import cop5556sp18.AST.ExpressionFunctionAppWithPixel;
import cop5556sp18.AST.ExpressionIdent;
import cop5556sp18.AST.ExpressionIntegerLiteral;
import cop5556sp18.AST.ExpressionPixel;
import cop5556sp18.AST.ExpressionPixelConstructor;
import cop5556sp18.AST.ExpressionPredefinedName;
import cop5556sp18.AST.ExpressionUnary;
import cop5556sp18.AST.LHSIdent;
import cop5556sp18.AST.LHSPixel;
import cop5556sp18.AST.LHSSample;
import cop5556sp18.AST.PixelSelector;
import cop5556sp18.AST.Program;
import cop5556sp18.AST.Statement;
import cop5556sp18.AST.StatementAssign;
import cop5556sp18.AST.StatementIf;
import cop5556sp18.AST.StatementInput;
import cop5556sp18.AST.StatementShow;
import cop5556sp18.AST.StatementSleep;
import cop5556sp18.AST.StatementWhile;
import cop5556sp18.AST.StatementWrite;
public class TypeChecker implements ASTVisitor {
SymbolTable symTab = new SymbolTable();
TypeChecker() {
}
@SuppressWarnings("serial")
public static class SemanticException extends Exception {
Token t;
public SemanticException(Token t, String message) {
super(message);
this.t = t;
}
}
// Name is only used for naming the output file.
// Visit the child block to type check program.
@Override
public Object visitProgram(Program program, Object arg) throws Exception {
program.block.visit(this, arg);
return null;
}
@Override
public Object visitBlock(Block block, Object arg) throws Exception {
// TODO Auto-generated method stub
symTab.enterScope();
for(ASTNode d: block.decsOrStatements)
{
if(d instanceof Declaration)
{
Declaration d1 = (Declaration) d;
d1.visit(this, arg);
}
else
{
Statement s1 = (Statement) d;
s1.visit(this, arg);
}
}
symTab.leaveScope();
return null;
}
@Override
public Object visitDeclaration(Declaration declaration, Object arg) throws Exception {
// TODO Auto-generated method stub
boolean b = symTab.insert(declaration.name, declaration);
if(!b){
throw new SemanticException(declaration.firstToken, "Error in visitDeclaration");
}
if((declaration.height == null && declaration.width != null) || (declaration.width == null && declaration.height != null))
{
throw new SemanticException(declaration.firstToken, "Error in visitDeclaration");
}
if(declaration.width != null && declaration.height != null)
{
Type t1 = (Type) declaration.width.visit(this, arg);
Type t2 = (Type) declaration.height.visit(this, arg);
if(t1.equals(Type.INTEGER) && t2.equals(Type.INTEGER) && declaration.type.equals(Kind.KW_image))
{
declaration.setType(Types.getType(declaration.type));
}
else
{
throw new SemanticException(declaration.firstToken, "Error in visitDeclaration");
}
}
else
{
declaration.setType(Types.getType(declaration.type));
}
return null;
}
@Override
public Object visitStatementWrite(StatementWrite statementWrite, Object arg) throws Exception {
// TODO Auto-generated method stub
Declaration sourceDec = symTab.lookup(statementWrite.sourceName);
Declaration destDec = symTab.lookup(statementWrite.destName);
if(sourceDec == null || destDec == null)
{
throw new SemanticException(statementWrite.firstToken, "Error in visitStatementWrite");
}
Type t1 = sourceDec.getType();
Type t2 = destDec.getType();
if(!t1.equals(Type.IMAGE) || !t2.equals(Type.FILE))
{
throw new SemanticException(statementWrite.firstToken, "Error in visitStatementWrite");
}
statementWrite.setSourceDec(sourceDec);
statementWrite.setDestDec(destDec);
return null;
}
@Override
public Object visitStatementInput(StatementInput statementInput, Object arg) throws Exception {
// TODO Auto-generated method stub
Declaration sourceDec = symTab.lookup(statementInput.destName);
if(sourceDec == null)
{
throw new SemanticException(statementInput.firstToken, "Error in visitStatementInput");
}
Type e = (Type) statementInput.e.visit(this, arg);
if(!e.equals(Type.INTEGER))
{
throw new SemanticException(statementInput.firstToken, "Error in visitStatementInput");
}
statementInput.setDec(sourceDec);
return null;
}
@Override
public Object visitPixelSelector(PixelSelector pixelSelector, Object arg) throws Exception {
// TODO Auto-generated method stub
Type t1 = (Type) pixelSelector.ex.visit(this, arg);
Type t2 = (Type) pixelSelector.ey.visit(this, arg);
if(!t1.equals(t2))
{
throw new SemanticException(pixelSelector.firstToken, "Error in visitPixelSelector");
}
else
{
if(!t1.equals(Type.INTEGER) && !t1.equals(Type.FLOAT))
{
throw new SemanticException(pixelSelector.firstToken, "Error in visitPixelSelector");
}
}
return null;
}
@Override
public Object visitExpressionConditional(ExpressionConditional expressionConditional, Object arg) throws Exception {
// TODO Auto-generated method stub
Type t1 = (Type) expressionConditional.guard.visit(this, arg);
if(!t1.equals(Type.BOOLEAN))
{
throw new SemanticException(expressionConditional.firstToken, "Error in visitExpressionConditional");
}
Type t2 = (Type) expressionConditional.trueExpression.visit(this, arg);
Type t3 = (Type) expressionConditional.falseExpression.visit(this, arg);
if(!t2.equals(t3))
{
throw new SemanticException(expressionConditional.firstToken, "Error in visitExpressionConditional");
}
expressionConditional.setType(t2);
return expressionConditional.getType();
}
@Override
public Object visitExpressionBinary(ExpressionBinary expressionBinary, Object arg) throws Exception {
// TODO Auto-generated method stub
Type t1 = (Type) expressionBinary.leftExpression.visit(this, arg);
Type t2 = (Type) expressionBinary.rightExpression.visit(this, arg);
Kind op = expressionBinary.op;
if(t1.equals(Type.INTEGER) && t2.equals(Type.INTEGER) && (op.equals(Kind.OP_PLUS) || op.equals(Kind.OP_MINUS) || op.equals(Kind.OP_TIMES) || op.equals(Kind.OP_DIV) || op.equals(Kind.OP_POWER) || op.equals(Kind.OP_MOD) || op.equals(Kind.OP_AND) || op.equals(Kind.OP_OR)))
{
expressionBinary.setType(Type.INTEGER);
}
else if(t1.equals(Type.INTEGER) && t2.equals(Type.INTEGER) && (op.equals(Kind.OP_EQ) || op.equals(Kind.OP_NEQ) || op.equals(Kind.OP_GT) || op.equals(Kind.OP_GE) || op.equals(Kind.OP_LT) || op.equals(Kind.OP_LE)))
{
expressionBinary.setType(Type.BOOLEAN);
}
else if(t1.equals(Type.FLOAT) && t2.equals(Type.FLOAT) && (op.equals(Kind.OP_PLUS) || op.equals(Kind.OP_MINUS) || op.equals(Kind.OP_TIMES) || op.equals(Kind.OP_DIV) || op.equals(Kind.OP_POWER)))
{
expressionBinary.setType(Type.FLOAT);
}
else if(t1.equals(Type.FLOAT) && t2.equals(Type.FLOAT) && (op.equals(Kind.OP_EQ) || op.equals(Kind.OP_NEQ) || op.equals(Kind.OP_GT) || op.equals(Kind.OP_GE) || op.equals(Kind.OP_LT) || op.equals(Kind.OP_LE)))
{
expressionBinary.setType(Type.BOOLEAN);
}
else if(((t1.equals(Type.FLOAT) && t2.equals(Type.INTEGER)) || (t1.equals(Type.INTEGER) && t2.equals(Type.FLOAT))) && (op.equals(Kind.OP_PLUS) || op.equals(Kind.OP_MINUS) || op.equals(Kind.OP_TIMES) || op.equals(Kind.OP_DIV) || op.equals(Kind.OP_POWER)))
{
expressionBinary.setType(Type.FLOAT);
}
else if(t1.equals(Type.BOOLEAN) && t2.equals(Type.BOOLEAN) && (op.equals(Kind.OP_AND) || op.equals(Kind.OP_OR) || op.equals(Kind.OP_GT) || op.equals(Kind.OP_GE) || op.equals(Kind.OP_LE) || op.equals(Kind.OP_LT) || op.equals(Kind.OP_EQ) || op.equals(Kind.OP_NEQ)))
{
expressionBinary.setType(Type.BOOLEAN);
}
else
{
throw new SemanticException(expressionBinary.firstToken, "Error in visitExpressionBinary");
}
return expressionBinary.getType();
}
@Override
public Object visitExpressionUnary(ExpressionUnary expressionUnary, Object arg) throws Exception {
// TODO Auto-generated method stub
Type t1 = (Type) expressionUnary.expression.visit(this, arg);
if((expressionUnary.op.equals(Kind.OP_PLUS) && t1.equals(Type.BOOLEAN)) || (expressionUnary.op.equals(Kind.OP_MINUS) && t1.equals(Type.BOOLEAN)) || (expressionUnary.op.equals(Kind.OP_EXCLAMATION) && t1.equals(Type.FLOAT)) || t1.equals(Type.IMAGE) || t1.equals(Type.FILE))
{
throw new SemanticException(expressionUnary.firstToken, "Error in visitExpressionUnary");
}
expressionUnary.setType(t1);
return expressionUnary.getType();
}
@Override
public Object visitExpressionIntegerLiteral(ExpressionIntegerLiteral expressionIntegerLiteral, Object arg)
throws Exception {
// TODO Auto-generated method stub
expressionIntegerLiteral.setType(Type.INTEGER);
return expressionIntegerLiteral.getType();
}
@Override
public Object visitBooleanLiteral(ExpressionBooleanLiteral expressionBooleanLiteral, Object arg) throws Exception {
// TODO Auto-generated method stub
expressionBooleanLiteral.setType(Type.BOOLEAN);
return expressionBooleanLiteral.getType();
}
@Override
public Object visitExpressionPredefinedName(ExpressionPredefinedName expressionPredefinedName, Object arg)
throws Exception {
// TODO Auto-generated method stub
expressionPredefinedName.setType(Type.INTEGER);
return expressionPredefinedName.getType();
}
@Override
public Object visitExpressionFloatLiteral(ExpressionFloatLiteral expressionFloatLiteral, Object arg)
throws Exception {
// TODO Auto-generated method stub
expressionFloatLiteral.setType(Type.FLOAT);
return expressionFloatLiteral.getType();
}
@Override
public Object visitExpressionFunctionAppWithExpressionArg(
ExpressionFunctionAppWithExpressionArg expressionFunctionAppWithExpressionArg, Object arg)
throws Exception {
// TODO Auto-generated method stub
Type t1 = (Type) expressionFunctionAppWithExpressionArg.e.visit(this, arg);
Kind func = expressionFunctionAppWithExpressionArg.function;
if(t1.equals(Type.INTEGER) && (func.equals(Kind.KW_abs) || func.equals(Kind.KW_red) || func.equals(Kind.KW_green) || func.equals(Kind.KW_blue) || func.equals(Kind.KW_alpha) || func.equals(Kind.KW_int)))
{
expressionFunctionAppWithExpressionArg.setType(Type.INTEGER);
}
else if(t1.equals(Type.INTEGER) && func.equals(Kind.KW_float))
{
expressionFunctionAppWithExpressionArg.setType(Type.FLOAT);
}
else if(t1.equals(Type.FLOAT) && (func.equals(Kind.KW_abs) || func.equals(Kind.KW_atan) || func.equals(Kind.KW_cos) || func.equals(Kind.KW_sin) || func.equals(Kind.KW_log) || func.equals(Kind.KW_float)))
{
expressionFunctionAppWithExpressionArg.setType(Type.FLOAT);
}
else if(t1.equals(Type.FLOAT) && func.equals(Kind.KW_int))
{
expressionFunctionAppWithExpressionArg.setType(Type.INTEGER);
}
else if(t1.equals(Type.IMAGE) && (func.equals(Kind.KW_width) || func.equals(Kind.KW_height)))
{
expressionFunctionAppWithExpressionArg.setType(Type.INTEGER);
}
else
{
throw new SemanticException(expressionFunctionAppWithExpressionArg.firstToken, "Error in visitExpressionFunctionAppWithExpressionArg");
}
return expressionFunctionAppWithExpressionArg.getType();
}
@Override
public Object visitExpressionFunctionAppWithPixel(ExpressionFunctionAppWithPixel expressionFunctionAppWithPixel,
Object arg) throws Exception {
// TODO Auto-generated method stub
Type t1 = (Type) expressionFunctionAppWithPixel.e0.visit(this, arg);
Type t2 = (Type) expressionFunctionAppWithPixel.e1.visit(this, arg);
Kind func = expressionFunctionAppWithPixel.name;
if((func.equals(Kind.KW_cart_x) || func.equals(Kind.KW_cart_y)) && t1.equals(Type.FLOAT) && t2.equals(Type.FLOAT))
{
expressionFunctionAppWithPixel.setType(Type.INTEGER);
}
else if((func.equals(Kind.KW_polar_a) || func.equals(Kind.KW_polar_r)) && t1.equals(Type.INTEGER) && t2.equals(Type.INTEGER))
{
expressionFunctionAppWithPixel.setType(Type.FLOAT);
}
else
{
throw new SemanticException(expressionFunctionAppWithPixel.firstToken, "Error in visitExpressionFunctionAppWithPixel");
}
return expressionFunctionAppWithPixel.getType();
}
@Override
public Object visitExpressionPixelConstructor(ExpressionPixelConstructor expressionPixelConstructor, Object arg)
throws Exception {
// TODO Auto-generated method stub
Type alpha = (Type) expressionPixelConstructor.alpha.visit(this, arg);
Type blue = (Type) expressionPixelConstructor.blue.visit(this, arg);
Type green = (Type) expressionPixelConstructor.green.visit(this, arg);
Type red = (Type) expressionPixelConstructor.red.visit(this, arg);
if(alpha.equals(Type.INTEGER) && blue.equals(Type.INTEGER) && green.equals(Type.INTEGER) && red.equals(Type.INTEGER))
{
expressionPixelConstructor.setType(Type.INTEGER);
}
else
{
throw new SemanticException(expressionPixelConstructor.firstToken, "Exception in visitExpressionPixelConstructor");
}
return expressionPixelConstructor.getType();
}
@Override
public Object visitStatementAssign(StatementAssign statementAssign, Object arg) throws Exception {
// TODO Auto-generated method stub
Type t1 = (Type) statementAssign.lhs.visit(this, arg);
Type t2 = (Type) statementAssign.e.visit(this, arg);
if(!t1.equals(t2))
{
throw new SemanticException(statementAssign.firstToken, "Error in visitStatementAssign");
}
return null;
}
@Override
public Object visitStatementShow(StatementShow statementShow, Object arg) throws Exception {
Type e = (Type) statementShow.e.visit(this, arg);
if(!e.equals(Type.INTEGER) && !e.equals(Type.FLOAT) && !e.equals(Type.IMAGE) && !e.equals(Type.BOOLEAN))
{
throw new SemanticException(statementShow.firstToken, "Error in visitStatementShow");
}
return null;
}
@Override
public Object visitExpressionPixel(ExpressionPixel expressionPixel, Object arg) throws Exception {
// TODO Auto-generated method stub
Declaration sourceDec = symTab.lookup(expressionPixel.name);
if(sourceDec != null)
{
Type t1 = sourceDec.getType();
if(t1.equals(Type.IMAGE))
{
expressionPixel.pixelSelector.visit(this, arg);
expressionPixel.setType(Type.INTEGER);
}
else
{
throw new SemanticException(expressionPixel.firstToken, "Exception in visitExpressionPixel");
}
}
else
{
throw new SemanticException(expressionPixel.firstToken, "Exception in visitExpressionPixel");
}
expressionPixel.setDec(sourceDec);
return expressionPixel.getType();
}
@Override
public Object visitExpressionIdent(ExpressionIdent expressionIdent, Object arg) throws Exception {
// TODO Auto-generated method stub
Declaration sourceDec = symTab.lookup(expressionIdent.name);
if(sourceDec != null)
{
expressionIdent.setType(sourceDec.getType());
}
else
{
throw new SemanticException(expressionIdent.firstToken, "Exception in visitExpressionIdent");
}
expressionIdent.setDec(sourceDec);
return expressionIdent.getType();
}
@Override
public Object visitLHSSample(LHSSample lhsSample, Object arg) throws Exception {
// TODO Auto-generated method stub
Declaration sourceDec = symTab.lookup(lhsSample.name);
if(sourceDec != null)
{
lhsSample.pixelSelector.visit(this, arg);
Type t1 = sourceDec.getType();
if(!t1.equals(Type.IMAGE))
{
throw new SemanticException(lhsSample.firstToken, "Error in visitLHSSample");
}
lhsSample.setType(Type.INTEGER);
}
else
{
throw new SemanticException(lhsSample.firstToken, "Error in visitLHSSample");
}
lhsSample.setDec(sourceDec);
return lhsSample.getType();
}
@Override
public Object visitLHSPixel(LHSPixel lhsPixel, Object arg) throws Exception {
// TODO Auto-generated method stub
Declaration sourceDec = symTab.lookup(lhsPixel.name);
if(sourceDec != null)
{
lhsPixel.pixelSelector.visit(this, arg);
Type t1 = sourceDec.getType();
if(!t1.equals(Type.IMAGE))
{
throw new SemanticException(lhsPixel.firstToken, "Error in visitLHSPixel");
}
lhsPixel.setType(Type.INTEGER);
}
else
{
throw new SemanticException(lhsPixel.firstToken, "Error in visitLHSPixel");
}
lhsPixel.setDec(sourceDec);
return lhsPixel.getType();
}
@Override
public Object visitLHSIdent(LHSIdent lhsIdent, Object arg) throws Exception {
// TODO Auto-generated method stub
Declaration sourceDec = symTab.lookup(lhsIdent.name);
if(sourceDec == null)
{
throw new SemanticException(lhsIdent.firstToken, "Error in visitLHSIdent");
}
Type t1 = sourceDec.getType();
lhsIdent.setType(t1);
lhsIdent.setDec(sourceDec);
return lhsIdent.getType();
}
@Override
public Object visitStatementIf(StatementIf statementIf, Object arg) throws Exception {
// TODO Auto-generated method stub
Type t1 = (Type) statementIf.guard.visit(this, arg);
if(!t1.equals(Type.BOOLEAN))
{
throw new SemanticException(statementIf.firstToken, "Exception in visitStatementIf");
}
statementIf.b.visit(this, arg);
return null;
}
@Override
public Object visitStatementWhile(StatementWhile statementWhile, Object arg) throws Exception {
// TODO Auto-generated method stub
Type t1 = (Type) statementWhile.guard.visit(this, arg);
if(!t1.equals(Type.BOOLEAN))
{
throw new SemanticException(statementWhile.firstToken, "Exception in visitStatementWhile");
}
statementWhile.b.visit(this, arg);
return null;
}
@Override
public Object visitStatementSleep(StatementSleep statementSleep, Object arg) throws Exception {
// TODO Auto-generated method stub
Type t1 = (Type) statementSleep.duration.visit(this, arg);
if(!t1.equals(Type.INTEGER))
{
throw new SemanticException(statementSleep.firstToken, "Exception in visitStatementSleep");
}
return null;
}
}