From: Brendan Hansen Date: Wed, 21 Apr 2021 19:05:02 +0000 (-0500) Subject: code cleanup; added iter.zip and iter.const X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=e85f29b379ba4c15cc62b791211142befc60de90;p=onyx.git code cleanup; added iter.zip and iter.const --- diff --git a/bin/onyx b/bin/onyx index 2b1992df..51554867 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/core/alloc/heap.onyx b/core/alloc/heap.onyx index 4bc70bee..cfe0e3fd 100644 --- a/core/alloc/heap.onyx +++ b/core/alloc/heap.onyx @@ -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; diff --git a/core/alloc/logging.onyx b/core/alloc/logging.onyx index 30d409b5..14f5144e 100644 --- a/core/alloc/logging.onyx +++ b/core/alloc/logging.onyx @@ -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 +} diff --git a/core/container/iter.onyx b/core/container/iter.onyx index ff8f0297..b285c79d 100644 --- a/core/container/iter.onyx +++ b/core/container/iter.onyx @@ -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); diff --git a/core/memory.onyx b/core/memory.onyx index f531b7e8..3209b438 100644 --- a/core/memory.onyx +++ b/core/memory.onyx @@ -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; + } +} diff --git a/docs/bugs b/docs/bugs index a174a863..172bc1ed 100644 --- 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 ]; diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index 079156c0..71de59dc 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -903,8 +903,6 @@ struct AstDirectiveOperator { }; -extern AstNode empty_node; - typedef enum EntityState { Entity_State_Error, diff --git a/src/onyxastnodes.c b/src/onyxastnodes.c index c168c111..08dad627 100644 --- a/src/onyxastnodes.c +++ b/src/onyxastnodes.c @@ -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", diff --git a/src/onyxwasm.c b/src/onyxwasm.c index 2166e567..49f9044d 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -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++; diff --git a/src/onyxwasm_output.c b/src/onyxwasm_output.c index 6e19836c..e1c57645 100644 --- a/src/onyxwasm_output.c +++ b/src/onyxwasm_output.c @@ -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); diff --git a/tests/lazy_iterators b/tests/lazy_iterators index 7442341f..47af0b1f 100644 --- a/tests/lazy_iterators +++ b/tests/lazy_iterators @@ -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... diff --git a/tests/lazy_iterators.onyx b/tests/lazy_iterators.onyx index 0e75b701..c069b3ab 100644 --- a/tests/lazy_iterators.onyx +++ b/tests/lazy_iterators.onyx @@ -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); + } }