about summary refs log tree commit diff
path: root/boot/tests/transpile/closure_capture.cskt
diff options
context:
space:
mode:
Diffstat (limited to 'boot/tests/transpile/closure_capture.cskt')
-rw-r--r--boot/tests/transpile/closure_capture.cskt37
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"