Commit 9b0aeb69
9b0aeb69a5d7Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified
Description
Add Struct.new / Data.define support
Const = Struct.new(:a, :b) registers a class named Const with a backing ivar + reader + writer per member; Const.new(args) maps positional args to the member ivars (types inferred from the construction). Struct constructors are synthesized (no user initialize); the defining constant write is a no-op and is not registered as a value constant.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 9b0aeb69a5d7
- Parent
- 449565ed5cce
src/analyze.cmodifiedView file
@@ -620,6 +620,44 @@
620620
}621621
}622622
623+
/* `Const = Struct.new(:a, :b)` / `Const = Data.define(:a, :b)` defines a624+
class named Const whose positional members are attr_accessors. Register625+
it as a class with one ivar + reader + writer per member. */626+
static int is_c_ident(const char *s);627+
static void register_structs(Compiler *c) {628+
const NodeTable *nt = c->nt;629+
for (int id = 0; id < nt->count; id++) {630+
const char *ty = nt_type(nt, id);631+
if (!ty || strcmp(ty, "ConstantWriteNode")) continue;632+
const char *cname = nt_str(nt, id, "name");633+
int val = nt_ref(nt, id, "value");634+
if (!cname || val < 0 || !is_c_ident(cname)) continue;635+
if (!nt_type(nt, val) || strcmp(nt_type(nt, val), "CallNode")) continue;636+
const char *mn = nt_str(nt, val, "name");637+
int vr = nt_ref(nt, val, "receiver");638+
const char *rn = vr >= 0 && nt_type(nt, vr) && !strcmp(nt_type(nt, vr), "ConstantReadNode")639+
? nt_str(nt, vr, "name") : NULL;640+
int is_struct = rn && ((!strcmp(rn, "Struct") && mn && !strcmp(mn, "new")) ||641+
(!strcmp(rn, "Data") && mn && !strcmp(mn, "define")));642+
if (!is_struct) continue;643+
if (comp_class_index(c, cname) >= 0) continue;644+
ClassInfo *cls = comp_class_new(c, cname, id);645+
cls->is_struct = 1;646+
int args = nt_ref(nt, val, "arguments");647+
int an = 0;648+
const int *argv = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;649+
for (int a = 0; a < an; a++) {650+
if (!nt_type(nt, argv[a]) || strcmp(nt_type(nt, argv[a]), "SymbolNode")) continue;651+
const char *m = nt_str(nt, argv[a], "value");652+
if (!m) continue;653+
char ivn[256]; snprintf(ivn, sizeof ivn, "@%s", m);654+
comp_ivar_intern(cls, ivn);655+
comp_add_reader(cls, m);656+
comp_add_writer(cls, m);657+
}658+
}659+
}660+
623661
/* Collect attr_reader/attr_writer/attr_accessor declarations in class624662
bodies, registering backing ivars + reader/writer method names. */625663
static void register_attrs(Compiler *c) {@@ -711,7 +749,8 @@
711749
}712750
else if (!strcmp(ty, "ConstantWriteNode")) {713751
const char *nm = nt_str(nt, id, "name");714-
if (nm && is_c_ident(nm)) comp_const_intern(c, nm);752+
/* a Struct/Data const names a class, not a value constant */753+
if (nm && is_c_ident(nm) && comp_class_index(c, nm) < 0) comp_const_intern(c, nm);715754
}716755
}717756
}@@ -1086,6 +1125,19 @@
10861125
if (rty && !strcmp(rty, "ConstantReadNode")) {10871126
int ci = comp_class_index(c, nt_str(nt, recv, "name"));10881127
if (ci >= 0) {1128+
if (!strcmp(name, "new") && c->classes[ci].is_struct) {1129+
/* Struct construction: positional args set member ivars in order. */1130+
ClassInfo *cls = &c->classes[ci];1131+
int args = nt_ref(nt, id, "arguments");1132+
int an = 0;1133+
const int *argv = args >= 0 ? nt_arr(nt, args, "arguments", &an) : NULL;1134+
for (int a = 0; a < an && a < cls->nivars; a++) {1135+
TyKind at = infer_type(c, argv[a]);1136+
TyKind m = ty_unify(cls->ivar_types[a], at);1137+
if (m != cls->ivar_types[a]) { cls->ivar_types[a] = m; changed = 1; }1138+
}1139+
continue;1140+
}10891141
if (!strcmp(name, "new"))10901142
changed |= bind_call_params(c, id, comp_method_in_chain(c, ci, "initialize", NULL));10911143
else@@ -1344,6 +1396,7 @@
13441396
top->body = nt_ref(c->nt, c->nt->root_id, "statements");13451397
13461398
walk_scope(c, c->nt->root_id, 0, -1);1399+
register_structs(c);13471400
register_locals(c);13481401
register_attrs(c);13491402
register_aliases(c);src/codegen.cmodifiedView file
Loading diff…
src/compiler.hmodifiedView file
Loading diff…