-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSecondVisitor.java
More file actions
384 lines (331 loc) · 17.5 KB
/
SecondVisitor.java
File metadata and controls
384 lines (331 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
import java.util.*;
import minipython.analysis.DepthFirstAdapter;
import minipython.node.*;
/**
* The SecondVisitor does the second traversal of the code to handle
* function calls that appear before their declaration.
* Additionally, variable declarations located in the body of a function are
* handled by this visitor.
*
* @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 SecondVisitor extends DepthFirstAdapter {
private final Hashtable<String, Node> variables;
private final Hashtable<String, Node> functions;
private final Hashtable<Node, FirstVisitor.VAR_TYPES> variableTypes;
public SecondVisitor(Hashtable<String, Node> variables, Hashtable<String, Node> functions,
Hashtable<Node, FirstVisitor.VAR_TYPES> variableTypes) {
this.variables = variables;
this.functions = functions;
this.variableTypes = variableTypes;
}
@Override
@SuppressWarnings("unchecked")
public void outAFunctionCall(AFunctionCall node) {
// Get the function's name
AIdentifier identifier = (AIdentifier) node.getIdentifier();
String name = identifier.getId().getText();
// Ensure that the function has been defined
if (!functions.containsKey(name)) {
FirstVisitor.printError(identifier, FirstVisitor.ERROR_TYPES.UNDEFINED_FUNCTION);
return;
}
// Retrieve the arguments from the function's definition
AFunction function = (AFunction) (functions.get(name).parent());
LinkedList<AArgument> arguments = function.getArgument();
List<String> expectedArguments = new ArrayList<>();
// Cast the first argument into an AIdentifier and then retrieve the name of the token
int defaultIndex = 0;
if (arguments.size() > 0) {
expectedArguments.add(((AIdentifier) arguments.get(0).getIdentifier()).getId().getText());
if (arguments.get(0).getAssignValue().size() > 0) {
defaultIndex = 1;
}
for (AMoreAssignments argument : ((LinkedList<AMoreAssignments>) arguments.get(0).getMoreAssignments())) {
expectedArguments.add(((AIdentifier) argument.getIdentifier()).getId().getText());
// Check for default parameters
LinkedList<AAssignValue> value = argument.getAssignValue();
if (value.size() > 0 && defaultIndex == 0) {
defaultIndex = expectedArguments.size();
} else if (value.size() == 0 && defaultIndex > 0) {
FirstVisitor.printError(function, FirstVisitor.ERROR_TYPES.UNORDERED_PARAMS);
}
}
}
// Get the arguments from the call statement
LinkedList<AArgList> argumentsCall = node.getArgList();
List<PArithmetics> givenArguments = new ArrayList<>();
// Cast and then get the arguments' values from the call statement
if (argumentsCall.size() > 0) {
givenArguments.add(argumentsCall.get(0).getArithmetics());
for (ACommaExpr argument : (LinkedList<ACommaExpr>) argumentsCall.get(0).getCommaExpr()) {
givenArguments.add(argument.getArithmetics());
}
}
// Compare the arguments from the definition with the ones from the call
if (givenArguments.size() < defaultIndex - 1 || givenArguments.size() > expectedArguments.size()) {
FirstVisitor.printError(node, FirstVisitor.ERROR_TYPES.WRONG_PARAMS);
}
// Check that the correct number of parameters has been given
FirstVisitor.VAR_TYPES type;
if (argumentsCall.size() > 0) {
LinkedList<AMoreAssignments> params = arguments.get(0).getMoreAssignments();
LinkedList<ACommaExpr> args = argumentsCall.get(0).getCommaExpr();
for (int i = 0; i < expectedArguments.size(); i++) {
if (i < givenArguments.size()) {
// Get the argument's type from the function's call
if (i == 0) {
type = variableTypes.get(givenArguments.get(i));
variableTypes.put(arguments.get(0).getIdentifier(), type);
} else {
type = variableTypes.get(args.get(i - 1).getArithmetics());
variableTypes.put(params.get(i - 1).getIdentifier(), type);
}
} else { // Default values included
// Use the default value to set the variable's type
AMoreAssignments value = params.get(i - 1);
type = variableTypes.get(((LinkedList<AAssignValue>) value.getAssignValue()).get(0).getArithmetics());
variableTypes.put(params.get(i - 1).getIdentifier(), type);
}
}
} else if (arguments.size() > 0) { // Only default parameters present
LinkedList<AAssignValue> firstValue = arguments.get(0).getAssignValue();
if (firstValue.size() > 0) {
type = variableTypes.get(firstValue.get(0).getArithmetics());
variableTypes.put(arguments.get(0).getIdentifier(), type);
}
LinkedList<AAssignValue> value;
for (AMoreAssignments param : (LinkedList<AMoreAssignments>) arguments.get(0).getMoreAssignments()) {
value = param.getAssignValue();
type = variableTypes.get(value.get(0).getArithmetics());
variableTypes.put(param.getIdentifier(), type);
}
}
if (function.getStatement() != null) {
function.getStatement().apply(this);
}
}
@Override
public void outANumberArithmetics(ANumberArithmetics node) {
variableTypes.put(node, FirstVisitor.getNumberSubtype(node.getNumber()));
}
@Override
public void outAStrlitArithmetics(AStrlitArithmetics node) {
variableTypes.put(node, FirstVisitor.VAR_TYPES.STRING);
}
@Override
public void outANoneArithmetics(ANoneArithmetics node) {
variableTypes.put(node, FirstVisitor.VAR_TYPES.NONE);
}
@Override
public void outALenArithmetics(ALenArithmetics node) {
variableTypes.put(node, FirstVisitor.VAR_TYPES.INTEGER);
}
@Override
public void outAMaxminArithmetics(AMaxminArithmetics node) {
variableTypes.put(node, FirstVisitor.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();
FirstVisitor.VAR_TYPES lType = variableTypes.get(lClass.cast(node.getL()));
FirstVisitor.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 == FirstVisitor.VAR_TYPES.NONE || lType == FirstVisitor.VAR_TYPES.NONE) {
FirstVisitor.printError(node, FirstVisitor.ERROR_TYPES.NONE_OPERATION);
} else if (lType == FirstVisitor.VAR_TYPES.UNKNOWN || rType == FirstVisitor.VAR_TYPES.UNKNOWN) {
variableTypes.put(node, FirstVisitor.VAR_TYPES.UNKNOWN);
} else if (lType == FirstVisitor.VAR_TYPES.INTEGER && rType == FirstVisitor.VAR_TYPES.INTEGER) {
variableTypes.put(node, FirstVisitor.VAR_TYPES.INTEGER);
} else if (FirstVisitor.isNumber(lType) && FirstVisitor.isNumber(rType)) {
variableTypes.put(node, FirstVisitor.VAR_TYPES.DOUBLE);
} else {
FirstVisitor.printError(node.getR(), FirstVisitor.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();
FirstVisitor.VAR_TYPES lType = variableTypes.get(lClass.cast(node.getL()));
FirstVisitor.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 == FirstVisitor.VAR_TYPES.NONE || lType == FirstVisitor.VAR_TYPES.NONE) {
FirstVisitor.printError(node, FirstVisitor.ERROR_TYPES.NONE_OPERATION);
} else if (lType == FirstVisitor.VAR_TYPES.UNKNOWN || rType == FirstVisitor.VAR_TYPES.UNKNOWN) {
variableTypes.put(node, FirstVisitor.VAR_TYPES.UNKNOWN);
} else if (lType == rType) {
variableTypes.put(node, lType);
} else if (FirstVisitor.isNumber(lType) && FirstVisitor.isNumber(rType)) {
variableTypes.put(node, FirstVisitor.VAR_TYPES.DOUBLE);
} else {
FirstVisitor.printError(node, FirstVisitor.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();
FirstVisitor.VAR_TYPES lType = variableTypes.get(lClass.cast(node.getL()));
FirstVisitor.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 == FirstVisitor.VAR_TYPES.NONE || lType == FirstVisitor.VAR_TYPES.NONE) {
FirstVisitor.printError(node, FirstVisitor.ERROR_TYPES.NONE_OPERATION);
} else if (lType == FirstVisitor.VAR_TYPES.UNKNOWN || rType == FirstVisitor.VAR_TYPES.UNKNOWN) {
variableTypes.put(node, FirstVisitor.VAR_TYPES.UNKNOWN);
} else if (lType == FirstVisitor.VAR_TYPES.INTEGER && rType == FirstVisitor.VAR_TYPES.INTEGER) {
variableTypes.put(node, FirstVisitor.VAR_TYPES.INTEGER);
} else if (lType == FirstVisitor.VAR_TYPES.STRING || rType == FirstVisitor.VAR_TYPES.STRING) {
FirstVisitor.printError(node, FirstVisitor.ERROR_TYPES.MINUS_TYPE_MISSMATCH);
} else if (FirstVisitor.isNumber(lType) && FirstVisitor.isNumber(rType)) {
variableTypes.put(node, FirstVisitor.VAR_TYPES.DOUBLE);
} else {
FirstVisitor.printError(node, FirstVisitor.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();
FirstVisitor.VAR_TYPES lType = variableTypes.get(lClass.cast(node.getL()));
FirstVisitor.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 == FirstVisitor.VAR_TYPES.NONE || lType == FirstVisitor.VAR_TYPES.NONE) {
FirstVisitor.printError(node, FirstVisitor.ERROR_TYPES.NONE_OPERATION);
} else if (lType == FirstVisitor.VAR_TYPES.UNKNOWN || rType == FirstVisitor.VAR_TYPES.UNKNOWN) {
variableTypes.put(node, FirstVisitor.VAR_TYPES.UNKNOWN);
} else if (lType == FirstVisitor.VAR_TYPES.INTEGER && rType == FirstVisitor.VAR_TYPES.INTEGER) {
variableTypes.put(node, FirstVisitor.VAR_TYPES.INTEGER);
} else if (lType == FirstVisitor.VAR_TYPES.INTEGER && rType == FirstVisitor.VAR_TYPES.STRING
|| lType == FirstVisitor.VAR_TYPES.STRING && rType == FirstVisitor.VAR_TYPES.INTEGER) {
variableTypes.put(node, FirstVisitor.VAR_TYPES.STRING);
} else if (FirstVisitor.isNumber(lType) && FirstVisitor.isNumber(rType)) {
variableTypes.put(node, FirstVisitor.VAR_TYPES.DOUBLE);
} else {
FirstVisitor.printError(node, FirstVisitor.ERROR_TYPES.TYPE_MISSMATCH);
}
}
@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();
FirstVisitor.VAR_TYPES lType = variableTypes.get(lClass.cast(node.getL()));
FirstVisitor.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 == FirstVisitor.VAR_TYPES.NONE || lType == FirstVisitor.VAR_TYPES.NONE) {
FirstVisitor.printError(node, FirstVisitor.ERROR_TYPES.NONE_OPERATION);
} else if (lType == FirstVisitor.VAR_TYPES.UNKNOWN || rType == FirstVisitor.VAR_TYPES.UNKNOWN) {
variableTypes.put(node, FirstVisitor.VAR_TYPES.UNKNOWN);
} else if (FirstVisitor.isNumber(lType) && FirstVisitor.isNumber(rType)) {
variableTypes.put(node, FirstVisitor.VAR_TYPES.DOUBLE);
} else {
FirstVisitor.printError(node, FirstVisitor.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();
// Find the function's definition
if (findVariableType(id) != FirstVisitor.VAR_TYPES.NONE) {
AFunction function = (AFunction) (functions.get(id).parent());
if (function.getStatement() instanceof AReturnStatement) {
function.getStatement().apply(new FirstVisitor(variables, functions, variableTypes));
// The function call has the same type as the function's return statement
PArithmetics arithmetics = ((AReturnStatement) function.getStatement()).getArithmetics();
// variableTypes.put(function.getIdentifier(), variableTypes.get(arithmetics)); // -0
variableTypes.put(node, variableTypes.get(arithmetics));
}
}
}
// Helper methods
/**
* Given a variable's name, return its type
* @param token the variable's name
* @return the variable's type
*/
private FirstVisitor.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;
}
// Getters
public Hashtable<String, Node> getVariables() {
return variables;
}
public Hashtable<String, Node> getFunctions() {
return functions;
}
public Hashtable<Node, FirstVisitor.VAR_TYPES> getVariableTypes() {
return variableTypes;
}
}