-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbyte_code_gen.cpp
377 lines (359 loc) · 13.7 KB
/
byte_code_gen.cpp
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "byte_code_gen.h"
TByteCodeGen::TByteCodeGen(const std::string fileName): Parser(new TParser(fileName)), Stack(TStack())
{
GenByteCode();
while (it < command.size()) {
command[it]->UpdateStack(Stack);
it++;
}
Stack.PrintResult();
}
TByteCodeGen::~TByteCodeGen() {
}
void TByteCodeGen::GenByteCode()
{
for (size_t i = 0; i < Parser->GetRootSize(); i++) {
Allocator(Parser->GetRootAt(i));
}
for (size_t i = 0; i < Parser->GetRootSize(); i++) {
GenExprValue(Parser->GetRootAt(i), Stack.GetGlobalEnviroment());
}
}
void TByteCodeGen::GenFuncByteCode(CallExprAST* func, Enviroment& defineParent)
{
for (size_t i = 0; i < func->GetArgsSize(); i++) {
GenExprValue(func->GetArgsAt(i), defineParent);
}
command.push_back(std::shared_ptr<TByteCodeCMD>(new TByteCodeCMDCall(func->GetName(), func->GetArgsSize(), it, command)));
}
void TByteCodeGen::GenTaliCallByteCode(CallExprAST *func, Enviroment& defineParent, size_t pos)
{
for (size_t i = 0; i < func->GetArgsSize(); i++) {
GenExprValue(func->GetArgsAt(i), defineParent);
}
command.push_back(std::shared_ptr<TByteCodeCMD>(new TByteCodeCMDTailCall(func->GetName(), pos, func->GetArgsSize(), it)));
}
void TByteCodeGen::GenIfElseByteCode(IfElseExprAST *expr, Enviroment& defineParent, size_t pos, std::string name)
{
if (!name.size()) {
GenExprValue(expr->GetTest(), defineParent);
command.push_back(std::shared_ptr<TByteCodeCMD>(new TByteCodeCMDIfElse(it)));
command.push_back(std::shared_ptr<TByteCodeCMD>(new TByteCodeCMDJump(it)));
size_t k = command.size()-1;
GenExprValue(expr->GetFirst(), defineParent);
if (expr->IsSecond()) {
dynamic_cast<TByteCodeCMDJump*>(command.at(k).get())->SetCurrentPos(command.size());
command.push_back(std::shared_ptr<TByteCodeCMD>(new TByteCodeCMDJump(it)));
k = command.size()-1;
GenExprValue(expr->GetSecond(), defineParent);
dynamic_cast<TByteCodeCMDJump*>(command.at(k).get())->SetCurrentPos(command.size()-1);
} else {
dynamic_cast<TByteCodeCMDJump*>(command.at(k).get())->SetCurrentPos(command.size());
}
} else {
GenDefineByteCode(expr->GetTest(), defineParent, name, pos);
command.push_back(std::shared_ptr<TByteCodeCMD>(new TByteCodeCMDIfElse(it)));
command.push_back(std::shared_ptr<TByteCodeCMD>(new TByteCodeCMDJump(it)));
size_t k = command.size()-1;
GenDefineByteCode(expr->GetFirst(), defineParent, name, pos);
if (expr->IsSecond()) {
dynamic_cast<TByteCodeCMDJump*>(command.at(k).get())->SetCurrentPos(command.size());
command.push_back(std::shared_ptr<TByteCodeCMD>(new TByteCodeCMDJump(it)));
k = command.size()-1;
GenDefineByteCode(expr->GetSecond(), defineParent, name, pos);
dynamic_cast<TByteCodeCMDJump*>(command.at(k).get())->SetCurrentPos(command.size()-1);
} else {
dynamic_cast<TByteCodeCMDJump*>(command.at(k).get())->SetCurrentPos(command.size());
}
}
}
void TByteCodeGen::GenBeginByteCode(BeginExprAST *expr, Enviroment &defineParent, size_t pos, std::string name)
{
if (!name.size()) {
for (size_t i = 0; i < expr->GetArgsSize(); i++) {
GenExprValue(expr->GetArgsAt(i), defineParent);
}
} else {
for (size_t i = 0; i < expr->GetArgsSize(); i++) {
GenDefineByteCode(expr->GetArgsAt(i), defineParent, name, pos);
}
}
command.push_back(std::shared_ptr<TByteCodeCMD>(new TByteCodeCMDBegin(expr->GetArgsSize())));
}
void TByteCodeGen::GenLambdaByteCode(LambdaExprAST *expr, Enviroment& defineParent, size_t pos, std::string name)
{
std::vector<std::string> args;
for (size_t j = 0; j < expr->GetIdentsSize(); j++) {
args.push_back(expr->GetIdentsAt(j)->GetName());
}
for (size_t i = 0; i < expr->GetArgsSize(); i++) {
GenExprValue(expr->GetArgsAt(i), defineParent);
}
command.push_back(std::shared_ptr<TByteCodeCMD>(new TByteCodeCMDLambda(args, defineParent)));
if (!name.size()) {
for (size_t i = 0; i < expr->GetBodySize(); i++) {
GenExprValue(expr->GetBodyAt(i), defineParent);
}
} else {
for (size_t i = 0; i < expr->GetBodySize(); i++) {
GenDefineByteCode(expr->GetBodyAt(i), defineParent, name, pos);
}
}
}
void TByteCodeGen::GenDefineByteCode(ExprAST* expr, Enviroment& defineParent, std::string name, size_t pos)
{
switch (expr->Type){
case SAT_IfElse: {
GenIfElseByteCode(dynamic_cast<IfElseExprAST*>(expr), defineParent, pos, name);
return;
}
case SAT_Lambda: {
GenLambdaByteCode(dynamic_cast<LambdaExprAST*>(expr), defineParent, pos, name);
return;
}
case SAT_Define: {
GenDefine(expr, defineParent);
return;
}
case SAT_Begin: {
GenBeginByteCode(dynamic_cast<BeginExprAST*>(expr), defineParent, pos, name);
return;
}
case AT_Func: {
if (dynamic_cast<CallExprAST*>(expr)->GetName() == name) {
GenTaliCallByteCode(dynamic_cast<CallExprAST*>(expr), defineParent, pos);
} else {
GenFuncByteCode(dynamic_cast<CallExprAST*>(expr), defineParent);
}
return;
}
default:
GenExprValue(expr, defineParent);
return;
}
}
void TByteCodeGen::GenDefine(ExprAST *expr, Enviroment& defineParent)
{
FunctionAST* currentDefine = dynamic_cast<FunctionAST*>(expr);
std::vector<std::string> args;
for (size_t j = 0; j < currentDefine->GetProto()->GetArgsSize(); j++) {
args.push_back(currentDefine->GetProto()->GetArgsAt(j)->GetName());
}
std::shared_ptr<TByteCodeCMDDefine> Define(new TByteCodeCMDDefine(currentDefine->GetProto()->GetName(), args, defineParent, command, it));
command.push_back(Define);
for (size_t j = 0; j < currentDefine->GetBodySize(); j++) {
GenDefineByteCode(currentDefine->GetBodyAt(j), *(Define->GetEnviroment()), currentDefine->GetProto()->GetName(), command.size());
}
command.push_back(std::shared_ptr<TByteCodeCMD>(new TByteCodeCMDJumpEndDefine(it)));
}
void TByteCodeGen::Allocator(ExprAST* expr)
{
if (IsInAllocatorValue(expr)) {
return;
}
if (expr->Type != AT_Func && expr->Type != AT_Ident && expr->Type != SAT_Define && expr->Type != SAT_IfElse && expr->Type != SAT_Begin && expr->Type != SAT_Lambda && !IsInAllocatorValue(expr)){
allocatorValue.insert(std::make_pair(expr, allocatorValue.size()));
}
switch (expr->Type) {
case AT_Int: {
command.push_back(std::shared_ptr<TByteCodeCMD>(new TByteCodeCMDAllocInt(dynamic_cast<NumberIntAST*>(expr)->GetValue())));
return;
}
case AT_Double : {
command.push_back(std::shared_ptr<TByteCodeCMD>(new TByteCodeCMDAllocDouble(dynamic_cast<NumberDoubleAST*>(expr)->GetValue())));
return;
}
case AT_String : {
command.push_back(std::shared_ptr<TByteCodeCMD>(new TByteCodeCMDAllocString(dynamic_cast<StringAST*>(expr)->GetValue())));
return;
}
case AT_Symbol : {
command.push_back(std::shared_ptr<TByteCodeCMD>(new TByteCodeCMDAllocSymbol(dynamic_cast<SymbolAST*>(expr)->GetValue())));
return;
}
case AT_Char : {
command.push_back(std::shared_ptr<TByteCodeCMD>(new TByteCodeCMDAllocChar(dynamic_cast<CharAST*>(expr)->GetValue())));
return;
}
case AT_Bool : {
command.push_back(std::shared_ptr<TByteCodeCMD>(new TByteCodeCMDAllocBool(dynamic_cast<BoolAST*>(expr)->GetValue())));
return;
}
case SAT_IfElse: {
Allocator(dynamic_cast<IfElseExprAST*>(expr)->GetTest());
Allocator(dynamic_cast<IfElseExprAST*>(expr)->GetFirst());
if (dynamic_cast<IfElseExprAST*>(expr)->IsSecond()) {
Allocator(dynamic_cast<IfElseExprAST*>(expr)->GetSecond());
}
return;
}
case SAT_Begin: {
BeginExprAST* e = dynamic_cast<BeginExprAST*>(expr);
for (size_t i = 0; i < e->GetArgsSize(); i++) {
Allocator(e->GetArgsAt(i));
}
return;
}
case SAT_Lambda: {
size_t sizeArgs = dynamic_cast<LambdaExprAST*>(expr)->GetArgsSize();
size_t sizeBody = dynamic_cast<LambdaExprAST*>(expr)->GetBodySize();
for (size_t i = 0; i < sizeArgs; i++) {
Allocator(dynamic_cast<LambdaExprAST*>(expr)->GetArgsAt(i));
}
for (size_t i = 0; i < sizeBody; i++) {
Allocator(dynamic_cast<LambdaExprAST*>(expr)->GetBodyAt(i));
}
return;
}
case AT_Func : {
size_t size = dynamic_cast<CallExprAST*>(expr)->GetArgsSize();
for (size_t i = 0; i < size; i++) {
Allocator(dynamic_cast<CallExprAST*>(expr)->GetArgsAt(i));
}
return;
}
case SAT_Define: {
size_t size = dynamic_cast<FunctionAST*>(expr)->GetBodySize();
for (size_t i = 0; i < size; i++) {
Allocator(dynamic_cast<FunctionAST*>(expr)->GetBodyAt(i));
}
return;
}
case AT_Ident: {
return;
}
default: {
throw new ParserExceptionIncorrectToken("cannot identify token type");
return;
}
}
}
void TByteCodeGen::GenExprValue(ExprAST *expr, Enviroment& defineParent) {
switch (expr->Type) {
case AT_Ident : {
command.push_back(std::shared_ptr<TByteCodeCMD>(new TByteCodeCMDPushIdent(dynamic_cast<IdentAST*>(expr)->GetName())));
return;
}
case SAT_IfElse: {
GenIfElseByteCode(dynamic_cast<IfElseExprAST*>(expr), defineParent);
return;
}
case AT_Func : {
GenFuncByteCode(dynamic_cast<CallExprAST*>(expr), defineParent);
return;
}
case SAT_Begin: {
GenBeginByteCode(dynamic_cast<BeginExprAST*>(expr), defineParent);
return;
}
case SAT_Define: {
GenDefine(expr, defineParent);
return;
}
case SAT_Lambda: {
GenLambdaByteCode(dynamic_cast<LambdaExprAST*>(expr), defineParent);
return;
}
default: {
command.push_back(std::shared_ptr<TByteCodeCMD>(new TByteCodeCMDPush(GetAllocatorValue(expr))));
return;
}
}
}
size_t TByteCodeGen::GetAllocatorValue(ExprAST *expr)
{
for (auto it = allocatorValue.begin(); it != allocatorValue.end(); it++) {
if (expr->Type == it->first->Type) {
switch (expr->Type) {
case AT_Int: {
if (dynamic_cast<NumberIntAST*>(expr)->GetValue() == dynamic_cast<NumberIntAST*>(it->first)->GetValue()) {
return it->second;
}
break;
}
case AT_Double: {
if (dynamic_cast<NumberDoubleAST*>(expr)->GetValue() == dynamic_cast<NumberDoubleAST*>(it->first)->GetValue()) {
return it->second;
}
break;
}
case AT_Char: {
if (dynamic_cast<CharAST*>(expr)->GetValue() == dynamic_cast<CharAST*>(it->first)->GetValue()) {
return it->second;
}
break;
}
case AT_String: {
if (dynamic_cast<StringAST*>(expr)->GetValue() == dynamic_cast<StringAST*>(it->first)->GetValue()) {
return it->second;
}
break;
}
case AT_Bool: {
if (dynamic_cast<BoolAST*>(expr)->GetValue() == dynamic_cast<BoolAST*>(it->first)->GetValue()) {
return it->second;
}
break;
}
case AT_Symbol: {
if (dynamic_cast<SymbolAST*>(expr)->GetValue() == dynamic_cast<SymbolAST*>(it->first)->GetValue()) {
return it->second;
}
break;
}
default: {
break;
}
}
}
}
}
bool TByteCodeGen::IsInAllocatorValue(ExprAST *expr)
{
for (auto it = allocatorValue.begin(); it != allocatorValue.end(); it++) {
if (expr->Type == it->first->Type) {
switch (expr->Type) {
case AT_Int: {
if (dynamic_cast<NumberIntAST*>(expr)->GetValue() == dynamic_cast<NumberIntAST*>(it->first)->GetValue()) {
return true;
}
break;
}
case AT_Double: {
if (dynamic_cast<NumberDoubleAST*>(expr)->GetValue() == dynamic_cast<NumberDoubleAST*>(it->first)->GetValue()) {
return true;
}
break;
}
case AT_Char: {
if (dynamic_cast<CharAST*>(expr)->GetValue() == dynamic_cast<CharAST*>(it->first)->GetValue()) {
return true;
}
break;
}
case AT_String: {
if (dynamic_cast<StringAST*>(expr)->GetValue() == dynamic_cast<StringAST*>(it->first)->GetValue()) {
return true;
}
break;
}
case AT_Bool: {
if (dynamic_cast<BoolAST*>(expr)->GetValue() == dynamic_cast<BoolAST*>(it->first)->GetValue()) {
return true;
}
break;
}
case AT_Symbol: {
if (dynamic_cast<SymbolAST*>(expr)->GetValue() == dynamic_cast<SymbolAST*>(it->first)->GetValue()) {
return true;
}
break;
}
default: {
break;
}
}
}
}
return false;
}