Commit 5125b0cf
5125b0cf6795Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 5 files · ~5 modified
Description
Proc#curry: deferred-application argument accumulator
proc.curry returns an sp_Curry accumulator over the target proc; each [arg]
(or .call(arg)) returns a fresh accumulator with the argument appended, and
the fully-applied value is realized at the point of use (sp_curry_to_int calls
the target with the collected mrb_int arguments). A partial curry thus behaves
as a deferred call rather than auto-invoking at arity, which keeps the static
type uniform (TY_CURRY) across the [] chain.
- runtime: sp_Curry { target, nargs, args[16] }, sp_curry_new/apply/to_int, with a GC scan hook marking the target proc.
- types: TY_CURRY (sp_Curry *), wired through c-type/default/needs_root/ is_scalar_ret.
- analyze: proc.curry -> TY_CURRY; curry[]/call -> TY_CURRY.
- codegen: sp_curry_new / sp_curry_apply emission; puts of a curry realizes the int result.
Curry works for non-colliding programs; test/i919 additionally reuses a name as
both a lambda parameter and a curry local (c), which the shared-slot block
parameter scoping types as a single conflicting variable -- that collision is
the separate, deferred per-block-rename issue, not a curry gap.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 5125b0cf6795
- Parent
- ec4682375e85
lib/sp_runtime.hmodifiedView file
c->inner = inner; return sp_proc_new_meta((void *)sp_proc_compose_fn, c, sp_proc_compose_scan, 1, TRUE, 1, NULL, NULL);}/* Proc#curry: an immutable argument accumulator over an sp_Proc target. `proc.curry` makes an empty accumulator; each `[arg]` returns a fresh accumulator with `arg` appended; the fully-applied value is realized by calling the target with the collected (mrb_int) arguments. Spinel defers the call to the point of use (sp_curry_to_int), so a partial curry behaves as a deferred call rather than auto-invoking at arity. */typedef struct { sp_Proc *target; mrb_int nargs; mrb_int args[16]; } sp_Curry;static void sp_curry_scan(void *p) { sp_Curry *c = (sp_Curry *)p; if (c->target) sp_gc_mark(c->target); }static sp_Curry *sp_curry_new(sp_Proc *p) { sp_Curry *c = (sp_Curry *)sp_gc_alloc(sizeof(sp_Curry), NULL, sp_curry_scan); c->target = p; c->nargs = 0; return c;}static sp_Curry *sp_curry_apply(sp_Curry *c, mrb_int arg) { sp_Curry *n = (sp_Curry *)sp_gc_alloc(sizeof(sp_Curry), NULL, sp_curry_scan); *n = *c; if (n->nargs < 16) n->args[n->nargs++] = arg; return n;}static mrb_int sp_curry_to_int(sp_Curry *c) { if (!c || !c->target) return 0; return sp_proc_call(c->target, c->args);}/* Hash#to_proc cap-scan: the proc's `cap` field IS the source hash (a single GC pointer), so marking it keeps the hash alive for the proc's lifetime. The per-variant lookup fn is emitted by codegensrc/analyze.cmodifiedView file
Loading diff…
src/codegen.cmodifiedView file
Loading diff…
src/types.cmodifiedView file
Loading diff…
src/types.hmodifiedView file
Loading diff…