-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSym.java
More file actions
148 lines (122 loc) · 3.15 KB
/
Sym.java
File metadata and controls
148 lines (122 loc) · 3.15 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
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
import java.util.*;
/**
* The Sym class defines a symbol-table entry.
* Each Sym contains a type (a Type).
*/
public class Sym {
private Type type;
private int offset = 0;
// Is set to false for local variables
private boolean isGlobal = false;
public Sym(Type type) {
this.type = type;
}
public Type getType() {
return type;
}
public String toString() {
return type.toString();
}
public int getOffset() {
return offset;
}
public void setOffset(int offset) {
this.offset = offset;
}
public boolean getGlobal() {
return isGlobal;
}
public void setGlobal(boolean isGlobal) {
this.isGlobal = isGlobal;
}
}
/**
* 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;
private int localsSize;
private int formalsSize;
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;
}
public int getLocalsSize() {
return localsSize;
}
public void setLocalsSize(int localsSize) {
this.localsSize = localsSize;
}
public int getFormalsSize() {
return formalsSize;
}
public void setFormalsSize(int formalsSize) {
this.formalsSize = formalsSize;
}
}
/**
* 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;
}
}