添加struct里使用lambda测试

This commit is contained in:
2025-06-20 17:26:36 +08:00
parent 362dc08ba5
commit 3e3c62941d
3 changed files with 38 additions and 14 deletions

View File

@@ -66,7 +66,7 @@ def calc_lambda_text(text:str):
def create_lambda(text:str):
global LAMBDA_INDEX
text=text.replace("lambda(","")[:-1] # 去掉lambda和首末括号
fun_name=f"lambda_{LAMBDA_INDEX}"
fun_name=f"__lambda_{LAMBDA_INDEX}"
LAMBDA_INDEX+=1
return_type=text[:text.find("(")]
# 有","则至少有两个参数否则可能有一个参数,可能没有

22
make.py
View File

@@ -132,7 +132,8 @@ def build_depend(src:list):
if(ret):
exit()
else:
print(f"{i} 没有更新源文件")
# print(f"{i} 没有更新源文件")
pass
# 生成中间文件
def build_object(src:list):
@@ -150,13 +151,15 @@ def build_object(src:list):
if(check_rebuild(dst,read_depend_files(cd))):
cmd=f"{CC} -c {i} -o {dst} {flags}"
else:
print(f"{i} 没有更新依赖关系")
# print(f"{i} 没有更新依赖关系")
pass
elif(file_type in ['s','S','asm','ASM']):
if(check_rebuild(dst,[i])):
# cmd=f"{AS} -c {i} -o {dst} {flags}"
pass
else:
print(f"{i} 没有更新依赖关系")
# print(f"{i} 没有更新依赖关系")
pass
if(len(cmd)>0):
print(cmd)
ret=os.system(cmd)
@@ -179,29 +182,26 @@ def build_target(src:list):
if(ret):
exit()
else:
print(f"{dst} 没有更新的链接文件")
# print(f"{dst} 没有更新的链接文件")
pass
def main():
global CSRC
global ASRC
if(not os.path.exists(BUILD_DIR)):
os.makedirs(BUILD_DIR)
CSRC+=find_type('soft',['c','C'])
CSRC+=find_type('cpu',['c','C'])
CSRC=search_lambda(CSRC)
# ASRC+=find_type('./',['s','S','asm','ASM'])
if(not os.path.exists(BUILD_DIR)):
os.makedirs(BUILD_DIR)
print("生成依赖关系")
build_depend(CSRC)
print("生成对象文件")
build_object(CSRC)
# build_object(ASRC)
print("生成目标文件")
build_target(CSRC+ASRC)
# os.system(f"{HEX} {BUILD_DIR}/{TARGET} {BUILD_DIR}/{TARGET}.hex")
# os.system(f"{BIN} {BUILD_DIR}/{TARGET} {BUILD_DIR}/{TARGET}.bin")
if __name__ == "__main__":
main()

View File

@@ -7,9 +7,17 @@
#include "main.h"
#include "lambda.h"
lambda_use
typedef struct test_struct{
int a;
int b;
int (*sub)(int a, int b);
int (*add)(int a, int b);
int (*sum)(struct test_struct* self);
} test_struct;
lambda_use
@@ -21,7 +29,19 @@ void run_callback(int (*fun)(int a, int b)) {
static test_struct test_struct_var = {
.a = 1,
.b = 2,
.sub = lambda(int(int a, int b) {
return a - b;
}),
.add = lambda(int(int a, int b) {
return a + b;
}),
.sum = lambda(int(struct test_struct* self) {
return self->a + self->b;
})
};
@@ -65,6 +85,10 @@ int thread_fun(void* t)
return a * b * 10;
}));
printf("test_struct_var.sub(1,2) = %d\n", test_struct_var.sub(1, 2));
printf("test_struct_var.add(1,2) = %d\n", test_struct_var.add(1, 2));
printf("test_struct_var.sum(&test_struct_var) = %d\n", test_struct_var.sum(&test_struct_var));
return 0;
}