From: Brendan Hansen Date: Wed, 16 Dec 2020 18:17:27 +0000 (-0600) Subject: added more robustness to builder and array api X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=ad713602e15b7803ac8e1c450fe37a7447899e48;p=onyx.git added more robustness to builder and array api --- diff --git a/core/array.onyx b/core/array.onyx index b367d3a5..18c6f0f8 100644 --- a/core/array.onyx +++ b/core/array.onyx @@ -21,21 +21,25 @@ clear :: proc (arr: ^[..] $T) { arr.count = 0; } -ensure_capacity :: proc (arr: ^[..] $T, cap: u32) { - if arr.capacity >= cap do return; +ensure_capacity :: proc (arr: ^[..] $T, cap: u32) -> bool { + if arr.capacity >= cap do return true; while cap > arr.capacity do arr.capacity <<= 1; - arr.data = cresize(arr.data, sizeof T * arr.capacity); + new_data := cresize(arr.data, sizeof T * arr.capacity); + if new_data == null do return false; + arr.data = new_data; + return true; } -push :: proc (arr: ^[..] $T, x: T) { - ensure_capacity(arr, arr.count + 1); +push :: proc (arr: ^[..] $T, x: T) -> bool { + if !ensure_capacity(arr, arr.count + 1) do return false; arr.data[arr.count] = x; arr.count += 1; + return true; } -insert :: proc (arr: ^[..] $T, idx: u32, x: T) { - ensure_capacity(arr, arr.count + 1); +insert :: proc (arr: ^[..] $T, idx: u32, x: T) -> bool { + if !ensure_capacity(arr, arr.count + 1) do return false; arr.count += 1; while i := arr.count; i > idx { @@ -44,6 +48,7 @@ insert :: proc (arr: ^[..] $T, idx: u32, x: T) { } arr.data[idx] = x; + return true; } remove :: proc (arr: ^[..] $T, elem: T) { diff --git a/core/string/builder.onyx b/core/string/builder.onyx index 37372e1a..69ee4d5a 100644 --- a/core/string/builder.onyx +++ b/core/string/builder.onyx @@ -31,7 +31,7 @@ add_str :: proc (use sb: ^Builder, s: str) -> ^Builder { if data.capacity < len_total do #context_scope { context.allocator = alloc; - array.ensure_capacity(^data, len_total); + if !array.ensure_capacity(^data, len_total) do return sb; } for i: 0 .. s.count do data[data.count + i] = s[i]; diff --git a/core/string/reader.onyx b/core/string/reader.onyx index f1dcaa78..b7bc8a7e 100644 --- a/core/string/reader.onyx +++ b/core/string/reader.onyx @@ -157,7 +157,7 @@ advance_line :: proc (use reader: ^StringReader) { if count == 0 do return; adv := 0; - while data[adv] != #char "\n" do adv += 1; + while data[adv] != #char "\n" && adv <= count - 1 do adv += 1; data += adv + 1; count -= adv + 1; diff --git a/onyx b/onyx index 51cdf8ea..9703240b 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/odin_example.onyx b/progs/odin_example.onyx index e3dea714..dcf5583a 100644 --- a/progs/odin_example.onyx +++ b/progs/odin_example.onyx @@ -91,9 +91,5 @@ main :: proc (args: [] cstr) { } } - print("The program \""); - print(program); - print("\" calculates the value "); - print(accumulator); - print("\n"); + printf("The program \"%s\" calculates the value %i\n", program, accumulator); } diff --git a/src/onyxparser.c b/src/onyxparser.c index dd76093d..47052f15 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -1251,15 +1251,18 @@ static AstNode* parse_statement(OnyxParser* parser) { assignment->right = builtin_context_variable; context_tmp->next = (AstNode *) assignment; - AstBlock* context_block = parse_block(parser); - needs_semicolon = 0; - assignment->next = (AstNode *) context_block; - AstBinaryOp* assignment2 = make_node(AstBinaryOp, Ast_Kind_Binary_Op); assignment2->operation = Binary_Op_Assign; assignment2->left = builtin_context_variable; assignment2->right = (AstTyped *) context_tmp; - context_block->next = (AstNode *) assignment2; + + AstDefer* defer_node = make_node(AstDefer, Ast_Kind_Defer); + defer_node->stmt = (AstNode *) assignment2; + assignment->next = (AstNode *) defer_node; + + AstBlock* context_block = parse_block(parser); + needs_semicolon = 0; + defer_node->next = (AstNode *) context_block; retval = (AstNode *) context_tmp; break;