Commit e74f9687

e74f9687ff35Merged

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

Description

Add M21: multiple assignment (a, b = 1, 2)

MultiWriteNode with an ArrayNode tuple RHS: each target's type is the corresponding element's type (analyze, in the recompute frame); codegen evaluates all RHS values into temps first so a, b = b, a swaps correctly, then assigns the local targets.

a,b=1,2 / swap / 3-way / mixed-type multiple assignment match CRuby. make test: 22 (correct capability; whole-test count steady).

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

Details

Commit
e74f9687ff35
Parent
d80329c1cb88

src/analyze.cmodifiedView file

@@ -698,6 +698,28 @@
698698 lv->type = ty_unify(lv->type, newt);
699699 }
700700
701+ /* Multiple assignment `a, b = e0, e1`: each target gets its element's
702+ type (the RHS ArrayNode is a tuple here, not an array value). */
703+ for (int id = 0; id < nt->count; id++) {
704+ if (strcmp(nt_type(nt, id) ? nt_type(nt, id) : "", "MultiWriteNode")) continue;
705+ int ln = 0;
706+ const int *lefts = nt_arr(nt, id, "lefts", &ln);
707+ int value = nt_ref(nt, id, "value");
708+ const char *vty = nt_type(nt, value);
709+ if (!vty || strcmp(vty, "ArrayNode")) continue;
710+ int en = 0;
711+ const int *els = nt_arr(nt, value, "elements", &en);
712+ for (int i = 0; i < ln && i < en; i++) {
713+ if (strcmp(nt_type(nt, lefts[i]) ? nt_type(nt, lefts[i]) : "", "LocalVariableTargetNode")) continue;
714+ const char *lnm = nt_str(nt, lefts[i], "name");
715+ TyKind et = infer_type(c, els[i]);
716+ if (et == TY_NIL) continue;
717+ LocalVar *lv = lnm ? scope_local(comp_scope_of(c, id), lnm) : NULL;
718+ if (!lv || lv->is_param || lv->is_block_param) continue;
719+ lv->type = ty_unify(lv->type, et);
720+ }
721+ }
722+
701723 /* Fold container usage into the local type: `a << x` / `a.push(x)` /
702724 `a[i] = x` on a local `a` promotes it to an array of x's type (this is
703725 how an empty `[]` gets its element type). Part of the same recompute