From: Brendan Hansen Date: Mon, 27 Jul 2020 20:43:05 +0000 (-0500) Subject: Bug fixes with first class functions X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=2713dd70fc92787d045d7002aa11f761e42319d6;p=onyx.git Bug fixes with first class functions --- diff --git a/onyx b/onyx index c90b1111..5691a378 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/fcf.onyx b/progs/fcf.onyx index f1303890..f043423f 100644 --- a/progs/fcf.onyx +++ b/progs/fcf.onyx @@ -3,7 +3,7 @@ use "progs/intrinsics" use package printing -call_me :: proc (f: proc (i32), val: i32) { +call_me :: proc (f: proc (i32) -> i32, val: i32) { f(val); } @@ -25,8 +25,15 @@ execute :: proc (use dc: ^DeferredCall) -> i32 { return func(left, right); } +echo :: proc (i: i32) -> i32 { + print(i); + return i; +} + proc #export "main" { - call_me(print_i32, 10); + call_me(echo, 10); + + print(add as i32); funcs[0] = add; funcs[1] = sub; diff --git a/src/onyxchecker.c b/src/onyxchecker.c index 5d5c7c10..d73db301 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -173,6 +173,13 @@ CHECK(call, AstCall* call) { while (actual_param != NULL) { if (check_expression((AstTyped **) &actual_param)) return 1; + if (actual_param->value->kind == Ast_Kind_Overloaded_Function) { + onyx_message_add(Msg_Type_Literal, + actual_param->token->pos, + "cannot pass overloaded functions as parameters."); + return 1; + } + // NOTE: Splat structures into multiple arguments if (actual_param->type->kind == Type_Kind_Struct) { if (!type_struct_is_simple(actual_param->type)) { diff --git a/src/onyxtypes.c b/src/onyxtypes.c index 71cab661..45b18079 100644 --- a/src/onyxtypes.c +++ b/src/onyxtypes.c @@ -154,6 +154,8 @@ b32 types_are_compatible(Type* t1, Type* t2) { if (t2->kind != Type_Kind_Function) return 0; if (t1->Function.param_count != t2->Function.param_count) return 0; + if (!types_are_compatible(t1->Function.return_type, t2->Function.return_type)) return 0; + fori (i, 0, t1->Function.param_count - 1) { if (!types_are_compatible(t1->Function.params[i], t2->Function.params[i])) return 0; } diff --git a/src/onyxwasm.c b/src/onyxwasm.c index 966b9ca8..56afd045 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -1067,6 +1067,14 @@ COMPILE_FUNC(cast, AstUnaryOp* cast) { return; } + if (to->kind == Type_Kind_Function) { + onyx_message_add(Msg_Type_Literal, + cast->token->pos, + "cannot cast to a function"); + WI(WI_DROP); + return; + } + if (to->kind == Type_Kind_Basic && to->Basic.kind == Basic_Kind_Void) { WI(WI_DROP); return;