Commit 7d48dd3b

7d48dd3bf979Merged

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

Description

Compile-time define_method unrolling

Support '[lits].each { |v| define_method("m_#{v}") { body } }' by synthesizing one method per literal element at analyze time, with a compile-time substitution of the loop variable in the method body (its read becomes the literal). New Scope.dm_subst_{name,node}; codegen substitutes the var inline, skips its decl/root, and drops the define_method/each statements from the class body.

Fixes compile_time_define_method_int / _strings / _predicates.

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

Details

Commit
7d48dd3bf979
Parent
0418fa9c2d50

src/analyze.cmodifiedView file

@@ -2543,6 +2543,139 @@
25432543 }
25442544 }
25452545
2546+static void walk_scope(Compiler *c, int id, int scope_idx, int class_id);
2547+
2548+/* String form of an int/string/symbol literal node, for compile-time
2549+ `define_method` name interpolation. Returns malloc'd, or NULL. */
2550+static char *dm_lit_str(Compiler *c, int lit) {
2551+ const NodeTable *nt = c->nt;
2552+ const char *ty = nt_type(nt, lit);
2553+ if (!ty) return NULL;
2554+ if (!strcmp(ty, "IntegerNode")) {
2555+ char buf[32]; snprintf(buf, sizeof buf, "%lld", (long long)nt_int(nt, lit, "value", 0));
2556+ return strdup(buf);
2557+ }
2558+ if (!strcmp(ty, "StringNode")) {
2559+ const char *s = nt_str(nt, lit, "content");
2560+ if (!s) s = nt_str(nt, lit, "unescaped");
2561+ return s ? strdup(s) : NULL;
2562+ }
2563+ if (!strcmp(ty, "SymbolNode")) { const char *s = nt_str(nt, lit, "value"); return s ? strdup(s) : NULL; }
2564+ return NULL;
2565+}
2566+
2567+/* Evaluate a `define_method(<name-expr>)` name with the each-loop variable
2568+ `bv` bound to literal `lit`. Handles string/symbol literals, a bare loop
2569+ variable, and (interpolated) string/symbol nodes. Returns malloc'd name
2570+ or NULL when not statically resolvable. */
2571+static char *dm_eval_name(Compiler *c, int node, const char *bv, int lit) {
2572+ const NodeTable *nt = c->nt;
2573+ const char *ty = nt_type(nt, node);
2574+ if (!ty) return NULL;
2575+ if (!strcmp(ty, "StringNode")) {
2576+ const char *s = nt_str(nt, node, "content");
2577+ if (!s) s = nt_str(nt, node, "unescaped");
2578+ return s ? strdup(s) : NULL;
2579+ }
2580+ if (!strcmp(ty, "SymbolNode")) { const char *s = nt_str(nt, node, "value"); return s ? strdup(s) : NULL; }
2581+ if (!strcmp(ty, "LocalVariableReadNode")) {
2582+ const char *nm = nt_str(nt, node, "name");
2583+ if (nm && bv && !strcmp(nm, bv)) return dm_lit_str(c, lit);
2584+ return NULL;
2585+ }
2586+ if (!strcmp(ty, "EmbeddedStatementsNode")) {
2587+ int body = nt_ref(nt, node, "statements");
2588+ int bn = 0; const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;
2589+ if (bn != 1) return NULL;
2590+ return dm_eval_name(c, bb[0], bv, lit);
2591+ }
2592+ if (!strcmp(ty, "InterpolatedStringNode") || !strcmp(ty, "InterpolatedSymbolNode")) {
2593+ int pn = 0; const int *parts = nt_arr(nt, node, "parts", &pn);
2594+ char *out = strdup("");
2595+ for (int k = 0; k < pn; k++) {
2596+ char *p = dm_eval_name(c, parts[k], bv, lit);
2597+ if (!p) { free(out); return NULL; }
2598+ size_t no = strlen(out) + strlen(p) + 1;
2599+ char *merged = malloc(no); snprintf(merged, no, "%s%s", out, p);
2600+ free(out); free(p); out = merged;
2601+ }
2602+ return out;
2603+ }
2604+ return NULL;
2605+}
2606+
2607+/* TyKind of an int/string/symbol literal node (for the unrolled method's
2608+ subst-var type and return type). */
2609+static TyKind dm_lit_type(Compiler *c, int lit) {
2610+ const char *ty = nt_type(c->nt, lit);
2611+ if (!ty) return TY_UNKNOWN;
2612+ if (!strcmp(ty, "IntegerNode")) return TY_INT;
2613+ if (!strcmp(ty, "StringNode")) return TY_STRING;
2614+ if (!strcmp(ty, "SymbolNode")) return TY_SYMBOL;
2615+ return TY_UNKNOWN;
2616+}
2617+
2618+/* Detect `[lit, ...].each { |v| define_method("m_#{v}") { body } }` in a
2619+ class body and synthesize one method scope per literal element, each with
2620+ a compile-time substitution of `v`. Returns 1 if handled. */
2621+static int collect_dm_each_unroll(Compiler *c, int id, int class_id) {
2622+ const NodeTable *nt = c->nt;
2623+ if (class_id < 0) return 0;
2624+ const char *nm = nt_str(nt, id, "name");
2625+ if (!nm || strcmp(nm, "each")) return 0;
2626+ int recv = nt_ref(nt, id, "receiver");
2627+ if (recv < 0 || !nt_type(nt, recv) || strcmp(nt_type(nt, recv), "ArrayNode")) return 0;
2628+ int blk = nt_ref(nt, id, "block");
2629+ if (blk < 0) return 0;
2630+ /* block parameter name */
2631+ int pn = nt_ref(nt, blk, "parameters");
2632+ int inner = pn >= 0 ? nt_ref(nt, pn, "parameters") : -1;
2633+ int pnode = inner >= 0 ? inner : pn;
2634+ int rnp = 0; const int *reqs = pnode >= 0 ? nt_arr(nt, pnode, "requireds", &rnp) : NULL;
2635+ if (rnp < 1) return 0;
2636+ const char *bv = nt_str(nt, reqs[0], "name");
2637+ if (!bv) return 0;
2638+ /* block body must be a single define_method call */
2639+ int body = nt_ref(nt, blk, "body");
2640+ int bn = 0; const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;
2641+ if (bn != 1) return 0;
2642+ int dc = bb[0];
2643+ if (!nt_type(nt, dc) || strcmp(nt_type(nt, dc), "CallNode")) return 0;
2644+ const char *dcn = nt_str(nt, dc, "name");
2645+ if (!dcn || strcmp(dcn, "define_method") || nt_ref(nt, dc, "receiver") >= 0) return 0;
2646+ int dargs = nt_ref(nt, dc, "arguments");
2647+ int dan = 0; const int *dav = dargs >= 0 ? nt_arr(nt, dargs, "arguments", &dan) : NULL;
2648+ if (dan < 1) return 0;
2649+ int dblk = nt_ref(nt, dc, "block");
2650+ if (dblk < 0) return 0;
2651+ int dbody = nt_ref(nt, dblk, "body");
2652+ /* iterate the array literal's elements */
2653+ int en = 0; const int *elems = nt_arr(nt, recv, "elements", &en);
2654+ if (en == 0) return 0;
2655+ for (int k = 0; k < en; k++) {
2656+ TyKind lt = dm_lit_type(c, elems[k]);
2657+ if (lt == TY_UNKNOWN) return 0; /* non-literal element: bail (unhandled) */
2658+ char *mname = dm_eval_name(c, dav[0], bv, elems[k]);
2659+ if (!mname) return 0;
2660+ Scope *ms = comp_scope_new(c, mname, dc);
2661+ free(mname);
2662+ ms->body = dbody;
2663+ ms->class_id = class_id;
2664+ ms->dm_subst_name = strdup(bv);
2665+ ms->dm_subst_node = elems[k];
2666+ /* the loop var reads inside the body resolve to the literal type */
2667+ LocalVar *lv = scope_local_intern(ms, bv);
2668+ lv->type = lt;
2669+ lv->is_param = 1; /* not a real C param, but keeps it out of decls */
2670+ /* Walk the (shared) define_method body in this synthetic scope so its
2671+ nodes get nscope attribution. The last element wins for the shared
2672+ body nodes; that is fine since all elements share the value type. */
2673+ int ms_idx = c->nscopes - 1;
2674+ if (dbody >= 0) walk_scope(c, dbody, ms_idx, class_id);
2675+ }
2676+ return 1;
2677+}
2678+
25462679 static void walk_scope(Compiler *c, int id, int scope_idx, int class_id) {
25472680 if (id < 0 || id >= c->nt->count) return;
25482681 c->nscope[id] = scope_idx;
@@ -2607,6 +2740,10 @@
26072740 child = new_idx;
26082741 }
26092742 else if (ty && !strcmp(ty, "CallNode") && class_id >= 0) {
2743+ /* [lits].each { |v| define_method("m_#{v}") { body } } -- unroll into one
2744+ method per element. Handled wholesale; skip the generic recursion so
2745+ the inner define_method isn't also processed as a normal call. */
2746+ if (collect_dm_each_unroll(c, id, class_id)) return;
26102747 /* define_method(:literal_name) { ... } at class scope: register as method scope */
26112748 const char *dm_cn = nt_str(c->nt, id, "name");
26122749 int dm_recv = nt_ref(c->nt, id, "receiver");