bugfixing and added enumerate to iter
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 6 Aug 2021 19:55:45 +0000 (14:55 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 6 Aug 2021 19:55:45 +0000 (14:55 -0500)
bin/onyx
core/container/iter.onyx
include/onyxastnodes.h
src/onyxutils.c

index 1799b4fdaab30c473fee7e85559e4705a974bffc..55b3613d75b8d21768e0ca37ac61cc39dd4d3163 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index 5687d486d52cd7373d524807cab61652027b108b..880c8245cd6683153a2ebcc86c49ccfba4ffc144 100644 (file)
@@ -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);
index 9a3911d3e172df7b2a37303c99c2af580f3de151..f9b8ee261b913343c07db3d91cc8c2a0c80e4b35 100644 (file)
@@ -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);
index be16d9f600a3d99996a6ec7a5d632724cdad431a..b2f741fcded9ea774b667c007837479c7daf9577 100644 (file)
@@ -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;
 }