Commit d80329c1

d80329c1cb88Merged

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

Description

Add M20: global variables and top-level constants

  • compiler: gvars / consts tables (LocalVar-based) with intern/find.
  • analyze: register $globals (identifier-named) and top-level ConstantWrite constants; infer their types in the fixpoint (nil writes excluded, operator-writes folded); GlobalVariableRead / ConstantRead typed from the tables.
  • codegen: emit file-scope statics (gv_ / cst_) initialized to the type default; reads -> gv_/cst_; writes and global operator-writes as statements (string += via sp_str_concat).

Top-level constants and $globals (int/string/float, with += accumulation) match CRuby. make test: 22 (up from 19).

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

Details

Commit
d80329c1cb88
Parent
48a64b36ca62

src/analyze.cmodifiedView file

@@ -269,6 +269,16 @@
269269 LocalVar *lv = nm ? scope_local(s, nm) : NULL;
270270 return lv ? lv->type : TY_UNKNOWN;
271271 }
272+ if (!strcmp(ty, "GlobalVariableReadNode")) {
273+ const char *nm = nt_str(nt, id, "name");
274+ LocalVar *lv = nm ? comp_gvar(c, nm + 1) : NULL;
275+ return lv ? lv->type : TY_UNKNOWN;
276+ }
277+ if (!strcmp(ty, "ConstantReadNode")) {
278+ const char *nm = nt_str(nt, id, "name");
279+ LocalVar *lv = nm ? comp_const(c, nm) : NULL;
280+ return lv ? lv->type : TY_UNKNOWN;
281+ }
272282 if (!strcmp(ty, "InstanceVariableReadNode")) {
273283 const char *nm = nt_str(nt, id, "name");
274284 Scope *s = comp_scope_of(c, id);
@@ -474,6 +484,66 @@
474484 }
475485 }
476486
487+static int is_c_ident(const char *s) {
488+ if (!s || !*s) return 0;
489+ for (const char *p = s; *p; p++)
490+ if (!((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') ||
491+ (*p >= '0' && *p <= '9') || *p == '_')) return 0;
492+ return 1;
493+}
494+
495+/* Register global variables ($g) and top-level constants (FOO). */
496+static void register_globals_consts(Compiler *c) {
497+ const NodeTable *nt = c->nt;
498+ for (int id = 0; id < nt->count; id++) {
499+ const char *ty = nt_type(nt, id);
500+ if (!ty) continue;
501+ if (!strcmp(ty, "GlobalVariableWriteNode") || !strcmp(ty, "GlobalVariableReadNode") ||
502+ !strcmp(ty, "GlobalVariableOperatorWriteNode") || !strcmp(ty, "GlobalVariableTargetNode")) {
503+ const char *nm = nt_str(nt, id, "name");
504+ if (nm && nm[0] == '$' && is_c_ident(nm + 1)) comp_gvar_intern(c, nm + 1);
505+ } else if (!strcmp(ty, "ConstantWriteNode")) {
506+ const char *nm = nt_str(nt, id, "name");
507+ if (nm && is_c_ident(nm)) comp_const_intern(c, nm);
508+ }
509+ }
510+}
511+
512+static int infer_global_const_types(Compiler *c) {
513+ const NodeTable *nt = c->nt;
514+ int changed = 0;
515+ for (int id = 0; id < nt->count; id++) {
516+ const char *ty = nt_type(nt, id);
517+ if (!ty) continue;
518+ LocalVar *lv = NULL;
519+ TyKind vt = TY_UNKNOWN;
520+ if (!strcmp(ty, "GlobalVariableWriteNode")) {
521+ const char *nm = nt_str(nt, id, "name");
522+ if (nm) lv = comp_gvar(c, nm + 1);
523+ vt = infer_type(c, nt_ref(nt, id, "value"));
524+ if (vt == TY_NIL) continue;
525+ } else if (!strcmp(ty, "GlobalVariableOperatorWriteNode")) {
526+ const char *nm = nt_str(nt, id, "name");
527+ if (nm) lv = comp_gvar(c, nm + 1);
528+ TyKind cur = lv ? lv->type : TY_UNKNOWN;
529+ TyKind v = infer_type(c, nt_ref(nt, id, "value"));
530+ if (cur == TY_STRING) vt = TY_STRING;
531+ else if (ty_is_numeric(cur) && ty_is_numeric(v)) vt = (cur == TY_FLOAT || v == TY_FLOAT) ? TY_FLOAT : TY_INT;
532+ else vt = cur;
533+ } else if (!strcmp(ty, "ConstantWriteNode")) {
534+ const char *nm = nt_str(nt, id, "name");
535+ if (nm) lv = comp_const(c, nm);
536+ vt = infer_type(c, nt_ref(nt, id, "value"));
537+ } else {
538+ continue;
539+ }
540+ if (!lv) continue;
541+ TyKind merged = ty_unify(lv->type, vt);
542+ if (merged != lv->type) { lv->type = merged; changed = 1; }
543+ }
544+ return changed;
545+}
546+
477547 /* Resolve each class's superclass index from its ClassNode. */
478548 static void resolve_parents(Compiler *c) {
479549 const NodeTable *nt = c->nt;
@@ -847,6 +917,7 @@
847917 walk_scope(c, c->nt->root_id, 0, -1);
848918 register_locals(c);
849919 register_attrs(c);
920+ register_globals_consts(c);
850921 resolve_parents(c);
851922 inherit_members(c);
852923
@@ -867,6 +938,7 @@
867938 ch |= infer_block_params(c);
868939 ch |= infer_ivar_types(c);
869940 ch |= infer_inherited_ivars(c);
941+ ch |= infer_global_const_types(c);
870942 ch |= infer_return_types(c);
871943 if (!ch) break;
872944 }