Commit c57e39a8
c57e39a832a0Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 6 files · ~6 modified
Description
Add M2: user-defined methods with inferred param/return types
Introduce per-method scopes and whole-program method inference on top of M1.
- compiler.{c,h}: replace the flat top-level local table with a Scope
model -- scope 0 is top level, one Scope per
def-- each holding its params, locals, and inferred return type. Add a node->scope map and method-name lookup. - node_table: generic child iteration (nt_num_refs/nt_ref_at/ nt_num_arrs/nt_arr_at) for the structural scope-assigning walk.
- analyze.c: assign scopes by walking the tree, register per-scope locals, and run a joint fixpoint over local types, parameter types (inferred from call-site argument types), and return types (implicit last-expression value + explicit return). Recursion converges through the fixpoint.
- codegen.c: emit method prototypes then definitions as static sp_(typed params), with tail-position implicit returns (the body's last value, recursing through if/else branches) and a trailing default return; void methods for non-scalar results; resolve no-receiver calls to user methods.
A program with multi-arg methods, string-returning methods, and a
recursive factorial compiles and matches CRuby. make test: 3 pass
(up from 1).
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- c57e39a832a0
- Parent
- 598b60c82c48
src/analyze.cmodifiedView file
@@ -10,7 +10,6 @@
1010
for (int i = 0; set[i]; i++) if (strcmp(s, set[i]) == 0) return 1;1111
return 0;1212
}13-
1413
static int is_arith_op(const char *op) {1514
static const char *const set[] = {"+", "-", "*", "/", "%", "**", NULL};1615
return str_in(op, set);@@ -40,20 +39,24 @@
4039
int argc = 0;4140
const int *argv = NULL;4241
if (args >= 0) argv = nt_arr(nt, args, "arguments", &argc);43-
4442
if (!name) return TY_UNKNOWN;4543
4644
TyKind rt = recv >= 0 ? infer_type(c, recv) : TY_UNKNOWN;4745
TyKind a0 = argc >= 1 ? infer_type(c, argv[0]) : TY_UNKNOWN;4846
49-
/* unary minus / plus: `-x`, `+x` */50-
if ((strcmp(name, "-@") == 0 || strcmp(name, "+@") == 0) && recv >= 0 && argc == 0)47+
/* user-defined method call (no receiver) */48+
if (recv < 0) {49+
int mi = comp_method_index(c, name);50+
if (mi >= 0) return c->scopes[mi].ret;51+
}52+
53+
if ((!strcmp(name, "-@") || !strcmp(name, "+@")) && recv >= 0 && argc == 0)5154
return ty_is_numeric(rt) ? rt : TY_UNKNOWN;52-
if (strcmp(name, "!") == 0) return TY_BOOL;55+
if (!strcmp(name, "!")) return TY_BOOL;5356
5457
if (recv >= 0 && argc == 1 && is_arith_op(name)) {5558
if (rt == TY_STRING) {56-
if (strcmp(name, "+") == 0 || strcmp(name, "*") == 0) return TY_STRING;59+
if (!strcmp(name, "+") || !strcmp(name, "*")) return TY_STRING;5760
return TY_UNKNOWN;5861
}5962
if (ty_is_numeric(rt) && ty_is_numeric(a0))@@ -61,23 +64,21 @@
6164
return TY_UNKNOWN;6265
}6366
if (recv >= 0 && argc == 1 && is_cmp_op(name)) {64-
if (strcmp(name, "<=>") == 0) return TY_INT;67+
if (!strcmp(name, "<=>")) return TY_INT;6568
return TY_BOOL;6669
}6770
if (argc == 1 && is_eq_op(name)) return TY_BOOL;6871
69-
/* predicate methods conventionally end in ? and return bool */7072
size_t nl = strlen(name);7173
if (nl > 0 && name[nl - 1] == '?') return TY_BOOL;7274
73-
/* common conversions / queries */74-
if (strcmp(name, "to_s") == 0 || strcmp(name, "inspect") == 0 ||75-
strcmp(name, "chr") == 0 || strcmp(name, "to_str") == 0) return TY_STRING;76-
if (strcmp(name, "to_i") == 0 || strcmp(name, "to_int") == 0 ||77-
strcmp(name, "length") == 0 || strcmp(name, "size") == 0 ||78-
strcmp(name, "ord") == 0 || strcmp(name, "abs") == 0) return TY_INT;79-
if (strcmp(name, "to_f") == 0) return TY_FLOAT;80-
if (strcmp(name, "to_sym") == 0) return TY_SYMBOL;75+
if (!strcmp(name, "to_s") || !strcmp(name, "inspect") ||76+
!strcmp(name, "chr") || !strcmp(name, "to_str")) return TY_STRING;77+
if (!strcmp(name, "to_i") || !strcmp(name, "to_int") ||78+
!strcmp(name, "length") || !strcmp(name, "size") ||79+
!strcmp(name, "ord") || !strcmp(name, "abs")) return TY_INT;80+
if (!strcmp(name, "to_f")) return TY_FLOAT;81+
if (!strcmp(name, "to_sym")) return TY_SYMBOL;8182
8283
if (is_void_call(name) && recv < 0) return TY_VOID;8384
@@ -91,46 +92,46 @@
9192
const char *ty = nt_type(nt, id);9293
if (!ty) return TY_UNKNOWN;9394
94-
if (strcmp(ty, "IntegerNode") == 0) return TY_INT;95-
if (strcmp(ty, "FloatNode") == 0) return TY_FLOAT;96-
if (strcmp(ty, "StringNode") == 0) return TY_STRING;97-
if (strcmp(ty, "InterpolatedStringNode") == 0) return TY_STRING;98-
if (strcmp(ty, "SymbolNode") == 0) return TY_SYMBOL;99-
if (strcmp(ty, "TrueNode") == 0) return TY_BOOL;100-
if (strcmp(ty, "FalseNode") == 0) return TY_BOOL;101-
if (strcmp(ty, "NilNode") == 0) return TY_NIL;102-
if (strcmp(ty, "RangeNode") == 0) return TY_RANGE;95+
if (!strcmp(ty, "IntegerNode")) return TY_INT;96+
if (!strcmp(ty, "FloatNode")) return TY_FLOAT;97+
if (!strcmp(ty, "StringNode")) return TY_STRING;98+
if (!strcmp(ty, "InterpolatedStringNode")) return TY_STRING;99+
if (!strcmp(ty, "SymbolNode")) return TY_SYMBOL;100+
if (!strcmp(ty, "TrueNode")) return TY_BOOL;101+
if (!strcmp(ty, "FalseNode")) return TY_BOOL;102+
if (!strcmp(ty, "NilNode")) return TY_NIL;103+
if (!strcmp(ty, "RangeNode")) return TY_RANGE;103104
104-
if (strcmp(ty, "LocalVariableReadNode") == 0) {105+
if (!strcmp(ty, "LocalVariableReadNode")) {105106
const char *nm = nt_str(nt, id, "name");106-
LocalVar *lv = nm ? comp_local(c, nm) : NULL;107+
Scope *s = comp_scope_of(c, id);108+
LocalVar *lv = nm ? scope_local(s, nm) : NULL;107109
return lv ? lv->type : TY_UNKNOWN;108110
}109-
if (strcmp(ty, "ParenthesesNode") == 0) {111+
if (!strcmp(ty, "ParenthesesNode")) {110112
int body = nt_ref(nt, id, "body");111-
if (body < 0) return TY_UNKNOWN;112-
/* body is a StatementsNode; type is its last statement */113+
if (body < 0) return TY_NIL;113114
int n = 0;114115
const int *b = nt_arr(nt, body, "body", &n);115116
return n > 0 ? infer_type(c, b[n - 1]) : TY_NIL;116117
}117-
if (strcmp(ty, "StatementsNode") == 0) {118+
if (!strcmp(ty, "StatementsNode")) {118119
int n = 0;119120
const int *b = nt_arr(nt, id, "body", &n);120121
return n > 0 ? infer_type(c, b[n - 1]) : TY_NIL;121122
}122-
if (strcmp(ty, "IfNode") == 0 || strcmp(ty, "UnlessNode") == 0) {123+
if (!strcmp(ty, "IfNode") || !strcmp(ty, "UnlessNode")) {123124
int then_b = nt_ref(nt, id, "statements");124125
int else_b = nt_ref(nt, id, "subsequent");125126
TyKind tt = then_b >= 0 ? infer_type(c, then_b) : TY_NIL;126127
TyKind et = else_b >= 0 ? infer_type(c, else_b) : TY_NIL;127128
return ty_unify(tt, et);128129
}129-
if (strcmp(ty, "ElseNode") == 0) {130+
if (!strcmp(ty, "ElseNode")) {130131
int s = nt_ref(nt, id, "statements");131132
return s >= 0 ? infer_type(c, s) : TY_NIL;132133
}133-
if (strcmp(ty, "CallNode") == 0) return infer_call(c, id);134+
if (!strcmp(ty, "CallNode")) return infer_call(c, id);134135
135136
return TY_UNKNOWN;136137
}@@ -142,24 +143,70 @@
142143
return t;143144
}144145
145-
/* ---- local variable registration + fixpoint ---- */146+
/* ---- scope assignment ---- */147+
148+
static void scope_add_param(Scope *s, const char *name) {149+
if (s->nparams % 8 == 0)150+
s->pnames = realloc(s->pnames, sizeof(char *) * (size_t)(s->nparams + 8));151+
s->pnames[s->nparams++] = strdup(name);152+
LocalVar *lv = scope_local_intern(s, name);153+
lv->is_param = 1;154+
}146155
156+
static void walk_scope(Compiler *c, int id, int scope_idx) {157+
if (id < 0 || id >= c->nt->count) return;158+
c->nscope[id] = scope_idx;159+
const char *ty = nt_type(c->nt, id);160+
int child = scope_idx;161+
162+
if (ty && !strcmp(ty, "DefNode")) {163+
const char *name = nt_str(c->nt, id, "name");164+
Scope *s = comp_scope_new(c, name, id);165+
int new_idx = c->nscopes - 1;166+
s->body = nt_ref(c->nt, id, "body");167+
int pn = nt_ref(c->nt, id, "parameters");168+
if (pn >= 0) {169+
int rn = 0;170+
const int *reqs = nt_arr(c->nt, pn, "requireds", &rn);171+
for (int i = 0; i < rn; i++) {172+
const char *pname = nt_str(c->nt, reqs[i], "name");173+
if (pname) scope_add_param(s, pname);174+
}175+
}176+
child = new_idx;177+
}178+
179+
int nr = nt_num_refs(c->nt, id);180+
for (int i = 0; i < nr; i++) {181+
int r = nt_ref_at(c->nt, id, i);182+
if (r >= 0) walk_scope(c, r, child);183+
}184+
int na = nt_num_arrs(c->nt, id);185+
for (int i = 0; i < na; i++) {186+
int n = 0;187+
const int *ids = nt_arr_at(c->nt, id, i, &n);188+
for (int j = 0; j < n; j++)189+
if (ids[j] >= 0) walk_scope(c, ids[j], child);190+
}191+
}192+
147193
static void register_locals(Compiler *c) {148194
const NodeTable *nt = c->nt;149195
for (int id = 0; id < nt->count; id++) {150196
const char *ty = nt_type(nt, id);151197
if (!ty) continue;152-
if (strcmp(ty, "LocalVariableWriteNode") == 0 ||153-
strcmp(ty, "LocalVariableTargetNode") == 0 ||154-
strcmp(ty, "LocalVariableReadNode") == 0 ||155-
strcmp(ty, "LocalVariableOperatorWriteNode") == 0) {198+
if (!strcmp(ty, "LocalVariableWriteNode") ||199+
!strcmp(ty, "LocalVariableTargetNode") ||200+
!strcmp(ty, "LocalVariableReadNode") ||201+
!strcmp(ty, "LocalVariableOperatorWriteNode")) {156202
const char *nm = nt_str(nt, id, "name");157-
if (nm) comp_local_intern(c, nm);203+
if (nm) scope_local_intern(comp_scope_of(c, id), nm);158204
}159205
}160206
}161207
162-
/* One pass over all writes; returns 1 if any local type changed. */208+
/* ---- fixpoint passes ---- */209+
163210
static int infer_write_types(Compiler *c) {164211
const NodeTable *nt = c->nt;165212
int changed = 0;@@ -168,27 +215,24 @@
168215
if (!ty) continue;169216
const char *nm = NULL;170217
TyKind newt = TY_UNKNOWN;171-
if (strcmp(ty, "LocalVariableWriteNode") == 0) {218+
if (!strcmp(ty, "LocalVariableWriteNode")) {172219
nm = nt_str(nt, id, "name");173-
int v = nt_ref(nt, id, "value");174-
newt = infer_type(c, v);175-
} else if (strcmp(ty, "LocalVariableOperatorWriteNode") == 0) {220+
newt = infer_type(c, nt_ref(nt, id, "value"));221+
} else if (!strcmp(ty, "LocalVariableOperatorWriteNode")) {176222
nm = nt_str(nt, id, "name");177-
const char *op = nt_str(nt, id, "binary_operator");178-
int v = nt_ref(nt, id, "value");179-
LocalVar *cur = nm ? comp_local(c, nm) : NULL;180-
TyKind vt = infer_type(c, v);223+
Scope *s = comp_scope_of(c, id);224+
LocalVar *cur = nm ? scope_local(s, nm) : NULL;225+
TyKind vt = infer_type(c, nt_ref(nt, id, "value"));181226
TyKind ct = cur ? cur->type : TY_UNKNOWN;182227
if (ct == TY_STRING) newt = TY_STRING;183228
else if (ty_is_numeric(ct) && ty_is_numeric(vt))184229
newt = (ct == TY_FLOAT || vt == TY_FLOAT) ? TY_FLOAT : TY_INT;185230
else newt = ct;186-
(void)op;187231
} else {188232
continue;189233
}190234
if (!nm) continue;191-
LocalVar *lv = comp_local(c, nm);235+
LocalVar *lv = scope_local(comp_scope_of(c, id), nm);192236
if (!lv) continue;193237
TyKind merged = ty_unify(lv->type, newt);194238
if (merged != lv->type) { lv->type = merged; changed = 1; }@@ -196,17 +240,80 @@
196240
return changed;197241
}198242
243+
static int infer_param_types(Compiler *c) {244+
const NodeTable *nt = c->nt;245+
int changed = 0;246+
for (int id = 0; id < nt->count; id++) {247+
const char *ty = nt_type(nt, id);248+
if (!ty || strcmp(ty, "CallNode")) continue;249+
if (nt_ref(nt, id, "receiver") >= 0) continue;250+
const char *name = nt_str(nt, id, "name");251+
int mi = comp_method_index(c, name);252+
if (mi < 0) continue;253+
Scope *m = &c->scopes[mi];254+
int args = nt_ref(nt, id, "arguments");255+
int argc = 0;256+
const int *argv = NULL;257+
if (args >= 0) argv = nt_arr(nt, args, "arguments", &argc);258+
int n = argc < m->nparams ? argc : m->nparams;259+
for (int k = 0; k < n; k++) {260+
TyKind at = infer_type(c, argv[k]);261+
LocalVar *p = scope_local(m, m->pnames[k]);262+
if (!p) continue;263+
TyKind merged = ty_unify(p->type, at);264+
if (merged != p->type) { p->type = merged; changed = 1; }265+
}266+
}267+
return changed;268+
}269+
270+
/* Value type of an explicit `return expr` (or nil for bare return). */271+
static TyKind return_node_type(Compiler *c, int id) {272+
int args = nt_ref(c->nt, id, "arguments");273+
if (args < 0) return TY_NIL;274+
int n = 0;275+
const int *a = nt_arr(c->nt, args, "arguments", &n);276+
return n > 0 ? infer_type(c, a[0]) : TY_NIL;277+
}278+
279+
static int infer_return_types(Compiler *c) {280+
const NodeTable *nt = c->nt;281+
int changed = 0;282+
/* implicit return: the body's value */283+
for (int s = 1; s < c->nscopes; s++) {284+
Scope *sc = &c->scopes[s];285+
TyKind r = sc->body >= 0 ? infer_type(c, sc->body) : TY_NIL;286+
/* explicit returns within this scope */287+
for (int id = 0; id < nt->count; id++) {288+
const char *ty = nt_type(nt, id);289+
if (ty && !strcmp(ty, "ReturnNode") && comp_scope_of(c, id) == sc)290+
r = ty_unify(r, return_node_type(c, id));291+
}292+
if (r != sc->ret) { sc->ret = r; changed = 1; }293+
}294+
return changed;295+
}296+
199297
void analyze_program(Compiler *c) {298+
/* scope 0 = top level */299+
Scope *top = comp_scope_new(c, NULL, -1);300+
top->body = nt_ref(c->nt, c->nt->root_id, "statements");301+
302+
walk_scope(c, c->nt->root_id, 0);200303
register_locals(c);201304
202-
/* Fixpoint on local variable types (bounded to avoid runaway). */203-
for (int iter = 0; iter < 64; iter++) {204-
if (!infer_write_types(c)) break;305+
for (int iter = 0; iter < 128; iter++) {306+
int ch = 0;307+
ch |= infer_write_types(c);308+
ch |= infer_param_types(c);309+
ch |= infer_return_types(c);310+
if (!ch) break;205311
}206312
207-
/* Mark gc-root needs and finalize the node type cache. */208-
for (int i = 0; i < c->nlocals; i++)209-
c->locals[i].gc_root = (c->locals[i].type == TY_STRING);313+
/* finalize: gc-root needs + full node type cache */314+
for (int s = 0; s < c->nscopes; s++)315+
for (int i = 0; i < c->scopes[s].nlocals; i++)316+
c->scopes[s].locals[i].gc_root = (c->scopes[s].locals[i].type == TY_STRING);210317
211318
for (int id = 0; id < c->nt->count; id++)212319
infer_type(c, id);src/codegen.cmodifiedView file
Loading diff…
src/compiler.cmodifiedView file
Loading diff…
src/compiler.hmodifiedView file
Loading diff…
src/node_table.cmodifiedView file
Loading diff…
src/node_table.hmodifiedView file
Loading diff…