-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVisitor1.java
84 lines (77 loc) · 3.32 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
import minipython.analysis.DepthFirstAdapter;
import minipython.node.*;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
public class Visitor1 extends DepthFirstAdapter {
private Hashtable symtable;
Visitor1(Hashtable symtable) {
this.symtable = symtable;
}
private String getValueType(PValue value) {
if (value instanceof AStringValue) return "string";
else if (value instanceof ANumberValue) return "number";
else if (value instanceof ANoneValue) return "none";
return null;
}
private void putToFunctions(Function f) {
boolean check = true;
if (!symtable.containsKey(f.getName())) {
List functions = new LinkedList();
functions.add(f);
symtable.put(f.getName(), functions);
} else {
List<Function> functions = (List) symtable.get(f.getName());
int allArgs = f.getNumOfAllParameters();
int nonDefaults = allArgs - f.getNumOfDefaultParameters();
for (Function function : functions) {
if (function.getNumOfAllParameters() == allArgs ||
(function.getNumOfAllParameters() - function.getNumOfDefaultParameters()) == nonDefaults) {
check = false;
System.out.println("Error on line: "+ f.getLine()+", Function " + f.getName() + " with "+ f.getNumOfAllParameters() + " arguments is already defined on line: " + function.getLine());
}
}
if (check) {
functions.add(f);
symtable.put(f.getName(), functions);
}
}
}
// TODO: Check if parameter already exists in argumentsArray
@Override
public void inAFunction(AFunction node) {
String name = node.getId().toString().trim();
int line = node.getId().getLine();
LinkedList<AArgument> arguments = node.getArgument(); //size = 0 or 1
AArgument argument;
Variable[] argumentsArray = null;
int defaultArgsNum = 0;
if (arguments.size() > 0) {
argument = arguments.getFirst();
LinkedList<ANotFirstArgument> nfargs = argument.getNotFirstArgument();
argumentsArray = new Variable[nfargs.size() + 1];
String varName = argument.getId().toString().trim();
String varType = null;
if (argument.getValue().size() > 0) {
varType = getValueType((PValue) (argument.getValue().getFirst()));
defaultArgsNum++;
}
argumentsArray[0] = new Variable(varName, varType, line);
int i = 1;
for (ANotFirstArgument nfarg : nfargs) {
varName = nfarg.getId().toString().trim();
varType = null;
if (nfarg.getValue().size() > 0) {
varType = getValueType((PValue) (nfarg.getValue().getFirst()));
defaultArgsNum++;
}
argumentsArray[i] = new Variable(varName, varType);
i++;
}
}
int numOfAllParams = 0;
if (argumentsArray != null) numOfAllParams = argumentsArray.length;
Function f = new Function(name, line, defaultArgsNum, numOfAllParams, node.getStatement(), argumentsArray);
putToFunctions(f);
}
}