From: Brendan Hansen Date: Fri, 13 Aug 2021 14:50:17 +0000 (-0500) Subject: polymorphic macros as overloads X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=c28ce7689b2bf94bfde8ca2395166f4b7b339502;p=onyx.git polymorphic macros as overloads --- diff --git a/bin/onyx b/bin/onyx index fe2bd2b2..755a2379 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index aa498532..e53efbfd 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -1412,6 +1412,7 @@ static inline ParamPassType type_get_param_pass(Type* type) { static inline AstFunction* get_function_from_node(AstNode* node) { if (node->kind == Ast_Kind_Function) return (AstFunction *) node; if (node->kind == Ast_Kind_Polymorphic_Proc) return ((AstPolyProc *) node)->base_func; + if (node->kind == Ast_Kind_Macro) return get_function_from_node((AstNode*) ((AstMacro *) node)->body); return NULL; } diff --git a/src/onyxchecker.c b/src/onyxchecker.c index 86425546..2704b088 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -953,6 +953,11 @@ static AstCall* binaryop_try_operator_overload(AstBinaryOp* binop) { b32 should_yield = 0; AstTyped* overload = find_matching_overload_by_arguments(operator_overloads[binop->operation], &args, &should_yield); + if (should_yield) { + bh_arr_free(args.values); + return (AstCall *) &node_that_signals_a_yield; + } + if (overload == NULL) { bh_arr_free(args.values); return NULL; @@ -1006,11 +1011,15 @@ CheckStatus check_binaryop(AstBinaryOp** pbinop) { (binop->left->type->kind != Type_Kind_Basic || binop->right->type->kind != Type_Kind_Basic)) { AstCall *implicit_call = binaryop_try_operator_overload(binop); - if (implicit_call != NULL) { - CHECK(call, &implicit_call); + if (implicit_call == (AstCall *) &node_that_signals_a_yield) + return Check_Yield_Macro; + if (implicit_call != NULL) { // NOTE: Not a binary op + implicit_call->next = binop->next; *pbinop = (AstBinaryOp *) implicit_call; + + CHECK(call, (AstCall **) pbinop); return Check_Success; } } @@ -1404,11 +1413,15 @@ CheckStatus check_subscript(AstSubscript** psub) { AstBinaryOp* binop = (AstBinaryOp *) sub; AstCall *implicit_call = binaryop_try_operator_overload(binop); - if (implicit_call != NULL) { - CHECK(call, &implicit_call); + if (implicit_call == (AstCall *) &node_that_signals_a_yield) + return Check_Yield_Macro; + if (implicit_call != NULL) { // NOTE: Not an array access + implicit_call->next = sub->next; *psub = (AstSubscript *) implicit_call; + + CHECK(call, (AstCall **) psub); return Check_Success; } } diff --git a/src/onyxutils.c b/src/onyxutils.c index 3ab680a5..6cac0d55 100644 --- a/src/onyxutils.c +++ b/src/onyxutils.c @@ -1083,7 +1083,7 @@ AstTyped* find_matching_overload_by_arguments(bh_arr(OverloadOption) overloads, AstFunction* overload = NULL; switch (node->kind) { case Ast_Kind_Function: overload = (AstFunction *) node; break; - case Ast_Kind_Macro: overload = (AstFunction *) ((AstMacro *) node)->body; break; + case Ast_Kind_Macro: overload = macro_resolve_header((AstMacro *) node, param_args, NULL); break; case Ast_Kind_Polymorphic_Proc: overload = polymorphic_proc_build_only_header((AstPolyProc *) node, PPLM_By_Arguments, param_args); break; } @@ -1263,7 +1263,8 @@ AstFunction* macro_resolve_header(AstMacro* macro, Arguments* args, OnyxToken* c return (AstFunction *) &node_that_signals_a_yield; } - onyx_report_error(callsite->pos, err_msg); + if (callsite) onyx_report_error(callsite->pos, err_msg); + return NULL; }