Commit f9bf1778

f9bf1778d3caMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

fix: N.times.map block body and elsif in expression context

Two bugs in expr.c codegen_expr_call:

  1. N.times.map { |i| ... } only compiled the last expression of the block body, skipping preceding statements. Now compiles all statements before pushing the last expression's value.

    Before: y, x, n assignments in life's next_gen were silently dropped After: all block statements execute, only last value is pushed

  2. if/elsif/else in expression context (ternary) only handled PM_ELSE_NODE for the subsequent branch, dropping elsif chains. Now recursively handles PM_IF_NODE as nested ternary.

    Before: (n == 3) ? 1 : 0 (elsif lost) After: (n == 3) ? 1 : ((n == 4 && cells[i] == 1) ? 1 : 0)

life benchmark: 8ms (buggy) → 37ms (correct). Now matches Ruby version. 60/60 tests, 39/39 benchmarks pass.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
f9bf1778d3ca
Parent
c712d835ecd5

src/expr.cmodifiedView file

@@ -54,6 +54,9 @@
5454 if (blk->body) {
5555 pm_statements_node_t *stmts = (pm_statements_node_t *)blk->body;
5656 if (stmts->body.size > 0) {
57+ /* Compile all statements before the last one */
58+ for (size_t si = 0; si + 1 < stmts->body.size; si++)
59+ codegen_stmt(ctx, stmts->body.nodes[si]);
5760 char *val = codegen_expr(ctx, stmts->body.nodes[stmts->body.size - 1]);
5861 emit(ctx, "sp_IntArray_push(_tmap_%d, %s);\n", tmp, val);
5962 free(val);
@@ -4941,6 +4944,11 @@
49414944 else_e = codegen_expr(ctx, el->statements->body.nodes[0]);
49424945 }
49434946 }
4947+ else if (PM_NODE_TYPE(n->subsequent) == PM_IF_NODE) {
4948+ /* elsif chain → nested ternary */
4949+ free(else_e);
4950+ else_e = codegen_expr(ctx, n->subsequent);
4951+ }
49444952 }
49454953 char *r = sfmt("(%s ? %s : %s)", cond, then_e, else_e);
49464954 free(cond); free(then_e); free(else_e);