typedef unsigned long size_t;
int printf(const char *fmt, ...);
typedef long (*func)();
typedef struct lambda {
func function;
void *captures[32];
} lambda;
#define CAPTURE(...) __VA_ARGS__
#define LAMBDA(name, captures, func...) func \
lambda name = { (void *)&lambda_function, captures }
#define lambda_ctx void **captures
#define ctx(n, type) *(type *)captures[n]
#define lambda_call(lambda, args...) \
lambda.function(lambda.captures, args)
int foo() {
int x = 11, y = 100;
char *foo = "Hello World";
LAMBDA( q,
CAPTURE({&x, &y, &foo}),
int lambda_function(lambda_ctx, int x) {
int a = 100;
int *p = &ctx(1, int);
char *str = ctx(2, char *);
printf("%s\n", str);
return a + ctx(0, int) + x;
}
);
return (int)lambda_call(q, 20);
}
Comments