From a5b24e3eedc8bd48de99cfb90abb3bececa5d29f Mon Sep 17 00:00:00 2001 From: Mel Date: Thu, 28 May 2026 01:17:32 +0200 Subject: Add tests for function value passing and capturing closure behavior Signed-off-by: Mel --- boot/tests/transpile/closure_capture.cskt | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 boot/tests/transpile/closure_capture.cskt (limited to 'boot/tests/transpile/closure_capture.cskt') diff --git a/boot/tests/transpile/closure_capture.cskt b/boot/tests/transpile/closure_capture.cskt new file mode 100644 index 0000000..305c58b --- /dev/null +++ b/boot/tests/transpile/closure_capture.cskt @@ -0,0 +1,37 @@ +a capturing closure transpiles to a fat-pair value (state+function) +and the captured local is accessed via the state. +lambda should also cast the generic pointer to the known state type. + +<<< + +main = fun () { + var bump int = 10 + var f fun (i int) int = fun (i int) int { + return i + bump + } + var r int = f(5) +} + +>>> + +#include "core.c" +struct __cat_type_0 { + void* state; + integer (*call)(void* state, integer); +}; +struct __cat_type_1; +struct __cat_type_1 { + integer* bump; +}; +void catskill_main(void); +integer __cat_lambda_0(void* state_void, integer i); +void catskill_main(void) { + integer bump = 10; + struct __cat_type_0 f = (struct __cat_type_0){ .state = &(struct __cat_type_1){ .bump = &bump }, .call = __cat_lambda_0 }; + integer r = f.call(f.state, 5); +} +integer __cat_lambda_0(void* state_void, integer i) { + struct __cat_type_1* state = state_void; + return i + (*state->bump); +} +#include "runtime.c" -- cgit 1.4.1