From a1a0938d8efb88dad6484df9834265a6ce87e2fc Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Wed, 8 Feb 2023 11:39:00 -0600 Subject: [PATCH] renamed `#precedence` to `#order` --- README.md | 4 ++-- compiler/include/astnodes.h | 8 +++---- compiler/src/parser.c | 20 ++++++++-------- compiler/src/symres.c | 4 ++-- compiler/src/utils.c | 12 +++++----- core/builtin.onyx | 4 ++-- core/container/iter.onyx | 4 ++-- core/hash/hash.onyx | 2 +- core/io/writer.onyx | 4 ++-- core/misc/method_ops.onyx | 40 +++++++++++++++---------------- core/string/string.onyx | 2 +- examples/14_overloaded_procs.onyx | 13 +++++----- tests/overload_precedence.onyx | 12 +++++----- 13 files changed, 65 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index ef1fa4bf..bb117e08 100644 --- a/README.md +++ b/README.md @@ -5,5 +5,5 @@ A simple, yet powerful language for WebAssembly. -[Try Online](https://onyxlang.io/playground) | -[Read the Wiki](https://github.com/brendanfh/onyx/wiki) +[Try Online](https://onyxlang.io/playground/) | +[Read the Wiki](https://github.com/onyx-lang/onyx/wiki) diff --git a/compiler/include/astnodes.h b/compiler/include/astnodes.h index b9b06986..69b04510 100644 --- a/compiler/include/astnodes.h +++ b/compiler/include/astnodes.h @@ -1077,7 +1077,7 @@ struct OverloadOption { // This is u64 because padding will make it that anyway. // Consider: would there be any practical benefit to having the precedence setting // be a compile-time known value? as opposed to a hardcoded value? - u64 precedence; + u64 order; AstTyped* option; }; @@ -1312,7 +1312,7 @@ struct AstDirectiveAddOverload { AstNode *overloaded_function; // See note in OverloadOption. This could be refactored into an OverloadOption? - u64 precedence; + u64 order; AstTyped *overload; }; @@ -1321,7 +1321,7 @@ struct AstDirectiveOperator { BinaryOp operator; - u64 precedence; + u64 order; AstTyped *overload; }; @@ -1814,7 +1814,7 @@ typedef struct OverloadReturnTypeCheck { OnyxToken *group; } OverloadReturnTypeCheck; -void add_overload_option(bh_arr(OverloadOption)* poverloads, u64 precedence, AstTyped* overload); +void add_overload_option(bh_arr(OverloadOption)* poverloads, u64 order, AstTyped* overload); AstTyped* find_matching_overload_by_arguments(bh_arr(OverloadOption) overloads, Arguments* args); AstTyped* find_matching_overload_by_type(bh_arr(OverloadOption) overloads, Type* type); void report_unable_to_match_overload(AstCall* call, bh_arr(OverloadOption) overloads); diff --git a/compiler/src/parser.c b/compiler/src/parser.c index 9c8ff9cd..7a47f718 100644 --- a/compiler/src/parser.c +++ b/compiler/src/parser.c @@ -2446,19 +2446,19 @@ static AstOverloadedFunction* parse_overloaded_function(OnyxParser* parser, Onyx expect_token(parser, '{'); - u64 precedence = 0; + u64 order = 0; while (!consume_token_if_next(parser, '}')) { if (parser->hit_unexpected_token) return ofunc; - if (parse_possible_directive(parser, "precedence")) { + if (parse_possible_directive(parser, "order")) { AstNumLit* pre = parse_int_literal(parser); if (parser->hit_unexpected_token) return ofunc; - precedence = bh_max(pre->value.l, 0); + order = bh_max(pre->value.l, 0); } AstTyped* option = parse_expression(parser, 0); - add_overload_option(&ofunc->overloads, precedence++, option); + add_overload_option(&ofunc->overloads, order++, option); if (parser->curr->type != '}') expect_token(parser, ','); @@ -3283,14 +3283,14 @@ static void parse_top_level_statement(OnyxParser* parser) { } operator_determined: - if (parse_possible_directive(parser, "precedence")) { + if (parse_possible_directive(parser, "order")) { AstNumLit* pre = parse_int_literal(parser); if (parser->hit_unexpected_token) return; - operator->precedence = bh_max(pre->value.l, 0); + operator->order = bh_max(pre->value.l, 0); } else { - operator->precedence = parser->overload_count++; + operator->order = parser->overload_count++; } operator->overload = parse_expression(parser, 0); @@ -3302,13 +3302,13 @@ static void parse_top_level_statement(OnyxParser* parser) { AstDirectiveAddOverload *add_overload = make_node(AstDirectiveAddOverload, Ast_Kind_Directive_Add_Overload); add_overload->token = dir_token; - if (parse_possible_directive(parser, "precedence")) { + if (parse_possible_directive(parser, "order")) { AstNumLit* pre = parse_int_literal(parser); if (parser->hit_unexpected_token) return; - add_overload->precedence = bh_max(pre->value.l, 0); + add_overload->order = bh_max(pre->value.l, 0); } else { - add_overload->precedence = parser->overload_count++; + add_overload->order = parser->overload_count++; } parser->parse_calls = 0; diff --git a/compiler/src/symres.c b/compiler/src/symres.c index 1ed93a93..7516ff4f 100644 --- a/compiler/src/symres.c +++ b/compiler/src/symres.c @@ -1410,7 +1410,7 @@ static SymresStatus symres_process_directive(AstNode* directive) { } SYMRES(expression, (AstTyped **) &add_overload->overload); - add_overload_option(&ofunc->overloads, add_overload->precedence, add_overload->overload); + add_overload_option(&ofunc->overloads, add_overload->order, add_overload->overload); break; } @@ -1430,7 +1430,7 @@ static SymresStatus symres_process_directive(AstNode* directive) { return Symres_Error; } - add_overload_option(&operator_overloads[operator->operator], operator->precedence, operator->overload); + add_overload_option(&operator_overloads[operator->operator], operator->order, operator->overload); break; } diff --git a/compiler/src/utils.c b/compiler/src/utils.c index fe06c8db..54edea36 100644 --- a/compiler/src/utils.c +++ b/compiler/src/utils.c @@ -379,12 +379,12 @@ void scope_clear(Scope* scope) { // * Resolving an overload from a TypeFunction (so an overloaded procedure can be passed as a parameter) // -void add_overload_option(bh_arr(OverloadOption)* poverloads, u64 precedence, AstTyped* overload) { +void add_overload_option(bh_arr(OverloadOption)* poverloads, u64 order, AstTyped* overload) { bh_arr(OverloadOption) overloads = *poverloads; i32 index = -1; fori (i, 0, bh_arr_length(overloads)) { - if (overloads[i].precedence > precedence) { + if (overloads[i].order > order) { index = i; break; } @@ -392,14 +392,14 @@ void add_overload_option(bh_arr(OverloadOption)* poverloads, u64 precedence, Ast if (index < 0) { bh_arr_push(overloads, ((OverloadOption) { - .precedence = precedence, - .option = overload, + .order = order, + .option = overload, })); } else { bh_arr_insertn(overloads, index, 1); - overloads[index].precedence = precedence; - overloads[index].option = overload; + overloads[index].order = order; + overloads[index].option = overload; } *poverloads = overloads; diff --git a/core/builtin.onyx b/core/builtin.onyx index d5a9d86e..883a29db 100644 --- a/core/builtin.onyx +++ b/core/builtin.onyx @@ -268,7 +268,7 @@ cfree :: (ptr: rawptr) do raw_free(context.allocator, ptr); // // This is the fallback option for make. It simply allocates a zero-intialized // element of type T. - #precedence 1000 (_: ^$T, allocator := context.allocator) -> ^T { + #order 1000 (_: ^$T, allocator := context.allocator) -> ^T { memory :: package core.memory res := cast(^T) raw_alloc(allocator, sizeof T); @@ -278,7 +278,7 @@ cfree :: (ptr: rawptr) do raw_free(context.allocator, ptr); } delete :: #match { - #precedence 1000 macro (x: ^$T, allocator := context.allocator) { + #order 1000 macro (x: ^$T, allocator := context.allocator) { if x != null do raw_free(allocator, x); } } diff --git a/core/container/iter.onyx b/core/container/iter.onyx index 6f129680..b481b77b 100644 --- a/core/container/iter.onyx +++ b/core/container/iter.onyx @@ -65,7 +65,7 @@ close :: (it: Iterator) { // type has the necessary methods. // -#overload #precedence 10000 +#overload #order 10000 as_iter :: (x: ^$T/ImplicitIterator) => { x->iter_open(); return generator_no_copy(x, T.iter_next, T.iter_close); @@ -86,7 +86,7 @@ ImplicitIterator :: interface (t: $T) { } -#overload #precedence 10000 +#overload #order 10000 as_iter :: macro (x: $T/HasAsIter) => x->as_iter(); #local diff --git a/core/hash/hash.onyx b/core/hash/hash.onyx index 1208f6dc..2a438c41 100644 --- a/core/hash/hash.onyx +++ b/core/hash/hash.onyx @@ -40,7 +40,7 @@ hash :: #match -> u32 { (key: type_expr) -> u32 { return to_u32(cast(u32) key); }, (key: bool) -> u32 { return 1 if key else 0; }, - #precedence 10000 + #order 10000 macro (key: $T/HasHashMethod) => key->hash() } diff --git a/core/io/writer.onyx b/core/io/writer.onyx index 222b78bb..1bc91798 100644 --- a/core/io/writer.onyx +++ b/core/io/writer.onyx @@ -176,8 +176,8 @@ write :: #match { write_format, - // Catch all for any type. Has a high precedence so you can easily override it. - #precedence 1000 macro (w: ^Writer, a: $T) { + // Catch all for any type. Has a high order so you can easily override it. + #order 1000 macro (w: ^Writer, a: $T) { write_format :: write_format write_format(w, "{}", a); } diff --git a/core/misc/method_ops.onyx b/core/misc/method_ops.onyx index f9d975d1..9862aea8 100644 --- a/core/misc/method_ops.onyx +++ b/core/misc/method_ops.onyx @@ -24,24 +24,24 @@ package core } #if #defined(runtime.vars.Onyx_Enable_Operator_Methods) { - #operator == #precedence 10000 macro (t: $T, r: $R) -> #auto where __HasEqMethod(T, R) do return T.__eq(t, r); - #operator != #precedence 10000 macro (t: $T, r: $R) -> #auto where __HasNeMethod(T, R) do return T.__ne(t, r); - #operator < #precedence 10000 macro (t: $T, r: $R) -> #auto where __HasLtMethod(T, R) do return T.__lt(t, r); - #operator <= #precedence 10000 macro (t: $T, r: $R) -> #auto where __HasLeMethod(T, R) do return T.__le(t, r); - #operator > #precedence 10000 macro (t: $T, r: $R) -> #auto where __HasGtMethod(T, R) do return T.__gt(t, r); - #operator >= #precedence 10000 macro (t: $T, r: $R) -> #auto where __HasGeMethod(T, R) do return T.__ge(t, r); - #operator + #precedence 10000 macro (t: $T, r: $R) -> #auto where __HasAddMethod(T, R) do return T.__add(t, r); - #operator - #precedence 10000 macro (t: $T, r: $R) -> #auto where __HasMinusMethod(T, R) do return T.__minus(t, r); - #operator * #precedence 10000 macro (t: $T, r: $R) -> #auto where __HasMulMethod(T, R) do return T.__mul(t, r); - #operator / #precedence 10000 macro (t: $T, r: $R) -> #auto where __HasDivMethod(T, R) do return T.__div(t, r); - #operator % #precedence 10000 macro (t: $T, r: $R) -> #auto where __HasModMethod(T, R) do return T.__mod(t, r); - #operator & #precedence 10000 macro (t: $T, r: $R) -> #auto where __HasAndMethod(T, R) do return T.__and(t, r); - #operator | #precedence 10000 macro (t: $T, r: $R) -> #auto where __HasOrMethod(T, R) do return T.__or(t, r); - #operator << #precedence 10000 macro (t: $T, r: $R) -> #auto where __HasShlMethod(T, R) do return T.__shl(t, r); - #operator >> #precedence 10000 macro (t: $T, r: $R) -> #auto where __HasShrMethod(T, R) do return T.__shr(t, r); - #operator >>> #precedence 10000 macro (t: $T, r: $R) -> #auto where __HasSarMethod(T, R) do return T.__sar(t, r); - #operator ^ #precedence 10000 macro (t: $T, r: $R) -> #auto where __HasXorMethod(T, R) do return T.__xor(t, r); - #operator && #precedence 10000 macro (t: $T, r: $R) -> #auto where __HasBandMethod(T, R) do return T.__band(t, r); - #operator || #precedence 10000 macro (t: $T, r: $R) -> #auto where __HasBorMethod(T, R) do return T.__bor(t, r); - #operator [] #precedence 10000 macro (t: $T, r: $R) -> #auto where __HasSubMethod(T, R) do return T.__sub(t, r); + #operator == #order 10000 macro (t: $T, r: $R) -> #auto where __HasEqMethod(T, R) do return T.__eq(t, r); + #operator != #order 10000 macro (t: $T, r: $R) -> #auto where __HasNeMethod(T, R) do return T.__ne(t, r); + #operator < #order 10000 macro (t: $T, r: $R) -> #auto where __HasLtMethod(T, R) do return T.__lt(t, r); + #operator <= #order 10000 macro (t: $T, r: $R) -> #auto where __HasLeMethod(T, R) do return T.__le(t, r); + #operator > #order 10000 macro (t: $T, r: $R) -> #auto where __HasGtMethod(T, R) do return T.__gt(t, r); + #operator >= #order 10000 macro (t: $T, r: $R) -> #auto where __HasGeMethod(T, R) do return T.__ge(t, r); + #operator + #order 10000 macro (t: $T, r: $R) -> #auto where __HasAddMethod(T, R) do return T.__add(t, r); + #operator - #order 10000 macro (t: $T, r: $R) -> #auto where __HasMinusMethod(T, R) do return T.__minus(t, r); + #operator * #order 10000 macro (t: $T, r: $R) -> #auto where __HasMulMethod(T, R) do return T.__mul(t, r); + #operator / #order 10000 macro (t: $T, r: $R) -> #auto where __HasDivMethod(T, R) do return T.__div(t, r); + #operator % #order 10000 macro (t: $T, r: $R) -> #auto where __HasModMethod(T, R) do return T.__mod(t, r); + #operator & #order 10000 macro (t: $T, r: $R) -> #auto where __HasAndMethod(T, R) do return T.__and(t, r); + #operator | #order 10000 macro (t: $T, r: $R) -> #auto where __HasOrMethod(T, R) do return T.__or(t, r); + #operator << #order 10000 macro (t: $T, r: $R) -> #auto where __HasShlMethod(T, R) do return T.__shl(t, r); + #operator >> #order 10000 macro (t: $T, r: $R) -> #auto where __HasShrMethod(T, R) do return T.__shr(t, r); + #operator >>> #order 10000 macro (t: $T, r: $R) -> #auto where __HasSarMethod(T, R) do return T.__sar(t, r); + #operator ^ #order 10000 macro (t: $T, r: $R) -> #auto where __HasXorMethod(T, R) do return T.__xor(t, r); + #operator && #order 10000 macro (t: $T, r: $R) -> #auto where __HasBandMethod(T, R) do return T.__band(t, r); + #operator || #order 10000 macro (t: $T, r: $R) -> #auto where __HasBorMethod(T, R) do return T.__bor(t, r); + #operator [] #order 10000 macro (t: $T, r: $R) -> #auto where __HasSubMethod(T, R) do return T.__sub(t, r); } diff --git a/core/string/string.onyx b/core/string/string.onyx index f3056d25..f57dbae3 100644 --- a/core/string/string.onyx +++ b/core/string/string.onyx @@ -9,7 +9,7 @@ as_str :: #match -> str {} { T.as_str(t) } -> str; } -#overload #precedence 10000 +#overload #order 10000 as_str :: macro (t: $T/HasAsStrMethod) -> str { return T.as_str(t); } diff --git a/examples/14_overloaded_procs.onyx b/examples/14_overloaded_procs.onyx index f1e95d7c..f60e9ae8 100644 --- a/examples/14_overloaded_procs.onyx +++ b/examples/14_overloaded_procs.onyx @@ -56,7 +56,7 @@ main :: (args: [] cstr) { person_set << .{ "Joe", 38 }; // "Joe" will only be printed once. - for person: set.iterator(^person_set) { + for person: set.as_iter(^person_set) { println(person); } } @@ -72,15 +72,16 @@ print_type :: #match { // type knowledge at runtime. To rectify this, let's add a new case that makes // this work for any type: -#match print_type #precedence 10 (x: $T) { +#match #order 10 print_type (x: $T) { printf("Fallback case: called with a {} ({})\n", T, x); } -// There are a couple things going on here. The #match directive takes two "parameters": the overloaded procedure to add a match option to, and the option itself. The other -// thing to notice is the "#precedence 10" specification. Onyx tries to match the arguments you provided +// There are a couple things going on here. The #match directive takes two "parameters": the overloaded +// procedure to add a match option to, and the option itself. The other +// thing to notice is the "#order 10" specification. Onyx tries to match the arguments you provided // with each of the options. The first one that matches perfectly, is the one that is used. The order in -// which it does this process is important, and that is why you can control it with the #precedence -// directive. The #precedence directive is allowed in two places: right before the option in the original +// which it does this process is important, and that is why you can control it with the #order +// directive. The #order directive is allowed in two places: right before the option in the original // #match directive, or in front of the option in an #match directive, as seen above. Currently, // the options are sorted in ascending order according to the precedence. I feel like this is backwards, // so that may change in the future. Either way, setting the precedence allows you to place the options diff --git a/tests/overload_precedence.onyx b/tests/overload_precedence.onyx index b4b2ecc0..07f4197c 100644 --- a/tests/overload_precedence.onyx +++ b/tests/overload_precedence.onyx @@ -3,9 +3,9 @@ use package core overloaded :: #match { - #precedence 10 (x: i32) { println("Option X"); }, - #precedence 5 (y: i32) { println("Option Y"); }, - #precedence 4 (z: i32) { println("Option Z"); }, + #order 10 (x: i32) { println("Option X"); }, + #order 5 (y: i32) { println("Option Y"); }, + #order 4 (z: i32) { println("Option Z"); }, } main :: (args: [] cstr) { @@ -15,6 +15,6 @@ main :: (args: [] cstr) { overloaded(z=10); } -#match #precedence 3 overloaded (q: i32) { println("Option Q"); } -#match #precedence 2 overloaded (r: i32) { println("Option R"); } -#match #precedence 1 overloaded (m: i32) { println("Option M"); } +#match #order 3 overloaded (q: i32) { println("Option Q"); } +#match #order 2 overloaded (r: i32) { println("Option R"); } +#match #order 1 overloaded (m: i32) { println("Option M"); } -- 2.25.1