From 7c18773011aa8eaaedb9bbde6884ee7e92585fea Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 15 Aug 2024 13:59:01 -0400 Subject: Bugfix for closures --- compile.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'compile.c') diff --git a/compile.c b/compile.c index ee4d2e5a..ae65fb4a 100644 --- a/compile.c +++ b/compile.c @@ -56,7 +56,7 @@ static bool promote(env_t *env, CORD *code, type_t *actual, type_t *needed) return true; if (needed->tag == ClosureType && actual->tag == FunctionType) { - *code = CORD_all("(closure_t){", *code, ", NULL}"); + *code = CORD_all("((closure_t){", *code, ", NULL})"); return true; } @@ -2078,7 +2078,7 @@ CORD compile(env_t *env, ast_t *ast) body = CORD_all(body, compile_statement(body_scope, FakeAST(Return)), "\n"); env->code->funcs = CORD_all(env->code->funcs, code, " {\n", body, "\n}\n"); - return CORD_all("(closure_t){", name, ", ", userdata, "}"); + return CORD_all("((closure_t){", name, ", ", userdata, "})"); } case MethodCall: { auto call = Match(ast, MethodCall); @@ -2147,7 +2147,7 @@ CORD compile(env_t *env, ast_t *ast) arg_t *arg_spec = new(arg_t, .name="by", .type=Type(ClosureType, .fn=fn_t)); comparison = compile_arguments(env, ast, arg_spec, call->args); } else { - comparison = CORD_all("(closure_t){.fn=generic_compare, .userdata=(void*)", compile_type_info(env, item_t), "}"); + comparison = CORD_all("((closure_t){.fn=generic_compare, .userdata=(void*)", compile_type_info(env, item_t), "})"); } return CORD_all("Array$", call->name, "(", self, ", ", comparison, ", ", padded_item_size, ")"); } else if (streq(call->name, "heapify")) { @@ -2168,8 +2168,12 @@ CORD compile(env_t *env, ast_t *ast) type_t *item_ptr = Type(PointerType, .pointed=item_t, .is_stack=true); type_t *fn_t = Type(FunctionType, .args=new(arg_t, .name="x", .type=item_ptr, .next=new(arg_t, .name="y", .type=item_ptr)), .ret=Type(IntType, .bits=32)); - ast_t *default_cmp = FakeAST(InlineCCode, .code=CORD_all("((closure_t){.fn=generic_compare, .userdata=(void*)", compile_type_info(env, item_t), "})"), .type=NewTypeAST(NULL, NULL, NULL, FunctionTypeAST)); - arg_t *arg_spec = new(arg_t, .name="item", .type=item_t, .next=new(arg_t, .name="by", .type=Type(ClosureType, .fn=fn_t), .default_val=default_cmp)); + ast_t *default_cmp = FakeAST(InlineCCode, + .code=CORD_all("((closure_t){.fn=generic_compare, .userdata=(void*)", + compile_type_info(env, item_t), "})"), + .type=NewTypeAST(NULL, NULL, NULL, FunctionTypeAST)); + arg_t *arg_spec = new(arg_t, .name="item", .type=item_t, + .next=new(arg_t, .name="by", .type=Type(ClosureType, .fn=fn_t), .default_val=default_cmp)); CORD arg_code = compile_arguments(env, ast, arg_spec, call->args); return CORD_all("Array$heap_push_value(", self, ", ", arg_code, ", ", padded_item_size, ")"); } else if (streq(call->name, "heap_pop")) { @@ -2177,7 +2181,10 @@ CORD compile(env_t *env, ast_t *ast) type_t *item_ptr = Type(PointerType, .pointed=item_t, .is_stack=true); type_t *fn_t = Type(FunctionType, .args=new(arg_t, .name="x", .type=item_ptr, .next=new(arg_t, .name="y", .type=item_ptr)), .ret=Type(IntType, .bits=32)); - ast_t *default_cmp = FakeAST(InlineCCode, .code=CORD_all("((closure_t){.fn=generic_compare, .userdata=(void*)", compile_type_info(env, item_t), "})"), .type=NewTypeAST(NULL, NULL, NULL, FunctionTypeAST)); + ast_t *default_cmp = FakeAST(InlineCCode, + .code=CORD_all("((closure_t){.fn=generic_compare, .userdata=(void*)", + compile_type_info(env, item_t), "})"), + .type=NewTypeAST(NULL, NULL, NULL, FunctionTypeAST)); arg_t *arg_spec = new(arg_t, .name="by", .type=Type(ClosureType, .fn=fn_t), .default_val=default_cmp); CORD arg_code = compile_arguments(env, ast, arg_spec, call->args); return CORD_all("Array$heap_pop_value(", self, ", ", arg_code, ", ", padded_item_size, ", ", compile_type(item_t), ")"); @@ -2186,8 +2193,12 @@ CORD compile(env_t *env, ast_t *ast) type_t *item_ptr = Type(PointerType, .pointed=item_t, .is_stack=true); type_t *fn_t = Type(FunctionType, .args=new(arg_t, .name="x", .type=item_ptr, .next=new(arg_t, .name="y", .type=item_ptr)), .ret=Type(IntType, .bits=32)); - ast_t *default_cmp = FakeAST(InlineCCode, .code=CORD_all("((closure_t){.fn=generic_compare, .userdata=(void*)", compile_type_info(env, item_t), "})"), .type=NewTypeAST(NULL, NULL, NULL, FunctionTypeAST)); - arg_t *arg_spec = new(arg_t, .name="target", .type=item_t, .next=new(arg_t, .name="by", .type=Type(ClosureType, .fn=fn_t), .default_val=default_cmp)); + ast_t *default_cmp = FakeAST(InlineCCode, + .code=CORD_all("((closure_t){.fn=generic_compare, .userdata=(void*)", + compile_type_info(env, item_t), "})"), + .type=NewTypeAST(NULL, NULL, NULL, FunctionTypeAST)); + arg_t *arg_spec = new(arg_t, .name="target", .type=item_t, + .next=new(arg_t, .name="by", .type=Type(ClosureType, .fn=fn_t), .default_val=default_cmp)); CORD arg_code = compile_arguments(env, ast, arg_spec, call->args); return CORD_all("Array$binary_search_value(", self, ", ", arg_code, ")"); } else if (streq(call->name, "clear")) { -- cgit v1.2.3