bugfixes and added lcm and gcd
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 28 Sep 2021 02:57:36 +0000 (21:57 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 28 Sep 2021 02:57:36 +0000 (21:57 -0500)
bin/onyx
core/container/iter.onyx
core/math.onyx
src/astnodes.c
src/parser.c

index 6989ac3995e134573bf32fdbe37e670f8668be67..a7ddb1649d369f508cc570c54e3bf106de28e851 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index 8bafc5d05ff64e1524fb2b3a154c60c447d55cc1..26e2019e5e6548b0f1adf04d3a1247234cd479fb 100644 (file)
@@ -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);
index 5396f70dfd8ba5a35d2e2b880a245cfdb4fd8d8b..e544a1413ef39f3860e767f35e66c28bc219e6dc 100644 (file)
@@ -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);
+}
index 5479bb0499ec50a3a8e40d1e5f76b8922bb28afe..972b9a8c060e05e1279dfe1e51d8c2d2b39026aa 100644 (file)
@@ -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;
     }
index ac19ae324d73549f3f78258729fab6a653362b4b..ea195a3170526788697e284bcdcbddb1d7e1cd14 100644 (file)
@@ -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