diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lower.c | 34 |
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; } |
