Can call normal functions too.

This commit is contained in:
Erik Svedäng 2018-06-18 14:10:35 +02:00
parent 28d2e2de42
commit 6ad3a1e2ac

View File

@ -2,9 +2,11 @@
#include <stdlib.h>
// (defn main []
// (let [x 123
// f (fn [y] (+ x y))]
// (f 1)))
// (let-do [x 123
// f (fn [y] (+ x y))
// g square]
// (f 1)
// (g 3))
// Environment for this particular lambda
typedef struct Env_main_0 {
@ -24,13 +26,18 @@ typedef struct Closure {
void (*delete)(void*);
} Closure;
typedef int (*Fn_CallClosure__Int)(void*, int);
typedef int (*Fn_CallClosure__Int_Int)(void*, int);
typedef int (*Fn_Int_Int)(int);
// The body of the lambda, but lifted to its own function. Takes the environment as a first argument.
int lifted_lambda_main_0(Env_main_0 *env) {
return 1 + env->x; // simplified for readability
}
int square(int x) {
return x * x;
}
int main() {
// let x
int x = 123;
@ -42,10 +49,19 @@ int main() {
.env = env_0,
.delete = (void*)delete_Env_main_0
};
//let g
Closure g = {
.callback = square,
.env = NULL,
.delete = NULL
};
// call f
Fn_CallClosure__Int casted_f = f.callback;
int _1 = casted_f(f.env, 1);
int _1 = f.env == NULL ? ((Fn_Int_Int)f.callback)(1) : ((Fn_CallClosure__Int_Int)f.callback)(f.env, 1);
// delete f
f.delete(f.env);
return _1;
if(f.delete) { f.delete(f.env); }
// call g
int _2 = g.env == NULL ? ((Fn_Int_Int)g.callback)(3) : ((Fn_CallClosure__Int_Int)g.callback)(f.env, 3);
// delete g
if(g.delete) { f.delete(f.env); }
printf("_1 = %d\n_2 = %d\n", _1, _2);
}