-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFirstVisitor.java
More file actions
544 lines (468 loc) · 17.5 KB
/
FirstVisitor.java
File metadata and controls
544 lines (468 loc) · 17.5 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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
import java.util.*;
import minipython.analysis.DepthFirstAdapter;
import minipython.node.*;
/**
* The FirstVisitor does a first traversal of the code and stores the variables
* and functions to their hashtables accordingly
*
* @param variables hashtable containing the name and node of the variables read
* @param funcitons hashtable containing the name and node of the functions read
* @param variableTypes hashtable containing the node and type of the variables read
*/
public class FirstVisitor extends DepthFirstAdapter {
private final Hashtable<String, Node> variables;
private final Hashtable<String, Node> functions;
private final Hashtable<Node, VAR_TYPES> variableTypes;
private final List<Function> functionList;
/**
* All the recognisable potential error types listed
*/
public static enum ERROR_TYPES {
UNDECLARED_VARIABLE,
UNDEFINED_FUNCTION,
UNORDERED_PARAMS,
WRONG_PARAMS,
TYPE_MISSMATCH,
ADD_TYPE_MISSMATCH,
MINUS_TYPE_MISSMATCH,
NONE_OPERATION,
IDENTICAL_FUNCTIONS,
}
/**
* All the potential variable types listed
*/
public static enum VAR_TYPES {
INTEGER,
DOUBLE,
STRING,
NONE,
UNKNOWN,
}
public FirstVisitor(Hashtable<String, Node> variables, Hashtable<String, Node> functions,
Hashtable<Node, VAR_TYPES> variableTypes) {
this.variables = variables;
this.functions = functions;
this.variableTypes = variableTypes;
this.functionList = new ArrayList<>();
}
@Override
public void outAGoal(AGoal node) {
// Check for functions with identical names and parameters' names
for (Function function : functionList) {
for (Function f : functionList) {
if (function != f && function.getName().equals(f.getName())) {
if (function.getTotalParams() == f.getTotalParams() || function.getParams() == f.getParams()) {
printError(node, ERROR_TYPES.IDENTICAL_FUNCTIONS, function.getName());
}
}
}
}
}
@Override
public void inAIdentifier(AIdentifier node) {
// Get the name, the line and the parent of the node
String name = node.getId().getText();
Node parent = node.parent();
// Check for undeclared variables
if (parent instanceof AIdentifierArithmetics) {
if (!variables.containsKey(name)) {
// Print error message
printError(node, ERROR_TYPES.UNDECLARED_VARIABLE);
}
} else if (parent instanceof AForStatement) {
AForStatement forLoop = (AForStatement) parent;
// Check that the second identifer is an existing variable
AIdentifier id2 = (AIdentifier) forLoop.getId2();
if (name.equals(id2.getId().getText()) && !variables.containsKey(name)) {
// Print error message
printError(node, ERROR_TYPES.UNDECLARED_VARIABLE);
}
}
}
@Override
public void outAIdentifier(AIdentifier node) {
// Get the name and the parent of the node
String name = node.getId().getText();
Node parent = node.parent();
// Check the parent's type and react accordingly
if (parent instanceof AAssignmentStatement
|| parent instanceof AMoreAssignments
|| parent instanceof AArgument) {
// Create a new variable
variables.put(name, node);
variableTypes.put(node, VAR_TYPES.UNKNOWN);
} else if (parent instanceof AFunction) {
// Create a new function
functions.put(name, node);
} else if (parent instanceof AForStatement) {
AForStatement forLoop = (AForStatement) parent;
// Create a variable for the first identifier
AIdentifier id1 = (AIdentifier) forLoop.getId1();
if (name.equals(id1.getId().getText())) {
variables.put(name, node);
}
}
}
/**
* Make sure no two functions share the same name and parameters' name
*/
@Override
@SuppressWarnings("unchecked")
public void inAFunction(AFunction node) {
// Get the function's name
String name = ((AIdentifier) node.getIdentifier()).getId().getText();
Function function = new Function(name);
// Get the function's parameter count
LinkedList<AArgument> arguments = node.getArgument();
if (arguments.size() > 0) {
if (arguments.get(0).getAssignValue().size() > 0) {
function.setDefaultParams(1);
} else {
function.setParams(1);
}
for (AMoreAssignments argument : ((LinkedList<AMoreAssignments>) arguments.get(0).getMoreAssignments())) {
// Check for default parameters
LinkedList<AAssignValue> value = argument.getAssignValue();
if (value.size() > 0) {
function.setDefaultParams(function.getDefaultParams() + 1);
} else {
function.setParams(function.getParams() + 1);
}
}
}
functionList.add(function);
}
@Override
public void outAFunction(AFunction node) {
// Retrieve the function's return type from it's return statement(if it exists)
if (node.getStatement() instanceof AReturnStatement) {
PArithmetics arithmetics = ((AReturnStatement) node.getStatement()).getArithmetics();
variableTypes.put(node.getIdentifier(), variableTypes.get(arithmetics));
} else { // Otheriwse None return type
variableTypes.put(node.getIdentifier(), VAR_TYPES.NONE);
}
}
@Override
public void outAAssignmentStatement(AAssignmentStatement node) {
// Replace previous assignment type
String name = ((AIdentifier) node.getIdentifier()).getId().getText();
String varName;
List<Node> toRemove = new ArrayList<>();
for (Node variable : variableTypes.keySet()) {
if (variable instanceof AIdentifier) {
varName = ((AIdentifier) variable).getId().getText();
if (varName.equals(name)) {
toRemove.add(variable);
break;
}
}
}
for (Node n : toRemove) {
variableTypes.remove(n);
}
PArithmetics arithmetics = node.getArithmetics();
Class<?> arithmeticsClass = arithmetics.getClass();
variableTypes.put(node.getIdentifier(), variableTypes.get(arithmeticsClass.cast(arithmetics)));
}
@Override
public void outANumberArithmetics(ANumberArithmetics node) {
variableTypes.put(node, getNumberSubtype(node.getNumber()));
}
@Override
public void outAStrlitArithmetics(AStrlitArithmetics node) {
variableTypes.put(node, VAR_TYPES.STRING);
}
@Override
public void outANoneArithmetics(ANoneArithmetics node) {
variableTypes.put(node, VAR_TYPES.NONE);
}
@Override
public void outALenArithmetics(ALenArithmetics node) {
variableTypes.put(node, VAR_TYPES.INTEGER);
}
@Override
public void outAMaxminArithmetics(AMaxminArithmetics node) {
variableTypes.put(node, VAR_TYPES.DOUBLE);
}
@Override
public void outAIdentifierArithmetics(AIdentifierArithmetics node) {
String id = ((AIdentifier) node.getIdentifier()).getId().getText().trim();
// Iterate all varibles present and find a match
for (Node n : variableTypes.keySet()) {
if (n instanceof AIdentifier) {
if (id.equals(((AIdentifier) n).getId().getText().trim())) {
// Same type as defined in the assignment statement
variableTypes.put(node, variableTypes.get(n));
break;
}
}
}
}
@Override
public void outAExpArithmetics(AExpArithmetics node) {
// Find the class type of left and right children
Class<?> lClass = node.getL().getClass();
Class<?> rClass = node.getR().getClass();
VAR_TYPES lType = variableTypes.get(lClass.cast(node.getL()));
VAR_TYPES rType = variableTypes.get(rClass.cast(node.getR()));
// AIdentifierArithmetics inserts Aidentifier node and not itself
// Hence, the AIdentifier's type should be retrieved
if (node.getL() instanceof AIdentifierArithmetics) {
lType = findVariableType(
((AIdentifier) ((AIdentifierArithmetics) node.getL()).getIdentifier()).getId().getText());
}
if (node.getR() instanceof AIdentifierArithmetics) {
rType = findVariableType(
((AIdentifier) ((AIdentifierArithmetics) node.getR()).getIdentifier()).getId().getText());
}
// All children must return a number for the expression to be valid
if (rType == VAR_TYPES.NONE || lType == VAR_TYPES.NONE) {
printError(node, ERROR_TYPES.NONE_OPERATION);
} else if (lType == VAR_TYPES.UNKNOWN || rType == VAR_TYPES.UNKNOWN) {
variableTypes.put(node, VAR_TYPES.UNKNOWN);
} else if (lType == VAR_TYPES.INTEGER && rType == VAR_TYPES.INTEGER) {
variableTypes.put(node, VAR_TYPES.INTEGER);
} else if (isNumber(lType) && isNumber(rType)) {
variableTypes.put(node, VAR_TYPES.DOUBLE);
} else {
printError(node.getR(), ERROR_TYPES.TYPE_MISSMATCH);
}
}
@Override
public void outAPlusArithmetics(APlusArithmetics node) {
// Find the class type of left and right children
Class<?> lClass = node.getL().getClass();
Class<?> rClass = node.getR().getClass();
VAR_TYPES lType = variableTypes.get(lClass.cast(node.getL()));
VAR_TYPES rType = variableTypes.get(rClass.cast(node.getR()));
if (node.getL() instanceof AIdentifierArithmetics) {
lType = findVariableType(
((AIdentifier) ((AIdentifierArithmetics) node.getL()).getIdentifier()).getId().getText());
}
if (node.getR() instanceof AIdentifierArithmetics) {
rType = findVariableType(
((AIdentifier) ((AIdentifierArithmetics) node.getR()).getIdentifier()).getId().getText());
}
// The childrens' types must match
if (rType == VAR_TYPES.NONE || lType == VAR_TYPES.NONE) {
printError(node, ERROR_TYPES.NONE_OPERATION);
} else if (lType == VAR_TYPES.UNKNOWN || rType == VAR_TYPES.UNKNOWN) {
variableTypes.put(node, VAR_TYPES.UNKNOWN);
} else if (lType == rType) {
variableTypes.put(node, lType);
} else if (isNumber(lType) && isNumber(rType)) {
variableTypes.put(node, VAR_TYPES.DOUBLE);
} else {
printError(node, ERROR_TYPES.ADD_TYPE_MISSMATCH);
}
}
@Override
public void outAMinusArithmetics(AMinusArithmetics node) {
// Find the class type of left and right children
Class<?> lClass = node.getL().getClass();
Class<?> rClass = node.getR().getClass();
VAR_TYPES lType = variableTypes.get(lClass.cast(node.getL()));
VAR_TYPES rType = variableTypes.get(rClass.cast(node.getR()));
if (node.getL() instanceof AIdentifierArithmetics) {
lType = findVariableType(
((AIdentifier) ((AIdentifierArithmetics) node.getL()).getIdentifier()).getId().getText());
}
if (node.getR() instanceof AIdentifierArithmetics) {
rType = findVariableType(
((AIdentifier) ((AIdentifierArithmetics) node.getR()).getIdentifier()).getId().getText());
}
if (rType == VAR_TYPES.NONE || lType == VAR_TYPES.NONE) {
printError(node, ERROR_TYPES.NONE_OPERATION);
} else if (lType == VAR_TYPES.UNKNOWN || rType == VAR_TYPES.UNKNOWN) {
variableTypes.put(node, VAR_TYPES.UNKNOWN);
} else if (lType == VAR_TYPES.INTEGER && rType == VAR_TYPES.INTEGER) {
variableTypes.put(node, VAR_TYPES.INTEGER);
} else if (lType == VAR_TYPES.STRING || rType == VAR_TYPES.STRING) {
printError(node, ERROR_TYPES.MINUS_TYPE_MISSMATCH);
} else if (FirstVisitor.isNumber(lType) && FirstVisitor.isNumber(rType)) {
variableTypes.put(node, VAR_TYPES.DOUBLE);
} else {
printError(node, ERROR_TYPES.MINUS_TYPE_MISSMATCH);
}
}
@Override
public void outAMultArithmetics(AMultArithmetics node) {
// Same as outAExpArithmetics
// Find the class type of left and right children
Class<?> lClass = node.getL().getClass();
Class<?> rClass = node.getR().getClass();
VAR_TYPES lType = variableTypes.get(lClass.cast(node.getL()));
VAR_TYPES rType = variableTypes.get(rClass.cast(node.getR()));
if (node.getL() instanceof AIdentifierArithmetics) {
lType = findVariableType(
((AIdentifier) ((AIdentifierArithmetics) node.getL()).getIdentifier()).getId().getText());
}
if (node.getR() instanceof AIdentifierArithmetics) {
rType = findVariableType(
((AIdentifier) ((AIdentifierArithmetics) node.getR()).getIdentifier()).getId().getText());
}
// All children must return a number for the expression to be valid
if (rType == VAR_TYPES.NONE || lType == VAR_TYPES.NONE) {
printError(node, ERROR_TYPES.NONE_OPERATION);
} else if (lType == VAR_TYPES.UNKNOWN || rType == VAR_TYPES.UNKNOWN) {
variableTypes.put(node, VAR_TYPES.UNKNOWN);
} else if (lType == VAR_TYPES.INTEGER && rType == VAR_TYPES.INTEGER) {
variableTypes.put(node, VAR_TYPES.INTEGER);
} else if (lType == VAR_TYPES.INTEGER && rType == VAR_TYPES.STRING
|| lType == VAR_TYPES.STRING && rType == VAR_TYPES.INTEGER) {
variableTypes.put(node, VAR_TYPES.STRING);
} else if (isNumber(lType) && isNumber(rType)) {
variableTypes.put(node, VAR_TYPES.DOUBLE);
} else {
printError(node, ERROR_TYPES.TYPE_MISSMATCH);
}
}
@Override
public void outAFunctionArithmetics(AFunctionArithmetics node) {
// Find the function's type using it's identifier
String id = ((AIdentifier) ((AFunctionCall) node.getFunctionCall()).getIdentifier()).getId().getText();
VAR_TYPES type = findVariableType(id);
variableTypes.put(node, type);
}
@Override
public void outADivArithmetics(ADivArithmetics node) {
// Same as outAExpArithmetics
// Find the class type of left and right children
Class<?> lClass = node.getL().getClass();
Class<?> rClass = node.getR().getClass();
VAR_TYPES lType = variableTypes.get(lClass.cast(node.getL()));
VAR_TYPES rType = variableTypes.get(rClass.cast(node.getR()));
if (node.getL() instanceof AIdentifierArithmetics) {
lType = findVariableType(
((AIdentifier) ((AIdentifierArithmetics) node.getL()).getIdentifier()).getId().getText());
}
if (node.getR() instanceof AIdentifierArithmetics) {
rType = findVariableType(
((AIdentifier) ((AIdentifierArithmetics) node.getR()).getIdentifier()).getId().getText());
}
if (rType == VAR_TYPES.NONE || lType == VAR_TYPES.NONE) {
printError(node, ERROR_TYPES.NONE_OPERATION);
} else if (lType == VAR_TYPES.UNKNOWN || rType == VAR_TYPES.UNKNOWN) {
variableTypes.put(node, VAR_TYPES.UNKNOWN);
} else if (isNumber(lType) && isNumber(rType)) {
variableTypes.put(node, VAR_TYPES.DOUBLE);
} else {
printError(node, ERROR_TYPES.TYPE_MISSMATCH);
}
}
// Helper methods
/**
* Prints the appropriate error message
* @param node the node causing the error
* @param type the error's type
*/
public static void printError(Node node, ERROR_TYPES type) {
String message = "Error";
AIdentifier id;
switch (type) {
case UNDECLARED_VARIABLE:
id = (AIdentifier) node;
message += "[" + id.getId().getLine() + "]: Undeclared variable named \'" + id.getId().getText() + "\'.";
break;
case UNDEFINED_FUNCTION:
id = (AIdentifier) node;
message += "[" + id.getId().getLine() + "]: Undefined function named \'" + id.getId().getText() + "\'.";
break;
case UNORDERED_PARAMS:
AFunction function = (AFunction) node;
id = (AIdentifier) function.getIdentifier();
message += "[" + id.getId().getLine() + "]: Parameter after default parameter in function \'" + id.getId().getText() + "\'.";
break;
case WRONG_PARAMS:
id = (AIdentifier) ((AFunctionCall) node).getIdentifier();
message += "[" + id.getId().getLine() + "]: Wrong parameters given for function named \'" + id.getId().getText() + "\'.";
break;
case TYPE_MISSMATCH:
message += ": Variable type missmatch at \'" + node.toString().stripTrailing() + "\'.";
break;
case ADD_TYPE_MISSMATCH:
message += ": Variable type missmatch in addition.";
break;
case MINUS_TYPE_MISSMATCH:
message += ": Variable type missmatch in substraction.";
break;
case NONE_OPERATION:
message += ": Illegal operation with None.";
break;
case IDENTICAL_FUNCTIONS:
break;
default:
message += ": Unknown error.";
}
if (!(node instanceof AGoal)) {
System.err.println(message);
System.exit(-1);
}
}
/**
* Prints the appropriate error messages for more advanced cases
* @param node the node causing the error
* @param type the error's type
* @param name the function's name
*/
public static void printError(Node node, ERROR_TYPES type, String name) {
String message = "Error";
switch (type) {
case IDENTICAL_FUNCTIONS:
message += ": Function \'" + name + "\' already defined with same parameter number.";
break;
default:
message += ": Unknown error.";
}
System.err.println(message);
System.exit(-1);
}
/**
* Given a token's name find the matching variable's type (for AIdentifier)
* @param token the given token's mame
* @return the variable's type
*/
private VAR_TYPES findVariableType(String token) {
for (Node node : variableTypes.keySet()) {
if (node instanceof AIdentifier) {
if (token.trim().equals(((AIdentifier) node).getId().getText().trim())) {
return variableTypes.get(node);
}
}
}
return null;
}
/**
* Finds a number's subtype
* @param number the given number
* @return the number's type (INTEGER or DOUBLE)
*/
public static VAR_TYPES getNumberSubtype(PNumber number) {
if (number instanceof AIntNumber) {
return VAR_TYPES.INTEGER;
} else {
return VAR_TYPES.DOUBLE;
}
}
/**
* Checks if a given variable type is that of a number
* @param type the given variable type
* @return true or false
*/
public static boolean isNumber(VAR_TYPES type) {
return type == VAR_TYPES.INTEGER || type == VAR_TYPES.DOUBLE;
}
// Getters
public Hashtable<String, Node> getVariables() {
return variables;
}
public Hashtable<String, Node> getFunctions() {
return functions;
}
public Hashtable<Node, VAR_TYPES> getVariableTypes() {
return variableTypes;
}
public List<Function> getFunctionList() {
return functionList;
}
}