-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sym.java
119 lines (98 loc) · 2.52 KB
/
Sym.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
import java.util.*;
/**
* The Sym class defines a symbol-table entry.
* Each Sym contains a type (a Type).
*/
public class Sym {
private Type type;
public int offset;
public Sym(Type type) {
this.type = type;
}
public Type getType() {
return type;
}
public String toString() {
return type.toString();
}
public boolean isGlobal = false;
public void isGloabl_set() {
this.isGlobal = true;
}
}
/**
* The FnSym class is a subclass of the Sym class just for functions.
* The returnType field holds the return type and there are fields to hold
* information about the parameters.
*/
class FnSym extends Sym {
// new fields
private Type returnType;
private int numParams;
private List<Type> paramTypes;
public FnSym(Type type, int numparams) {
super(new FnType());
returnType = type;
numParams = numparams;
}
public void addFormals(List<Type> L) {
paramTypes = L;
}
public Type getReturnType() {
return returnType;
}
public int getNumParams() {
return numParams;
}
public List<Type> getParamTypes() {
return paramTypes;
}
public String toString() {
// make list of formals
String str = "";
boolean notfirst = false;
for (Type type : paramTypes) {
if (notfirst)
str += ",";
else
notfirst = true;
str += type.toString();
}
str += "->" + returnType.toString();
return str;
}
}
/**
* The StructSym class is a subclass of the Sym class just for variables
* declared to be a struct type.
* Each StructSym contains a symbol table to hold information about its
* fields.
*/
class StructSym extends Sym {
// new fields
private IdNode structType; // name of the struct type
public StructSym(IdNode id) {
super(new StructType(id));
structType = id;
}
public IdNode getStructType() {
return structType;
}
}
/**
* The StructDefSym class is a subclass of the Sym class just for the
* definition of a struct type.
* Each StructDefSym contains a symbol table to hold information about its
* fields.
*/
class StructDefSym extends Sym {
// new fields
private SymTable symTab;
public StructDefSym(SymTable table) {
super(new StructDefType());
symTab = table;
}
public SymTable getSymTable() {
return symTab;
}
}