Skip to content

Commit c92c6e2

Browse files
Liviarodrigues1BeyondMagic
authored andcommitted
feat(codegen): emitir if/else em Lua
1 parent a67e855 commit c92c6e2

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/codegen_lua.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,8 @@ static void emit_statement(FILE *out, const AstStmt *stmt, const FunctionTable *
168168
{
169169
if (!stmt->data.decl.init || !expr_has_side_effects(stmt->data.decl.init))
170170
{
171-
return; // Dead Code Elimination: remove unused locals without side effects
171+
return; // Dead Code Elimination
172172
}
173-
// Unused variable with initializer that has side effects: emit only the initializer as a statement
174173
emit_indent(out, indent);
175174
emit_expression_expected(out, stmt->data.decl.init, functions, stmt->data.decl.type);
176175
fputc('\n', out);
@@ -244,6 +243,26 @@ static void emit_statement(FILE *out, const AstStmt *stmt, const FunctionTable *
244243
emit_indent(out, indent);
245244
fputs("end\n", out);
246245
break;
246+
247+
case STMT_IF:
248+
emit_indent(out, indent);
249+
fputs("if ", out);
250+
emit_expression_as_bool(out, stmt->data.if_stmt.condition, functions);
251+
fputs(" then\n", out);
252+
253+
emit_statement(out, stmt->data.if_stmt.then_branch, functions, signature, indent + 1);
254+
255+
if (stmt->data.if_stmt.else_branch)
256+
{
257+
emit_indent(out, indent);
258+
fputs("else\n", out);
259+
emit_statement(out, stmt->data.if_stmt.else_branch, functions, signature, indent + 1);
260+
}
261+
262+
emit_indent(out, indent);
263+
fputs("end\n", out);
264+
break;
265+
247266
case STMT_EXPR:
248267
if (stmt->data.expr)
249268
{
@@ -270,6 +289,8 @@ static void emit_statement(FILE *out, const AstStmt *stmt, const FunctionTable *
270289
}
271290
}
272291

292+
/* resto do arquivo fica igual ao seu original */
293+
273294
static void emit_expression_expected(FILE *out, const AstExpr *expr, const FunctionTable *functions, TypeKind expected_type)
274295
{
275296
if (!expr)
@@ -777,4 +798,4 @@ static void emit_indent(FILE *out, int indent)
777798
{
778799
fputs("\t", out);
779800
}
780-
}
801+
}

0 commit comments

Comments
 (0)