From: Brendan Hansen Date: Sat, 29 Aug 2020 04:53:38 +0000 (-0500) Subject: weird bugfix with operator precedence X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=31472908ecda1d32260e01ca04c39fcd3a490b42;p=onyx.git weird bugfix with operator precedence --- diff --git a/core/builtin.onyx b/core/builtin.onyx index 3224e303..022be1df 100644 --- a/core/builtin.onyx +++ b/core/builtin.onyx @@ -103,6 +103,21 @@ array_contains :: proc (arr: ^[..] $T, x: T) -> bool { return false; } +array_pop :: proc (arr: ^[..] $T) -> T { + arr.count -= 1; + return arr.data[arr.count]; +} + +array_average :: proc (arr: ^[..] $T) -> T { + sum := cast(T) 0; + for i: 0, arr.count { + sum += arr.data[i]; + } + + return sum / cast(T) arr.count; +} + + context : struct { allocator : Allocator; temp_allocator : Allocator; diff --git a/core/string.onyx b/core/string.onyx index 0cd56794..c56d6f6e 100644 --- a/core/string.onyx +++ b/core/string.onyx @@ -197,7 +197,7 @@ i64_to_string :: proc (n_: i64, base: u64, buf: Buffer) -> string { return string.{ data = c + 1, count = len }; } -string_builder_add_u64 :: proc (use sb: ^StringBuilder, n: u64, base := 10l) -> ^StringBuilder { +string_builder_add_i64 :: proc (use sb: ^StringBuilder, n: i64, base := 10l) -> ^StringBuilder { buf : [256] u8; s := i64_to_string(n, base, Buffer.{ cast(rawptr) buf, 256 }); return string_builder_add_string(sb, s); @@ -216,7 +216,7 @@ string_builder_add_bool :: proc (use sb: ^StringBuilder, b: bool) -> ^StringBuil string_builder_append :: proc #overloaded { string_builder_add_string, string_builder_add_cstring, - string_builder_add_u64, + string_builder_add_i64, string_builder_add_bool, } diff --git a/onyx b/onyx index 40d74102..c9f25d80 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/poly_test.onyx b/progs/poly_test.onyx index 050a3cde..430f4636 100644 --- a/progs/poly_test.onyx +++ b/progs/poly_test.onyx @@ -42,7 +42,23 @@ print_arr :: proc (arr: ^[..] $T) { print("\n"); } +print_vec_arr :: proc (arr: ^[..] Vec3) { + for i: 0, arr.count { + print("Vec3("); + print(arr.data[i].x); + print(", "); + print(arr.data[i].y); + print(", "); + print(arr.data[i].z); + print(")\n"); + } + + print("\n"); +} + main :: proc (args: [] cstring) { + print(cast(u32) __heap_start, 16); + iarr : [..] i32; array_init(^iarr, 24); defer array_free(^iarr); @@ -71,6 +87,8 @@ main :: proc (args: [] cstring) { print_arr_details(^iarr); print_arr(^iarr); + print(array_average(^iarr)); + print("\n"); @@ -78,14 +96,35 @@ main :: proc (args: [] cstring) { array_init(^barr, 10); defer array_free(^barr); - for i: 0, 1000 { + for i: 0, 500 { array_add(^barr, cast(u64) (3 * i * i + 4 * i - 2)); } print_arr_details(^barr); print_arr(^barr); + print(array_average(^barr)); + print("\n"); - print("Does the array contain 2638? "); - print(array_contains(^barr, 2638l)); + print("Does the array contain 2637? "); + print(array_contains(^barr, 2637l)); print("\n"); + + + + varr : [..] Vec3; + array_init(^varr); + defer array_free(^varr); + + for i: 0, 20 { + array_add(^varr, Vec3.{ i, i * i, i * i * i }); + } + + print_arr_details(^varr); + print_vec_arr(^varr); +} + +Vec3 :: struct { + x: i32; + y: i32; + z: i32; } \ No newline at end of file diff --git a/src/onyxparser.c b/src/onyxparser.c index 85d51b0d..8db27f25 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -630,7 +630,7 @@ static inline i32 get_precedence(BinaryOp kind) { // With expected precedence rules static AstTyped* parse_expression(OnyxParser* parser) { bh_arr(AstBinaryOp*) tree_stack = NULL; - bh_arr_new(global_scratch_allocator, tree_stack, 4); + bh_arr_new(global_heap_allocator, tree_stack, 4); bh_arr_set_length(tree_stack, 0); AstTyped* left = parse_factor(parser); @@ -720,6 +720,8 @@ static AstTyped* parse_expression(OnyxParser* parser) { } } + bh_arr_free(tree_stack); + expression_done: return root; }