-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassTable.java
More file actions
227 lines (202 loc) · 6.89 KB
/
ClassTable.java
File metadata and controls
227 lines (202 loc) · 6.89 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
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
import java.io.PrintStream;
/** This class may be used to contain the semantic information such as
* the inheritance graph. You may use it or not as you like: it is only
* here to provide a container for the supplied methods. */
class ClassTable {
private int semantErrors;
private PrintStream errorStream;
/** Creates data structures representing basic Cool classes (Object,
* IO, Int, Bool, String). Please note: as is this method does not
* do anything useful; you will need to edit it to make if do what
* you want.
* */
private void installBasicClasses() {
AbstractSymbol filename
= AbstractTable.stringtable.addString("<basic class>");
// The following demonstrates how to create dummy parse trees to
// refer to basic Cool classes. There's no need for method
// bodies -- these are already built into the runtime system.
// IMPORTANT: The results of the following expressions are
// stored in local variables. You will want to do something
// with those variables at the end of this method to make this
// code meaningful.
// The Object class has no parent class. Its methods are
// cool_abort() : Object aborts the program
// type_name() : Str returns a string representation
// of class name
// copy() : SELF_TYPE returns a copy of the object
class_c Object_class =
new class_c(0,
TreeConstants.Object_,
TreeConstants.No_class,
new Features(0)
.appendElement(new method(0,
TreeConstants.cool_abort,
new Formals(0),
TreeConstants.Object_,
new no_expr(0)))
.appendElement(new method(0,
TreeConstants.type_name,
new Formals(0),
TreeConstants.Str,
new no_expr(0)))
.appendElement(new method(0,
TreeConstants.copy,
new Formals(0),
TreeConstants.SELF_TYPE,
new no_expr(0))),
filename);
// The IO class inherits from Object. Its methods are
// out_string(Str) : SELF_TYPE writes a string to the output
// out_int(Int) : SELF_TYPE " an int " " "
// in_string() : Str reads a string from the input
// in_int() : Int " an int " " "
class_c IO_class =
new class_c(0,
TreeConstants.IO,
TreeConstants.Object_,
new Features(0)
.appendElement(new method(0,
TreeConstants.out_string,
new Formals(0)
.appendElement(new formalc(0,
TreeConstants.arg,
TreeConstants.Str)),
TreeConstants.SELF_TYPE,
new no_expr(0)))
.appendElement(new method(0,
TreeConstants.out_int,
new Formals(0)
.appendElement(new formalc(0,
TreeConstants.arg,
TreeConstants.Int)),
TreeConstants.SELF_TYPE,
new no_expr(0)))
.appendElement(new method(0,
TreeConstants.in_string,
new Formals(0),
TreeConstants.Str,
new no_expr(0)))
.appendElement(new method(0,
TreeConstants.in_int,
new Formals(0),
TreeConstants.Int,
new no_expr(0))),
filename);
// The Int class has no methods and only a single attribute, the
// "val" for the integer.
class_c Int_class =
new class_c(0,
TreeConstants.Int,
TreeConstants.Object_,
new Features(0)
.appendElement(new attr(0,
TreeConstants.val,
TreeConstants.prim_slot,
new no_expr(0))),
filename);
// Bool also has only the "val" slot.
class_c Bool_class =
new class_c(0,
TreeConstants.Bool,
TreeConstants.Object_,
new Features(0)
.appendElement(new attr(0,
TreeConstants.val,
TreeConstants.prim_slot,
new no_expr(0))),
filename);
// The class Str has a number of slots and operations:
// val the length of the string
// str_field the string itself
// length() : Int returns length of the string
// concat(arg: Str) : Str performs string concatenation
// substr(arg: Int, arg2: Int): Str substring selection
class_c Str_class =
new class_c(0,
TreeConstants.Str,
TreeConstants.Object_,
new Features(0)
.appendElement(new attr(0,
TreeConstants.val,
TreeConstants.Int,
new no_expr(0)))
.appendElement(new attr(0,
TreeConstants.str_field,
TreeConstants.prim_slot,
new no_expr(0)))
.appendElement(new method(0,
TreeConstants.length,
new Formals(0),
TreeConstants.Int,
new no_expr(0)))
.appendElement(new method(0,
TreeConstants.concat,
new Formals(0)
.appendElement(new formalc(0,
TreeConstants.arg,
TreeConstants.Str)),
TreeConstants.Str,
new no_expr(0)))
.appendElement(new method(0,
TreeConstants.substr,
new Formals(0)
.appendElement(new formalc(0,
TreeConstants.arg,
TreeConstants.Int))
.appendElement(new formalc(0,
TreeConstants.arg2,
TreeConstants.Int)),
TreeConstants.Str,
new no_expr(0))),
filename);
/* Do somethind with Object_class, IO_class, Int_class,
Bool_class, and Str_class here */
}
public ClassTable(Classes cls) {
semantErrors = 0;
errorStream = System.err;
/* fill this in */
}
/** Prints line number and file name of the given class.
*
* Also increments semantic error count.
*
* @param c the class
* @return a print stream to which the rest of the error message is
* to be printed.
*
* */
public PrintStream semantError(class_c c) {
return semantError(c.getFilename(), c);
}
/** Prints the file name and the line number of the given tree node.
*
* Also increments semantic error count.
*
* @param filename the file name
* @param t the tree node
* @return a print stream to which the rest of the error message is
* to be printed.
*
* */
public PrintStream semantError(AbstractSymbol filename, TreeNode t) {
errorStream.print(filename + ":" + t.getLineNumber() + ": ");
return semantError();
}
/** Increments semantic error count and returns the print stream for
* error messages.
*
* @return a print stream to which the error message is
* to be printed.
*
* */
public PrintStream semantError() {
semantErrors++;
return errorStream;
}
/** Returns true if there are any static semantic errors. */
public boolean errors() {
return semantErrors != 0;
}
}