stage1 完成15测试

This commit is contained in:
zzy
2026-07-06 21:26:27 +08:00
parent 0892c084ee
commit 69cea030dc
6 changed files with 91 additions and 45 deletions

View File

@@ -37,6 +37,11 @@ static void parse_ret_stmt(spl_comp_t *ctx) {
advance(ctx); /* ret */
skip_nl(ctx);
/* Before returning, execute all pending defers from innermost scope outward */
for (int d = ctx->scope_depth; d >= 1; d--) {
spl_emit_defer_epilogue(ctx, d);
}
if (peek(ctx)->type == TOK_SEMICOLON || peek(ctx)->type == TOK_R_BRACE ||
peek(ctx)->type == TOK_ENDLINE) {
/* void return */
@@ -151,6 +156,7 @@ void spl_parse_block(spl_comp_t *ctx) {
skip_nl(ctx);
}
spl_emit_defer_epilogue(ctx, ctx->scope_depth);
spl_pop_scope(ctx);
if (peek(ctx)->type == TOK_R_BRACE)
advance(ctx);
@@ -339,6 +345,7 @@ static void parse_for_stmt(spl_comp_t *ctx) {
ctx->in_loop = saved_loop;
ctx->continue_target = saved_continue;
spl_emit_defer_epilogue(ctx, ctx->scope_depth);
spl_pop_scope(ctx);
return;
}
@@ -520,7 +527,7 @@ static void parse_defer_stmt(spl_comp_t *ctx) {
advance(ctx); /* defer */
skip_nl(ctx);
/* Record current position for defer */
/* Record current position for defer (emits a JMP skip placeholder) */
spl_emit_defer(ctx);
/* Parse the deferred statement/block */
@@ -529,6 +536,12 @@ static void parse_defer_stmt(spl_comp_t *ctx) {
} else {
spl_parse_stmt(ctx);
}
/* Emit JMP exit placeholder — will be patched at scope exit */
if (ctx->defer_count > 0) {
spl_defer_entry_t *e = &ctx->defer_stack[ctx->defer_count - 1];
e->jmp_exit = spl_emit_jmp(ctx);
}
}
/* ============================================================