From: Brendan Hansen Date: Fri, 6 Aug 2021 19:55:45 +0000 (-0500) Subject: bugfixing and added enumerate to iter X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=c77057e3409ab66ba5c245e9e1137d058d44d7f0;p=onyx.git bugfixing and added enumerate to iter --- diff --git a/bin/onyx b/bin/onyx index 1799b4fd..55b3613d 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/core/container/iter.onyx b/core/container/iter.onyx index 5687d486..880c8245 100644 --- a/core/container/iter.onyx +++ b/core/container/iter.onyx @@ -27,7 +27,7 @@ filter :: (it: Iterator($T), predicate: (T) -> bool) -> Iterator(T) { } close :: ($T: type_expr, fi: ^FilterIterator(T)) { - fi.iterator.close(fi.iterator.data); + if fi.iterator.close != null_proc do fi.iterator.close(fi.iterator.data); cfree(fi); } @@ -56,7 +56,7 @@ map :: (it: Iterator($T), transform: (T) -> $R) -> Iterator(R) { } close :: ($T: type_expr, $R: type_expr, mi: ^MapIterator(T, R)) { - mi.iterator.close(mi.iterator.data); + if mi.iterator.close != null_proc do mi.iterator.close(mi.iterator.data); cfree(mi); } @@ -114,7 +114,7 @@ take_while :: (it: Iterator($T), predicate: (T) -> bool) -> Iterator(T) { } close :: ($T: type_expr, ti: ^TakeIterator(T)) { - ti.iterator.close(ti.iterator.data); + if ti.iterator.close != null_proc do ti.iterator.close(ti.iterator.data); cfree(ti); } @@ -151,7 +151,7 @@ skip :: (it: Iterator($T), count: u32) -> Iterator(T) { } close :: ($T: type_expr, si: ^SkipIterator(T)) { - si.iterator.close(si.iterator.data); + if si.iterator.close != null_proc do si.iterator.close(si.iterator.data); cfree(si); } @@ -185,8 +185,8 @@ zip :: (left_iterator: Iterator($T), right_iterator: Iterator($R)) -> Iterator(Z } close :: ($T: type_expr, $R: type_expr, zi: ^ZippedIterator(T, R)) { - zi.iterator1.close(zi.iterator1.data); - zi.iterator2.close(zi.iterator2.data); + if zi.iterator1.close != null_proc do zi.iterator1.close(zi.iterator1.data); + if zi.iterator2.close != null_proc do zi.iterator2.close(zi.iterator2.data); cfree(zi); } @@ -212,6 +212,79 @@ const :: (value: $T) -> Iterator(T) { }; } +#private_file Enumeration_Value :: struct (T: type_expr) { + index: i32; + value: T; +} + +enumerate :: (it: Iterator($T), start_index: i32 = 0) -> Iterator(Enumeration_Value(T)) { + Enumeration_Context :: struct (T: type_expr) { + iterator: Iterator(T); + current_index: i32; + } + + ec := make(#type Enumeration_Context(T)); + ec.iterator = it; + ec.current_index = start_index; + + next :: ($T: type_expr, use data: ^Enumeration_Context(T)) -> (Enumeration_Value(T), bool) { + value, cont := iterator.next(iterator.data); + + if !cont do return .{ current_index, __zero_value(T) }, false; + + defer current_index += 1; + return .{ current_index, value }, true; + } + + close :: ($T: type_expr, use data: ^Enumeration_Context(T)) { + if iterator.close != null_proc do iterator.close(iterator.data); + cfree(data); + } + + return .{ + data = ec, + next = #solidify next { T = T }, + close = #solidify close { T = T }, + }; +} + +from_array :: (arr: [..] $T) -> Iterator(^T) { + return from_slice((#type [] T).{ arr.data, arr.count }); +} + +from_slice :: (arr: [] $T) -> Iterator(^T) { + Context :: struct (T: type_expr) { + data: ^T; + count: u32; + current: u32; + } + + c := make(#type Context(T)); + c.data = arr.data; + c.count = arr.count; + c.current = 0; + + next :: ($T: type_expr, use _: ^Context(T)) -> (^T, bool) { + if current < count { + defer current += 1; + return ^data[current], true; + + } else { + return null, false; + } + } + + close :: (data: rawptr) { + cfree(data); + } + + return .{ + data = c, + next = #solidify next { T = T }, + close = close, + }; +} + fold :: (it: Iterator($T), initial_value: R, combine: (T, $R) -> R) -> R { for value: it { initial_value = combine(value, initial_value); diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index 9a3911d3..f9b8ee26 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -1131,7 +1131,7 @@ void entity_heap_remove_top(EntityHeap* entities); // If target_arr is null, the entities will be placed directly in the heap. void add_entities_for_node(bh_arr(Entity *)* target_arr, AstNode* node, Scope* scope, Package* package); -void entity_bring_to_state(Entity* ent, EntityState state); +b32 entity_bring_to_state(Entity* ent, EntityState state); void symres_entity(Entity* ent); void check_entity(Entity* ent); void emit_entity(Entity* ent); diff --git a/src/onyxutils.c b/src/onyxutils.c index be16d9f6..b2f741fc 100644 --- a/src/onyxutils.c +++ b/src/onyxutils.c @@ -962,7 +962,7 @@ AstFunction* polymorphic_proc_build_only_header(AstPolyProc* pp, PolyProcLookupM .scope = solidified_func.poly_scope, }; - entity_bring_to_state(&func_header_entity, Entity_State_Code_Gen); + b32 successful = entity_bring_to_state(&func_header_entity, Entity_State_Code_Gen); if (onyx_has_errors()) { onyx_clear_errors(); return NULL; @@ -1084,8 +1084,8 @@ AstTyped* find_matching_overload_by_arguments(bh_arr(OverloadOption) overloads, // NOTE: Overload is not something that is known to be overloadable. if (overload == NULL) continue; if (overload->kind != Ast_Kind_Function) continue; - if (overload->type == NULL) continue; - assert(overload->type->kind == Type_Kind_Function); + // if (overload->type == NULL) continue; + // assert(overload->type->kind == Type_Kind_Function); // NOTE: If the arguments cannot be placed successfully in the parameters list if (!fill_in_arguments(&args, (AstNode *) overload, NULL)) continue; @@ -1324,7 +1324,7 @@ AstStructType* polymorphic_struct_lookup(AstPolyStructType* ps_type, bh_arr(AstP return NULL; } -void entity_bring_to_state(Entity* ent, EntityState state) { +b32 entity_bring_to_state(Entity* ent, EntityState state) { EntityState last_state = ent->state; while (ent->state != state) { @@ -1333,14 +1333,16 @@ void entity_bring_to_state(Entity* ent, EntityState state) { case Entity_State_Check_Types: check_entity(ent); break; case Entity_State_Code_Gen: emit_entity(ent); break; - default: return; + default: return 0; } - if (ent->state == last_state) break; + if (ent->state == last_state) return 0; last_state = ent->state; - if (onyx_has_errors()) return; + if (onyx_has_errors()) return 0; } + + return 1; }