-
Notifications
You must be signed in to change notification settings - Fork 1
/
ast_stmt.cc
175 lines (161 loc) · 4.58 KB
/
ast_stmt.cc
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
/* File: ast_stmt.cc
* -----------------
* Implementation of statement node classes.
*/
#include "ast_stmt.h"
#include "ast_type.h"
#include "ast_decl.h"
#include "ast_expr.h"
#include "scope.h"
#include "errors.h"
#include "codegen.h"
Program::Program(List<Decl*> *d) {
Assert(d != NULL);
(decls=d)->SetParentAll(this);
}
void Program::Check() {
nodeScope = new Scope();
decls->DeclareAll(nodeScope);
decls->CheckAll();
}
void Program::Emit() {
bool found = false;
for (int i=0; i < decls->NumElements(); i++) {
Decl *d = decls->Nth(i);
if (!strcmp(d->GetName(), "main") && d->IsFnDecl()) {
found = true;
break;
}
}
if (!found) {
ReportError::NoMainFound();
return;
}
CodeGenerator *cg = new CodeGenerator();
decls->EmitAll(cg);
if (ReportError::NumErrors() == 0)
cg->DoFinalCodeGen();
}
StmtBlock::StmtBlock(List<VarDecl*> *d, List<Stmt*> *s) {
Assert(d != NULL && s != NULL);
(decls=d)->SetParentAll(this);
(stmts=s)->SetParentAll(this);
}
void StmtBlock::Check() {
nodeScope = new Scope();
decls->DeclareAll(nodeScope);
decls->CheckAll();
stmts->CheckAll();
}
void StmtBlock::Emit(CodeGenerator *cg) {
decls->EmitAll(cg);
stmts->EmitAll(cg);
}
ConditionalStmt::ConditionalStmt(Expr *t, Stmt *b) {
Assert(t != NULL && b != NULL);
(test=t)->SetParent(this);
(body=b)->SetParent(this);
}
void ConditionalStmt::Check() {
if (!test->CheckAndComputeResultType()->IsCompatibleWith(Type::boolType))
ReportError::TestNotBoolean(test);
body->Check();
}
ForStmt::ForStmt(Expr *i, Expr *t, Expr *s, Stmt *b): LoopStmt(t, b) {
Assert(i != NULL && t != NULL && s != NULL && b != NULL);
(init=i)->SetParent(this);
(step=s)->SetParent(this);
}
void ForStmt::Emit(CodeGenerator *cg) {
init->Emit(cg);
char *topLoop = cg->NewLabel();
afterLoopLabel = cg->NewLabel();
cg->GenLabel(topLoop);
test->Emit(cg);
cg->GenIfZ(test->result, afterLoopLabel);
body->Emit(cg);
step->Emit(cg);
cg->GenGoto(topLoop);
cg->GenLabel(afterLoopLabel);
}
void WhileStmt::Emit(CodeGenerator *cg) {
char *topLoop = cg->NewLabel();
afterLoopLabel = cg->NewLabel();
cg->GenLabel(topLoop);
test->Emit(cg);
cg->GenIfZ(test->result, afterLoopLabel);
body->Emit(cg);
cg->GenGoto(topLoop);
cg->GenLabel(afterLoopLabel);
}
IfStmt::IfStmt(Expr *t, Stmt *tb, Stmt *eb): ConditionalStmt(t, tb) {
Assert(t != NULL && tb != NULL); // else can be NULL
elseBody = eb;
if (elseBody) elseBody->SetParent(this);
}
void IfStmt::Check() {
ConditionalStmt::Check();
if (elseBody) elseBody->Check();
}
void IfStmt::Emit(CodeGenerator *cg) {
test->Emit(cg);
char *afterElse, *elseL = cg->NewLabel();
cg->GenIfZ(test->result, elseL);
body->Emit(cg);
if (elseBody) {
afterElse = cg->NewLabel();
cg->GenGoto(afterElse);
}
cg->GenLabel(elseL);
if (elseBody) {
elseBody->Emit(cg);
cg->GenLabel(afterElse);
}
}
void BreakStmt::Check() {
if (!FindSpecificParent<LoopStmt>())
ReportError::BreakOutsideLoop(this);
}
void BreakStmt::Emit(CodeGenerator *cg) {
LoopStmt *enclosingLoop = FindSpecificParent<LoopStmt>();
cg->GenGoto(enclosingLoop->GetLoopExitLabel());
}
ReturnStmt::ReturnStmt(yyltype loc, Expr *e) : Stmt(loc) {
Assert(e != NULL);
(expr=e)->SetParent(this);
}
void ReturnStmt::Check() {
Type *got = expr->CheckAndComputeResultType();
Type *expected = FindSpecificParent<FnDecl>()->GetReturnType();
if (!got->IsCompatibleWith(expected))
ReportError::ReturnMismatch(this, got, expected);
}
void ReturnStmt::Emit(CodeGenerator *cg) {
expr->Emit(cg);
cg->GenReturn(expr->result);
}
PrintStmt::PrintStmt(List<Expr*> *a) {
Assert(a != NULL);
(args=a)->SetParentAll(this);
}
void PrintStmt::Check() {
for (int i = 0; i < args->NumElements();i++) {
Type *t = args->Nth(i)->CheckAndComputeResultType();
if (t->IsEquivalentTo(Type::errorType)) continue;
if (!(t->IsEquivalentTo(Type::intType) || t->IsEquivalentTo(Type::stringType) || t->IsEquivalentTo(Type::boolType)))
ReportError::PrintArgMismatch(args->Nth(i),i + 1, t);
}
}
void PrintStmt::Emit(CodeGenerator *cg) {
for (int i = 0; i < args->NumElements(); i++) {
Expr *arg = args->Nth(i);
Type *argType = arg->CheckAndComputeResultType();
arg->Emit(cg);
BuiltIn b = PrintInt;
if (argType->IsEquivalentTo(Type::stringType))
b = PrintString;
else if (argType->IsEquivalentTo(Type::boolType))
b = PrintBool;
cg->GenBuiltInCall(b, arg->result);
}
}