aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2026-02-26 01:19:44 +0200
committerKimplul <kimi.h.kuparinen@gmail.com>2026-02-26 01:19:44 +0200
commitbdb66a2de5cd1e6c192f7342b9d0b3349d8c9c6a (patch)
treeedb4a1be34ce1e0411592ff8fcb6fe4a7585af94 /src
parent1b9cc15a515180aa154d2af40c0b54823363c2bf (diff)
downloadfwd-bdb66a2de5cd1e6c192f7342b9d0b3349d8c9c6a.tar.gz
fwd-bdb66a2de5cd1e6c192f7342b9d0b3349d8c9c6a.zip
fix nil check lowering
Diffstat (limited to 'src')
-rw-r--r--src/lower.c34
1 files changed, 27 insertions, 7 deletions
diff --git a/src/lower.c b/src/lower.c
index 1795292..3b6193f 100644
--- a/src/lower.c
+++ b/src/lower.c
@@ -112,7 +112,9 @@ static struct state create_state(struct state *parent)
static size_t uniq(struct state *state)
{
- return state->uniq++;
+ (void)state;
+ static size_t q = 0;
+ return q++;
}
static void increase_indent(struct state *state)
@@ -656,17 +658,30 @@ static int lower_if(struct state *state, struct ast *stmt, bool last)
static int lower_nil_check(struct state *state, struct ast *stmt, bool last)
{
+ struct ast *var = nil_check_ref(stmt);
+ char *type = lower_type_str(state, var->scope, var->t);
+ char *name = mangle2(var);
+
+ fprintf(state->ctx, " %s %s;\n", type, name);
+
indent(state);
- fprintf(state->code, "if (");
+ fprintf(state->code, "ctx->%s = ", name);
if (lower_expr(state, nil_check_expr(stmt)))
return -1;
- fprintf(state->code, ")\n");
+ fprintf(state->code, ";\n");
+
+ indent(state);
+ fprintf(state->code, "if (!ctx->%s)", name);
if (lower_block(state, nil_check_body(stmt), last))
return -1;
indent(state);
fprintf(state->code, "else\n");
+
+ free(type);
+ free(name);
+
return lower_block(state, nil_check_rest(stmt), last);
}
@@ -710,15 +725,14 @@ static int lower_explode(struct state *state, struct ast *stmt, bool last)
static int lower_let(struct state *state, struct ast *stmt, bool last)
{
- /* not relevant */
- (void)last;
-
struct ast *var = let_var(stmt);
char *type = lower_type_str(state, var->scope, var->t);
char *name = mangle2(var);
+ fprintf(state->ctx, " %s %s;\n", type, name);
+
indent(state);
- fprintf(state->code, "%s %s = ", type, name);
+ fprintf(state->code, "ctx->%s = ", name);
free(type);
free(name);
@@ -727,6 +741,12 @@ static int lower_let(struct state *state, struct ast *stmt, bool last)
return -1;
fprintf(state->code, ";\n");
+
+ if (last) {
+ indent(state);
+ fprintf(state->code, "return stack;\n");
+ }
+
return 0;
}