Add lambda emulation

This commit is contained in:
2024-01-07 09:57:37 +01:00
parent 16de6b0cc8
commit 5dbc5ba15e
5 changed files with 110 additions and 0 deletions

View File

@@ -14,6 +14,9 @@ target_link_libraries(ecs_defer_test LINK_PRIVATE Breeze)
add_executable(heap_test heap_test.c)
target_link_libraries(heap_test LINK_PRIVATE Breeze)
add_executable(lambda_test lambda_test.c)
target_link_libraries(lambda_test LINK_PRIVATE Breeze)
add_executable(array_test array_test.c)
target_link_libraries(array_test LINK_PRIVATE Breeze)

View File

@@ -0,0 +1,42 @@
#include <stdio.h>
#include <breeze.h>
int BZ_LAMBDA_DECL(lambdaAdd, int a) {
BZ_LAMBDA_UNPACK_START();
BZ_LAMBDA_UNPACK(int, x); // still have access to x
return a + x;
}
void BZ_LAMBDA_NOP_DECL(printArgs) {
BZ_LAMBDA_UNPACK_START();
BZ_LAMBDA_UNPACK(int, argc);
BZ_LAMBDA_UNPACK(char **, argv);
printf("Program arguments:\n");
for (int i = 0; i < argc; i++) {
printf("\t%s\n", argv[i]);
}
}
BzLambda foo(int x) {
BzLambda lam = BZ_LAMBDA(lambdaAdd);
BZ_LAMBDA_BEGIN_CAPTURE(&lam);
BZ_LAMBDA_CAPTURE(&lam, x);
// x goes out of scope
return lam;
}
int main(int argc, char **argv) {
BzLambda lambda1 = foo(8);
printf("%d\n", BZ_LAMBDA_CALL(&lambda1, int, 1));
BzLambda printLam = BZ_LAMBDA(printArgs);
BZ_LAMBDA_BEGIN_CAPTURE(&printLam);
BZ_LAMBDA_CAPTURE(&printLam, argc);
BZ_LAMBDA_CAPTURE(&printLam, argv);
BZ_LAMBDA_NOP_CALL(&printLam, void);
return 0;
}