code cleanup; added iter.zip and iter.const
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 21 Apr 2021 19:05:02 +0000 (14:05 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 21 Apr 2021 19:05:02 +0000 (14:05 -0500)
12 files changed:
bin/onyx
core/alloc/heap.onyx
core/alloc/logging.onyx
core/container/iter.onyx
core/memory.onyx
docs/bugs
include/onyxastnodes.h
src/onyxastnodes.c
src/onyxwasm.c
src/onyxwasm_output.c
tests/lazy_iterators
tests/lazy_iterators.onyx

index 2b1992dfce0cf8c6ac843d75f47e347ea154b742..51554867e6f646b313c53d36745aefa25ed65c80 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index 4bc70bee30474ec0f6cc74dbbed3ed39ebd1a10a..cfe0e3fd15a757d7851cefcf5e43b6c20e83345d 100644 (file)
@@ -6,13 +6,13 @@ package core.alloc.heap
 // of the language. You will not make your own instance of the heap
 // allocator, since it controls WASM intrinsics such as memory_grow.
 
-#load "core/memory"
 #load "core/intrinsics/wasm"
 
 use package core.intrinsics.wasm {
     memory_size, memory_grow,
     memory_copy,
 }
+#private_file memory :: package core.memory
 
 // The global heap state
 #private_file
@@ -28,14 +28,13 @@ heap_block :: struct {
     next : ^heap_block;
 }
 
+// FIX: This does not respect the choice of alignment
 #private_file
 heap_alloc :: (size_: u32, align: u32) -> rawptr {
     if size_ == 0 do return null;
 
     size := size_ + sizeof heap_block;
-    if size % align != 0 {
-        size += align - (size % align);
-    }
+    memory.align(~~^size, ~~align);
 
     prev := ^heap_state.free_list;
     hb := heap_state.free_list;
index 30d409b5985bcd50ed71245f9ea4cb2b7d8a989c..14f5144eb3d685a34561c6950f33bd81ed142188 100644 (file)
@@ -1,5 +1,7 @@
 package core.alloc.log
 
+// FIXME: This should use context.logger instead of printf.
+
 Allocation_Action_Strings := str.[
     " alloc",
     "  free",
@@ -23,4 +25,4 @@ logging_allocator :: (alloc: ^Allocator) -> Allocator {
         func = logging_allocator_proc,
         data = alloc,
     };
-}
\ No newline at end of file
+}
index ff8f0297ba482b272115df560ba821bf5d2f885d..b285c79d43b85d8310d0a931722744fffc8ea2ec 100644 (file)
@@ -176,6 +176,60 @@ skip :: (it: Iterator($T), count: u32) -> Iterator(T) {
     };
 }
 
+#private_file Zipped :: struct (T: type_expr, R: type_expr) {
+    first: T;
+    second: R;
+}
+
+zip :: (left_iterator: Iterator($T), right_iterator: Iterator($R)) -> Iterator(Zipped(T, R)) {
+    ZippedIterator :: struct (T: type_expr, R: type_expr) {
+        iterator1: Iterator(T);
+        iterator2: Iterator(R);
+    }
+
+    zipped_iterator := new(#type ZippedIterator(T, R));
+    zipped_iterator.iterator1 = left_iterator;
+    zipped_iterator.iterator2 = right_iterator;
+
+    next :: ($T: type_expr, $R: type_expr, data: rawptr) -> (Zipped(T, R), bool) {
+        zi := cast(^ZippedIterator(T, R)) data;
+
+        v1, cont1 := zi.iterator1.next(zi.iterator1.data);
+        v2, cont2 := zi.iterator2.next(zi.iterator2.data);
+
+        return .{ v1, v2 }, cont1 && cont2;
+    }
+
+    close :: ($T: type_expr, $R: type_expr, data: rawptr) {
+        zi := cast(^ZippedIterator(T, R)) data;
+        zi.iterator1.close(zi.iterator1.data);
+        zi.iterator2.close(zi.iterator2.data);
+        cfree(data);
+    }
+
+    return .{
+        data  = zipped_iterator,
+        next  = #solidify next  { T=T, R=R },
+        close = #solidify close { T=T, R=R },
+    };
+}
+
+const :: (value: $T) -> Iterator(T) {
+    next :: ($T: type_expr, data: rawptr) -> (T, bool) {
+        value := *(cast(^T) data);
+        return value, true;
+    }
+
+    allocated := cast(^T) calloc(sizeof T);
+    *allocated = value;
+
+    return .{
+        data = allocated,
+        next = #solidify next { T=T },
+        close = cfree,
+    };
+}
+
 fold :: (it: Iterator($T), initial_value: R, combine: (T, $R) -> R) -> R {
     for value: it {
         initial_value = combine(value, initial_value);
index f531b7e8e70d08a22c6a77058e3b861dd1d85c64..3209b438de00ef79c3eafb9054df0ea7b6210d51 100644 (file)
@@ -43,3 +43,18 @@ make_slice :: ($T: type_expr, count: i32, allocator := context.allocator) -> []
         count = count
     };
 }
+
+align :: proc {
+    (size: ^u64, align: u64) {
+        if *size % align != 0 {
+            *size += align - (*size % align);
+        }
+    },
+
+    (size: u64, align: u64) -> u64 {
+        if size % align != 0 {
+            size += align - (size % align);
+        }
+        return size;
+    }        
+}
index a174a863425e66563b7e901197493e004eb6b16c..172bc1edf29a1a5cfeb4f449e0438c8dfe504284 100644 (file)
--- a/docs/bugs
+++ b/docs/bugs
@@ -15,7 +15,7 @@ List of known bugs:
         println("Test 2");
     }
 
-[ ] Array literals used as default values for struct members can break things.
+[X] Array literals used as default values for struct members can break things.
     
     Foo :: struct {
         arr : [5] u32 = u32.[ 2, 3, 5, 7, 11 ];
index 079156c094bd1d4bf4680abc326c72f427dcf778..71de59dce5cf4866bd000664258898ab26506330 100644 (file)
@@ -903,8 +903,6 @@ struct AstDirectiveOperator {
 };
 
 
-extern AstNode empty_node;
-
 typedef enum EntityState {
     Entity_State_Error,
     
index c168c1117df3dbc055c2bcbcce7fb812e3cc9448..08dad627242fda01c8d6f77dfa414f339379e58e 100644 (file)
@@ -2,8 +2,6 @@
 #include "onyxparser.h"
 #include "onyxutils.h"
 
-AstNode empty_node = { Ast_Kind_Error, 0, NULL, NULL };
-
 static const char* ast_node_names[] = {
     "ERROR",
     "PACKAGE",
index 2166e5675a17958f68308792c98dcb5bd20a27be..49f9044d87e6260953695f870bbcf21177c9a0ea 100644 (file)
@@ -1016,8 +1016,6 @@ EMIT_FUNC(for_iterator, AstFor* for_node, u64 iter_local) {
     emit_deferred_stmts(mod, &code);
     emit_leave_structured_block(mod, &code);
 
-    // Flush deferred statements
-    
     local_raw_free(mod->local_alloc, WASM_TYPE_PTR);
     local_raw_free(mod->local_alloc, WASM_TYPE_FUNC);
     local_raw_free(mod->local_alloc, WASM_TYPE_FUNC);
@@ -3203,6 +3201,7 @@ OnyxWasmModule onyx_wasm_module_create(bh_allocator alloc) {
         .idx = 0,
     };
     // :ArbitraryConstant
+    // :WasmMemory
     bh_table_put(WasmExport, module.exports, "memory", mem_export);
     module.export_count++;
 
index 6e19836cffc5c79c8e3eb2afeba8dc524899ca14..e1c576450d53f5851314ac58fb6a4753c597befe 100644 (file)
@@ -170,6 +170,9 @@ static i32 output_memorysection(OnyxWasmModule* module, bh_buffer* buff) {
     u8* leb = uint_to_uleb128((u64) 1, &leb_len);
     bh_buffer_append(&vec_buff, leb, leb_len);
     
+    // FIXME: This needs to be dynamically chosen depending on the size of
+    // the data section and stack size pre-requeseted.
+    // :WasmMemory
     output_limits(1024, -1, &vec_buff);
     
     leb = uint_to_uleb128((u64) (vec_buff.length), &leb_len);
index 7442341ff6485ff63777e8749718d1d28016afbc..47af0b1f14d77762a67229b029b2b0eb2887d97d 100644 (file)
@@ -7,3 +7,11 @@ Starting the iteration...
 Closing the count iterator...
 Closing the count iterator...
 58
+
+Starting the zipped iteration...
+54   42.0000
+56   42.0000
+58   42.0000
+60   42.0000
+62   42.0000
+Closing the count iterator...
index 0e75b701baf943a0612a14eb69d1d05d5c9ab689..c069b3aba3cbb6b6bce93f3234ccefa9a9a2b8b9 100644 (file)
@@ -35,6 +35,13 @@ count_iterator :: (lo: i32, hi: i32, step := 1) -> Iterator(i32) {
 }
 
 main :: (args: [] cstr) {
+    // Hopefully soon, the following will be possible.
+    // iterator := count_iterator(1, 10)
+    //             |> iter.map(x => x * 2)
+    //             |> iter.filter(x => x > 10)
+    //             |> iter.map(x => x + 42)
+    //             |> iter.take(5)
+    //             |> iter.to_array();
 
     iterator := count_iterator(1, 10)
                 |> iter.map((x: i32) -> i32     { return x * 2; })
@@ -51,4 +58,15 @@ main :: (args: [] cstr) {
             |> iter.to_array();
 
     println(arr[2]);
+
+    println("\nStarting the zipped iteration...");
+    zipped_iterator := count_iterator(1, 10)
+                |> iter.map((x: i32) -> i32     { return x * 2; })
+                |> iter.filter((x: i32) -> bool { return x > 10; })
+                |> iter.map((x: i32) -> i32     { return x + 42; })
+                |> iter.zip(iter.const(42.0f));
+
+    for value: zipped_iterator {
+        printf("%i   %f\n", value.first, value.second);
+    }
 }