-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPPAST.h
More file actions
259 lines (201 loc) · 5.36 KB
/
CPPAST.h
File metadata and controls
259 lines (201 loc) · 5.36 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
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
#ifndef CPPAST_H
#define CPPAST_H
#include <string>
#include <list>
#include <iostream>
class CPPStatement {
public:
virtual ~CPPStatement() {
}
virtual std::string genCode() {
return ";";
}
};
class CPPExpression {
public:
virtual ~CPPExpression() {
}
virtual std::string genCode() {
return "";
}
};
class CPPIntExpression: public CPPExpression {
public:
CPPIntExpression(int val) {
value = val;
}
virtual std::string genCode() {
return "makeInt(" + std::to_string(value) + ", env)";
}
int value;
};
class CPPCharExpression: public CPPExpression {
public:
CPPCharExpression(char val) {
value = val;
}
virtual std::string genCode() {
return "makeChar(" + std::to_string(value) + ", env)";
}
char value;
};
class CPPBoolExpression: public CPPExpression {
public:
CPPBoolExpression(bool val) {
value = val;
}
virtual std::string genCode() {
return "makeBool(" + std::to_string(value) + ", env)";
}
bool value;
};
class CPPRealExpression: public CPPExpression {
public:
CPPRealExpression(double val) {
value = val;
}
virtual std::string genCode() {
return "makeReal(" + std::to_string(value) + ", env)";
}
double value;
};
class CPPRecordExpression: public CPPExpression {
public:
CPPRecordExpression(std::list < std::pair < std::string, CPPExpression * >> f) : fields(f) {
}
virtual std::string genCode() {
std::string str = "makeRecord({";
for(std::pair < std::string, CPPExpression * > f : fields) {
str += "std::make_pair(\"" + f.first + "\", " + f.second->genCode() + "),";
}
if(!fields.empty()) {
str = str.substr(0,str.length() - 1);
}
str += "}, env)";
return str;
}
std::list < std::pair < std::string, CPPExpression * >> fields;
};
class CPPFuncExpression: public CPPExpression {
public:
CPPFuncExpression(std::string name) : id(name) {
}
virtual std::string genCode() {
return "makeFun(*" + id + ", \"" + id + "\", new Environment(*env), env)";
}
std::string id;
};
class CPPIfStatement: public CPPStatement {
public:
CPPIfStatement(CPPExpression * cond, CPPStatement * left, CPPStatement * right)
: condition(cond), ifthen(left), ifelse(right) {
}
~CPPIfStatement(){
delete condition;
delete ifthen;
delete ifelse;
}
virtual std::string genCode() {
return "if (unwrapBool(" + condition->genCode() + ")) { \n\t\t" \
+ ifthen->genCode() \
+ "\n\t} else {\n\t\t" \
+ ifelse->genCode() \
+ "\n\t}";
}
CPPExpression * condition;
CPPStatement * ifthen;
CPPStatement * ifelse;
};
class CPPInvokeExpression: public CPPExpression {
public:
CPPInvokeExpression(CPPExpression * f, CPPExpression * a) : fun(f), arg(a) {
}
virtual std::string genCode() {
return "invoke(" + fun->genCode() + ", " + arg->genCode() + ", env)";
}
CPPExpression* fun;
CPPExpression* arg;
};
class CPPGetExpression: public CPPExpression {
public:
CPPGetExpression(CPPExpression * a, std::string f) : field(f), rec(a) {
}
virtual std::string genCode() {
return "_get_(" + rec->genCode() + ", \"" + field + "\", env)";
}
std::string field;
CPPExpression* rec;
};
class CPPNativeExpression: public CPPExpression {
public:
CPPNativeExpression(std::string f, CPPExpression * a) : fun(f), arg(a) {
}
virtual std::string genCode() {
return fun + "(" + arg->genCode() + ", env)";
}
std::string fun;
CPPExpression* arg;
};
class CPPLookupExpression: public CPPExpression {
public:
CPPLookupExpression(std::string id) : name(id) {
}
virtual std::string genCode() {
return "env->lookup(\"" + name + "\")";
}
std::string name;
};
class CPPBinding: public CPPStatement {
public:
CPPBinding(std::string id, CPPExpression * e) : name(id), expr(e) {
}
~CPPBinding() {
delete expr;
}
virtual std::string genCode() {
return "env->bind(\"" + name + "\", " + expr->genCode() + ");";
}
std::string name;
CPPExpression* expr;
};
class CPPReturn: public CPPStatement {
public:
CPPReturn(CPPExpression * e) : expr(e) {
}
~CPPReturn() {
delete expr;
}
virtual std::string genCode() {
return "ret = " + expr->genCode() + ";";
}
CPPExpression* expr;
};
class CPPFunction {
public:
CPPFunction(std::string n, std::string p, std::list < CPPStatement * > b)
: name(n), param(p), body(b) {
}
~CPPFunction() {
for(CPPStatement* s : body) {
delete s;
}
}
virtual std::string genCode() {
std::string fun = "Obj* " + name + "(Obj* param, Environment *env) {\n";
fun += "\tenv->push();\n";
fun += "\tenv->bind(\"" + param + "\", param);\n";
fun += "\t Obj* ret = nullptr;\n";
for(CPPStatement* statement : body) {
fun += "\t" + statement->genCode() + "\n";
}
fun += "\tret->mark();\n";
fun += "\tenv->pop();\n";
fun += "\treturn ret;\n";
fun += "}\n";
return fun;
}
std::string name;
std::string param;
std::list < CPPStatement * > body;
};
#endif