From: Brendan Hansen Date: Tue, 28 Sep 2021 02:57:36 +0000 (-0500) Subject: bugfixes and added lcm and gcd X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=108c051585c28f19cf159ecd65a27abc121429b2;p=onyx.git bugfixes and added lcm and gcd --- diff --git a/bin/onyx b/bin/onyx index 6989ac39..a7ddb164 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/core/container/iter.onyx b/core/container/iter.onyx index 8bafc5d0..26e2019e 100644 --- a/core/container/iter.onyx +++ b/core/container/iter.onyx @@ -2,6 +2,10 @@ package core.iter use package core.intrinsics.onyx { __zero_value } +close :: (it: Iterator($T)) { + it.close(it.data); +} + filter :: (it: Iterator($T), predicate: (T) -> bool) -> Iterator(T) { FilterIterator :: struct (T: type_expr) { iterator: Iterator(T); diff --git a/core/math.onyx b/core/math.onyx index 5396f70d..e544a141 100644 --- a/core/math.onyx +++ b/core/math.onyx @@ -327,3 +327,15 @@ choose :: (n: $T, k: T) -> T { return ret; } + +gcd :: (a: $T, b: T) -> T { + if a < 0 do a = -a; + if b < 0 do b = -b; + + if b == 0 do return a; + return gcd(b, a % b); +} + +lcm :: (a: $T, b: T) -> T { + return (a * b) / gcd(a, b); +} diff --git a/src/astnodes.c b/src/astnodes.c index 5479bb04..972b9a8c 100644 --- a/src/astnodes.c +++ b/src/astnodes.c @@ -504,6 +504,9 @@ b32 convert_numlit_to_type(AstNumLit* num, Type* type) { } // NOTE: Returns 0 if it was not possible to make the types compatible. +// TODO: This function should be able return a "yield" condition. There +// are a couple cases that need to yield in order to be correct, like +// polymorphic functions with a typeof for the return type. b32 unify_node_and_type_(AstTyped** pnode, Type* type, b32 permanent) { AstTyped* node = *pnode; if (type == NULL) return 0; @@ -563,6 +566,9 @@ b32 unify_node_and_type_(AstTyped** pnode, Type* type, b32 permanent) { AstFunction* func = polymorphic_proc_lookup((AstPolyProc *) node, PPLM_By_Function_Type, type, node->token); if (func == NULL) return 0; + // FIXME: This is incorrect. It should actually yield and not return a failure. + if (func == &node_that_signals_a_yield) return 0; + *pnode = (AstTyped *) func; node = *pnode; } diff --git a/src/parser.c b/src/parser.c index ac19ae32..ea195a31 100644 --- a/src/parser.c +++ b/src/parser.c @@ -2129,8 +2129,7 @@ static AstFunction* parse_function_definition(OnyxParser* parser, OnyxToken* tok func_def->body = body_block; - bh_arr_free(polymorphic_vars); - return func_def; + goto function_defined; } if (consume_token_if_next(parser, Token_Type_Right_Arrow)) { @@ -2172,6 +2171,7 @@ static AstFunction* parse_function_definition(OnyxParser* parser, OnyxToken* tok func_def->body = parse_block(parser, 1); +function_defined: if (bh_arr_length(polymorphic_vars) > 0) { AstPolyProc* pp = make_node(AstPolyProc, Ast_Kind_Polymorphic_Proc); pp->token = func_def->token; @@ -2191,6 +2191,8 @@ static b32 parse_possible_function_definition(OnyxParser* parser, AstTyped** ret OnyxToken* matching_paren = find_matching_paren(parser->curr); if (matching_paren == NULL) return 0; + if (next_tokens_are(parser, 4, '(', ')', '=', '>')) return 0; + // :LinearTokenDependent OnyxToken* token_after_paren = matching_paren + 1; if (token_after_paren->type != Token_Type_Right_Arrow