-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatement.cpp
327 lines (288 loc) · 9.37 KB
/
statement.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
#include "statement.h"
#include "symtable.h"
Stmt_Goto::Stmt_Goto(Symbol* Sym) : LabelSym(Sym){};
Stmt_Goto_Label::Stmt_Goto_Label(Symbol* Sym) : LabelSym(Sym) {};
Stmt_Conditional::Stmt_Conditional(Expr* Exp) : Exp(Exp){};
Stmt_If::Stmt_If(Expr* Exp, Statement* Stmt, Statement* StmtElse) : Stmt_Conditional(Exp), Stmt(Stmt), StmtElse(StmtElse){}
Stmt_For::Stmt_For(Expr* Exp_1, Expr* Exp_2, bool isTO, Statement* Stmt) : Exp_1(Exp_1), Exp_2(Exp_2), isTO(isTO), Stmt(Stmt),
Stmt_Conditional(new ExprBinOp(((ExprAssign*)Exp_1)->Left, Token(Position(), isTO ? ">" : "<", isTO ? TK_GREAT : TK_LESS), Exp_2)) {};
Stmt_While::Stmt_While(Expr* Cond, Statement* Stmt) : Stmt_Conditional(Cond), Stmt(Stmt) {};
Stmt_Repeat::Stmt_Repeat(Expr* Cond, vector<Statement*> StmtList) : Stmt_Conditional(Cond), StmtList(StmtList) {};
Case_Selector::Case_Selector(Expr* Exp_1, Expr* Exp_2, Statement* Stmt) : Exp_1(Exp_1), Exp_2(Exp_2), Stmt(Stmt) {};
Stmt_Case::Stmt_Case(Expr* Exp) : Stmt_Conditional(Exp), StmtElse(nullptr) {};
Stmt_Try_Except::Stmt_Try_Except(vector<Statement*> Stmt_Try, vector<Statement*> Stmt_Except) : Stmt_List_Try(Stmt_Try), Stmt_List_Except(Stmt_Except){}
Stmt_Try_Finally::Stmt_Try_Finally(vector<Statement*> Stmt_Try, vector<Statement*> Stmt_Finally) : Stmt_List_Try(Stmt_Try), Stmt_List_Finally(Stmt_Finally){}
Stmt_Raise::Stmt_Raise(Expr* Exp) : Exp(Exp) {};
Stmt_Assign::Stmt_Assign(Expr* Exp) : Exp(Exp) {};
Stmt_Call::Stmt_Call(Expr* Exp) : Exp(Exp) {}
void Stmt_Compound::Add(Statement* Stmt){
StmtList.push_back(Stmt);
}
void Stmt_Case::Add(Case_Selector Selector) {
SelectorList.push_back(Selector);
}
void Stmt_Compound::Print(int Spaces) {
print_indent(Spaces);
cout << "begin" << endl;
for (auto it = StmtList.begin(); it < StmtList.end(); ++it) {
(*it)->Print(Spaces + 1);
}
print_indent(Spaces);
cout << "end" << endl;
}
void Stmt_Goto::Print(int Spaces) {
print_indent(Spaces);
cout << "goto" << indent << LabelSym->Name << endl;
}
void Stmt_Goto_Label::Print(int Spaces) {
print_indent(Spaces);
cout << "goto_label" << indent << LabelSym->Name << endl;
}
void Stmt_If::Print(int Spaces){
print_indent(Spaces);
cout << "if" << endl;
Exp->Print(Spaces + 1);
print_indent(Spaces);
cout << "then" << endl;
Stmt->Print(Spaces + 1);
if (StmtElse != nullptr) {
print_indent(Spaces);
cout << "else" << endl;
StmtElse->Print(Spaces + 1);
}
}
void Stmt_For::Print(int Spaces){
print_indent(Spaces);
cout << "for" << endl;
Exp_1->Print(Spaces + 1);
print_indent(Spaces + 1);
isTO ? cout << "true" << endl : cout << "false" << endl;
Exp_2->Print(Spaces + 1);
print_indent(Spaces);
cout << "do" << endl;
if (Stmt != nullptr) {
Stmt->Print(Spaces + 1);
}
}
void Stmt_While::Print(int Spaces) {
print_indent(Spaces);
cout << "while" << endl;
Exp->Print(Spaces + 1);
print_indent(Spaces);
cout << "do" << endl;
if (Stmt != nullptr) {
Stmt->Print(Spaces + 1);
}
}
void Stmt_Repeat::Print(int Spaces) {
print_indent(Spaces);
cout << "until" << endl;
Exp->Print(Spaces + 1);
print_indent(Spaces);
cout << "repeat" << endl;
for (auto it = StmtList.begin(); it < StmtList.end(); ++it){
(*it)->Print(Spaces + 1);
}
}
void Stmt_Break::Print(int Spaces) {
print_indent(Spaces);
cout << "break" << endl;
}
void Stmt_Continue::Print(int Spaces) {
print_indent(Spaces);
cout << "continue" << endl;
}
void Stmt_Case::Print(int Spaces) {
print_indent(Spaces);
cout << "case" << endl;
Exp->Print(Spaces);
for (auto it = SelectorList.begin(); it < SelectorList.end(); ++it) {
(*it).Stmt->Print(Spaces + 1);
}
}
void Stmt_Try_Except::Print(int Spaces) {
print_indent(Spaces);
cout << "try" << endl;
for (auto it = Stmt_List_Try.begin(); it < Stmt_List_Try.end(); ++it){
(*it)->Print(Spaces + 1);
}
print_indent(Spaces);
cout << "except" << endl;
for (auto it = Stmt_List_Except.begin(); it < Stmt_List_Except.end(); ++it) {
(*it)->Print(Spaces + 1);
}
print_indent(Spaces);
cout << "end" << endl;
}
void Stmt_Try_Finally::Print(int Spaces) {
print_indent(Spaces);
cout << "try" << endl;
for (auto it = Stmt_List_Try.begin(); it < Stmt_List_Try.end(); ++it) {
(*it)->Print(Spaces + 1);
}
print_indent(Spaces);
cout << "finally" << endl;
for (auto it = Stmt_List_Finally.begin(); it < Stmt_List_Finally.end(); ++it) {
(*it)->Print(Spaces + 1);
}
print_indent(Spaces);
cout << "end" << endl;
}
void Stmt_Raise::Print(int Spaces) {
print_indent(Spaces);
cout << "raise" << endl;
Exp->Print(Spaces + 1);
}
void Stmt_Assign::Print(int Spaces) {
Exp->Print(Spaces);
}
void Stmt_Call::Print(int Spaces) {
print_indent(Spaces);
cout << "Call" << endl;
Exp->Print(Spaces + 1);
}
void Stmt_Goto::Generate(Asm_Code* Code) {
Code->Add(Jmp, Code->GetGlobalLabelName(LabelSym->Name));
}
void Stmt_Goto_Label::Generate(Asm_Code* Code) {
Code->AddLabel(Code->GetGlobalLabelName(LabelSym->Name));
}
void Stmt_Compound::Generate(Asm_Code* Code) {
for (auto it = StmtList.begin(); it < StmtList.end(); ++it) {
(*it)->Generate(Code);
}
}
void Stmt_Conditional::Generate_Condition(Asm_Code* Code) {
Exp->Generate(Code);
Code->Add(Pop, EAX);
Code->Add(Test, EAX, EAX);
}
void Stmt_If::Generate(Asm_Code* Code) {
string LabelName_Else = Code->GetLocalLabelName();
Generate_Condition(Code);
Code->Add(Jz, LabelName_Else);
Stmt->Generate(Code);
Code->AddLabel(LabelName_Else);
if (StmtElse != nullptr) {
StmtElse->Generate(Code);
}
}
void Stmt_Case::Generate(Asm_Code* Code) {
string LabelName_Break = Code->GetLocalLabelName();
for (auto it = SelectorList.begin(); it < SelectorList.end(); ++it) {
if (it->Exp_2 == nullptr) {
ExprBinOp(Exp, Token(TK_EQUAL), it->Exp_1).Generate(Code);
Code->Add(Pop, EAX);
}
else {
ExprBinOp(Exp, Token(TK_GREAT_EQUAL), it->Exp_1).Generate(Code);
ExprBinOp(Exp, Token(TK_LESS_EQUAL), it->Exp_2).Generate(Code);
Code->Add(Pop, EAX);
Code->Add(Pop, EBX);
Code->Add(And, EAX, EBX);
}
Code->Add(Test, EAX, EAX);
string LabelName_Else = Code->GetLocalLabelName();
Code->Add(Jz, LabelName_Else);
it->Stmt->Generate(Code);
Code->Add(Jmp, LabelName_Break);
Code->AddLabel(LabelName_Else);
}
if (StmtElse != nullptr) {
StmtElse->Generate(Code);
}
Code->AddLabel(LabelName_Break);
}
void Stmt_For::Generate(Asm_Code* Code) {
Exp_1->Generate(Code);
string LabelName_Cond = Code->GetLocalLabelName();
string LabelName_Break = Code->GetLocalLabelName();
Code->SaveLabels(LabelName_Cond, LabelName_Break);
Code->AddLabel(LabelName_Cond);
Generate_Condition(Code);
Code->Add(Jnz, LabelName_Break);
Stmt->Generate(Code);
ExprAssign(((ExprAssign*)Exp_1)->Left, new ExprBinOp(((ExprAssign*)Exp_1)->Left, Token(isTO ? TK_PLUS : TK_MINUS), new ExprIntConst("1"))).Generate(Code);
Code->Add(Jmp, LabelName_Cond);
Code->AddLabel(LabelName_Break);
Code->LoadLabels();
}
void Stmt_While::Generate(Asm_Code* Code) {
string LabelName_Cond = Code->GetLocalLabelName();
string LabelName_Break = Code->GetLocalLabelName();
Code->SaveLabels(LabelName_Cond, LabelName_Break);
Code->AddLabel(LabelName_Cond);
Generate_Condition(Code);
Code->Add(Jz, LabelName_Break);
Stmt->Generate(Code);
Code->Add(Jmp, LabelName_Cond);
Code->AddLabel(LabelName_Break);
Code->LoadLabels();
}
void Stmt_Repeat::Generate(Asm_Code* Code) {
string LabelName_Body = Code->GetLocalLabelName();
string LabelName_Break = Code->GetLocalLabelName();
string LabelName_Cond = Code->GetLocalLabelName();
Code->SaveLabels(LabelName_Cond, LabelName_Break);
Code->AddLabel(LabelName_Body);
for (auto it = StmtList.begin(); it < StmtList.end(); ++it) {
(*it)->Generate(Code);
}
Code->AddLabel(LabelName_Cond);
Generate_Condition(Code);
Code->Add(Jz, LabelName_Body);
Code->AddLabel(LabelName_Break);
Code->LoadLabels();
}
void Stmt_Break::Generate(Asm_Code* Code) {
Code->Add(Jmp, Code->GetLabelBreak());
}
void Stmt_Continue::Generate(Asm_Code* Code) {
Code->Add(Jmp, Code->GetLabelContinue());
}
void Stmt_Try_Except::Generate(Asm_Code* Code) {
string OldErrorLabelName = Code->ErrroLabelName;
string LabelAfterExcept = Code->GetGlobalLabelName();
Code->ErrroLabelName = Code->GetNewErrorLabelName();
for (auto it = Stmt_List_Try.begin(); it < Stmt_List_Try.end(); ++it) {
(*it)->Generate(Code);
}
Code->Add(Jmp, LabelAfterExcept);
Code->AddLabel(Code->ErrroLabelName);
for (auto it = Stmt_List_Except.begin(); it < Stmt_List_Except.end(); ++it) {
(*it)->Generate(Code);
}
Code->AddLabel(LabelAfterExcept);
}
void Stmt_Try_Finally::Generate(Asm_Code* Code) {
string OldErrorLabelName = Code->ErrroLabelName;
Code->ErrroLabelName = Code->GetNewErrorLabelName();
for (auto it = Stmt_List_Try.begin(); it < Stmt_List_Try.end(); ++it) {
(*it)->Generate(Code);
}
Code->AddLabel(Code->ErrroLabelName);
for (auto it = Stmt_List_Finally.begin(); it < Stmt_List_Finally.end(); ++it) {
(*it)->Generate(Code);
}
}
void Stmt_Raise::Generate(Asm_Code* Code) {
string FormatName = Code->AddFormat("\'%s\', 0xA, 0x0");
Exp->Generate(Code);
Code->Add(Push, FormatName);
Code->Add(Call, "_printf");
Code->Add(Add, ESP, 8);
Code->Add(Jmp, Code->ErrroLabelName);
}
void Stmt_Assign::Generate(Asm_Code* Code) {
Exp->Generate(Code);
}
void Stmt_Call::Generate(Asm_Code* Code) {
if (((ExprIdent*)((ExprFunction*)Exp)->Left)->Sym->Section == DeclFunction) {
Exp->Generate(Code);
SymFunction* Sym = (SymFunction*)((ExprIdent*)((ExprFunction*)Exp)->Left)->Sym;
Code->Add(Add, ESP, ((SymIdent*)Sym->Table->Symbols[Sym->argc - 1])->Type->GetSize());
}
else {
Exp->Generate(Code);
}
}