diff options
| author | Mel <mel@rnrd.eu> | 2026-05-28 01:17:32 +0200 |
|---|---|---|
| committer | Mel <mel@rnrd.eu> | 2026-05-28 01:17:32 +0200 |
| commit | a5b24e3eedc8bd48de99cfb90abb3bececa5d29f (patch) | |
| tree | 8268e61906adde451f010dfe73241841edbec453 /boot/tests/transpile/closure_capture.cskt | |
| parent | 351433f3f7dc3f1051a9875402502663fe8a0be8 (diff) | |
| download | catskill-a5b24e3eedc8bd48de99cfb90abb3bececa5d29f.tar.zst catskill-a5b24e3eedc8bd48de99cfb90abb3bececa5d29f.zip | |
Add tests for function value passing and capturing closure behavior
Signed-off-by: Mel <mel@rnrd.eu>
Diffstat (limited to 'boot/tests/transpile/closure_capture.cskt')
| -rw-r--r-- | boot/tests/transpile/closure_capture.cskt | 37 |
1 files changed, 37 insertions, 0 deletions
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" |
