-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSymbolTable.java
288 lines (271 loc) · 12.5 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
import visitor.GJDepthFirst;
import syntaxtree.*;
import java.util.*;
import java.io.*;
class SymbolTable{
LinkedHashMap<String, Classes> classes;
LinkedHashMap<String, Methods> methods;
Classes currentclass, inclass;
Methods currentmethod;
static String scope;
static String methodpars;
static int numpars;
static int numargs;
public SymbolTable(){
this.classes = new LinkedHashMap<String, Classes>();
this.methods = new LinkedHashMap<String, Methods>();
this.scope = ""; this.methodpars = ""; this.numpars = 0; this.numargs = 0;
}
public void putvar(SymbolTable oldst, String nclass, String nmethod, Variables nvar, String scope){
if(scope.equals("Class")){
if(classes.get(nclass)!=null){
if(nvar.type.equals("int") || nvar.type.equals("boolean") || nvar.type.equals("int[]") || nvar.type.equals("boolean[]"))
classes.get(nclass).vars.put(nvar.name, nvar);
else{
if(oldst!=null){
if(oldst.classes.get(nvar.type)!=null) classes.get(nclass).vars.put(nvar.name, nvar);
else {
throw new RuntimeException("Cannot find symbol: "+nvar.type+" in var declaration.");
}
}
}
}
}
else if(scope.equals("Vars")){
if(methods.get(nclass+nmethod)!=null){ methods.get(nclass+nmethod).vars.put(nvar.name, nvar); }
}
else if(scope.equals("Args")){
if(methods.get(nclass+nmethod)!=null){ methods.get(nclass+nmethod).args.put(nvar.name, nvar); }
}
}
public void vardecl(String nclass, String nmethod, Variables nvar, String scope){
if(scope.equals("Class")){
if(classes.get(nclass)!=null){
if(classes.get(nclass).vars.get(nvar.name)==null){
classes.get(nclass).vars.put(nvar.name, nvar);
}
else{
throw new RuntimeException("Variable "+nvar.name+" is already defined in class.");
}
}
}
else if(scope.equals("Vars")){
if(methods.get(nclass+nmethod)!=null){
if((methods.get(nclass+nmethod).args.get(nvar.name)==null) && (methods.get(nclass+nmethod).vars.get(nvar.name)==null)){
methods.get(nclass+nmethod).vars.put(nvar.name, nvar);
}
else{
throw new RuntimeException("Variable "+nvar.name+" is already defined in method.");
}
}
}
else if(scope.equals("Args")){
if(methods.get(nclass+nmethod)!=null){
if(methods.get(nclass+nmethod).args.get(nvar.name)==null){
methods.get(nclass+nmethod).args.put(nvar.name, nvar);
}
else{
throw new RuntimeException("Variable "+nvar.name+" is already defined in method.");
}
}
}
}
public Variables findvar(String nclass, String nmethod, String nvar, boolean gencode){
Variables var=null;
boolean not_found=true;
inclass = null;
if(methods.get(nclass+nmethod)!=null){
if(methods.get(nclass+nmethod).vars.get(nvar)!=null) var = methods.get(nclass+nmethod).vars.get(nvar);
else if(methods.get(nclass+nmethod).args.get(nvar)!=null) var = methods.get(nclass+nmethod).args.get(nvar);
else if(classes.get(nclass)!=null){
if(classes.get(nclass).vars.get(nvar)!=null) {
var = classes.get(nclass).vars.get(nvar);
if(gencode) inclass = classes.get(nclass);
}
else{
String parentname;
if(classes.get(nclass).parent!=null){
parentname = classes.get(nclass).parent;
while(not_found){
if(classes.get(parentname).vars.get(nvar)!=null) {
var = classes.get(parentname).vars.get(nvar);
if(gencode) inclass = classes.get(parentname);
not_found = false;
}
else{
if(classes.get(parentname).parent!=null) parentname = classes.get(parentname).parent;
else not_found = false;
}
}
}
}
}
}
return var;
}
public Methods findmethod(SymbolTable oldst, String nclass, String nmethod, String primname, String methodname){
Methods method = null;
Variables var = findvar(nclass, nmethod, primname, false);
boolean not_found = true;
if(var == null) nclass = primname;
else nclass = var.type;
nmethod = methodname;
if(oldst.classes.get(nclass)!=null){
if(oldst.methods.get(nclass+nmethod)!=null) method = oldst.methods.get(nclass+nmethod);
else{
String parentname;
if(oldst.classes.get(nclass).parent!=null){
parentname = oldst.classes.get(nclass).parent;
while(not_found){
if(oldst.methods.get(parentname+nmethod)!=null) {
method = oldst.methods.get(parentname+nmethod);
not_found = false;
}
else{
if(oldst.classes.get(parentname).parent!=null) parentname = oldst.classes.get(parentname).parent;
else not_found = false;
}
}
}
}
}
return method;
}
public boolean checkparent(SymbolTable oldst, String classA, String classB){
// Check if classB is subclass of classA (for multilevel inheritance), for assignment: eg. A a = new B()
boolean returned = false;
Classes parent;
boolean not_found = false;
if((parent = oldst.classes.get(classB))!=null){
if(oldst.classes.get(classA)!=null){
while(!not_found){
if(parent.name.equals(classA)){ not_found = true; returned = true; }
else{
if(oldst.classes.get(parent.parent)!=null) parent = oldst.classes.get(parent.parent);
else not_found = true;
}
}
}
}
return returned;
}
public boolean isclasstype(SymbolTable oldst, String classname){
boolean returned = false;
if(oldst.classes.get(classname)!=null){ returned = true; }
return returned;
}
// -1 -> not found similar method, -2-> found similar method NOT SAME, 0>-> found similar method AND SAME
public int overloading(Classes nclass, Methods nmethod){
int returned = -1;
Classes parent;
Methods parentmethod;
boolean not_found = false;
if((parent = classes.get(nclass.parent))!=null){
while(!not_found){
if((parentmethod = methods.get(parent.name+nmethod.name))!=null){
if(parentmethod.name.equals(nmethod.name)){
returned = -2;
if(parentmethod.type.equals(nmethod.type)){
returned = -2;
if(ArgstoString(nmethod.args).equals(ArgstoString(parentmethod.args))){
not_found = true; returned = parentmethod.offset;
}
}
}
}
if(classes.get(parent.parent)!=null) parent = classes.get(parent.parent);
else not_found = true;
}
}
return returned;
}
public String ArgstoString(LinkedHashMap<String, Variables> hm){
String returned = "";
for (String key : hm.keySet()) {
if(returned.equals("")) returned = hm.get(key).type;
else returned = returned+","+hm.get(key).type;
}
return returned;
}
public void addoffsets(){
int i=0;
for (String keyclass : classes.keySet()) {
if(i!=0){
// System.out.println("-----------Class "+classes.get(keyclass).name+"-----------");
// System.out.println("--Variables---");
boolean inparent = false;
for (String keyvars : classes.get(keyclass).vars.keySet()) {
String parentname;
if((!inparent) && ((parentname = classes.get(keyclass).parent)!=null)){
classes.get(keyclass).varoffset = classes.get(parentname).varoffset;
inparent = true;
}
classes.get(keyclass).vars.get(keyvars).offset = classes.get(keyclass).varoffset;
Variables var = classes.get(keyclass).vars.get(keyvars);
// System.out.println(classes.get(keyclass).name+"."+var.name+" : "+var.offset);
if(var.type.equals("int")) classes.get(keyclass).varoffset+=4;
else if(var.type.equals("boolean")) classes.get(keyclass).varoffset+=1;
else classes.get(keyclass).varoffset+=8;
}
// System.out.println("---Methods---");
boolean inmethparent = false;
for (String keymethod : methods.keySet()) {
if(methods.get(keymethod).classpar.equals(classes.get(keyclass).name)){
String parentname;
boolean overl = false;
if((parentname = classes.get(keyclass).parent)!=null){
if(!inmethparent) {
classes.get(keyclass).methodoffset = classes.get(parentname).methodoffset;
inmethparent = true;
}
int methodoffset;
if((methodoffset = overloading(classes.get(keyclass), methods.get(keymethod)))>=0){
methods.get(keymethod).offset = methodoffset;
overl = true;
}
}
if(!overl){
methods.get(keymethod).offset = classes.get(keyclass).methodoffset;
Methods method = methods.get(keymethod);
// System.out.println(classes.get(keyclass).name+"."+method.name+" : "+method.offset);
classes.get(keyclass).methodoffset+=8;
}
}
}
// System.out.println("\n");
}
i++;
}
}
public int sizeClass(String nclass){
int size=0;
Classes parent;
while((parent = classes.get(nclass))!=null){
for (String keyvars : classes.get(nclass).vars.keySet()) {
if("int".equals(classes.get(nclass).vars.get(keyvars).type)) size+=4;
else if ("boolean".equals(classes.get(nclass).vars.get(keyvars).type)) size+=1;
else size+=8;
}
nclass = classes.get(nclass).parent;
}
return size;
}
public void IterateSymbolTable(){
System.out.println("\n\nLINKEDHASHMAP printing: ");
for (String keyclass : classes.keySet()) {
System.out.println(classes.get(keyclass).name+" : \t("+classes.get(keyclass).parent+")");
for (String keyvars : classes.get(keyclass).vars.keySet()) {
System.out.println("\t"+classes.get(keyclass).vars.get(keyvars).type+" "+classes.get(keyclass).vars.get(keyvars).name);
}
}
for (String keymethod : methods.keySet()) {
System.out.println(methods.get(keymethod).type+" "+methods.get(keymethod).name);
for (String keyvars : methods.get(keymethod).args.keySet()) {
System.out.println("\t["+methods.get(keymethod).args.get(keyvars).type+" "+methods.get(keymethod).args.get(keyvars).name+"]");
}
for (String keyvars : methods.get(keymethod).vars.keySet()) {
System.out.println("\t"+methods.get(keymethod).vars.get(keyvars).type+" "+methods.get(keymethod).vars.get(keyvars).name);
}
}
}
}