-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSym.java
More file actions
149 lines (121 loc) · 2.98 KB
/
Sym.java
File metadata and controls
149 lines (121 loc) · 2.98 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
149
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;
private boolean global = false;
public Sym(Type type) {
this.type = type;
}
public void setOffset(int x){
offset = x;
}
public boolean isGlobal(){
return global;
}
public void setGlobal(){
global = true;
}
public int getOffset(){
return offset;
}
public Type getType() {
return type;
}
public String toString() {
return type.toString();
}
}
/**
* 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 size;
private int paramSize;
public FnSym(Type type, int numparams) {
super(new FnType());
returnType = type;
numParams = numparams;
}
public void setSize(int x){
size = x;
}
public int getSize(){
return size;
}
public void setParamSize(int x){
paramSize = x;
}
public int getParamSize(){
return paramSize;
}
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;
}
}