Repository navigation
Expand file tree
/
Copy pathSTBuilder.java
More file actions
90 lines (63 loc) · 2.24 KB
/
Copy pathSTBuilder.java
File metadata and controls
90 lines (63 loc) · 2.24 KB
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
import syntaxtree.*;
import visitor.*;
import symboltable.GlobalTable;
import java.util.List;
import symboltable.ClassSymbol;
import symboltable.MethodSymbol;
import symboltable.VarSymbol;
import util.Util;
class STBuilder extends DepthFirstVisitor {
GlobalTable globalTable = new GlobalTable();
ClassSymbol currentClass = null;
MethodSymbol currentMethod = null;
@Override
public void visit(MainClass n) throws Exception {
String mainClassName = n.f1.f0.tokenImage;
ClassSymbol newClass = globalTable.addClass(mainClassName, null);
currentMethod = newClass.setMainMethod(n.f11.f0.tokenImage);
currentClass = newClass;
super.visit(n);
currentMethod = null;
currentClass = null;
}
@Override
public void visit(ClassDeclaration n) throws Exception {
String className = n.f1.f0.tokenImage;
ClassSymbol newClass = globalTable.addClass(className, null);
currentClass = newClass;
super.visit(n);
currentClass = null;
}
@Override
public void visit(ClassExtendsDeclaration n) throws Exception {
String className = n.f1.f0.tokenImage;
String parentName = n.f3.f0.tokenImage;
ClassSymbol newClass = globalTable.addClass(className, parentName);
currentClass = newClass;
super.visit(n);
currentClass = null;
}
@Override
public void visit(VarDeclaration n) throws Exception {
String varName = n.f1.f0.tokenImage;
String varTypeName = Util.typeToString(n.f0);
if (currentMethod == null) {
currentClass.addField(varName, varTypeName);
} else {
currentMethod.addLocal(varName, varTypeName);
}
}
@Override
public void visit(MethodDeclaration n) throws Exception {
String returnTypeName = Util.typeToString(n.f1);
String methodName = n.f2.f0.tokenImage;
List<VarSymbol> params = Util.nodeoptToVarSymbolList(n.f4);
MethodSymbol newMethod = currentClass.addMethod(methodName, returnTypeName, params);
currentMethod = newMethod;
super.visit(n);
currentMethod = null;
}
@Override
public void visit(FormalParameter n) throws Exception {
}
}