Commit 5a0a023b

5a0a023baf3eMerged

Yasuhiro Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified

Description

codegen: support case/in (pattern match) in value position

A CaseMatchNode used as a value -- assignment RHS, method receiver, or any expression -- hit "unsupported expression" (or silently produced nil in return position). Add a value mode to emit_case_match that assigns each arm's body value (boxed to the case's result type) to a result temp, and an emit_expr handler that declares the temp, emits the match into the prelude, and yields the temp.

Co-Authored-By: Claude [email protected]

Details

Commit
5a0a023baf3e
Parent
4258ef5b0248

src/codegen_expr.cmodifiedView file

@@ -357,6 +357,20 @@
357357 }
358358 if (!strcmp(ty, "LambdaNode")) { emit_proc_literal(c, id, b); return; }
359359 if (!strcmp(ty, "CaseNode")) { emit_case_expr(c, id, b); return; }
360+ if (!strcmp(ty, "CaseMatchNode")) {
361+ /* case/in as a value: declare a result temp, emit the match into g_pre
362+ with each arm assigning its body value to it, then yield the temp. */
363+ TyKind rt = comp_ntype(c, id);
364+ if (rt == TY_UNKNOWN || rt == TY_VOID) rt = TY_POLY;
365+ int cr = ++g_tmp;
366+ emit_indent(g_pre, g_indent);
367+ emit_ctype(c, rt, g_pre);
368+ buf_printf(g_pre, " _t%d = %s;\n", cr, rt == TY_RANGE ? "(sp_Range){0}" : default_value(rt));
369+ if (needs_root(rt)) { emit_indent(g_pre, g_indent); buf_printf(g_pre, "SP_GC_ROOT(_t%d);\n", cr); }
370+ emit_case_match(c, id, g_pre, g_indent, 0, cr);
371+ buf_printf(b, "_t%d", cr);
372+ return;
373+ }
360374
361375 if (!strcmp(ty, "RangeNode")) {
362376 int left = nt_ref(nt, id, "left");