-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisitor1.java
582 lines (520 loc) · 18.4 KB
/
Visitor1.java
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
import minipython.analysis.*;
import minipython.node.*;
import java.util.*;
public class Visitor1 extends DepthFirstAdapter
{
private Hashtable symtable;
private Hashtable symtable_var;
private Error error;
private Hashtable expressionType;
Visitor1(Hashtable symtable, Hashtable symtable_var)
{
error = Error.getInstance();
this.symtable = symtable;
this.symtable_var = symtable_var;
expressionType = new Hashtable();
}
//Find out if this variable(identifier) has been initialized (exists)
public void inAEqualsStatement(AEqualsStatement node)
{
boolean errorOccurred = false;
String vName = node.getId().toString();
int line = ((TId) node.getId()).getLine();
symtable_var.put(vName, node);
}
public void outAEqualsStatement(AEqualsStatement node)
{
boolean errorOccurred = false;
String vName = node.getId().toString();
int line = ((TId) node.getId()).getLine();
PValue v = (PValue)expressionType.get(node.getExpression().toString());
//System.out.println(vName + " = " + v);
// If the assignment expression did not produce any errors
if (v != null)
{
expressionType.put(vName, v);
}
}
private boolean throwErrorOrAccept(PExpression mainExp, PValue lv, PValue rv, String op)
{
boolean errorOccurred = false;
//System.out.println("test " + mainExp);
if (lv instanceof ANumberValue && rv instanceof AStringValue && !op.equals("*"))
{
errorOccurred = error.printError("Line " /*+ line*/ + ": " +" " + "unsupported operand type(s) for "+ op + ": 'number' and 'str'", "add1");
}else if (lv instanceof AStringValue && rv instanceof ANumberValue && !op.equals("*"))
{
errorOccurred = error.printError("Line " /*+ line*/+ ": " +" " + "unsupported operand type(s) for "+ op + ": 'str' and 'number'", "add2");
}else if (lv instanceof AStringValue && rv instanceof AStringValue && !op.equals("+"))
{
errorOccurred = error.printError("Line " /*+ line*/+ ": " +" " + "unsupported operand type(s) for "+ op + ": 'str' and 'str'", "add3");
}else if (lv instanceof ANumberValue && rv instanceof AStringValue && op.equals("*"))
{
expressionType.put(mainExp.toString(), new AStringValue());
}
else if (lv instanceof AStringValue && rv instanceof ANumberValue && op.equals("*"))
{
expressionType.put(mainExp.toString(), new AStringValue());
}else if (lv instanceof ANumberValue && rv instanceof ANumberValue)
{
expressionType.put(mainExp.toString(), new ANumberValue());
}else if (lv instanceof AStringValue && rv instanceof AStringValue && op.equals("+"))
{
expressionType.put(mainExp.toString(), new AStringValue());
}
return errorOccurred;
}
private boolean checkValues(PExpression mainExp, PExpression l, PExpression r, String op)
{
boolean errorOccurred = false;
PValue lv = null, rv = null;
if (l instanceof AValueExpression && r instanceof AValueExpression)
{
lv = ((AValueExpression)l).getValue();
rv = ((AValueExpression)r).getValue();
errorOccurred = throwErrorOrAccept(mainExp, lv, rv, op);
}else
{
// find the type of each operant
//lv = getValue(l);
lv = (PValue)expressionType.get(l.toString());
//rv = getValue(r);
rv = (PValue)expressionType.get(r.toString());
//System.out.println(rv + " " + lv);
if (lv != null && rv != null)
{
errorOccurred = throwErrorOrAccept(mainExp,lv,rv, op);
}
}
return errorOccurred;
}
public void inAForStatement(AForStatement node)
{
boolean errorOccurred = false;
String vName = node.getRightId().toString();
int line = ((TId) node.getRightId()).getLine();
if (!node.getRightId().getText().equals(node.getLeftId().getText()) && !symtable_var.containsKey(vName))
{
errorOccurred = error.printError("Line " + line + ": " +" Variable " + vName + "is not defined in this scope", "aek88");
}
}
public void outAAddExpression(AAddExpression node)
{
boolean errorOccurred = false;
String vName = node.getL().toString();
//int line = ((TId) node.getL()).getLine();
PExpression l = null, r = null;
l = node.getL();
r = node.getR();
checkValues(node,l,r,"+");
}
public void outAMultExpression(AMultExpression node)
{
boolean errorOccurred = false;
String vName = node.getL().toString();
//int line = ((TId) node.getL()).getLine();
PExpression l = null, r = null;
l = node.getL();
r = node.getR();
checkValues(node,l,r,"*");
}
public void outADivExpression(ADivExpression node)
{
boolean errorOccurred = false;
String vName = node.getL().toString();
//int line = ((TId) node.getL()).getLine();
PExpression l = null, r = null;
l = node.getL();
r = node.getR();
checkValues(node,l,r,"/");
}
public void outAPowExpression(APowExpression node)
{
//System.out.println(node);
boolean errorOccurred = false;
String vName = node.getL().toString();
//int line = ((TId) node.getL()).getLine();
PExpression l = null, r = null;
l = node.getL();
r = node.getR();
checkValues(node,l,r,"**");
}
public void outASubExpression(ASubExpression node)
{
boolean errorOccurred = false;
String vName = node.getL().toString();
//int line = ((TId) node.getL()).getLine();
PExpression l = null, r = null;
l = node.getL();
r = node.getR();
checkValues(node,l,r,"-");
}
public void outAValueExpression(AValueExpression node)
{
expressionType.put(node.toString(), node.getValue());
}
public void outAIdExpression(AIdExpression node)
{
String vName = node.getId().toString();
PValue v = (PValue)expressionType.get(vName);
if (v != null)
expressionType.put(node.toString(), v);
}
public void outAParExpExpression(AParExpExpression node)
{
//System.out.println("start out parExp " + node);
PExpression exp = node.getExpression();
//System.out.println("pepepepe");
PValue expVal = (PValue)expressionType.get(exp.toString());
//System.out.println("poopopopopop");
//System.out.println(expVal == null ? "NULL EXPVAL" : "NOT NULL EXPVAL");
if (expVal != null)
expressionType.put(node.toString(), expVal);
//System.out.println("end of out parExp");
}
public void outAGreatComparison(AGreatComparison node)
{
boolean errorOccurred = false;
PExpression l = null, r = null;
PValue lv = null, rv = null;
l = node.getLeftExp();
r = node.getRightExp();
lv = (PValue)expressionType.get(l.toString());
rv = (PValue)expressionType.get(r.toString());
if (lv instanceof AStringValue && rv instanceof ANumberValue)
{
errorOccurred = error.printError("Line " + /*+ line +*/ ": " +"'>' not supported between instances of 'str' and 'int'");
}else if (lv instanceof ANumberValue && rv instanceof AStringValue)
{
errorOccurred = error.printError("Line " + /*+ line +*/ ": " +"'>' not supported between instances of 'int' and 'str'");
}
}
public void outALessComparison(ALessComparison node)
{
boolean errorOccurred = false;
PExpression l = null, r = null;
PValue lv = null, rv = null;
l = node.getLeftExp();
r = node.getRightExp();
lv = (PValue)expressionType.get(l.toString());
rv = (PValue)expressionType.get(r.toString());
if (lv instanceof AStringValue && rv instanceof ANumberValue)
{
errorOccurred = error.printError("Line " + /*+ line +*/ ": " +"'<' not supported between instances of 'str' and 'int'");
}else if (lv instanceof ANumberValue && rv instanceof AStringValue)
{
errorOccurred = error.printError("Line " + /*+ line +*/ ": " +"'<' not supported between instances of 'int' and 'str'");
}
}
public void outADifComparison(ADifComparison node)
{
boolean errorOccurred = false;
PExpression l = null, r = null;
PValue lv = null, rv = null;
l = node.getLeftExp();
r = node.getRightExp();
lv = (PValue)expressionType.get(l.toString());
rv = (PValue)expressionType.get(r.toString());
if (lv instanceof AStringValue && rv instanceof ANumberValue)
{
errorOccurred = error.printError("Line " + /*+ line +*/ ": " +"'!=' not supported between instances of 'str' and 'int'");
}else if (lv instanceof ANumberValue && rv instanceof AStringValue)
{
errorOccurred = error.printError("Line " + /*+ line +*/ ": " +"'!=' not supported between instances of 'int' and 'str'");
}
}
public void outAEqComparison(AEqComparison node)
{
boolean errorOccurred = false;
PExpression l = null, r = null;
PValue lv = null, rv = null;
l = node.getLeftExp();
r = node.getRightExp();
lv = (PValue)expressionType.get(l.toString());
rv = (PValue)expressionType.get(r.toString());
if (lv instanceof AStringValue && rv instanceof ANumberValue)
{
errorOccurred = error.printError("Line " + /*+ line +*/ ": " +"'==' not supported between instances of 'str' and 'int'");
}else if (lv instanceof ANumberValue && rv instanceof AStringValue)
{
errorOccurred = error.printError("Line " + /*+ line +*/ ": " +"'==' not supported between instances of 'int' and 'str'");
}
}
public void inAIdExpression(AIdExpression node)
{
boolean errorOccurred = false;
String vName = node.getId().toString();
int line = ((TId) node.getId()).getLine();
//getting return statement parameters
Node parent = node.parent();
while (parent != null && !(parent instanceof AReturnStatement) && !(parent instanceof AForStatement) && !(parent instanceof AFunction)){
//System.out.println(vName + " " + parent.getClass());
parent = parent.parent();
}
if(parent instanceof AReturnStatement || parent instanceof AFunction)
{
while (parent != null && (!(parent instanceof AFunction))){
parent = parent.parent();
}
if(parent != null)
{
AFunction func = (AFunction) parent;
LinkedList func_args = func.getArgument();
if (func_args.size() != 0)
{
AArgument arg = (AArgument) func_args.get(0);
// check if the current id is the first argument
if (!node.getId().getText().equals(arg.getId().getText()))
{
LinkedList cids = arg.getCommaid();
boolean found = false;
for(int i = 0; i < cids.size(); ++i)
{
ACommaid cid = (ACommaid)cids.get(i);
if (node.getId().getText().equals(cid.getId().getText()))
{
found = true;
break;
}
}
if (!found)
{
// we are sure that the id is defined inside the scope of the function
if (!symtable_var.containsKey(vName))
{
String fName = func.getId().getText();
errorOccurred = error.printError("Line " + line + ": " +" Variable " + vName + "is not defined in the scope of function " + fName, "aek66");
return;
}
}
}
}else
{
// we are sure that the id is defined inside the scope of the function
if (!symtable_var.containsKey(vName))
{
String fName = func.getId().getText();
errorOccurred = error.printError("Line " + line + ": " +" Variable " + vName + "is not defined in the scope of function " + fName, "aek661");
return;
}
//String fName = func.getId().getText();
//errorOccurred = error.printError("Line " + line + ": " +" Variable " + vName + "is not defined in the scope of function " + fName, "aek666");
//return;
}
}
return;
}else if(parent instanceof AForStatement)
{
//System.out.println("instanceof for");
if(parent != null)
{
AForStatement fors = (AForStatement) parent;
//System.out.println(fors.getLeftId().getText());
if (!fors.getLeftId().getText().equals(node.getId().getText()))
{
if (!symtable_var.containsKey(vName))
{
errorOccurred = error.printError("Line " + line + ": " +" Variable " + vName + "is not defined in the scope of the for statement", "aek77");
return;
}
}
// we are sure that the id is defined inside the scope of the for statement
return;
}
}
//checking if a variable is included in symtable_var hashtable
if (!(symtable_var.containsKey(vName)))
{
errorOccurred = error.printError("Line " + line + ": " +" Variable " + vName + "is not defined in this scope", "aek55");
return;
}
}
/*
TODO def foo(a) and def foo(b) -> error must be thrown
TODO def foo(a,a) -> error must be thrown
TODO find out why the fuck does it print the wrong lines???
*/
public void inAFunction(AFunction node)
{
boolean errorOccurred = false;
String fName = node.getId().toString();
int line = ((TId) node.getId()).getLine();
// Here we check if the function is already defined
if (symtable.containsKey(fName))
{
LinkedList args = node.getArgument();
AFunction other = (AFunction)symtable.get(fName);
LinkedList other_args = other.getArgument();
if (other_args.size() == args.size())
{
if(args.size() == 0)
{
errorOccurred = error.printError("Line " + line + ": " +" Function " + fName +" is already defined", "test5");
return;
}
AArgument arg1 = (AArgument) args.get(0);
AArgument arg2 = (AArgument) other_args.get(0);
LinkedList list1;
LinkedList list2;
list1 = arg1.getCommaid();
list2 = arg2.getCommaid();
if(list1.size() == list2.size())
{
// Same name and argument number
errorOccurred = error.printError("Line " + line + ": " +" Function " + fName +" is already defined", "test3");
//return;
}else
{
// If the arguments have lists of different sizes
// we find the smalest and the longet lsit
LinkedList small = null, big = null;
if (list1.size() > list2.size())
{
big = list1;
small = list2;
}else
{
big = list2;
small = list1;
}
int i = 0;
if (small.size() != 0)
i = small.size();
ACommaid commaid = (ACommaid) big.get(i);
LinkedList eqval = commaid.getEqvalue();
if (eqval.size() != 0)
{
//commonIdsDefault = false;
errorOccurred = error.printError("Line " + line + ": " +" Function " + fName +" is already defined", "test7");
//return;
}
}
}else
{
// Here we must check if the two functions have the same name but one of them has
// default arguments(case 6 on the Symbol_Semantic.html)
// def add(x = 0, y = 0) and def add() -> should throw an error
LinkedList hasArgs = null, hasNotArgs = null;
if (args.size() != 0)
{
hasArgs = args;
hasNotArgs = other_args;
}else
{
hasArgs = other_args;
hasNotArgs = args;
}
AArgument argument = (AArgument)hasArgs.get(0);
AEqvalue eqval = null;
LinkedList comids = null;
// now we have to check if every id has a default value
// if it does then an error must be thrown
if (argument.getEqvalue().size() != 0)
{
boolean isAllDefault = true;
comids = argument.getCommaid();
for (int i = 0; i < comids.size(); ++i)
{
LinkedList vallist = ((ACommaid)comids.get(i)).getEqvalue();
if (vallist.size() == 0)
{
isAllDefault = false;
break;
}
}
if (isAllDefault)
{
errorOccurred = error.printError("Line " + line + ": " +" Function " + fName +" is already defined", "test1");
//return;
}
}
}
}
// here we check if a non-default argument follows default argument e.g foo(x=2, y)
if (node.getArgument().size() != 0)
{
LinkedList args = node.getArgument();
AEqvalue eqval = null;
LinkedList comids = null;
AArgument argument = (AArgument)args.get(0);
// First we check the value of the very first id
if (argument.getEqvalue().size() != 0)
eqval = (AEqvalue)argument.getEqvalue().get(0);
comids = argument.getCommaid();
// if there are more arguments and the first id has a default value
if (eqval != null && comids.size() != 0 && ((ACommaid)comids.get(0)).getEqvalue().size() == 0)
{
errorOccurred = error.printError("Line " + line + ": " +" Function " + fName +" a non-default argument follows default argument");
//return;
}
comids = argument.getCommaid();
// Now check the rest of the id values
for (int i = 0; i < comids.size(); ++i)
{
LinkedList vallist = ((ACommaid)comids.get(i)).getEqvalue();
if (vallist.size() != 0 && i+1 < comids.size() && ((ACommaid)comids.get(i+1)).getEqvalue().size() == 0)
{
errorOccurred = error.printError("Line " + line + ": " +" Function " + fName +" a non-default argument follows default argument");
//return;
}
}
}
// We check if the function definition contains an argument with a name that has already been used in a previous argument.
// We must parse the arguments and throw an error if the previous happens.
LinkedList args = node.getArgument();
if (args.size() != 0)
{
HashSet<String> duplicatedIds = new HashSet<String>();
AArgument arg = (AArgument)args.get(0);
String currentIdName = arg.getId().getText(), duplicatedIdNames = "";
LinkedList comids = arg.getCommaid();
for (int i = 0; i < comids.size(); ++i)
{
ACommaid cid = (ACommaid)comids.get(i);
if (cid.getId().getText().equals(currentIdName))
{
//System.out.println("hereee");
duplicatedIds.add(currentIdName);
//errorOccurred = error.printError("Line " + line + ": " +" Function " + fName +" duplicate argument " + currentIdName +" in function definition ", "test_same_args");
}
}
if (comids.size() != 0)
{
for (int i = 0; i < comids.size() - 1; ++i)
{
currentIdName = ((ACommaid)comids.get(i)).getId().getText();
for (int j = i+1; j < comids.size(); ++j)
{
ACommaid cid = (ACommaid)comids.get(j);
if (cid.getId().getText().equals(currentIdName))
{
duplicatedIds.add(currentIdName);
//errorOccurred = error.printError("Line " + line + ": " +" Function " + fName +" duplicate argument " + currentIdName +" in function definition ", "test_same_args");
break;
}
}
}
}
if (duplicatedIds.size() != 0)
{
String msg = "";
Iterator iter = duplicatedIds.iterator();
while (iter.hasNext())
{
msg += " " + iter.next();
}
errorOccurred = error.printError("Line " + line + ": " +" Function " + fName +" duplicate argument(s)" + msg +" in function definition ", "test_same_args");
}
}
// if a function produces at least one error, then it is not inserted into the symbol table (is this the correct behavior?)
if (!errorOccurred)
{
//System.out.println("Function " + fName + " was added");
symtable.put(fName, node);
}
}
public int getErrorCount()
{
return error.getErrorCount();
}
}