Commit 630fd313

630fd313cb3cMerged

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

Description

Add class methods, for..in loops, loop, break/next

  • def self.foo: class (singleton) methods, emitted as sp_s with no instance self; Const.cmethod(args) dispatch + return/param type inference.
  • for v in range/array: counted loop / array walk, index typed from the collection element type (survives the write-types reset).
  • loop { } -> for(;;), and BreakNode/NextNode/RedoNode -> break/continue.

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

Details

Commit
630fd313cb3c
Parent
db5b5032f4a3

src/analyze.cmodifiedView file

@@ -63,6 +63,18 @@
6363 }
6464 }
6565
66+ /* Class.cmethod(...) -> the class method's return type */
67+ if (recv >= 0) {
68+ const char *rty = nt_type(nt, recv);
69+ if (rty && !strcmp(rty, "ConstantReadNode")) {
70+ int ci = comp_class_index(c, nt_str(nt, recv, "name"));
71+ if (ci >= 0) {
72+ int mi = comp_cmethod_in_chain(c, ci, name, NULL);
73+ if (mi >= 0) return c->scopes[mi].ret;
74+ }
75+ }
76+ }
77+
6678 /* obj.method(...) -> the method's return type (walks the superclass chain) */
6779 if (recv >= 0 && ty_is_object(rt)) {
6880 int cid = ty_object_class(rt);
@@ -458,6 +470,8 @@
458470 int new_idx = c->nscopes - 1;
459471 s->body = nt_ref(c->nt, id, "body");
460472 s->class_id = class_id; /* instance method of the enclosing class */
473+ /* `def self.foo` / `def Klass.foo`: a class (singleton) method. */
474+ if (nt_ref(c->nt, id, "receiver") >= 0) s->is_cmethod = 1;
461475 int pn = nt_ref(c->nt, id, "parameters");
462476 if (pn >= 0) {
463477 int rn = 0;
@@ -953,14 +967,20 @@
953967 changed |= bind_call_params(c, id, mi);
954968 continue;
955969 }
956- /* Class.new -> initialize params */
957- if (!strcmp(name, "new")) {
970+ /* Class.new -> initialize params; Class.cmethod -> cmethod params */
971+ {
958972 const char *rty = nt_type(nt, recv);
959973 if (rty && !strcmp(rty, "ConstantReadNode")) {
960974 int ci = comp_class_index(c, nt_str(nt, recv, "name"));
961- if (ci >= 0) changed |= bind_call_params(c, id, comp_method_in_chain(c, ci, "initialize", NULL));
975+ if (ci >= 0) {
976+ if (!strcmp(name, "new"))
977+ changed |= bind_call_params(c, id, comp_method_in_chain(c, ci, "initialize", NULL));
978+ else
979+ changed |= bind_call_params(c, id, comp_cmethod_in_chain(c, ci, name, NULL));
980+ continue;
981+ }
962982 }
963- continue;
983+ if (!strcmp(name, "new")) continue;
964984 }
965985 /* obj.method -> instance method params */
966986 TyKind rt = infer_type(c, recv);
@@ -970,6 +990,29 @@
970990 return changed;
971991 }
972992
993+/* `for x in coll` binds x to the collection's element type (int for a
994+ range, the array element type for an array). */
995+static int infer_for_index(Compiler *c) {
996+ const NodeTable *nt = c->nt;
997+ int changed = 0;
998+ for (int id = 0; id < nt->count; id++) {
999+ const char *ty = nt_type(nt, id);
1000+ if (!ty || strcmp(ty, "ForNode")) continue;
1001+ int idx = nt_ref(nt, id, "index");
1002+ int coll = nt_ref(nt, id, "collection");
1003+ if (idx < 0 || coll < 0) continue;
1004+ const char *vn = nt_str(nt, idx, "name");
1005+ if (!vn) continue;
1006+ TyKind ct = infer_type(c, coll);
1007+ TyKind et = ct == TY_RANGE ? TY_INT : ty_is_array(ct) ? ty_array_elem(ct) : TY_UNKNOWN;
1008+ if (et == TY_UNKNOWN) continue;
1009+ LocalVar *lv = scope_local_intern(comp_scope_of(c, idx), vn);
1010+ lv->is_block_param = 1; /* iteration-bound: survives the write-types reset */
1011+ if (lv->type != et) { lv->type = et; changed = 1; }
1012+ }
1013+ return changed;
1014+}
1015+
9731016 /* Name of a block's idx-th required parameter, or NULL. */
9741017 const char *block_param_name(Compiler *c, int block, int idx) {
9751018 int bp = nt_ref(c->nt, block, "parameters"); /* BlockParametersNode */
@@ -1212,6 +1255,7 @@
12121255 ch |= infer_param_types(c);
12131256 ch |= infer_default_param_types(c);
12141257 ch |= infer_block_params(c);
1258+ ch |= infer_for_index(c);
12151259 ch |= infer_ivar_types(c);
12161260 ch |= infer_inherited_ivars(c);
12171261 ch |= infer_global_const_types(c);