-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeCheckVisitor.java
More file actions
431 lines (376 loc) · 15.2 KB
/
Copy pathTypeCheckVisitor.java
File metadata and controls
431 lines (376 loc) · 15.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
package cop5556sp17;
import cop5556sp17.AST.ASTVisitor;
import cop5556sp17.AST.Tuple;
import cop5556sp17.AST.Type;
import cop5556sp17.AST.AssignmentStatement;
import cop5556sp17.AST.BinaryChain;
import cop5556sp17.AST.BinaryExpression;
import cop5556sp17.AST.Block;
import cop5556sp17.AST.BooleanLitExpression;
import cop5556sp17.AST.Chain;
import cop5556sp17.AST.ChainElem;
import cop5556sp17.AST.ConstantExpression;
import cop5556sp17.AST.Dec;
import cop5556sp17.AST.Expression;
import cop5556sp17.AST.FilterOpChain;
import cop5556sp17.AST.FrameOpChain;
import cop5556sp17.AST.IdentChain;
import cop5556sp17.AST.IdentExpression;
import cop5556sp17.AST.IdentLValue;
import cop5556sp17.AST.IfStatement;
import cop5556sp17.AST.ImageOpChain;
import cop5556sp17.AST.IntLitExpression;
import cop5556sp17.AST.ParamDec;
import cop5556sp17.AST.Program;
import cop5556sp17.AST.SleepStatement;
import cop5556sp17.AST.Statement;
import cop5556sp17.AST.Type.TypeName;
import cop5556sp17.AST.WhileStatement;
import java.util.ArrayList;
import java.util.List;
import cop5556sp17.Scanner.Kind;
import cop5556sp17.Scanner.Token;
import static cop5556sp17.AST.Type.TypeName.*;
import static cop5556sp17.Scanner.Kind.KW_HIDE;
import static cop5556sp17.Scanner.Kind.KW_MOVE;
import static cop5556sp17.Scanner.Kind.KW_SHOW;
import static cop5556sp17.Scanner.Kind.KW_XLOC;
import static cop5556sp17.Scanner.Kind.KW_YLOC;
import static cop5556sp17.Scanner.Kind.OP_BLUR;
import static cop5556sp17.Scanner.Kind.OP_CONVOLVE;
import static cop5556sp17.Scanner.Kind.OP_GRAY;
import static cop5556sp17.Scanner.Kind.OP_HEIGHT;
import static cop5556sp17.Scanner.Kind.OP_WIDTH;
import static cop5556sp17.Scanner.Kind.*;
public class TypeCheckVisitor implements ASTVisitor {
@SuppressWarnings("serial")
public static class TypeCheckException extends Exception {
TypeCheckException(String message) {
super(message);
}
}
SymbolTable symtab = new SymbolTable();
@Override
public Object visitBinaryChain(BinaryChain binaryChain, Object arg) throws Exception {
Chain chain = binaryChain.getE0();
chain.visit(this, arg);
TypeName chainType = chain.getTypeName();
ChainElem chainElem = binaryChain.getE1();
chainElem.visit(this, arg);
TypeName chainElemType = chainElem.getTypeName();
Token arrow = binaryChain.getArrow();
if(chainType.isType(TypeName.URL) && arrow.kind.equals(Kind.ARROW) && chainElemType.isType(TypeName.IMAGE))
binaryChain.setTypeName(TypeName.IMAGE);
else if(chainType.isType(TypeName.FILE) && arrow.kind.equals(Kind.ARROW) && chainElemType.isType(TypeName.IMAGE))
binaryChain.setTypeName(TypeName.IMAGE);
else if(chainType.isType(TypeName.FRAME) && arrow.kind.equals(Kind.ARROW) && chainElem instanceof FrameOpChain){
if(chainElem.getFirstToken().kind.equals(KW_XLOC) || chainElem.getFirstToken().kind.equals(KW_YLOC)) {
binaryChain.setTypeName(TypeName.INTEGER);
}
else if(chainElem.getFirstToken().kind.equals(KW_SHOW) || chainElem.getFirstToken().kind.equals(KW_HIDE) || chainElem.getFirstToken().kind.equals(KW_MOVE))
binaryChain.setTypeName(TypeName.FRAME);
else
throw new TypeCheckException("");
}
else if(chainType.isType(TypeName.IMAGE) && arrow.kind.equals(Kind.ARROW) && chainElem instanceof ImageOpChain){
if(chainElem.getFirstToken().kind.equals(OP_WIDTH) || chainElem.getFirstToken().kind.equals(OP_HEIGHT)) {
binaryChain.setTypeName(TypeName.INTEGER);
}
else if(chainElem.getFirstToken().kind.equals(KW_SCALE)) {
binaryChain.setTypeName(TypeName.IMAGE);
}
else
throw new TypeCheckException("ChainElement is not an instance of FrameOpChain");
}
else if(chainType.isType(TypeName.IMAGE) && arrow.kind.equals(Kind.ARROW) && chainElemType.isType(TypeName.FRAME)){
binaryChain.setTypeName(TypeName.FRAME);
}
else if(chainType.isType(TypeName.IMAGE) && arrow.kind.equals(Kind.ARROW) && chainElemType.isType(TypeName.FILE)){
binaryChain.setTypeName(TypeName.NONE);
}
else if(chainType.isType(TypeName.IMAGE) && (arrow.kind.equals(Kind.ARROW) || arrow.kind.equals(Kind.BARARROW)) && chainElem instanceof FilterOpChain){
if(chainElem.getFirstToken().kind.equals(OP_GRAY) || chainElem.getFirstToken().kind.equals(OP_BLUR) || chainElem.getFirstToken().kind.equals(OP_CONVOLVE)) {
binaryChain.setTypeName(TypeName.IMAGE);
}
else
throw new TypeCheckException("ChainElement is not an instance of FrameOpChain");
}
// TODO validate this
else if(chainType.isType(TypeName.IMAGE) && arrow.kind.equals(Kind.ARROW) && chainElem instanceof IdentChain && chainElemType.isType(TypeName.IMAGE)){
binaryChain.setTypeName(TypeName.IMAGE);
}
else if(chainType.isType(TypeName.INTEGER) && arrow.kind.equals(Kind.ARROW) && chainElem instanceof IdentChain && chainElemType.isType(TypeName.INTEGER)){
binaryChain.setTypeName(TypeName.INTEGER);
}
else
throw new TypeCheckException("Operator is not arrow or bararrow in Binary Chain");
return null;
}
@Override
public Object visitBinaryExpression(BinaryExpression binaryExpression, Object arg) throws Exception {
Expression e0 = binaryExpression.getE0();
Expression e1 = binaryExpression.getE1();
Token op = binaryExpression.getOp();
e0.visit(this, arg);
TypeName e0type = e0.getTypeName();
e1.visit(this, arg);
TypeName e1type = e1.getTypeName();
if (e0type.isType(TypeName.INTEGER) && e1type.isType(TypeName.INTEGER)){
if(op.kind.equals(TIMES) || op.kind.equals(PLUS) || op.kind.equals(MINUS) || op.kind.equals(DIV) || op.kind.equals(MOD)){
binaryExpression.setTypeName(TypeName.INTEGER);
}
else if (op.kind.equals(LT) || op.kind.equals(GT) || op.kind.equals(LE) || op.kind.equals(GE)){
binaryExpression.setTypeName(TypeName.BOOLEAN);
}
else if(op.kind.equals(EQUAL) || op.kind.equals(NOTEQUAL)){
binaryExpression.setTypeName(TypeName.BOOLEAN);
}
else
throw new TypeCheckException("Operation is not allowed between two integers");
}
else if(e0type.isType(TypeName.BOOLEAN) && e1type.isType(TypeName.BOOLEAN)){
if (op.kind.equals(LT) || op.kind.equals(GT) || op.kind.equals(LE) || op.kind.equals(GE)){
binaryExpression.setTypeName(TypeName.BOOLEAN);
}
else if(op.kind.equals(EQUAL) || op.kind.equals(NOTEQUAL)){
binaryExpression.setTypeName(TypeName.BOOLEAN);
}
else if(op.kind.equals(AND) || op.kind.equals(OR)) {
binaryExpression.setTypeName(TypeName.BOOLEAN);
}
else
throw new TypeCheckException("Operation is not allowed between two booleans");
}
else if(e0type.isType(TypeName.IMAGE) && e1type.isType(TypeName.IMAGE)){
if(op.kind.equals(PLUS) || op.kind.equals(MINUS)){
binaryExpression.setTypeName(TypeName.IMAGE);
}
else if(op.kind.equals(EQUAL) || op.kind.equals(NOTEQUAL)){
binaryExpression.setTypeName(TypeName.BOOLEAN);
}
else
throw new TypeCheckException("Operation is not allowed between two images");
}
else if((e0type.isType(TypeName.IMAGE) && e1type.isType(TypeName.INTEGER))){
if(op.kind.equals(TIMES) || op.kind.equals(DIV) || op.kind.equals(MOD))
binaryExpression.setTypeName(TypeName.IMAGE);
else
throw new TypeCheckException("Operation is not allowed between two image and integer");
}
else if((e0type.isType(TypeName.INTEGER) && e1type.isType(TypeName.IMAGE))){
if(op.kind.equals(TIMES))
binaryExpression.setTypeName(TypeName.IMAGE);
else
throw new TypeCheckException("Operation is not allowed between two integer and image");
}
else if (e0type.isType(e1type)){
if(op.kind.equals(EQUAL) || op.kind.equals(NOTEQUAL)){
binaryExpression.setTypeName(TypeName.BOOLEAN);
}
else
throw new TypeCheckException("Operation is not allowed between two expressions of same type");
}
else
throw new TypeCheckException("Operation is not allowed between two expressions");
return null;
}
@Override
public Object visitBlock(Block block, Object arg) throws Exception {
symtab.enterScope();
// visit decs
ArrayList<Dec> decs = block.getDecs();
for(Dec dec : decs)
dec.visit(this, arg);
//visit statement
ArrayList<Statement> statements = block.getStatements();
for(Statement statement : statements)
statement.visit(this, arg);
symtab.leaveScope();
return null;
}
@Override
public Object visitBooleanLitExpression(BooleanLitExpression booleanLitExpression, Object arg) throws Exception {
booleanLitExpression.setTypeName(TypeName.BOOLEAN);
return null;
}
@Override
public Object visitFilterOpChain(FilterOpChain filterOpChain, Object arg) throws Exception {
int tupleLength = filterOpChain.getArg().getExprList().size();
if(tupleLength != 0)
throw new TypeCheckException("Tuple length is not zero for FilterOpChain");
else
filterOpChain.setTypeName(TypeName.IMAGE);
filterOpChain.getArg().visit(this, arg);
return null;
}
@Override
public Object visitFrameOpChain(FrameOpChain frameOpChain, Object arg) throws Exception {
Kind kind = frameOpChain.firstToken.kind;
int tupleLength = frameOpChain.getArg().getExprList().size();
frameOpChain.getArg().visit(this, arg);
if(kind == Kind.KW_SHOW || kind == Kind.KW_HIDE){
if(tupleLength != 0)
throw new TypeCheckException("Tuple length is not zero for FrameOpChain");
else
frameOpChain.setTypeName(Type.TypeName.NONE);
}
else if(kind == Kind.KW_XLOC || kind == Kind.KW_YLOC){
if(tupleLength != 0)
throw new TypeCheckException("Tuple length is not zero for FrameOpChain");
else
frameOpChain.setTypeName(Type.TypeName.INTEGER);
}
else if(kind == Kind.KW_MOVE){
if(tupleLength != 2)
throw new TypeCheckException("Tuple length is not two for FrameOpChain");
else
frameOpChain.setTypeName(Type.TypeName.NONE);
}
return null;
}
@Override
public Object visitIdentChain(IdentChain identChain, Object arg) throws Exception {
Dec dec = symtab.lookup(identChain.getFirstToken().getText());
if(dec == null)
throw new TypeCheckException("Ident has not been declared and is visible in the current scope");
else {
// FIXME - validate this
dec.setTypeName(Type.getTypeName(dec.getFirstToken()));
identChain.setDec(dec);
identChain.setTypeName(Type.getTypeName(dec.getFirstToken()));
}
return null;
}
@Override
public Object visitIdentExpression(IdentExpression identExpression, Object arg) throws Exception {
Dec dec = symtab.lookup(identExpression.getFirstToken().getText());
TypeName typeName;
if(dec == null)
throw new TypeCheckException("ident has not been declared and is not visible in the current scope");
else {
typeName = Type.getTypeName(dec.getFirstToken());
identExpression.setTypeName(typeName);
dec.setTypeName(typeName);
identExpression.setDec(dec);
}
return null;
}
@Override
public Object visitIfStatement(IfStatement ifStatement, Object arg) throws Exception {
Expression e = ifStatement.getE();
e.visit(this, arg);
TypeName typeName = e.getTypeName();
if(!typeName.isType(TypeName.BOOLEAN))
throw new TypeCheckException("Expression is not a Boolean Expression for ifStatement");
ifStatement.getB().visit(this, arg);
return null;
}
@Override
public Object visitIntLitExpression(IntLitExpression intLitExpression, Object arg) throws Exception {
intLitExpression.setTypeName(TypeName.INTEGER);
return null;
}
@Override
public Object visitSleepStatement(SleepStatement sleepStatement, Object arg) throws Exception {
Expression e = sleepStatement.getE();
e.visit(this, arg);
if(!e.getTypeName().equals(INTEGER))
throw new TypeCheckException("Expression type is not INTEGER for SleepStatement");
return null;
}
@Override
public Object visitWhileStatement(WhileStatement whileStatement, Object arg) throws Exception {
Expression e = whileStatement.getE();
e.visit(this, arg);
TypeName typeName = e.getTypeName();
// TODO - validate isType
if(!typeName.isType(TypeName.BOOLEAN))
throw new TypeCheckException("Expression is not a Boolean Expression for whileStatement");
whileStatement.getB().visit(this, arg);
return null;
}
@Override
public Object visitDec(Dec declaration, Object arg) throws Exception {
boolean insert = symtab.insert(declaration.getIdent().getText(), declaration);
if(!insert)
throw new TypeCheckException("Ident is declared twice");
else
declaration.setTypeName(Type.getTypeName(declaration.getFirstToken()));
return null;
}
@Override
public Object visitProgram(Program program, Object arg) throws Exception {
ArrayList<ParamDec> params = program.getParams();
Block b = program.getB();
for(ParamDec paramDec : params)
paramDec.visit(this, arg);
b.visit(this, arg);
return null;
}
@Override
public Object visitAssignmentStatement(AssignmentStatement assignStatement, Object arg) throws Exception {
IdentLValue var = assignStatement.getVar();
var.visit(this, arg);
Expression e = assignStatement.getE();
e.visit(this, arg);
if(!e.getTypeName().isType(var.getDec().getTypeName()))
throw new TypeCheckException("IdentLValue and Expression are not of the same type");
return null;
}
@Override
public Object visitIdentLValue(IdentLValue identX, Object arg) throws Exception {
Dec dec = symtab.lookup(identX.firstToken.getText());
TypeName typeName;
if(dec == null)
throw new TypeCheckException("Ident is not declared or visible in the current scope");
else {
typeName = Type.getTypeName(dec.getFirstToken());
dec.setTypeName(typeName);
identX.setDec(dec);
}
return null;
}
@Override
public Object visitParamDec(ParamDec paramDec, Object arg) throws Exception {
boolean insert = symtab.insert(paramDec.getIdent().getText(), paramDec);
if(!insert)
throw new TypeCheckException("Symbol table insert failed");
else
paramDec.setTypeName(Type.getTypeName(paramDec.getFirstToken()));
return null;
}
@Override
public Object visitConstantExpression(ConstantExpression constantExpression, Object arg) {
constantExpression.setTypeName(TypeName.INTEGER);
return null;
}
@Override
public Object visitImageOpChain(ImageOpChain imageOpChain, Object arg) throws Exception {
Kind kind = imageOpChain.firstToken.kind;
int tupleLength = imageOpChain.getArg().getExprList().size();
imageOpChain.getArg().visit(this, arg);
if (kind == Kind.OP_WIDTH || kind == Kind.OP_HEIGHT) {
if(tupleLength != 0)
throw new TypeCheckException("Tuple length is not zero for ImageOpChain");
else
imageOpChain.setTypeName(Type.TypeName.INTEGER);
}
else if (kind == Kind.KW_SCALE) {
if (tupleLength != 1)
throw new TypeCheckException("Tuple length is not one for ImageOpChain");
else
imageOpChain.setTypeName(Type.TypeName.IMAGE);
}
return null;
}
@Override
public Object visitTuple(Tuple tuple, Object arg) throws Exception {
List<Expression> exprList = tuple.getExprList();
for (Expression expr : exprList) {
expr.visit(this, arg);
if (!expr.getTypeName().isType(TypeName.INTEGER))
throw new TypeCheckException("Expression in tuple are not integer");
}
return null;
}
}