Commit 7fefd7a2
7fefd7a25fffMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
Add CaseMatchNode codegen and pattern variable type inference
- analyze.c: scan CaseMatchNode conditions to infer types for pattern-bound locals. Handles LocalVariableTargetNode (bare bind), IfNode (guard clause, binding in statements body), CapturePatternNode (=> var capture), and ArrayPatternNode (requireds + rest splat).
- codegen.c: emit_case_match() generates if/goto chains for each InNode arm. Binds pattern variables before optional guard check; goto end-label skips remaining arms after a match.
Fixes pattern_match_splat_rest and pattern_match_as_and_guard tests.
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 7fefd7a25fff
- Parent
- 43165ee5c09b
src/analyze.cmodifiedView file
@@ -3685,6 +3685,110 @@
36853685
}36863686
}36873687
3688+
/* CaseMatchNode: `case X; in PATTERN; ...` — infer locals bound by pattern.3689+
Handles: bare LV (`in x`), guard (`in x if cond`), capture (`in P => x`),3690+
and array patterns (`in [first, *rest]` / `in Array(head, *tail)`). */3691+
for (int id = 0; id < nt->count; id++) {3692+
const char *ty = nt_type(nt, id);3693+
if (!ty || strcmp(ty, "CaseMatchNode")) continue;3694+
int pred = nt_ref(nt, id, "predicate");3695+
if (pred < 0) continue;3696+
TyKind scrutinee_t = infer_type(c, pred);3697+
int cn = 0;3698+
const int *conds = nt_arr(nt, id, "conditions", &cn);3699+
for (int ci = 0; ci < cn; ci++) {3700+
const char *cty = nt_type(nt, conds[ci]);3701+
if (!cty || strcmp(cty, "InNode")) continue;3702+
int pat = nt_ref(nt, conds[ci], "pattern");3703+
if (pat < 0) continue;3704+
Scope *ms = comp_scope_of(c, conds[ci]);3705+
const char *pty = nt_type(nt, pat);3706+
if (!pty) continue;3707+
int bind_lv_node = -1;3708+
int array_pat = -1;3709+
TyKind array_scrutinee = TY_UNKNOWN;3710+
if (!strcmp(pty, "LocalVariableTargetNode")) {3711+
/* in x */3712+
bind_lv_node = pat;3713+
}3714+
else if (!strcmp(pty, "IfNode")) {3715+
/* in x if guard — binding is in IfNode.statements body */3716+
int stmts = nt_ref(nt, pat, "statements");3717+
if (stmts >= 0 && nt_type(nt, stmts) &&3718+
!strcmp(nt_type(nt, stmts), "StatementsNode")) {3719+
int bn = 0;3720+
const int *body = nt_arr(nt, stmts, "body", &bn);3721+
for (int k = 0; k < bn; k++) {3722+
const char *bty = nt_type(nt, body[k]);3723+
if (bty && !strcmp(bty, "LocalVariableTargetNode")) {3724+
bind_lv_node = body[k]; break;3725+
}3726+
}3727+
}3728+
}3729+
else if (!strcmp(pty, "CapturePatternNode")) {3730+
/* in PATTERN => var */3731+
int tgt = nt_ref(nt, pat, "target");3732+
if (tgt >= 0 && nt_type(nt, tgt) &&3733+
!strcmp(nt_type(nt, tgt), "LocalVariableTargetNode"))3734+
bind_lv_node = tgt;3735+
/* inner ArrayPatternNode also gets element-level types */3736+
int val = nt_ref(nt, pat, "value");3737+
if (val >= 0 && nt_type(nt, val) &&3738+
!strcmp(nt_type(nt, val), "ArrayPatternNode")) {3739+
array_pat = val; array_scrutinee = scrutinee_t;3740+
}3741+
}3742+
else if (!strcmp(pty, "ArrayPatternNode")) {3743+
/* in [first, *rest] or in Array(head, *tail) */3744+
array_pat = pat; array_scrutinee = scrutinee_t;3745+
}3746+
/* Bind simple LV target to scrutinee type */3747+
if (bind_lv_node >= 0 && scrutinee_t != TY_UNKNOWN) {3748+
const char *lnm = nt_str(nt, bind_lv_node, "name");3749+
LocalVar *lv = lnm ? scope_local(ms, lnm) : NULL;3750+
if (lv && !lv->is_param && !lv->is_block_param) {3751+
TyKind mg = ty_unify(lv->type, scrutinee_t);3752+
if (mg != lv->type) { lv->type = mg; changed = 1; }3753+
}3754+
}3755+
/* Handle ArrayPatternNode requireds and rest splat */3756+
if (array_pat >= 0) {3757+
TyKind elem_t = ty_is_array(array_scrutinee) ? ty_array_elem(array_scrutinee) : TY_UNKNOWN;3758+
int apn = 0;3759+
const int *reqs = nt_arr(nt, array_pat, "requireds", &apn);3760+
for (int k = 0; k < apn; k++) {3761+
const char *lty2 = nt_type(nt, reqs[k]);3762+
if (!lty2 || strcmp(lty2, "LocalVariableTargetNode")) continue;3763+
const char *lnm = nt_str(nt, reqs[k], "name");3764+
LocalVar *lv = lnm ? scope_local(ms, lnm) : NULL;3765+
if (!lv || lv->is_param || lv->is_block_param) continue;3766+
TyKind et = (elem_t != TY_UNKNOWN) ? elem_t : TY_INT;3767+
TyKind mg = ty_unify(lv->type, et);3768+
if (mg != lv->type) { lv->type = mg; changed = 1; }3769+
}3770+
/* rest splat: *name gets array type */3771+
int rest_nid = nt_ref(nt, array_pat, "rest");3772+
if (rest_nid >= 0) {3773+
const char *rsty2 = nt_type(nt, rest_nid);3774+
int inner = -1;3775+
if (rsty2 && !strcmp(rsty2, "SplatNode"))3776+
inner = nt_ref(nt, rest_nid, "expression");3777+
if (inner >= 0 && nt_type(nt, inner) &&3778+
!strcmp(nt_type(nt, inner), "LocalVariableTargetNode")) {3779+
const char *rnm = nt_str(nt, inner, "name");3780+
LocalVar *lv = rnm ? scope_local(ms, rnm) : NULL;3781+
if (lv && !lv->is_param && !lv->is_block_param) {3782+
TyKind rest_arr = ty_is_array(array_scrutinee) ? array_scrutinee : TY_INT_ARRAY;3783+
TyKind mg = ty_unify(lv->type, rest_arr);3784+
if (mg != lv->type) { lv->type = mg; changed = 1; }3785+
}3786+
}3787+
}3788+
}3789+
}3790+
}3791+
36883792
/* Fold container usage into the local type so an empty `[]` / `{}` gets36893793
its element / key+value type from how it is filled. `a << x` /36903794
`a.push(x)` / `a[i] = x` (int key) -> array; `h[k] = v` / `h[k] op= v`src/codegen.cmodifiedView file
Loading diff…