-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeChecker.java
More file actions
485 lines (413 loc) · 10.1 KB
/
TypeChecker.java
File metadata and controls
485 lines (413 loc) · 10.1 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
import java.util.Stack;
public class TypeChecker
{
SemanticTable st;
ParseNode root;
public TypeChecker(SemanticTable semTable, ParseNode r)
{
st = semTable;
root = r;
System.out.println(checkTypes());
st.printTable(true);
}
Stack<ParseNode> stack = new Stack<ParseNode>();
public String checkTypes()
{
String output = "";
ParseNode current;
stack.push(root);
Boolean status;
while(!stack.isEmpty())
{
current = stack.pop();
status = typeCheck(current);
//if current node is a type check node
if(status==null)
{
//null status means not a valid node to check
for(int i = current.children.size()-1;i>=0;i--)
{
stack.push(current.children.get(i));
}
}
else if(status==false)//false
{
output+=current.printPruned()+ " -> " + "is not correctly typed!\n";
continue;
}
else if(status)
{
continue;
}
}
return output;
}
Boolean typeCheck(ParseNode current)
{
switch(current.type)
{
case ASSIGN:
return isAssignSameAsVar(current);
case CALC:
System.out.print("CALC ("+current.data+"): ");
return isBothChildrenNumbers(current);
case CONDLOOP:
return isLoopTypedCorrectly(current);
case COND_BRANCH:
return isBranchTypedCorrectly(current);
case BOOL:
return isNodeABool(current);
case NAME:
return isNodeAProc(current);
}
return null;
}
boolean isNodeAProc(ParseNode node)
{
return (node.type==NodeType.NAME && st.getVarDecleration(node)!=null);
}
boolean isBranchTypedCorrectly(ParseNode node)
{
for(int i = 1; i < node.children.size();i++)
stack.push(node.children.get(i));
if(node.data.contains("t") || node.data.contains("s"))//then or else
{
stack.push(node.children.get(0));
return true;
}
System.out.print("COND_BRANCH (IF): ");
if(isNodeABool(node.children.get(0)))
{
st.getEntry(node.num).varType="B";
System.out.println(" -> B");
return true;
}
else
{
st.getEntry(node.num).varType="?";
System.out.println("-> is not correctly typed!");
return false;
}
}
boolean isLoopTypedCorrectly(ParseNode node)
{
if(node.data.contains("f"))//for loop
{
System.out.print("CONDLOOP (FOR): ");
SemanticTable.TableEntry child;
boolean status = true;
//check that every var child is of type N
for(ParseNode n: node.children)
{
child = st.getEntry(n.num);
if(child.node.type==NodeType.VAR)
{
if(child.varType==null)
{
System.out.println(n.printPruned() + " -> is not declared!");
status = false;
continue;
}
if(!child.varType.equals("N"))
{
System.out.println(node.printPruned() + " -> is not correctly typed!");
status = false;
}
}
else
stack.push(n);
}
if(status)
{
System.out.println(node.printPruned() + " -> N");
return true;
}
else
return false;
}
else
if(node.data.contains("w"))//while loop
{
boolean status;
//fisrt child must evaluate to type B
System.out.print("CONDLOOP (WHILE): ");
if( isNodeABool(node.children.get(0)) )
{
st.getEntry(node.num).varType="B";
System.out.println("-> B");
status = true;
}
else
{
st.getEntry(node.num).varType="?";
System.out.println(" -> ?");
status = false;
}
for(int i = 1; i < node.children.size();i++)
stack.push(node.children.get(i));
return status;
}
else
{
System.out.println("Error typing CONDLOOP at: "+node.num);
return false;
}
}
boolean isBothChildrenTheSame(ParseNode node)
{
if(node.children.size()!=2)
return false;
ParseNode left = node.children.get(0);
ParseNode right = node.children.get(1);
SemanticTable.TableEntry temp = st.getVarDecleration(left);
if(temp==null)
{
System.out.println(node.printPruned() + " -> has not been declared!");
return false;
}
//left is first child
if(temp.varType!=null)
{
if(temp.varType.equals("N"))
{
// System.out.println("left is N");
return isNodeANumber(right);
}
if(temp.varType.equals("S"))
{
// System.out.println("left is S");
return isNodeAString(right);
}
if(temp.varType.equals("B"))
{
// System.out.println("left is B");
return isNodeABool(right);
}
System.out.println(node.printPruned()+" -> cannot match type!");
}
else
{
if(isNodeABool(left))
return isNodeABool(right);
if(isNodeANumber(left))
return isNodeANumber(right);
System.out.println(node.printPruned()+" -> cannt match to sibling!");
}
return false;
}
boolean isAssignSameAsVar(ParseNode node)
{
if(node.children.size()!=2)
return false;
ParseNode left = node.children.get(0);
ParseNode right = node.children.get(1);
SemanticTable.TableEntry temp = st.getVarDecleration(left);
if(temp==null)
{
System.out.println(left.printPruned()+" -> is not declared!");
return false; //var is not declared
}
//left has to be a var - left is first child
if(temp.varType==null)
{
System.out.println(left.printPruned() +" -> is not a var!");
return false;
}
System.out.print("Assign: " + temp.varType);
if(temp.varType.equals("N"))
// if(isNodeANumber(left))
if(isNodeANumber(right))
{
System.out.println(" -> N");
st.getEntry(node.num).varType = "N = N";
// st.getEntry(left.num).hv = st.getEntry(right.num).hv;
return true;
}
if(temp.varType.equals("S"))
// if(isNodeAString(left))
if(isNodeAString(right))
{
System.out.println(" -> S");
st.getEntry(node.num).varType = "S = S";
// st.getEntry(left.num).hv = st.getEntry(right.num).hv;
return true;
}
if(temp.varType.equals("B"))
// if(isNodeABool(left))
if(isNodeABool(right))
{
System.out.println(" -> B");
st.getEntry(node.num).varType = "B = B";
// st.getEntry(left.num).hv = st.getEntry(right.num).hv;
return true;
}
System.out.println(" -> ? \n"+node.printPruned()+" -> type is not the same on both sides of assign!");
st.getEntry(node.num).varType = temp.varType + " = ?";
return false;
}
boolean isNodeABool(ParseNode node)
{
if(node.data.equals("T") || node.data.equals("F"))
{
st.getEntry(node.num).hv = HasValue.YES;
st.getEntry(node.num).varType = "B";
return true;
}
if(node.data.equals("not"))
if(!isNodeABool(node.children.get(0)))
return false;
SemanticTable.TableEntry temp;
if(node.type==NodeType.VAR)
{
temp = st.getVarDecleration(node);
if(temp==null || temp.varType==null || !temp.varType.equals("B"))
return false;
}
else if(node.type==NodeType.BOOL)//is it an expr?
{
//is it an explicit < or > expr?
if(node.data.equals(">")||node.data.equals("<"))
if(isBothChildrenNumbers(node))
{
st.getEntry(node.num).varType = "B";
return true;
}
else
{
st.getEntry(node.num).varType = "?";
return false;
}
if(node.data.equals("eq"))
{
if(isBothChildrenTheSame(node))
{
st.getEntry(node.num).varType="B";
return true;
}
else
{
st.getEntry(node.num).varType="?";
return false;
}
}
if(!isBothChildrenBool(node))
return false;
}
else//therefore it is incorrect
{
return false;
}
return true;
}
boolean isBothChildrenBool(ParseNode node)
{
//check if we have reached a terminal
if(node.children.size()!=2)
return true;
ParseNode left = node.children.get(0);
ParseNode right = node.children.get(1);
boolean lb,rb;
if ( isNodeABool(left) && isNodeABool(right) )
// if(lb && rb)
{
st.getEntry(node.num).varType = "B";
// if(st.getEntry(left.num).hv == st.getEntry(right.num).hv && st.getEntry(left.num).hv == HasValue.YES)
// st.getEntry(node.num).hv=HasValue.YES;
// else
// st.getEntry(node.num).hv = HasValue.MAYBE;
return true;
}
else
{
st.getEntry(node.num).varType = "?";
return false;
}
}
boolean isNodeAString(ParseNode node)
{
SemanticTable.TableEntry te = st.getEntry(node.num);
if(te.varType!=null && te.varType.equals("S"))
return true;
if(node.type==NodeType.STR)
{
st.getEntry(node.num).hv = HasValue.YES;
st.getEntry(node.num).varType = "S";
return true;
}
SemanticTable.TableEntry temp = st.getVarDecleration(node);
return ( temp!=null && temp.varType.equals("S") );
}
boolean isNodeANumber(ParseNode node)
{
SemanticTable.TableEntry te = st.getEntry(node.num);
if(te.varType!=null && te.varType.equals("N"))
return true;
SemanticTable.TableEntry temp;
//check left - child 0
if(node.type==NodeType.VAR)
{
temp = st.getVarDecleration(node);
// System.out.println(temp.node.printPruned());
if(temp==null || temp.varType==null || !temp.varType.equals("N"))
{
System.out.println(node.printPruned() + " -> is not a number!");
return false;
}
}
else if(node.type==NodeType.CALC)//is it an expr?
{
if(!isBothChildrenNumbers(node))
return false;
}
else if(node.type==NodeType.NUMEXPR)
{
st.getEntry(node.num).hv = HasValue.YES;
return true;
}
else
return false;
return true;
}
boolean isBothChildrenNumbers(ParseNode node)
{
//check if we have reached a terminal
if(node.children.size()!=2)
return true;
ParseNode left = node.children.get(0);
ParseNode right = node.children.get(1);
if(isNodeANumber(left) && isNodeANumber(right))
{
// if(st.getEntry(left.num).hv == st.getEntry(right.num).hv && st.getEntry(left.num).hv == HasValue.YES)
// st.getEntry(node.num).hv = HasValue.YES;
// else
// st.getEntry(left.num).hv = HasValue.MAYBE;
return true;
}
else
{
// st.getEntry(node.num).hv = HasValue.NO;
return false;
}
}
void printTree(boolean verbose)
{
String output="";
if(verbose)
{
for(SemanticTable.TableEntry t: st.entries)
if(t.varType==null)
output+=t.node.printWithTabs()+"\n";
else
output+=t.node.printWithTabs()+" -> " + t.varType+"\n";
}
else
{
for(SemanticTable.TableEntry t: st.entries)
if(t.varType==null)
output+=t.node.getTabs()+t.t_id+"|"+t.node.type+"|"+t.name+"\n";
else
output+=t.node.getTabs()+t.t_id+"|"+t.node.type+"|"+t.name+" -> "+t.varType+"\n";
}
System.out.println(output);
}
}