about summary refs log tree commit diff
path: root/boot/tests/transpile/closure_capture.cskt
blob: 305c58bf5a497fccd79808b7590eb1c3625919da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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"