-
Notifications
You must be signed in to change notification settings - Fork 0
/
SymbolTable.java
213 lines (188 loc) · 7.71 KB
/
SymbolTable.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
import minipython.analysis.DepthFirstAdapter;
import minipython.node.*;
import java.util.ArrayList;
import java.util.Hashtable;
public class SymbolTable extends DepthFirstAdapter {
// variables: defined variables
// fcall: called functions
// fdef: defined functions
private Hashtable<String, Variable> variables;
private Hashtable<String, ArrayList<FunctionCall>> fcall;
private Hashtable<String, ArrayList<FunctionDefinition>> fdef;
boolean isReady = false;
boolean hasError = false;
SymbolTable() {
this.variables = new Hashtable<String, Variable>();
this.fcall = new Hashtable<String, ArrayList<FunctionCall>>();
this.fdef = new Hashtable<String, ArrayList<FunctionDefinition>>();
}
@Override
public void inAAssignStatement(AAssignStatement node) {
String vname = node.getIdentifier().getText();
//resolve the type of the variable
variables.put(vname, new Variable(vname, getExpressionType(node.getExpression())));
}
// called when a variable is used
@Override
public void inAIdentifierExpression(AIdentifierExpression node) {
String vname = node.getIdentifier().getText();
int line = node.getIdentifier().getLine();
int pos = node.getIdentifier().getPos();
if(variables.get(vname)==null){
showError(line, pos, "undefined variable "+vname);
}
}
// This is useful because we assume that a function argument can be referened inside and outside the function
@Override
public void inAIdentifierValue(AIdentifierValue node) {
String vname = node.getIdentifier().getText();
variables.put(vname,new Variable(vname,"argument"));
}
public void caseAFunction(AFunction node) {
inAFunction(node);
if(node.getIdentifier() != null) {
node.getIdentifier().apply(this);
}
{
int sumNonDef = 0;
Object temp[] = node.getIdentifierValue().toArray();
ArrayList<AIdentifierValue> idValuesList = new ArrayList<>();
ArrayList<String> varNames = new ArrayList<>();
PStatement statement = node.getStatement();
for(int i = 0; i < temp.length; i++) {
idValuesList.add((AIdentifierValue) temp[i]);
((PIdentifierValue) temp[i]).apply(this);
if(((AIdentifierValue) temp[i]).getValue()==null){
sumNonDef+=1;
}
varNames.add(((AIdentifierValue) temp[i]).getIdentifier().getText());
}
String fname = node.getIdentifier().getText();
int line = node.getIdentifier().getLine();
int pos = node.getIdentifier().getPos();
FunctionDefinition newFunDef = new FunctionDefinition(fname, line, pos, sumNonDef, temp.length);
// Finding expected argument types
TypeFinder mtf = new TypeFinder(varNames);
System.out.println("Applygin tpy finder for func " + fname);
node.getStatement().apply(mtf);
ArrayList<String> expectedTypes = mtf.getExpectedTypes();
newFunDef.setExpectedTypes(expectedTypes);
System.out.println("Expected types " + expectedTypes);
newFunDef.setIdentifierValues(idValuesList);
newFunDef.setStatement(statement);
if(fdef.get(fname)==null){
fdef.put(fname, new ArrayList<FunctionDefinition>());
fdef.get(fname).add(newFunDef);
}
else{
ArrayList<FunctionDefinition> functions = fdef.get(fname);
for (FunctionDefinition f:functions){
if(newFunDef.redefines(f)){
showError(newFunDef.line, newFunDef.col,"Redefinition of "+fname+" with "+sumNonDef+" arguments");
}
}
fdef.get(fname).add(newFunDef);
}
}
if(node.getStatement() != null) {
node.getStatement().apply(this);
}
outAFunction(node);
}
public void caseAFunctionCall(AFunctionCall node) {
inAFunctionCall(node);
if(node.getIdentifier() != null) {
node.getIdentifier().apply(this);
}
{
Object temp[] = node.getExpression().toArray();
for(int i = 0; i < temp.length; i++)
{
((PExpression) temp[i]).apply(this);
}
String fname = node.getIdentifier().getText();
int line = node.getIdentifier().getLine();
int pos = node.getIdentifier().getPos();
if(fcall.get(fname)==null){
FunctionCall newFunCall = new FunctionCall(fname, line, pos, temp.length);
fcall.put(fname, new ArrayList<FunctionCall>());
fcall.get(fname).add(newFunCall);
}
}
outAFunctionCall(node);
}
@Override
public void outStart(Start node) {
for (String k: fcall.keySet()) {
ArrayList<FunctionCall> funcCalls = fcall.get(k);
if(fdef.get(k)==null){
showError(funcCalls.get(0).line,funcCalls.get(0).column,"Undefined function");
}
else{
ArrayList<FunctionDefinition> allDefinitions = fdef.get(k);
for(FunctionCall funcCall:funcCalls){
boolean isOk = false;
for(FunctionDefinition fd:allDefinitions){
if(fd.matches(funcCall)){
isOk = true;
}
}
if(!isOk) {
showError(funcCall.line, funcCall.column, "Wrong number of parameters given at function "+k);
}
}
}
}
isReady = true;
}
public String getExpressionType(PExpression e){
if (e instanceof AArithmeticExpression
| e instanceof AMinExpression
| e instanceof AMaxExpression){
return "number";
}
if (e instanceof AIdentifierExpression){
String vname = ((AIdentifierExpression) e).getIdentifier().getText();
return this.getVariable(vname).type;
}
if (e instanceof AValueExpression){
AValueExpression aValueExpression = (AValueExpression) e;
if (aValueExpression.getValue() instanceof ANumberValue) {
return "number";
}
}
if (e instanceof AParExpression){
return getExpressionType(((AParExpression) e).getExpression());
}
return "not_number";
}
public FunctionCall getFuncCallObject(AFuncCallExpression expr){
AFunctionCall func = (AFunctionCall) expr.getFunctionCall();
return getFuncCallObject(func);
}
public FunctionCall getFuncCallObject(AFunctionCall call){
Object temp[] = call.getExpression().toArray();
String fname = call.getIdentifier().getText();
int line = call.getIdentifier().getLine();
int pos = call.getIdentifier().getPos();
return new FunctionCall(fname, line, pos, temp.length);
}
public FunctionDefinition getDefinitionForCall(FunctionCall call){
for (FunctionDefinition fd : fdef.get(call.name)){
if (fd.matches(call)){
return fd;
}
}
return null;
}
public Variable getVariable(String s){
return variables.get(s);
}
public Hashtable<String, ArrayList<FunctionDefinition>> getFunctionDefinitions(){
return fdef;
}
public void showError(int line , int col , String message){
System.out.printf("[%d,%d] %s%n" , line , col , message);
hasError = true;
}
}