small bug fixes with polymorphic array lengths
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 7 Jan 2021 23:03:35 +0000 (17:03 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 7 Jan 2021 23:03:35 +0000 (17:03 -0600)
bin/onyx
bin/test
onyx.exe
src/onyxclone.c
src/onyxutils.c
tests/polymorphic_array_lengths [new file with mode: 0644]
tests/polymorphic_array_lengths.onyx [new file with mode: 0644]

index a62f136fbe4fdfd9e010430cbb4114a9d35adabb..f1c5173cb19698e24dafb5f29bc347237d5ed0f2 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index d327cc40e947d3d92a47b1aef4d51f2c4b230c0e..e5408703ed592179c2f8ce2cd282946b27d216ec 100755 (executable)
--- a/bin/test
+++ b/bin/test
@@ -1,12 +1,21 @@
 #!/bin/sh
 
+print_check() {
+    if [ ! -z "$TERM " ]; then
+        printf "%-$((($(tput cols) - 8)))s" "⏲ Checking $1.onyx" ;
+    else
+        printf "%s ... " "⏲ Checking $1.onyx" ;
+    fi
+}
+
+
 failed=0
 for test_file in $(find tests/ -name '*.onyx'); do
     filename=$(basename -- "$test_file")
     dirname="$(dirname -- "$test_file")"
     name="${filename%.*}"
 
-    printf "%-$((($(tput cols) - 8)))s" "⏲ Checking $name.onyx"
+    print_check "$name"
 
     if ! ./bin/onyx "$test_file" -o "./tests/$name.wasm" >/dev/null; then
         print "\n❌ Failed to compile $name.onyx.\n"
index fd15f1d2b55c8bbf19c6af23bce4e3f59509ccc1..4a501d357c1dec782e5208537e99526be82a9bf3 100644 (file)
Binary files a/onyx.exe and b/onyx.exe differ
index a982bbcff2458521659fecb01bdd7146c68eae9f..85d7186330efb0a39031bc8517c48ee5d5bc1000 100644 (file)
@@ -399,6 +399,7 @@ AstNode* ast_clone(bh_allocator a, void* n) {
 
                        bh_arr_each(AstPolySolution, sln, sd->known_polyvars) {
                                AstPolySolution new_sln;
+                new_sln.kind     = sln->kind;
                                new_sln.poly_sym = (AstNode *) ast_clone(a, (AstNode *) sln->poly_sym);
                                new_sln.ast_type = (AstType *) ast_clone(a, (AstNode *) sln->ast_type);
                                bh_arr_push(dd->known_polyvars, new_sln);
index 54e09c238fbdff2e1961bb9d20ed6c6759d12a4a..aa9b4f374a3076cb5a61bd52f554deae47d7365c 100644 (file)
@@ -471,6 +471,8 @@ static PolySolveResult solve_poly_type(AstNode* target, AstType* type_expr, Type
             break;
         }
 
+        if (elem.kind != PSK_Type) continue;
+
         switch (elem.type_expr->kind) {
             case Ast_Kind_Pointer_Type: {
                 if (elem.actual->kind != Type_Kind_Pointer) break;
@@ -808,6 +810,12 @@ AstNode* polymorphic_proc_try_solidify(AstPolyProc* pp, bh_arr(AstPolySolution)
         new_pp->flags = pp->flags;
         new_pp->poly_params = pp->poly_params;
 
+        // POTENTIAL BUG: Copying this doesn't feel right...
+        if (pp->concrete_funcs == NULL) {
+            bh_table_init(global_heap_allocator, pp->concrete_funcs, 8);
+        }
+        new_pp->concrete_funcs = pp->concrete_funcs;
+
         new_pp->known_slns = NULL;
         bh_arr_new(global_heap_allocator, new_pp->known_slns, bh_arr_length(pp->known_slns) + bh_arr_length(slns));
 
diff --git a/tests/polymorphic_array_lengths b/tests/polymorphic_array_lengths
new file mode 100644 (file)
index 0000000..6189705
--- /dev/null
@@ -0,0 +1,20 @@
+1 2 3 4 5 0.0
+1.0
+1.4142
+1.7320
+2.0
+2.2360
+2.4494
+2.6457
+2.8284
+3.0
+3.1622
+3.3166
+3.4641
+3.6055
+3.7416
+3.8729
+4.0
+4.1231
+4.2426
+4.3588
diff --git a/tests/polymorphic_array_lengths.onyx b/tests/polymorphic_array_lengths.onyx
new file mode 100644 (file)
index 0000000..787693c
--- /dev/null
@@ -0,0 +1,23 @@
+#load "core/std/js"
+
+use package core
+
+main :: proc (args: [] cstr) {
+    arr := u32.[ 1, 2, 3, 4, 5 ];
+    for elem: array_to_slice(arr) do printf("%i ", elem);
+
+    roots : [20] f32;
+    compute_roots(roots);
+
+    for root: roots do println(root);
+
+    array_to_slice :: proc (arr: [$N] $T) -> [] T {
+        return (#type [] T).{ ~~arr, N };
+    }
+
+    compute_roots :: proc (arr: [$N] f32) {
+        for i: 0 .. N {
+            arr[i] = math.sqrt(cast(f32) i);
+        }
+    }
+}
\ No newline at end of file