-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVisitor2.java
72 lines (64 loc) · 2.68 KB
/
Visitor2.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
import minipython.analysis.DepthFirstAdapter;
import minipython.node.*;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
public class Visitor2 extends DepthFirstAdapter {
private Hashtable<String, Variable> variables;
private Hashtable<String, List<Function>> functions;
Visitor2(Hashtable<String, Variable> variables, Hashtable<String, List<Function>> functions) {
this.variables = variables;
this.functions = functions;
}
//Q1
@Override
public void inAIdentifierExpression(AIdentifierExpression node) {
String variable_name = node.getId().getText();
int line = node.getId().getLine();
int pos = node.getId().getPos();
if (!variables.containsKey(variable_name)) {
System.out.println("Error variable " + variable_name + ", line: " + line + " position: "+ pos+ " does not exist");
}
}
@Override
public void inAAssignEqStatement(AAssignEqStatement node) {
String name = node.getId().getText();
int line = node.getId().getLine();
variables.put(name, new Variable(name, null, line));
}
@Override
public void inAFunction(AFunction node) {
LinkedList<AArgument> arguments = node.getArgument(); //size = 0 or 1
AArgument argument;
if (arguments.size() > 0) {
argument = arguments.getFirst();
LinkedList<ANotFirstArgument> nfargs = argument.getNotFirstArgument();
String varName = argument.getId().toString().trim();
int line = argument.getId().getLine();
variables.put(varName, new Variable(varName, null, line));
for (ANotFirstArgument nfarg : nfargs) {
varName = nfarg.getId().toString().trim();
line = nfarg.getId().getLine();
variables.put(varName, new Variable(varName, null, line));
}
}
}
@Override
public void inAFuncCall(AFuncCall node) {
String function_name
= node.getId().getText();
int numOfArgs = node.getExpression().size();
int line = node.getId().getLine();
int pos = node.getId().getPos();
//Q2
if (!functions.containsKey(function_name)) {
System.out.println("Error function " + function_name + ", line: " + line + " position: " + pos + " does not exist");
} else {
//Q3
Function f = Utils.getFunction(functions, function_name, numOfArgs);
if (f == null) {
System.out.println("Error function " + function_name + " with " + numOfArgs + " parameters in line: " + line + " position: " + pos + " does not exist");
}
}
}
}