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/function_as_value.cskt | 43 +++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 boot/tests/transpile/function_as_value.cskt (limited to 'boot/tests/transpile/function_as_value.cskt') diff --git a/boot/tests/transpile/function_as_value.cskt b/boot/tests/transpile/function_as_value.cskt new file mode 100644 index 0000000..864eae2 --- /dev/null +++ b/boot/tests/transpile/function_as_value.cskt @@ -0,0 +1,43 @@ +any function can be passed around as a normal value. +this requires us to create a wrapper around the function, which +converts the thin (normal) calling convention of the function into +the fat calling convention of a function containing some state. +this wrapper is called a "thunk". + +<<< + +add = fun (a int, b int) int { return a + b } + +apply = fun (f fun (a int, b int) int, x int, y int) int { + return f(x, y) +} + +main = fun () { + var r int = apply(add, 5, 7) +} + +>>> + +#include "core.c" +struct __cat_type_0 { + void* state; + integer (*call)(void* state, integer, integer); +}; +integer add(integer a, integer b); +integer __cat_thunk_add(void* state, integer, integer); +integer apply(struct __cat_type_0 f, integer x, integer y); +void catskill_main(void); +integer add(integer a, integer b) { + return a + b; +} +integer __cat_thunk_add(void* state, integer a, integer b) { + (void)state; + return add(a, b); +} +integer apply(struct __cat_type_0 f, integer x, integer y) { + return f.call(f.state, x, y); +} +void catskill_main(void) { + integer r = apply((struct __cat_type_0){ .state = nil, .call = __cat_thunk_add }, 5, 7); +} +#include "runtime.c" -- cgit 1.4.1