Commit 27710149
277101496b51Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
Add MatchRequiredNode (rightward pattern assign) codegen and inference
value => pattern desugars to:
- ArrayPatternNode: evaluate value into a typed-array temp, length-check against requireds count (raises NoMatchingPatternError on mismatch), then assign sp_Array_get(arr, i) to each LocalVariableTargetNode.
- HashPatternNode: evaluate value into a hash temp, then look up each key and assign sp_Hash_get(hash, key) to each target local.
Type inference (in infer_write_types): infer locals from pattern shape using literal ArrayNode/HashNode value elements.
Fixes test/rightward_assign_destructure.rb (was ERR).
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 277101496b51
- Parent
- 745bd90271e2
src/analyze.cmodifiedView file
@@ -2484,6 +2484,74 @@
24842484
}24852485
}24862486
2487+
/* MatchRequiredNode: `value => pattern` — infer locals from pattern shape. */2488+
for (int id = 0; id < nt->count; id++) {2489+
const char *ty = nt_type(nt, id);2490+
if (!ty || strcmp(ty, "MatchRequiredNode")) continue;2491+
int value = nt_ref(nt, id, "value");2492+
int pattern = nt_ref(nt, id, "pattern");2493+
if (value < 0 || pattern < 0) continue;2494+
const char *pty = nt_type(nt, pattern);2495+
if (!pty) continue;2496+
Scope *ms = comp_scope_of(c, id);2497+
if (!strcmp(pty, "ArrayPatternNode")) {2498+
int rn = 0;2499+
const int *reqs = nt_arr(nt, pattern, "requireds", &rn);2500+
/* Try to get types from a literal ArrayNode value. */2501+
const char *vty = nt_type(nt, value);2502+
int en = 0;2503+
const int *els = (vty && !strcmp(vty, "ArrayNode")) ? nt_arr(nt, value, "elements", &en) : NULL;2504+
TyKind arr_elem = TY_UNKNOWN;2505+
if (ty_is_array(infer_type(c, value))) arr_elem = ty_array_elem(infer_type(c, value));2506+
for (int i = 0; i < rn; i++) {2507+
const char *lty2 = nt_type(nt, reqs[i]);2508+
if (!lty2 || strcmp(lty2, "LocalVariableTargetNode")) continue;2509+
const char *lnm = nt_str(nt, reqs[i], "name");2510+
LocalVar *lv = lnm ? scope_local(ms, lnm) : NULL;2511+
if (!lv || lv->is_param || lv->is_block_param) continue;2512+
TyKind et = (els && i < en) ? infer_type(c, els[i]) : arr_elem;2513+
if (et == TY_UNKNOWN || et == TY_NIL) continue;2514+
TyKind mg = ty_unify(lv->type, et);2515+
if (mg != lv->type) { lv->type = mg; changed = 1; }2516+
}2517+
}2518+
else if (!strcmp(pty, "HashPatternNode")) {2519+
int pn = 0;2520+
const int *pelms = nt_arr(nt, pattern, "elements", &pn);2521+
/* Try to match keys from a literal HashNode value. */2522+
const char *vty = nt_type(nt, value);2523+
int vn = 0;2524+
const int *velms = (vty && !strcmp(vty, "HashNode")) ? nt_arr(nt, value, "elements", &vn) : NULL;2525+
for (int i = 0; i < pn; i++) {2526+
const char *ety = nt_type(nt, pelms[i]);2527+
if (!ety || strcmp(ety, "AssocNode")) continue;2528+
int pkey = nt_ref(nt, pelms[i], "key");2529+
int ptgt = nt_ref(nt, pelms[i], "value");2530+
if (ptgt < 0) continue;2531+
const char *tty = nt_type(nt, ptgt);2532+
if (!tty || strcmp(tty, "LocalVariableTargetNode")) continue;2533+
const char *lnm = nt_str(nt, ptgt, "name");2534+
LocalVar *lv = lnm ? scope_local(ms, lnm) : NULL;2535+
if (!lv || lv->is_param || lv->is_block_param) continue;2536+
/* find matching key in value hash */2537+
const char *pkey_val = (pkey >= 0 && nt_type(nt, pkey) &&2538+
!strcmp(nt_type(nt, pkey), "SymbolNode")) ? nt_str(nt, pkey, "value") : NULL;2539+
TyKind et = TY_UNKNOWN;2540+
if (pkey_val && velms) {2541+
for (int j = 0; j < vn; j++) {2542+
int vkey = nt_ref(nt, velms[j], "key");2543+
const char *vkty = vkey >= 0 ? nt_type(nt, vkey) : NULL;2544+
const char *vkval = (vkty && !strcmp(vkty, "SymbolNode")) ? nt_str(nt, vkey, "value") : NULL;2545+
if (vkval && !strcmp(vkval, pkey_val)) { et = infer_type(c, nt_ref(nt, velms[j], "value")); break; }2546+
}2547+
}2548+
if (et == TY_UNKNOWN || et == TY_NIL) continue;2549+
TyKind mg = ty_unify(lv->type, et);2550+
if (mg != lv->type) { lv->type = mg; changed = 1; }2551+
}2552+
}2553+
}2554+
24872555
/* Fold container usage into the local type so an empty `[]` / `{}` gets24882556
its element / key+value type from how it is filled. `a << x` /24892557
`a.push(x)` / `a[i] = x` (int key) -> array; `h[k] = v` / `h[k] op= v`src/codegen.cmodifiedView file
Loading diff…