From: Brendan Hansen Date: Sat, 29 Aug 2020 02:34:56 +0000 (-0500) Subject: adding array functionality and bugfixes X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=6c1b5116f7676b6771281564fb23a4bc26ffbe25;p=onyx.git adding array functionality and bugfixes --- diff --git a/core/builtin.onyx b/core/builtin.onyx index e302b252..0191f775 100644 --- a/core/builtin.onyx +++ b/core/builtin.onyx @@ -38,9 +38,9 @@ calloc :: proc (size: u32) -> rawptr do return alloc(context.allocator, size); cresize :: proc (ptr: rawptr, size: u32) -> rawptr do return resize(context.allocator, ptr, size); cfree :: proc (ptr: rawptr) do free(context.allocator, ptr); -array_init :: proc(arr: ^[..] $T) { +array_init :: proc(arr: ^[..] $T, initial_cap := 4) { arr.count = 0; - arr.capacity = 4; + arr.capacity = initial_cap; arr.data = calloc(sizeof T * arr.capacity); } @@ -54,6 +54,27 @@ array_add :: proc (arr: ^[..] $T, x: T) { arr.count += 1; } +array_remove_at :: proc (arr: ^[..] $T, idx: u32) { + if idx >= arr.count do return; + + for i: idx, arr.count - 1 { + arr.data[i] = arr.data[i + 1]; + } + + arr.count -= 1; +} + +array_remove :: proc (arr: ^[..] $T, elem: T) { + move := 0; + + for i: 0, arr.count - move { + if arr.data[i + move] == elem do move += 1; + if move != 0 do arr.data[i] = arr.data[i + move]; + } + + arr.count -= move; +} + context : struct { allocator : Allocator; temp_allocator : Allocator; diff --git a/onyx b/onyx index 7bf59014..1ed9b95e 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/poly_test.onyx b/progs/poly_test.onyx index b1e367d8..93ef5a1b 100644 --- a/progs/poly_test.onyx +++ b/progs/poly_test.onyx @@ -1,6 +1,6 @@ package main -#include_folder "/usr/share/onyx/core" +#include_folder "./core" #include_file "builtin" #include_file "wasi" @@ -33,9 +33,9 @@ print_arr_details :: proc (arr: ^[..] $T) { print("\n\n"); } -print_arr :: proc (arr: [..] $T) { +print_arr :: proc (arr: ^[..] $T) { for i: 0, arr.count { - print(arr[i]); + print(arr.data[i]); print(" "); } @@ -44,15 +44,25 @@ print_arr :: proc (arr: [..] $T) { main :: proc (args: [] cstring) { arr : [..] i32; - array_init(^arr); + array_init(^arr, 24); print_arr_details(^arr); array_add(^arr, 1234); - for i: 0, 12 do array_add(^arr, i * 2 + 2); + for i: 0, 12 do array_add(^arr, i % 5); print_arr_details(^arr); + print_arr(^arr); - print_arr(arr); + array_remove_at(^arr, 4); + + print_arr_details(^arr); + print_arr(^arr); + + array_remove(^arr, 1); + array_remove(^arr, 4); + + print_arr_details(^arr); + print_arr(^arr); } \ No newline at end of file diff --git a/src/onyxutils.c b/src/onyxutils.c index e194fd8a..73cb70e4 100644 --- a/src/onyxutils.c +++ b/src/onyxutils.c @@ -431,8 +431,10 @@ AstFunction* polymorphic_proc_lookup(AstPolyProc* pp, AstCall* call) { bh_table_put(AstFunction *, pp->concrete_funcs, key_buf, func); symres_function(func); + if (onyx_message_has_errors()) return NULL; if (check_function_header(func)) return NULL; if (check_function(func)) return NULL; + if (onyx_message_has_errors()) return NULL; bh_arr_push(semstate.other_entities, ((Entity) { .type = Entity_Type_Function_Header,