From: Brendan Hansen Date: Sat, 29 Aug 2020 03:50:35 +0000 (-0500) Subject: bugfixes and more testing X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=215bf1dd5567820a67a8709a786a91a0b3c668bf;p=onyx.git bugfixes and more testing --- diff --git a/core/builtin.onyx b/core/builtin.onyx index 0191f775..3224e303 100644 --- a/core/builtin.onyx +++ b/core/builtin.onyx @@ -38,30 +38,43 @@ 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, initial_cap := 4) { +array_init :: proc (arr: ^[..] $T, initial_cap := 4) { arr.count = 0; arr.capacity = initial_cap; arr.data = calloc(sizeof T * arr.capacity); } -array_add :: proc (arr: ^[..] $T, x: T) { - if arr.count + 1 > arr.capacity { - arr.capacity <<= 1; - arr.data = cresize(arr.data, sizeof T * arr.capacity); - } +array_free :: proc (arr: ^[..] $T) { + arr.count = 0; + arr.capacity = 0; + + cfree(arr.data); + arr.data = null; +} + +array_ensure_capacity :: proc (arr: ^[..] $T, cap: u32) { + if arr.capacity >= cap do return; + + while cap > arr.capacity do arr.capacity <<= 1; + arr.data = cresize(arr.data, sizeof T * arr.capacity); +} +array_add :: proc (arr: ^[..] $T, x: T) { + array_ensure_capacity(arr, arr.count + 1); arr.data[arr.count] = x; arr.count += 1; } -array_remove_at :: proc (arr: ^[..] $T, idx: u32) { - if idx >= arr.count do return; +array_add_at :: proc (arr: ^[..] $T, x: T, idx: u32) { + array_ensure_capacity(arr, arr.count + 1); - for i: idx, arr.count - 1 { - arr.data[i] = arr.data[i + 1]; + arr.count += 1; + while i := arr.count; i > idx { + arr.data[i] = arr.data[i - 1]; + i -= 1; } - arr.count -= 1; + arr.data[idx] = x; } array_remove :: proc (arr: ^[..] $T, elem: T) { @@ -75,6 +88,21 @@ array_remove :: proc (arr: ^[..] $T, elem: T) { arr.count -= move; } +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_contains :: proc (arr: ^[..] $T, x: T) -> bool { + for i: 0, arr.count do if arr.data[i] == x do return true; + return false; +} + context : struct { allocator : Allocator; temp_allocator : Allocator; diff --git a/core/stdio.onyx b/core/stdio.onyx index 9fc618e0..5eab4dfe 100644 --- a/core/stdio.onyx +++ b/core/stdio.onyx @@ -10,14 +10,16 @@ print_string :: proc (s: string) { } print_cstring :: proc (s: cstring) do string_builder_append(^cout_state.sb, s); -print_u64 :: proc (n: u64, base := 10l) do string_builder_append(^cout_state.sb, n); +print_u64 :: proc (n: u64, base := 10l) do string_builder_append(^cout_state.sb, n, base); print_u32 :: proc (n: u32, base := 10) do string_builder_append(^cout_state.sb, cast(u64) n, cast(u64) base); +print_bool :: proc (b: bool) do string_builder_append(^cout_state.sb, b); print :: proc #overloaded { print_string, print_cstring, print_u64, print_u32, + print_bool, } print_flush :: proc { diff --git a/core/string.onyx b/core/string.onyx index ac7ab375..0cd56794 100644 --- a/core/string.onyx +++ b/core/string.onyx @@ -138,8 +138,14 @@ string_builder_add_cstring :: proc (use sb: ^StringBuilder, cstr: cstring) -> ^S return string_builder_add_string(sb, s); } -u64_to_string :: proc (n_: u64, base: u64, buf: Buffer) -> string { - n := n_; +i64_to_string :: proc (n_: i64, base: u64, buf: Buffer) -> string { + n := cast(u64) n_; + + is_neg := false; + if n_ < 0l && base == 10l { + is_neg = true; + n = cast(u64) -n_; + } str :: cast(^u8) buf.data; for i: 0, buf.count do str[i] = #char "\0"; @@ -182,19 +188,36 @@ u64_to_string :: proc (n_: u64, base: u64, buf: Buffer) -> string { c -= 1; } + if is_neg { + *c = #char "-"; + len += 1; + c -= 1; + } + return string.{ data = c + 1, count = len }; } string_builder_add_u64 :: proc (use sb: ^StringBuilder, n: u64, base := 10l) -> ^StringBuilder { buf : [256] u8; - s := u64_to_string(n, base, Buffer.{ cast(rawptr) buf, 256 }); + s := i64_to_string(n, base, Buffer.{ cast(rawptr) buf, 256 }); return string_builder_add_string(sb, s); } +string_builder_add_bool :: proc (use sb: ^StringBuilder, b: bool) -> ^StringBuilder { + if b { + return string_builder_add_string(sb, "true"); + } else { + return string_builder_add_string(sb, "false"); + } + + return null; +} + string_builder_append :: proc #overloaded { string_builder_add_string, string_builder_add_cstring, string_builder_add_u64, + string_builder_add_bool, } string_builder_to_string :: proc (use sb: ^StringBuilder) -> string { diff --git a/onyx b/onyx index 1ed9b95e..40d74102 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/poly_test.onyx b/progs/poly_test.onyx index 93ef5a1b..050a3cde 100644 --- a/progs/poly_test.onyx +++ b/progs/poly_test.onyx @@ -24,11 +24,11 @@ use package file use package stdio print_arr_details :: proc (arr: ^[..] $T) { - print("\nArray details:\n"); + print("\nArray details:\n\tSize: "); print(arr.count); - print("\n"); + print("\n\tCapacity: "); print(arr.capacity); - print("\n"); + print("\n\tData ptr: "); print(cast(u32) arr.data, 16); print("\n\n"); } @@ -43,26 +43,49 @@ print_arr :: proc (arr: ^[..] $T) { } main :: proc (args: [] cstring) { - arr : [..] i32; - array_init(^arr, 24); + iarr : [..] i32; + array_init(^iarr, 24); + defer array_free(^iarr); + + print_arr_details(^iarr); + + array_add(^iarr, 1234); + + for i: 0, 12 do array_add(^iarr, i % 5); + + print_arr_details(^iarr); + print_arr(^iarr); - print_arr_details(^arr); + array_remove_at(^iarr, 4); - array_add(^arr, 1234); + print_arr_details(^iarr); + print_arr(^iarr); - for i: 0, 12 do array_add(^arr, i % 5); + array_remove(^iarr, 1); + array_remove(^iarr, 4); - print_arr_details(^arr); - print_arr(^arr); + print_arr_details(^iarr); + print_arr(^iarr); - array_remove_at(^arr, 4); + array_add_at(^iarr, 5678, 2); - print_arr_details(^arr); - print_arr(^arr); + print_arr_details(^iarr); + print_arr(^iarr); - array_remove(^arr, 1); - array_remove(^arr, 4); - print_arr_details(^arr); - print_arr(^arr); + + barr : [..] i64; + array_init(^barr, 10); + defer array_free(^barr); + + for i: 0, 1000 { + array_add(^barr, cast(u64) (3 * i * i + 4 * i - 2)); + } + + print_arr_details(^barr); + print_arr(^barr); + + print("Does the array contain 2638? "); + print(array_contains(^barr, 2638l)); + print("\n"); } \ No newline at end of file diff --git a/src/onyxclone.c b/src/onyxclone.c index 1c95c46e..459d4630 100644 --- a/src/onyxclone.c +++ b/src/onyxclone.c @@ -219,6 +219,10 @@ AstNode* ast_clone(bh_allocator a, void* n) { case Ast_Kind_While: ((AstIfWhile *) nn)->local = (AstLocal *) ast_clone(a, ((AstIfWhile *) node)->local); ((AstIfWhile *) nn)->assignment = (AstBinaryOp *) ast_clone(a, ((AstIfWhile *) node)->assignment); + + if (((AstIfWhile *) nn)->assignment) + ((AstIfWhile *) nn)->assignment->left = (AstTyped *) ((AstIfWhile *) nn)->local; + ((AstIfWhile *) nn)->cond = (AstTyped *) ast_clone(a, ((AstIfWhile *) node)->cond); ((AstIfWhile *) nn)->true_stmt = (AstBlock *) ast_clone(a, ((AstIfWhile *) node)->true_stmt); ((AstIfWhile *) nn)->false_stmt = (AstBlock *) ast_clone(a, ((AstIfWhile *) node)->false_stmt); @@ -230,6 +234,8 @@ AstNode* ast_clone(bh_allocator a, void* n) { dw->local = (AstLocal *) ast_clone(a, sw->local); dw->assignment = (AstBinaryOp *) ast_clone(a, sw->assignment); + if (dw->assignment) + dw->assignment->left = (AstTyped *) sw->local; dw->expr = (AstTyped *) ast_clone(a, sw->expr); dw->default_case = (AstBlock *) ast_clone(a, sw->default_case);