-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast_expr.h
executable file
·244 lines (203 loc) · 5.99 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
/* 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.
*/
#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() {}
};
/* 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:
const char *GetPrintNameForNode() { return "Empty"; }
};
class IntConstant : public Expr
{
protected:
int value;
public:
IntConstant(yyltype loc, int val);
const char *GetPrintNameForNode() { return "IntConstant"; }
void PrintChildren(int indentLevel);
};
class DoubleConstant : public Expr
{
protected:
double value;
public:
DoubleConstant(yyltype loc, double val);
const char *GetPrintNameForNode() { return "DoubleConstant"; }
void PrintChildren(int indentLevel);
};
class BoolConstant : public Expr
{
protected:
bool value;
public:
BoolConstant(yyltype loc, bool val);
const char *GetPrintNameForNode() { return "BoolConstant"; }
void PrintChildren(int indentLevel);
};
class StringConstant : public Expr
{
protected:
char *value;
public:
StringConstant(yyltype loc, const char *val);
const char *GetPrintNameForNode() { return "StringConstant"; }
void PrintChildren(int indentLevel);
};
class NullConstant: public Expr
{
public:
NullConstant(yyltype loc) : Expr(loc) {}
const char *GetPrintNameForNode() { return "NullConstant"; }
};
class Operator : public Node
{
protected:
char tokenString[4];
public:
Operator(yyltype loc, const char *tok);
const char *GetPrintNameForNode() { return "Operator"; }
void PrintChildren(int indentLevel);
};
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
void PrintChildren(int indentLevel);
};
class ArithmeticExpr : public CompoundExpr
{
public:
ArithmeticExpr(Expr *lhs, Operator *op, Expr *rhs) : CompoundExpr(lhs,op,rhs) {}
ArithmeticExpr(Operator *op, Expr *rhs) : CompoundExpr(op,rhs) {}
const char *GetPrintNameForNode() { return "ArithmeticExpr"; }
};
class RelationalExpr : public CompoundExpr
{
public:
RelationalExpr(Expr *lhs, Operator *op, Expr *rhs) : CompoundExpr(lhs,op,rhs) {}
const char *GetPrintNameForNode() { return "RelationalExpr"; }
};
class EqualityExpr : public CompoundExpr
{
public:
EqualityExpr(Expr *lhs, Operator *op, Expr *rhs) : CompoundExpr(lhs,op,rhs) {}
const char *GetPrintNameForNode() { return "EqualityExpr"; }
};
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"; }
};
class AssignExpr : public CompoundExpr
{
public:
AssignExpr(Expr *lhs, Operator *op, Expr *rhs) : CompoundExpr(lhs,op,rhs) {}
const char *GetPrintNameForNode() { return "AssignExpr"; }
};
class LValue : public Expr
{
public:
LValue(yyltype loc) : Expr(loc) {}
};
class This : public Expr
{
public:
This(yyltype loc) : Expr(loc) {}
const char *GetPrintNameForNode() { return "This"; }
};
class ArrayAccess : public LValue
{
protected:
Expr *base, *subscript;
public:
ArrayAccess(yyltype loc, Expr *base, Expr *subscript);
const char *GetPrintNameForNode() { return "ArrayAccess"; }
void PrintChildren(int indentLevel);
};
/* 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
const char *GetPrintNameForNode() { return "FieldAccess"; }
void PrintChildren(int indentLevel);
};
/* 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);
const char *GetPrintNameForNode() { return "Call"; }
void PrintChildren(int indentLevel);
};
class NewExpr : public Expr
{
protected:
NamedType *cType;
public:
NewExpr(yyltype loc, NamedType *clsType);
const char *GetPrintNameForNode() { return "NewExpr"; }
void PrintChildren(int indentLevel);
};
class NewArrayExpr : public Expr
{
protected:
Expr *size;
Type *elemType;
public:
NewArrayExpr(yyltype loc, Expr *sizeExpr, Type *elemType);
const char *GetPrintNameForNode() { return "NewArrayExpr"; }
void PrintChildren(int indentLevel);
};
class ReadIntegerExpr : public Expr
{
public:
ReadIntegerExpr(yyltype loc) : Expr(loc) {}
const char *GetPrintNameForNode() { return "ReadIntegerExpr"; }
};
class ReadLineExpr : public Expr
{
public:
ReadLineExpr(yyltype loc) : Expr (loc) {}
const char *GetPrintNameForNode() { return "ReadLineExpr"; }
};
#endif