about summary refs log tree commit diff
path: root/boot/tests/transpile/closure_nested.cskt
blob: ac913b7545ea15ffc886d77918786f1c421726b6 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
support for multi-level closures, and capturing through them.
when a closure captures a local which is multiple function levels
away from it, the capture should cascade to every single closure
between the capturer and the capturee.
when the closure state is created, the captured local needs
to be accessed via the state structure of the parent closure,
and not via a simple reference as usual.

<<<

main = fun () {
    var a int = 1
    var outer fun (x int) int = fun (x int) int {
        var b int = 10
        var inner fun (y int) int = fun (y int) int {
            return a + b + x + y
        }
        return inner(100)
    }
    var r int = outer(1000)
}

>>>

#include "core.c"
struct __cat_type_0 {
    void* state;
    integer (*call)(void* state, integer);
};
struct __cat_type_1;
struct __cat_type_2;
struct __cat_type_1 {
    integer* a;
    integer* b;
    integer* x;
};
struct __cat_type_2 {
    integer* a;
};
void catskill_main(void);
integer __cat_lambda_0(void* state_void, integer x);
integer __cat_lambda_1(void* state_void, integer y);
void catskill_main(void) {
    integer a = 1;
    struct __cat_type_0 outer = (struct __cat_type_0){ .state = &(struct __cat_type_2){ .a = &a }, .call = __cat_lambda_0 };
    integer r = outer.call(outer.state, 1000);
}
integer __cat_lambda_0(void* state_void, integer x) {
    struct __cat_type_2* state = state_void;
    integer b = 10;
    struct __cat_type_0 inner = (struct __cat_type_0){ .state = &(struct __cat_type_1){ .a = state->a, .b = &b, .x = &x }, .call = __cat_lambda_1 };
    return inner.call(inner.state, 100);
}
integer __cat_lambda_1(void* state_void, integer y) {
    struct __cat_type_1* state = state_void;
    return (*state->a) + (*state->b) + (*state->x) + y;
}
#include "runtime.c"