-
Notifications
You must be signed in to change notification settings - Fork 0
/
SymbolTable.java
421 lines (354 loc) · 14.2 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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import java.util.HashMap;
import java.util.ArrayList;
import java.util.*;
import absyn.*;
public class SymbolTable {
final static int SPACES = 3;
private static String SymbolTableOutPut = "";
private static ArrayList<HashMap> scopes = new ArrayList<HashMap>();
private static HashMap functions = new HashMap();
private static String returnType;
private static Boolean returnExists;
private static Boolean positiveIndex;
SymbolTable( ExpList tree )
{
//add defaults
functions.put("input", new FunDec(-1, "int", "input", null, null));
functions.put("output", new FunDec(-1, "void", "output", new ExpList(new VarDec(-1, "int", "x"), null), null));
enterScopeMessege("Global");
//create variable scope 0
HashMap globals = new HashMap();
scopes.add(globals);
while( tree != null ) {
typeCheck(tree.head, globals);
tree = tree.tail;
}
leaveScopeMessege("Global");
}
static public String returnScopes()
{
return SymbolTableOutPut;
}
static private void enterScopeMessege(String scopeName)
{
int indent = scopes.size() - 1;
if (indent < 0)
indent = 0;
indent(indent);
SymbolTableOutPut += "\n";
indent(indent);
SymbolTableOutPut += "Entering scope " + scopeName + "\n";
}
static private void leaveScopeMessege(String scopeName)
{
int indent = scopes.size();
printScopeVars(scopes.get(scopes.size() - 1));
if (--indent < 0)
indent = 0;
indent(indent);
SymbolTableOutPut += "Leaving scope " + scopeName + "\n";
}
static private void printScopeVars(HashMap scope)
{
scope.forEach((name, var) -> {
indent(scopes.size());
if (var instanceof VarDec)
{
VarDec i = (VarDec)var;
SymbolTableOutPut += "VarDec: " + i.name + ", type: " + i.type + "\n";
}
else if (var instanceof ArrDec)
{
ArrDec i = (ArrDec)var;
SymbolTableOutPut += "ArrDec: " + i.name + ", type: " + i.type + ", size: " + i.size + "\n";
}
else
System.out.println( "Jesus Christmas!" );
});
}
static private void indent( int indents ) {
for (int ind = 0; ind < indents; ind++)
{
for( int i = 0; i < SPACES; i++ )
SymbolTableOutPut += " ";
SymbolTableOutPut += "|";
}
}
static public void typeCheck( ExpList tree, HashMap curScope ) {
while( tree != null ){
typeCheck( tree.head, curScope );
tree = tree.tail;
}
}
static private Object inScopeAs(String name)
{
Object obj;
for (int i = scopes.size() - 1; i >= 0; i--)
{
obj = scopes.get(i).get(name);
if (obj != null)
return obj;
}
return null;
}
static private String typeCheck( Exp tree, HashMap curScope ) {
if( tree instanceof AssignExp )
return typeCheck( (AssignExp)tree, curScope );
else if( tree instanceof VarDec )
typeCheck( (VarDec)tree, curScope );
else if( tree instanceof IfExp )
typeCheck( (IfExp)tree, curScope );
else if( tree instanceof RelOp )
return typeCheck( (RelOp)tree, curScope );
else if( tree instanceof BinOp )
return typeCheck( (BinOp)tree, curScope );
else if( tree instanceof VarExp )
return typeCheck( (VarExp)tree, curScope );
else if( tree instanceof ArrExp )
return typeCheck( (ArrExp)tree, curScope );
else if( tree instanceof FunCall )
return typeCheck( (FunCall)tree, curScope);
else if( tree instanceof IntVal )
return typeCheck( (IntVal)tree, curScope );
else if( tree instanceof ArrDec )
typeCheck( (ArrDec)tree, curScope);
else if( tree instanceof RetExp )
typeCheck( (RetExp)tree, curScope );
else if( tree instanceof ComStmt )
typeCheck( (ComStmt)tree, curScope );
else if( tree instanceof FunDec )
typeCheck( (FunDec)tree, curScope );
else if( tree instanceof WhileExp )
typeCheck( (WhileExp)tree, curScope );
else if( tree == null)
return null;
else
System.out.println( "You didn't define this you dingus: " + tree.toString() +tree.pos );
return null;
}
static private void typeCheck( VarDec tree, HashMap curScope)
{
Object inMap = curScope.get(tree.name);
if (inMap != null)
if (inMap instanceof VarDec)
System.out.println("Error line " + tree.pos + ": Variable \"" + tree.name + "\" already defined at line " + ((VarDec)inMap).pos);
else
System.out.println("Error line " + tree.pos + ": Variable \"" + tree.name + "\" already defined at line " + ((ArrDec)inMap).pos);
else
{
inMap = functions.get(tree.name);
if (inMap != null)
if (((FunDec)inMap).pos == -1)
System.out.println("Error line " + tree.pos + ": Variable \"" + tree.name + "\" trying to redefine built in function");
else
System.out.println("Error line " + tree.pos + ": Variable \"" + tree.name + "\" already defined at line " + ((FunDec)inMap).pos + " as a function");
else
curScope.put(tree.name, tree);
}
}
static private void typeCheck( ArrDec tree, HashMap curScope)
{
Object inMap = curScope.get(tree.name);
if (inMap != null)
System.out.println("Error line " + tree.pos + ": Variable \"" + tree.name + "\" already defined at line " + ((ArrDec)inMap).pos);
else
{
inMap = functions.get(tree.name);
if (inMap != null)
{
if (((FunDec)inMap).pos == -1)
System.out.println("Error line " + tree.pos + ": Variable \"" + tree.name + "\" trying to redefine built in");
else
System.out.println("Error line " + tree.pos + ": Variable \"" + tree.name + "\" already defined at line " + ((ArrDec)inMap).pos + " as a function");
}
else
curScope.put(tree.name, tree);
}
}
static private void typeCheck( FunDec tree, HashMap curScope)
{
Object inMap = functions.get(tree.name);
if (inMap != null)
if (((FunDec)inMap).pos == -1)
System.out.println("Error line " + tree.pos + ": Function " + tree.name + " trying to redefine build in");
else
System.out.println("Error line " + tree.pos + ": Function " + tree.name + " already defined at line " + ((FunDec)inMap).pos);
else
functions.put(tree.name, tree);
returnType = tree.type;
//used to check if a return exists in the following
returnExists = false;
//add new scope
curScope = new HashMap();
scopes.add(curScope);
enterScopeMessege("Function\"" + tree.name + "\"");
//check parameters
typeCheck(tree.paramList, curScope);
//check contents
ComStmt body = (ComStmt)(tree.comStmt);
typeCheck(body.locals, curScope);
typeCheck(body.statements, curScope);
//leaving scope
leaveScopeMessege("Function\"" + tree.name + "\"");
scopes.remove(scopes.size() - 1);
if (tree.type != "void" && !returnExists)
System.out.println("Error line " + tree.pos + ": Function \"" + tree.name + "\" is missing return statement");
}
static private void typeCheck( ComStmt tree, HashMap curScope)
{
curScope = new HashMap();
scopes.add(curScope);
enterScopeMessege("ComStmt");
typeCheck(tree.locals, curScope);
typeCheck(tree.statements, curScope);
leaveScopeMessege("ComStmt");
scopes.remove(scopes.size() - 1);
}
static private void typeCheck( RetExp tree, HashMap curScope)
{
String type = typeCheck(tree.toRet, curScope);
if (type != returnType)
System.out.println("Error line " + tree.pos + ": Mismatching return types. Expected " + returnType + " got " + type);
returnExists = true;
}
static private String typeCheck( IntVal tree, HashMap curScope)
{
if (tree.val < 0) //basic check to see if an array index is negative, may not be robust
positiveIndex = false;
return "int"; //WEW
}
static private void typeCheck( IfExp tree, HashMap curScope)
{
typeCheck(tree.test, curScope);
typeCheck(tree.thenpart, curScope);
//NewScope
if (tree.elsepart != null)
{
typeCheck(tree.elsepart, curScope);
}
}
static private void typeCheck( WhileExp tree, HashMap curScope)
{
typeCheck(tree.test, curScope);
typeCheck(tree.statements, curScope);
}
static private String typeCheck( VarExp tree, HashMap curScope)
{
Object def = inScopeAs(tree.name);
//VarDec def = (VarDec)inScopeAs(tree.name);
if (def == null)
{
System.out.println("Error line " + tree.pos + ": Variable \"" + tree.name + "\" undefined");
return "err";
}
return objToType(def);
}
static private String typeCheck( AssignExp tree, HashMap curScope)
{
String lType = typeCheck(tree.lhs, curScope);
String rType = typeCheck(tree.rhs, curScope);
if (lType != "err" && rType != "err")
if (lType == rType)
return lType;
else
System.out.println("Error line " + tree.pos + ": Mismatching types on assignment. Expected " + lType + " got " + rType);
return "err";
}
static private String objToType(Object obj)
{
if (obj instanceof VarDec)
return ((VarDec)obj).type;
else if (obj instanceof ArrDec)
return ((ArrDec)obj).type;
else if (obj instanceof IntVal)
return "int";
else
System.out.println("Oh Gosh");
return "kek";
}
static private String typeCheck( FunCall tree, HashMap curScope)
{
FunDec def = (FunDec)functions.get(tree.name);
//check if it exists
if (def == null)
{
System.out.println("Error line " + tree.pos + ": Function \"" + tree.name + "\" undefined");
return "err";
}
//check parameter types
ExpList defParam = def.paramList;
ExpList argParam = tree.argList;
String defName;
String defType;
String argType;
while (defParam != null && argParam != null)
{
if (defParam.head instanceof VarDec)
{
defName = ((VarDec)(defParam.head)).name;
defType = ((VarDec)(defParam.head)).type;
}
else //must be ArrDec
{
defName = ((ArrDec)(defParam.head)).name;
defType = ((ArrDec)(defParam.head)).type;
}
argType = typeCheck(argParam.head, curScope);
if (argType != "err" && defType != "err" && argType != defType)
{
System.out.println("Error line " + tree.pos + ": Function argument \"" + defName + "\" is of type " + defType + ". Got " + argType);
}
defParam = defParam.tail;
argParam = argParam.tail;
}
//Could produce more detailed error messege, forgoing this to make sure I finish in time
if (argParam != null)
{
System.out.println("Error line " + tree.pos + ": Too many arguments for function \"" + tree.name + "\"");
}
else if (defParam != null)
{
System.out.println("Error line " + tree.pos + ": Not enough arguments for function \"" + tree.name + "\"");
}
return def.type;
}
static private String typeCheck( ArrExp tree, HashMap curScope)
{
ArrDec def = (ArrDec)inScopeAs(tree.name);
positiveIndex = true;
String type = typeCheck(tree.index, curScope);
if ("int" != type)
System.out.println("Error line " + tree.pos + ": Array index must be int, got " + type);
else if (positiveIndex == false)
System.out.println("Error line " + tree.pos + ": Array index must be positive");
positiveIndex = true; //set it back to true to avoid propagating this error
if (def == null)
{
System.out.println("Error line " + tree.pos + ": Array Variable \"" + tree.name + "\" undefined");
return "err";
}
return def.type;
}
static private String typeCheck( RelOp tree, HashMap curScope)
{
String lType = typeCheck(tree.left, curScope);
String rType = typeCheck(tree.right, curScope);
if (lType != "err" && rType != "err")
if (lType == rType)
return lType;
else
System.out.println("Error line " + tree.pos + ": Mismatching types on comparison. Expected " + lType + " got " + rType);
return "err";
}
static private String typeCheck( BinOp tree, HashMap curScope)
{
String lType = typeCheck(tree.left, curScope);
String rType = typeCheck(tree.right, curScope);
if (lType != "err" && rType != "err")
if (lType == rType)
return lType;
else
System.out.println("Error line " + tree.pos + ": Mismatching types on binary operator. Expected " + lType + " got " + rType);
return "err";
}
}