@@ -22,6 +22,21 @@ import string_buffer;
22
22
import stdio;
23
23
import string;
24
24
25
+ fn void Generator.emitVarDecl(Generator* gen, VarDecl* vd, string_buffer.Buf* out, bool emit_init) {
26
+ Decl* d = cast<Decl*>(vd);
27
+ if (vd.hasLocalQualifier()) out.add("static ");
28
+ gen.emitTypePre(out, d.getType());
29
+ out.space();
30
+ out.add(d.getName());
31
+ gen.emitTypePost(out, d.getType());
32
+ d.setGenerated();
33
+ Expr* ie = vd.getInit();
34
+ if (ie && emit_init) {
35
+ out.add(" = ");
36
+ gen.emitExpr(out, ie);
37
+ }
38
+ }
39
+
25
40
fn void Generator.emitStmt(Generator* gen, Stmt* s, u32 indent, bool newline) {
26
41
string_buffer.Buf* out = gen.out;
27
42
@@ -87,14 +102,21 @@ fn void Generator.emitStmt(Generator* gen, Stmt* s, u32 indent, bool newline) {
87
102
Stmt* cond = w.getCond();
88
103
bool is_decl = cond.isDecl();
89
104
if (is_decl) {
90
- // TODO leave out init part
91
- gen.emitStmt(cond, 0, true);
92
- out.indent(indent);
93
- out.add("while (");
105
+ out.add("{\n");
106
+ indent++;
94
107
DeclStmt* ds = cast<DeclStmt*>(cond);
95
108
VarDecl* vd = ds.getDecl();
109
+ out.indent(indent);
110
+ gen.emitVarDecl(vd, out, false);
111
+ out.add(";\n");
112
+ out.indent(indent);
113
+ out.add("while (");
96
114
out.add(vd.asDecl().getName());
97
- // TODO also add init part
115
+ // add init part
116
+ out.add(" = ");
117
+ Expr* ie = vd.getInit();
118
+ assert(ie);
119
+ gen.emitExpr(out, ie);
98
120
} else {
99
121
out.add("while (");
100
122
gen.emitStmt(cond, 0, false);
@@ -106,6 +128,11 @@ fn void Generator.emitStmt(Generator* gen, Stmt* s, u32 indent, bool newline) {
106
128
out.add1(';');
107
129
}
108
130
out.newline();
131
+ if (is_decl) {
132
+ indent--;
133
+ out.indent(indent);
134
+ out.add("}\n");
135
+ }
109
136
break;
110
137
case For:
111
138
ForStmt* f = cast<ForStmt*>(s);
@@ -174,18 +201,7 @@ fn void Generator.emitStmt(Generator* gen, Stmt* s, u32 indent, bool newline) {
174
201
case Decl:
175
202
DeclStmt* ds = cast<DeclStmt*>(s);
176
203
VarDecl* vd = ds.getDecl();
177
- Decl* d = cast<Decl*>(vd);
178
- if (vd.hasLocalQualifier()) out.add("static ");
179
- gen.emitTypePre(out, d.getType());
180
- out.space();
181
- out.add(d.getName());
182
- gen.emitTypePost(out, d.getType());
183
- d.setGenerated();
184
- Expr* ie = vd.getInit();
185
- if (ie) {
186
- out.add(" = ");
187
- gen.emitExpr(out, ie);
188
- }
204
+ gen.emitVarDecl(vd, out, true);
189
205
if (newline) out.add(";\n");
190
206
break;
191
207
case Asm:
0 commit comments