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);
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);
+}
}
// 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;
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;
}
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)) {
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;
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