Commit 745bd902
745bd90271e2Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Support multi-stmt if/unless expressions and MatchRequiredNode stub
Multi-stmt if/unless as a value expression:
- When either branch has more than one statement, the previous code
fell through to
unsupported. Now: allocate a result temp, emit a real if/else block into the prolog buffer that sets the temp for each branch, and return the temp as the expression value. - Leading statements in each branch are emitted for side effects; the last statement's value is assigned to the temp.
- ElseNode branches are handled inline; IfNode subsequents (elsif chains) recurse through emit_expr so each level creates its own temp and they chain via _tN = _tM assignments.
- Single-stmt ternary fast path is unchanged.
Fixes test/if_expr_multi_stmt_body.rb (was ERR).
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 745bd90271e2
- Parent
- d987d75cff61
src/codegen.cmodifiedView file
@@ -8252,7 +8252,101 @@
82528252
buf_puts(b, ")");82538253
return;82548254
}8255-
unsupported(c, id, "if/unless expression");8255+
/* Multi-stmt branches or no-else: emit as if/else block with a result temp.8256+
Preludes and the if structure go into g_pre; `b` receives only _t<N>. */8257+
{8258+
TyKind res = comp_ntype(c, id);8259+
int tr = ++g_tmp;8260+
/* Declare the temp and default-initialize it. */8261+
emit_indent(g_pre, g_indent);8262+
emit_ctype(c, res, g_pre);8263+
buf_printf(g_pre, " _t%d = %s;\n", tr,8264+
res == TY_RANGE ? "(sp_Range){0}" : default_value(res));8265+
/* Emit condition — any prolog from the condition expr also goes to g_pre. */8266+
emit_indent(g_pre, g_indent);8267+
buf_puts(g_pre, "if (");8268+
if (is_unless) buf_puts(g_pre, "!(");8269+
emit_cond(c, pred, g_pre);8270+
if (is_unless) buf_puts(g_pre, ")");8271+
buf_puts(g_pre, ") {\n");8272+
/* Then branch: side-effect stmts, then assign last expr to temp. */8273+
for (int i = 0; i < tn - 1; i++) emit_stmt(c, tb[i], g_pre, g_indent + 2);8274+
if (tn > 0) {8275+
int last_then = tb[tn - 1];8276+
TyKind lt = comp_ntype(c, last_then);8277+
if (lt == TY_NIL || lt == TY_UNKNOWN) {8278+
emit_stmt(c, last_then, g_pre, g_indent + 2);8279+
}8280+
else {8281+
int saved_gi = g_indent; g_indent = g_indent + 2;8282+
Buf le; memset(&le, 0, sizeof le);8283+
emit_expr(c, last_then, &le);8284+
g_indent = saved_gi;8285+
emit_indent(g_pre, g_indent + 2);8286+
buf_printf(g_pre, "_t%d = ", tr);8287+
if (res == TY_POLY && lt != TY_POLY) {8288+
Buf bx; memset(&bx, 0, sizeof bx);8289+
emit_boxed_text(c, lt, le.p ? le.p : default_value(lt), &bx);8290+
buf_puts(g_pre, bx.p ? bx.p : "sp_box_nil()"); free(bx.p);8291+
}8292+
else buf_puts(g_pre, le.p ? le.p : default_value(res));8293+
buf_puts(g_pre, ";\n");8294+
free(le.p);8295+
}8296+
}8297+
emit_indent(g_pre, g_indent);8298+
buf_puts(g_pre, "}\n");8299+
/* Else / elsif branch. */8300+
if (sub >= 0) {8301+
const char *sub_ty = nt_type(nt, sub);8302+
if (sub_ty && !strcmp(sub_ty, "ElseNode")) {8303+
emit_indent(g_pre, g_indent);8304+
buf_puts(g_pre, "else {\n");8305+
for (int i = 0; i < en - 1; i++) emit_stmt(c, eb[i], g_pre, g_indent + 2);8306+
if (en > 0) {8307+
int last_else = eb[en - 1];8308+
TyKind lt2 = comp_ntype(c, last_else);8309+
if (lt2 == TY_NIL || lt2 == TY_UNKNOWN) {8310+
emit_stmt(c, last_else, g_pre, g_indent + 2);8311+
}8312+
else {8313+
int saved_gi2 = g_indent; g_indent = g_indent + 2;8314+
Buf le2; memset(&le2, 0, sizeof le2);8315+
emit_expr(c, last_else, &le2);8316+
g_indent = saved_gi2;8317+
emit_indent(g_pre, g_indent + 2);8318+
buf_printf(g_pre, "_t%d = ", tr);8319+
if (res == TY_POLY && lt2 != TY_POLY) {8320+
Buf bx2; memset(&bx2, 0, sizeof bx2);8321+
emit_boxed_text(c, lt2, le2.p ? le2.p : default_value(lt2), &bx2);8322+
buf_puts(g_pre, bx2.p ? bx2.p : "sp_box_nil()"); free(bx2.p);8323+
}8324+
else buf_puts(g_pre, le2.p ? le2.p : default_value(res));8325+
buf_puts(g_pre, ";\n");8326+
free(le2.p);8327+
}8328+
}8329+
emit_indent(g_pre, g_indent);8330+
buf_puts(g_pre, "}\n");8331+
}8332+
else {8333+
/* elsif (sub is IfNode) or other subsequent: recurse via emit_expr */8334+
emit_indent(g_pre, g_indent);8335+
buf_puts(g_pre, "else {\n");8336+
int saved_gi3 = g_indent; g_indent = g_indent + 2;8337+
Buf sub_e; memset(&sub_e, 0, sizeof sub_e);8338+
emit_expr(c, sub, &sub_e);8339+
g_indent = saved_gi3;8340+
emit_indent(g_pre, g_indent + 2);8341+
buf_printf(g_pre, "_t%d = %s;\n", tr, sub_e.p ? sub_e.p : default_value(res));8342+
free(sub_e.p);8343+
emit_indent(g_pre, g_indent);8344+
buf_puts(g_pre, "}\n");8345+
}8346+
}8347+
buf_printf(b, "_t%d", tr);8348+
return;8349+
}82568350
}82578351
if (!strcmp(ty, "CallNode")) { emit_call(c, id, b); return; }82588352
if (!strcmp(ty, "SuperNode") || !strcmp(ty, "ForwardingSuperNode")) {