Commit 9daca9d7

9daca9d7db76Merged

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

Description

feat(codegen): multi-arg lambda support up to arity 4 (#400)

Fixes #400.

->(a, b) { a + b } previously emitted broken code: only the first required param was registered as formal; 2nd+ were treated as free variables to capture, the call site sp_lam_call(f, a) dropped extra args, and the body fn signature was 1-arg.

Three coordinated changes:

  1. lib/sp_runtime.h: add sp_lam_call2 / sp_lam_call3 / sp_lam_call4 (and matching sp_fn{2,3,4}_t typedefs) that re-cast the stored 1-arg fn ptr to the right arity at the call site.

  2. spinel_codegen.rb scan_lambda_free_vars + compile_lambda_expr: collect every required param (was reqs[0] only). The lambda fn signature gets one sp_Val *_argN per param plus aliasing lv_<name> decls inside the body.

  3. compile_lambda_call_expr dispatches to sp_lam_callN based on arg count; arity 1 still uses the original sp_lam_call.

compile_lambda_body_expr also learns * / - / / / % / <>= arms so multi-arg arithmetic bodies (a * b * c, a > b) lower correctly. Without these, those CallNodes fell through to the catch-all &sp_lam_nil_val and the lambda returned nil for any non-add expression.

The fn ptr stored in sp_Val.u.proc.fn keeps its 1-arg sp_fn_t type; the multi-arg path casts on both sides (sp_lam_proc receive + sp_lam_callN dispatch). Arities beyond 4 keep the legacy 1-arg path (caller is on its own).

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

Details

Commit
9daca9d7db76
Parent
9aa3b3bbdd3b

lib/sp_runtime.hmodifiedView file

@@ -1309,6 +1309,16 @@
13091309 static sp_Val *sp_lam_bool(mrb_bool b) { sp_Val *v = (sp_Val *)sp_lam_alloc(sizeof(sp_Val)); v->tag = SP_BOOL2; v->u.bval = b; return v; }
13101310 static sp_Val sp_lam_nil_val = { .tag = SP_NIL2 };
13111311 static sp_Val *sp_lam_call(sp_Val *f, sp_Val *arg) { return f->u.proc.fn(f, arg); }
1312+/* Multi-arg lambda dispatch (issue #400). The fn pointer is stored
1313+ typed as `sp_fn_t` (1-arg) so re-cast at the call site to the
1314+ right arity. The generated lambda body is declared with the
1315+ matching arity so the actual ABI matches. */
1316+typedef sp_Val *(*sp_fn2_t)(sp_Val *self, sp_Val *a, sp_Val *b);
1317+typedef sp_Val *(*sp_fn3_t)(sp_Val *self, sp_Val *a, sp_Val *b, sp_Val *c);
1318+typedef sp_Val *(*sp_fn4_t)(sp_Val *self, sp_Val *a, sp_Val *b, sp_Val *c, sp_Val *d);
1319+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); }
1320+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); }
1321+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); }
13121322 static mrb_int sp_lam_to_int(sp_Val *v) { return v->u.ival; }
13131323
13141324 /* ---- Fiber runtime (ucontext) ---- */