From: Brendan Hansen Date: Thu, 7 Jan 2021 23:03:35 +0000 (-0600) Subject: small bug fixes with polymorphic array lengths X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=41f6910fbf4e102cb35e2d8aef97e10c6309104f;p=onyx.git small bug fixes with polymorphic array lengths --- diff --git a/bin/onyx b/bin/onyx index a62f136f..f1c5173c 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/bin/test b/bin/test index d327cc40..e5408703 100755 --- 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" diff --git a/onyx.exe b/onyx.exe index fd15f1d2..4a501d35 100644 Binary files a/onyx.exe and b/onyx.exe differ diff --git a/src/onyxclone.c b/src/onyxclone.c index a982bbcf..85d71863 100644 --- a/src/onyxclone.c +++ b/src/onyxclone.c @@ -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); diff --git a/src/onyxutils.c b/src/onyxutils.c index 54e09c23..aa9b4f37 100644 --- a/src/onyxutils.c +++ b/src/onyxutils.c @@ -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 index 00000000..6189705e --- /dev/null +++ b/tests/polymorphic_array_lengths @@ -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 index 00000000..787693c7 --- /dev/null +++ b/tests/polymorphic_array_lengths.onyx @@ -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