-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypeChecker.java
292 lines (266 loc) · 11.8 KB
/
TypeChecker.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
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
import minipython.analysis.DepthFirstAdapter;
import minipython.node.*;
import java.util.*;
/**
* Adapter class responsible for performing type checks
*/
public class TypeChecker extends DepthFirstAdapter
{
SymbolTable symbolTable;
HashMap<String,Variable> variables;
boolean hasError = false;
// Indicates if the adapter is inside a function definition
boolean inAFunction = false;
public TypeChecker(SymbolTable symbolTable) {
this.symbolTable = symbolTable;
variables = new HashMap<>();
Hashtable<String, ArrayList<FunctionDefinition>> fdefs = symbolTable.getFunctionDefinitions();
// Because when fdefs are created the symbol table is not ready we need to translate them now
for (String k : fdefs.keySet()) {
for (FunctionDefinition fd : fdefs.get(k)) {
if(fd.retType.equals("to_be_decided")) {
fd.retDef = symbolTable.getDefinitionForCall(
symbolTable.getFuncCallObject((AFunctionCall) fd.retFuncCall)
);
}
}
}
// Algorithm pls...
// If the function has a arithm expression return type, ret type is number
// If the function has func call ret statement -> ret type is to be defined
for (String k : fdefs.keySet()){
for (FunctionDefinition fd : fdefs.get(k)) {
FunctionDefinition initial = fd;
FunctionDefinition current = fd;
HashSet<FunctionDefinition> visited = new HashSet<>();
visited.add(current);
boolean found = false;
while (current.retType.equals("to_be_decided")) {
// get functiondefinition of the returned function
// TODO get function def
FunctionDefinition returnfdef = current.getReturnDef();
// If there is a cycle in the call graph, we can't infer the type so we set as unknown
if (visited.contains(returnfdef)) {
// set the final ret type
initial.retType = "unknown";
found = true;
break;
}
visited.add(returnfdef);
// Advance to next function
current = returnfdef;
}
if (!found) {
initial.retType = current.retType;
}
}
}
}
@Override
public void inAFunction(AFunction node) {
super.inAFunction(node);
inAFunction = true;
}
@Override
public void outAFunction(AFunction node) {
super.inAFunction(node);
inAFunction = false;
}
@Override
public void inAFunctionCall(AFunctionCall node) {
String func_name = node.getIdentifier().getText();
LinkedList args = node.getExpression();
Object[] argsArray = args.toArray();
FunctionDefinition def = symbolTable.getDefinitionForCall(symbolTable.getFuncCallObject(node));
ArrayList<String> expectedTypes = def.getExpectedTypes();
FunctionCall funcCallObject = symbolTable.getFuncCallObject(node);
// Expected types is larger than args because it coantains types for default arguments as well
for (int i = 0 ; i < argsArray.length ; i++){
PExpression expr = (PExpression) argsArray[i];
String type = getExpressionType(expr);
if (!type.equals(expectedTypes.get(i)) && !expectedTypes.get(i).equals("any")){
showError(funcCallObject.line, funcCallObject.column, "Unexpected type " + type + " as " +
"function argument");
System.out.println("Expected types " + expectedTypes);
System.out.println("Current type " + type);
}
}
}
// TODO get expected types of function return values (if they have) what do we do when they do not return anything though
@Override
public void inAAssignStatement(AAssignStatement node) {
String vname = node.getIdentifier().getText();
if(node.getExpression() instanceof AValueExpression){
PValue value = ((AValueExpression) node.getExpression()).getValue();
if(value instanceof ANoneValue){
// none
variables.put(vname,new Variable(vname,"none"));
}
else if(value instanceof ANumberValue){
//number
variables.put(vname,new Variable(vname,"number"));
}
else if(value instanceof AStringValue){
//string
variables.put(vname,new Variable(vname,"string"));
}
}
else if(node.getExpression() instanceof AListDefExpression){
variables.put(vname,new Variable(vname,"list"));
}
else if(node.getExpression() instanceof AIdentifierExpression){
variables.put(vname,new Variable(vname,"identifier"));
}
else if(node.getExpression() instanceof AFuncCallExpression){
variables.put(vname,new Variable(vname,"function call"));
}
else if(node.getExpression() instanceof AArithmeticExpression|
node.getExpression() instanceof AMinExpression|
node.getExpression() instanceof AMaxExpression){
variables.put(vname,new Variable(vname,"number"));
}
else if(node.getExpression() instanceof ASubscriptionExpression){
variables.put(vname,new Variable(vname,"unknown"));
}
// another problem , we need to refactor this to a general method of finding types of expression
else if(node.getExpression() instanceof AParExpression){
variables.put(vname,new Variable(vname,"unknown"));
}
else{
//number
variables.put(vname,new Variable(vname,"unknown"));
}
}
// TODO we need to know if this is an arithmetic expression inside of a function ( we dont know the arguments types)
@Override
public void outAArithmeticExpression(AArithmeticExpression node) {
PExpression left = node.getE1();
PExpression right = node.getE2();
int line = getBinopLine(node.getBinop());
int pos = getBinopCol(node.getBinop());
// checks if Arithmetic expressions(including addone and minusone) have the right number of operands
if (expressionIsNone(left) || (expressionIsNone(right) &&
(!(node.getBinop() instanceof AAddoneBinop) || !(node.getBinop() instanceof AMinusBinop)))){
showError(line , pos, "An arithmetic expression cannot have None value operands");
}
// All further checks can't be procesed if we are inside a function definition (we don't know arguments types)
if (inAFunction){
return;
}
// TODO if return type of function is not a number show error
// if left is function call expression and ret type is not number show error
if (left instanceof AFuncCallExpression){
AFuncCallExpression functionCall= ((AFuncCallExpression) left);
FunctionCall call = symbolTable.getFuncCallObject(functionCall);
FunctionDefinition fdef = symbolTable.getDefinitionForCall(call);
System.out.println("Return type of fnc 1 " + fdef.retType );
if (!(fdef.retType.equals("number") || fdef.retType.equals("unknown"))){
showError(line , pos, "Invalid type for arithmetic expression");
}
}
// if right is function call expression and ret type is not number show error
if (right instanceof AFuncCallExpression){
AFuncCallExpression functionCall= ((AFuncCallExpression) right);
FunctionCall call = symbolTable.getFuncCallObject(functionCall);
FunctionDefinition fdef = symbolTable.getDefinitionForCall(call);
System.out.println("Return type of fnc 2 " + fdef.retType );
if (!(fdef.retType.equals("number") || fdef.retType.equals("unknown")) ){
showError(line , pos, "Invalid type for arithmetic expression");
}
}
// Todo check identifiers type ..
if (right instanceof AIdentifierExpression){
String name = ((AIdentifierExpression) right).getIdentifier().getText();
Variable x = variables.get(name);
System.out.println("Left variable name " + name);
if (!(x.type.equals("number") || x.type.equals("unknown"))){
showError(line , pos, "Invalid type for arithmetic expression");
}
System.out.println("Left variable type " + x.type);
}
if (left instanceof AIdentifierExpression){
String name = ((AIdentifierExpression) left).getIdentifier().getText();
Variable x = variables.get(name);
System.out.println("Right variable name " + name);
if (!(x.type.equals("number") || x.type.equals("unknown"))){
showError(line , pos, "Invalid type for arithmetic expression");
}
System.out.println("Right variable type " + x.type);
}
}
public static boolean expressionIsNone(PExpression e){
if (e instanceof AValueExpression){
AValueExpression v = (AValueExpression) e;
return v.getValue() instanceof ANoneValue;
}
return false;
}
// TODO Easier way to do this ??
public static int getBinopLine(PBinop op){
if (op instanceof AMinusBinop){
return ((AMinusBinop) op).getMinus().getLine();
}
if (op instanceof APlusBinop){
return ((APlusBinop) op).getPlus().getLine();
}
if (op instanceof ADivBinop){
return ((ADivBinop) op).getDiv().getLine();
}
if (op instanceof AMultBinop){
return ((AMultBinop) op).getMult().getLine();
}
if (op instanceof AModuloBinop){
return ((AModuloBinop) op).getMode().getLine();
}
if (op instanceof APowBinop){
return ((APowBinop) op).getDmult().getLine();
}
return -1;
}
public static int getBinopCol(PBinop op){
if (op instanceof AMinusBinop){
return ((AMinusBinop) op).getMinus().getPos();
}
if (op instanceof APlusBinop){
return ((APlusBinop) op).getPlus().getPos();
}
if (op instanceof ADivBinop){
return ((ADivBinop) op).getDiv().getPos();
}
if (op instanceof AMultBinop){
return ((AMultBinop) op).getMult().getPos();
}
if (op instanceof AModuloBinop){
return ((AModuloBinop) op).getMode().getPos();
}
if (op instanceof APowBinop){
return ((APowBinop) op).getDmult().getPos();
}
return -1;
}
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 symbolTable.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 void showError(int line , int col , String message){
System.out.printf("[%d,%d] %s%n" , line , col , message);
hasError = true;
}
}