Commit c99456f6

c99456f66821Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 5 files · +2 added · ~3 modified

Description

runtime,codegen: Proc#<< / #>> composition

f << g and f >> g emitted raw C shift operators on the proc/lambda pointer, failing C compile ("invalid operands to binary <<"). Add composition helpers for both proc representations: sp_lam_compose for the sp_Val * lambda arena values and sp_proc_compose for the sp_Proc * block-procs. Each captures the two operands and threads the call's argument inner-then-outer. analyze types f << g / f >> g as the receiver's proc/lambda type so the result is callable.

<< keeps recv as outer; >> swaps so (f >> g).(x) == g(f(x)).

Fixes #920

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
c99456f66821
Parent
39a30d368438

lib/sp_runtime.hmodifiedView file

@@ -3471,6 +3471,28 @@
34713471 static mrb_int sp_proc_call(sp_Proc *p, mrb_int *args) { if (!p || !p->fn) return 0; if (!args) { mrb_int noargs[16] = {0}; return ((mrb_int (*)(void *, mrb_int *))p->fn)(p->cap, noargs); } return ((mrb_int (*)(void *, mrb_int *))p->fn)(p->cap, args); }
34723472 static sp_PolyArray *sp_proc_parameters(sp_Proc *p) { sp_PolyArray *r = sp_PolyArray_new(); if (!p || p->param_count <= 0 || !p->param_kinds) return r; SP_GC_ROOT(r); for (mrb_int i = 0; i < p->param_count; i++) { sp_PolyArray *pair = sp_PolyArray_new(); sp_PolyArray_push(pair, sp_box_sym(p->param_kinds[i])); if (p->param_names && p->param_names[i] >= 0) sp_PolyArray_push(pair, sp_box_sym(p->param_names[i])); sp_PolyArray_push(r, sp_box_poly_array(pair)); } return r; }
34733473
3474+/* Proc#<< / Proc#>> composition. The composed proc captures the two
3475+ operands and, on call, threads its single argument through inner
3476+ then outer: `(f << g).call(x)` == f(g(x)). For `>>` the codegen
3477+ swaps the operands so `(f >> g).call(x)` == g(f(x)). */
3478+typedef struct { sp_Proc *outer; sp_Proc *inner; } sp_ProcCompose;
3479+static void sp_proc_compose_scan(void *p) { sp_ProcCompose *c = (sp_ProcCompose *)p; if (c->outer) sp_gc_mark(c->outer); if (c->inner) sp_gc_mark(c->inner); }
3480+static mrb_int sp_proc_compose_fn(void *cap, mrb_int *args) {
3481+ sp_ProcCompose *c = (sp_ProcCompose *)cap;
3482+ mrb_int inner_args[16] = {0};
3483+ if (args) inner_args[0] = args[0];
3484+ mrb_int mid = sp_proc_call(c->inner, inner_args);
3485+ mrb_int outer_args[16] = {0};
3486+ outer_args[0] = mid;
3487+ return sp_proc_call(c->outer, outer_args);
3488+}
3489+static sp_Proc *sp_proc_compose(sp_Proc *outer, sp_Proc *inner) {
3490+ sp_ProcCompose *c = (sp_ProcCompose *)sp_gc_alloc(sizeof(sp_ProcCompose), NULL, sp_proc_compose_scan);
3491+ c->outer = outer;
3492+ c->inner = inner;
3493+ return sp_proc_new_meta((void *)sp_proc_compose_fn, c, sp_proc_compose_scan, 1, TRUE, 1, NULL, NULL);
3494+}
3495+
34743496 /* ---- StringIO runtime ---- */
34753497 typedef struct { char *buf; int64_t len; int64_t cap; int64_t pos; int64_t lineno; int closed; } sp_StringIO;
34763498 static void sio_grow(sp_StringIO *sio, int64_t need) { int64_t req = sio->pos + need; if (req <= sio->cap) return; int64_t nc = sio->cap ? sio->cap : 64; while (nc < req) nc *= 2; sio->buf = (char *)realloc(sio->buf, nc + 1); sio->cap = nc; }
@@ -3527,6 +3549,11 @@
35273549 static sp_Val *sp_lam_call2(sp_Val *f, sp_Val *a, sp_Val *b) { return ((sp_fn2_t)(uintptr_t)f->u.proc.fn)(f, a, b); }
35283550 static sp_Val *sp_lam_call3(sp_Val *f, sp_Val *a, sp_Val *b, sp_Val *c) { return ((sp_fn3_t)(uintptr_t)f->u.proc.fn)(f, a, b, c); }
35293551 static sp_Val *sp_lam_call4(sp_Val *f, sp_Val *a, sp_Val *b, sp_Val *c, sp_Val *d) { return ((sp_fn4_t)(uintptr_t)f->u.proc.fn)(f, a, b, c, d); }
3552+/* lambda#<< / #>> composition over the sp_Val * representation.
3553+ captures[0] = outer, captures[1] = inner; `(f << g).(x)` == f(g(x)).
3554+ The codegen swaps operands for `>>`. */
3555+static sp_Val *sp_lam_compose_fn(sp_Val *self, sp_Val *arg) { return sp_lam_call(self->captures[0], sp_lam_call(self->captures[1], arg)); }
3556+static sp_Val *sp_lam_compose(sp_Val *outer, sp_Val *inner) { sp_Val *v = sp_lam_proc(sp_lam_compose_fn, 2); v->captures[0] = outer; v->captures[1] = inner; return v; }
35303557 static mrb_int sp_lam_to_int(sp_Val *v) { return v->u.ival; }
35313558
35323559 /* ---- Fiber runtime (ucontext) ---- */