Commit 52299d2c

52299d2c09e2Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

case as an expression

A case used for its value now compiles: analyze infers its type as the unify of each when-branch's value (a missing else means a no-match falls through to nil, widening to poly), and codegen emits a GCC statement-expression that assigns the matched branch's value to a result temp (boxed when the result is poly), defaulting to nil/the type default on no match. The statement form is unchanged.

make test: 178 pass / 5 fail / 661 error (was 175 / 5 / 664); case_fallthrough and two others green, no regressions.

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

Details

Commit
52299d2c09e2
Parent
b6e2b8364eeb

src/analyze.cmodifiedView file

@@ -694,6 +694,20 @@
694694 const int *b = nt_arr(nt, id, "body", &n);
695695 return n > 0 ? infer_type(c, b[n - 1]) : TY_NIL;
696696 }
697+ if (!strcmp(ty, "CaseNode")) {
698+ /* value = unify of each when's body; a missing else means a no-match
699+ falls through to nil */
700+ int nw = 0; const int *whens = nt_arr(nt, id, "conditions", &nw);
701+ int else_c = nt_ref(nt, id, "else_clause");
702+ TyKind r = TY_UNKNOWN;
703+ for (int w = 0; w < nw; w++) {
704+ int st = nt_ref(nt, whens[w], "statements");
705+ r = ty_unify(r, st >= 0 ? infer_type(c, st) : TY_NIL);
706+ }
707+ if (else_c >= 0) { int st = nt_ref(nt, else_c, "statements"); r = ty_unify(r, st >= 0 ? infer_type(c, st) : TY_NIL); }
708+ else r = ty_unify(r, TY_NIL);
709+ return r;
710+ }
697711 if (!strcmp(ty, "IfNode") || !strcmp(ty, "UnlessNode")) {
698712 int then_b = nt_ref(nt, id, "statements");
699713 int else_b = nt_ref(nt, id, "subsequent");