bugfixes and more testing
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 29 Aug 2020 03:50:35 +0000 (22:50 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 29 Aug 2020 03:50:35 +0000 (22:50 -0500)
core/builtin.onyx
core/stdio.onyx
core/string.onyx
onyx
progs/poly_test.onyx
src/onyxclone.c

index 0191f775bbc7cce56de59c873626d1829ff08fcf..3224e3036695a70a962c76912bcab4fb8b2bc308 100644 (file)
@@ -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;
index 9fc618e053ffeb160fb39d8d99942eab0bec6a20..5eab4dfea99722a3a52ab3ad60a72174cf6aa0ca 100644 (file)
@@ -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 {
index ac7ab375ee95d18ecb49bb5b404a0f3e999e304b..0cd56794215b101963f7d4a7a38a3a73ecc62e5f 100644 (file)
@@ -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 1ed9b95e71601b88df7cfe7eba62694c5bd728b1..40d74102c540f67517a5b4d90784215375394f1d 100755 (executable)
Binary files a/onyx and b/onyx differ
index 93ef5a1b2eb2c52d7b42e80966f6ccd10d08b7d5..050a3cde7ea55a9102fbcb33895d5e71e492696d 100644 (file)
@@ -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
index 1c95c46e989ff16e9adcc73d7286f929b7eedda4..459d4630adf8388dfdcef3261e50d573cec48cf6 100644 (file)
@@ -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);