Add lambda emulation
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
42
engine/tests/lambda_test.c
Normal file
42
engine/tests/lambda_test.c
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user