forked from frankchn/cs143
/
ast_expr.h
303 lines (256 loc) · 7.96 KB
/
ast_expr.h
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* File: ast_expr.h
* ----------------
* The Expr class and its subclasses are used to represent
* expressions in the parse tree. For each expression in the
* language (add, call, New, etc.) there is a corresponding
* node class for that construct.
*
* pp3: You will need to extend the Expr classes to implement
* semantic analysis for rules pertaining to expressions.
*/
#ifndef _H_ast_expr
#define _H_ast_expr
#include "ast.h"
#include "ast_stmt.h"
#include "list.h"
class NamedType; // for new
class Type; // for NewArray
class Expr : public Stmt
{
public:
Expr(yyltype loc) : Stmt(loc) {}
Expr() : Stmt() {}
virtual Type* Check(Hashtable<Decl*>* symbolTable);
virtual Location* Emit(CodeGenerator *cg);
Type* cachedType;
};
/* This node type is used for those places where an expression is optional.
* We could use a NULL pointer, but then it adds a lot of checking for
* NULL. By using a valid, but no-op, node, we save that trouble */
class EmptyExpr : public Expr
{
public:
virtual Type* Check(Hashtable<Decl*>* symbolTable);
virtual Location* Emit(CodeGenerator *cg);
};
class IntConstant : public Expr
{
protected:
int value;
public:
int GetValue() { return value; }
IntConstant(yyltype loc, int val);
virtual Type* Check(Hashtable<Decl*>* symbolTable);
virtual Location* Emit(CodeGenerator *cg);
};
class DoubleConstant : public Expr
{
protected:
double value;
public:
DoubleConstant(yyltype loc, double val);
virtual Type* Check(Hashtable<Decl*>* symbolTable);
virtual Location* Emit(CodeGenerator *cg);
};
class BoolConstant : public Expr
{
protected:
bool value;
public:
BoolConstant(yyltype loc, bool val);
virtual Type* Check(Hashtable<Decl*>* symbolTable);
virtual Location* Emit(CodeGenerator *cg);
};
class StringConstant : public Expr
{
protected:
char *value;
public:
StringConstant(yyltype loc, const char *val);
virtual Type* Check(Hashtable<Decl*>* symbolTable);
virtual Location* Emit(CodeGenerator *cg);
};
class NullConstant: public Expr
{
public:
NullConstant(yyltype loc) : Expr(loc) {}
virtual Type* Check(Hashtable<Decl*>* symbolTable);
virtual Location* Emit(CodeGenerator *cg);
};
class ErrorConstant: public Expr
{
public:
virtual Type* Check(Hashtable<Decl*>* symbolTable);
virtual Location* Emit(CodeGenerator *cg);
};
class Operator : public Node
{
protected:
char tokenString[4];
public:
Operator(yyltype loc, const char *tok);
friend ostream& operator<<(ostream& out, Operator *o) { return out << o->tokenString; }
virtual Type* Check(Hashtable<Decl*>* symbolTable) { return NULL; }
virtual Location* Emit(CodeGenerator *cg) { return NULL; }
char* GetToken() { return tokenString; }
};
class PostfixExpr : public Expr
{
protected:
Operator *op;
Expr *left;
public:
PostfixExpr(Expr *lhs, Operator *op);
virtual Type* Check(Hashtable<Decl*>* symbolTable);
virtual Location* Emit(CodeGenerator *cg);
};
class CompoundExpr : public Expr
{
protected:
Operator *op;
Expr *left, *right; // left will be NULL if unary
public:
CompoundExpr(Expr *lhs, Operator *op, Expr *rhs); // for binary
CompoundExpr(Operator *op, Expr *rhs); // for unary
virtual Type* Check(Hashtable<Decl*>* symbolTable) = 0;
virtual Location* Emit(CodeGenerator *cg) = 0;
};
class ArithmeticExpr : public CompoundExpr
{
public:
ArithmeticExpr(Expr *lhs, Operator *op, Expr *rhs) : CompoundExpr(lhs,op,rhs) {}
ArithmeticExpr(Operator *op, Expr *rhs) : CompoundExpr(op,rhs) {}
virtual Type* Check(Hashtable<Decl*>* symbolTable);
virtual Location* Emit(CodeGenerator *cg);
};
class RelationalExpr : public CompoundExpr
{
public:
RelationalExpr(Expr *lhs, Operator *op, Expr *rhs) : CompoundExpr(lhs,op,rhs) {}
virtual Type* Check(Hashtable<Decl*>* symbolTable);
virtual Location* Emit(CodeGenerator *cg);
};
class EqualityExpr : public CompoundExpr
{
public:
EqualityExpr(Expr *lhs, Operator *op, Expr *rhs) : CompoundExpr(lhs,op,rhs) {}
const char *GetPrintNameForNode() { return "EqualityExpr"; }
virtual Type* Check(Hashtable<Decl*>* symbolTable);
virtual Location* Emit(CodeGenerator *cg);
};
class LogicalExpr : public CompoundExpr
{
public:
LogicalExpr(Expr *lhs, Operator *op, Expr *rhs) : CompoundExpr(lhs,op,rhs) {}
LogicalExpr(Operator *op, Expr *rhs) : CompoundExpr(op,rhs) {}
const char *GetPrintNameForNode() { return "LogicalExpr"; }
virtual Type* Check(Hashtable<Decl*>* symbolTable);
virtual Location* Emit(CodeGenerator *cg);
};
class AssignExpr : public CompoundExpr
{
public:
AssignExpr(Expr *lhs, Operator *op, Expr *rhs) : CompoundExpr(lhs,op,rhs) { isLocalVariable = false; leftType = NULL; rightType = NULL; }
const char *GetPrintNameForNode() { return "AssignExpr"; }
virtual Type* Check(Hashtable<Decl*>* symbolTable);
virtual Location* Emit(CodeGenerator *cg);
Type* leftType;
Type* rightType;
bool isLocalVariable;
};
class LValue : public Expr
{
public:
LValue(yyltype loc) : Expr(loc) {}
virtual Type* Check(Hashtable<Decl*>* symbolTable) = 0;
virtual Location* Emit(CodeGenerator *cg) = 0;
};
class This : public Expr
{
public:
This(yyltype loc) : Expr(loc) {}
virtual Type* Check(Hashtable<Decl*>* symbolTable);
virtual Location* Emit(CodeGenerator *cg);
};
class ArrayAccess : public LValue
{
protected:
Expr *base, *subscript;
public:
ArrayAccess(yyltype loc, Expr *base, Expr *subscript);
virtual Type* Check(Hashtable<Decl*>* symbolTable);
virtual Location* Emit(CodeGenerator *cg);
Location* EmitMemoryLocation(CodeGenerator *cg);
};
/* Note that field access is used both for qualified names
* base.field and just field without qualification. We don't
* know for sure whether there is an implicit "this." in
* front until later on, so we use one node type for either
* and sort it out later. */
class FieldAccess : public LValue
{
protected:
Expr *base; // will be NULL if no explicit base
Identifier *field;
public:
FieldAccess(Expr *base, Identifier *field); //ok to pass NULL base
virtual Type* Check(Hashtable<Decl*>* symbolTable);
virtual Location* Emit(CodeGenerator *cg);
Type* baseType;
Location* EmitReference(CodeGenerator *cg);
};
/* Like field access, call is used both for qualified base.field()
* and unqualified field(). We won't figure out until later
* whether we need implicit "this." so we use one node type for either
* and sort it out later. */
class Call : public Expr
{
protected:
Expr *base; // will be NULL if no explicit base
Identifier *field;
List<Expr*> *actuals;
public:
Call(yyltype loc, Expr *base, Identifier *field, List<Expr*> *args);
virtual Type* Check(Hashtable<Decl*>* symbolTable);
virtual Location* Emit(CodeGenerator *cg);
// For Code Generator
char* fullyQualifiedFnName;
bool isClassFunction;
bool hasReturnType;
bool isArrayLengthCheck;
Decl* baseDecl;
};
class NewExpr : public Expr
{
protected:
NamedType *cType;
public:
NewExpr(yyltype loc, NamedType *clsType);
virtual Type* Check(Hashtable<Decl*>* symbolTable);
virtual Location* Emit(CodeGenerator *cg);
};
class NewArrayExpr : public Expr
{
protected:
Expr *size;
Type *elemType;
public:
NewArrayExpr(yyltype loc, Expr *sizeExpr, Type *elemType);
virtual Type* Check(Hashtable<Decl*>* symbolTable);
virtual Location* Emit(CodeGenerator *cg);
};
class ReadIntegerExpr : public Expr
{
public:
ReadIntegerExpr(yyltype loc) : Expr(loc) {}
virtual Type* Check(Hashtable<Decl*>* symbolTable);
virtual Location* Emit(CodeGenerator *cg);
};
class ReadLineExpr : public Expr
{
public:
ReadLineExpr(yyltype loc) : Expr (loc) {}
virtual Type* Check(Hashtable<Decl*>* symbolTable);
virtual Location* Emit(CodeGenerator *cg);
};
#endif