Commit ffbb0117

ffbb01170c74Merged

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

Description

Support global variable compound writes and multi-assignment

  • GlobalVariableOrWriteNode / GlobalVariableAndWriteNode: emit conditional write (if (!gv_x) / if (gv_x)) in codegen and infer the assigned type in analyze.
  • GlobalVariableTargetNode in MultiWriteNode lefts: assign the appropriate temp slot in the multi-write emission path.
  • MultiWriteNode arm in infer_global_const_types: propagate element types from a literal ArrayNode RHS to each GlobalVariableTargetNode so global vars set via $a, $b = 1, 2 get a concrete type.

Fixes test/global_var.rb (was ERR).

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
ffbb01170c74
Parent
3a93f97a1021

src/analyze.cmodifiedView file

@@ -1756,7 +1756,8 @@
17561756 const char *ty = nt_type(nt, id);
17571757 if (!ty) continue;
17581758 if (!strcmp(ty, "GlobalVariableWriteNode") || !strcmp(ty, "GlobalVariableReadNode") ||
1759- !strcmp(ty, "GlobalVariableOperatorWriteNode") || !strcmp(ty, "GlobalVariableTargetNode")) {
1759+ !strcmp(ty, "GlobalVariableOperatorWriteNode") || !strcmp(ty, "GlobalVariableTargetNode") ||
1760+ !strcmp(ty, "GlobalVariableOrWriteNode") || !strcmp(ty, "GlobalVariableAndWriteNode")) {
17601761 const char *nm = nt_str(nt, id, "name");
17611762 /* skip alias names - they resolve to the original and need no separate slot */
17621763 if (nm && nm[0] == '$' && is_c_ident(nm + 1) &&
@@ -1831,11 +1832,39 @@
18311832 else if (ty_is_numeric(cur) && ty_is_numeric(v)) vt = (cur == TY_FLOAT || v == TY_FLOAT) ? TY_FLOAT : TY_INT;
18321833 else vt = cur;
18331834 }
1835+ else if (!strcmp(ty, "GlobalVariableOrWriteNode") || !strcmp(ty, "GlobalVariableAndWriteNode")) {
1836+ const char *nm = nt_str(nt, id, "name");
1837+ const char *rn = nm ? comp_resolve_gvar(c, nm + 1) : NULL;
1838+ if (rn) lv = comp_gvar(c, rn);
1839+ vt = infer_type(c, nt_ref(nt, id, "value"));
1840+ if (vt == TY_NIL) continue;
1841+ }
18341842 else if (!strcmp(ty, "ConstantWriteNode")) {
18351843 const char *nm = nt_str(nt, id, "name");
18361844 if (nm) lv = comp_const(c, nm);
18371845 vt = infer_type(c, nt_ref(nt, id, "value"));
18381846 }
1847+ else if (!strcmp(ty, "MultiWriteNode")) {
1848+ int ln = 0;
1849+ const int *lefts = nt_arr(nt, id, "lefts", &ln);
1850+ int value = nt_ref(nt, id, "value");
1851+ const char *vty = nt_type(nt, value);
1852+ int en = 0;
1853+ const int *els = (vty && !strcmp(vty, "ArrayNode")) ? nt_arr(nt, value, "elements", &en) : NULL;
1854+ for (int i = 0; i < ln; i++) {
1855+ const char *lty2 = nt_type(nt, lefts[i]);
1856+ if (!lty2 || strcmp(lty2, "GlobalVariableTargetNode")) continue;
1857+ const char *gnm = nt_str(nt, lefts[i], "name");
1858+ const char *rn2 = gnm ? comp_resolve_gvar(c, gnm + 1) : NULL;
1859+ LocalVar *glv = rn2 ? comp_gvar(c, rn2) : NULL;
1860+ if (!glv) continue;
1861+ TyKind vt2 = (els && i < en) ? infer_type(c, els[i]) : TY_UNKNOWN;
1862+ if (vt2 == TY_NIL || vt2 == TY_UNKNOWN) continue;
1863+ TyKind merged2 = ty_unify(glv->type, vt2);
1864+ if (merged2 != glv->type) { glv->type = merged2; changed = 1; }
1865+ }
1866+ continue;
1867+ }
18391868 else {
18401869 continue;
18411870 }