From 57b05a815b1054097a90f7683a01c6cbf3c0df50 Mon Sep 17 00:00:00 2001 From: Judah Caruso Date: Tue, 5 Dec 2023 11:30:52 -0700 Subject: [PATCH] add array w/ scalar operations; update tests --- core/operations.onyx | 35 ++++++++++++++++++++++++++--------- scripts/run_tests.onyx | 4 ++-- tests/array_programming | 7 +++++++ tests/array_programming.onyx | 25 +++++++++++++++++++++++++ tests/bugs/array_lengths | 4 ++++ tests/bugs/array_lengths.onyx | 8 ++++---- 6 files changed, 68 insertions(+), 15 deletions(-) create mode 100644 tests/array_programming create mode 100644 tests/array_programming.onyx create mode 100644 tests/bugs/array_lengths diff --git a/core/operations.onyx b/core/operations.onyx index 0d12172d..d91592a1 100644 --- a/core/operations.onyx +++ b/core/operations.onyx @@ -1,5 +1,7 @@ package builtin +use core { intrinsics } + // // This file contains builtin operator overloads. // It's in a separate file because we need to defer resolution of some definitions @@ -13,15 +15,30 @@ package builtin // // Allows for basic array programming support -#operator + macro (l, r: [$N]$T) => __array_math_op(l, r, [a, b](a + b)); -#operator - macro (l, r: [$N]$T) => __array_math_op(l, r, [a, b](a - b)); -#operator * macro (l, r: [$N]$T) => __array_math_op(l, r, [a, b](a * b)); -#operator / macro (l, r: [$N]$T) => __array_math_op(l, r, [a, b](a / b)); -#operator == macro (l, r: [$N]$T) => core.intrinsics.wasm.memory_equal(cast(rawptr)l, cast(rawptr)r, N * sizeof T); + +// Array w/ array operations +#operator + macro (l, r: [$N]$T) => __array_op_array(l, r, [a, b](a + b)); +#operator - macro (l, r: [$N]$T) => __array_op_array(l, r, [a, b](a - b)); +#operator * macro (l, r: [$N]$T) => __array_op_array(l, r, [a, b](a * b)); +#operator / macro (l, r: [$N]$T) => __array_op_array(l, r, [a, b](a / b)); + +// Array w/ scalar operations +#operator + macro (l: [$N]$T, r: T) => __array_op_scalar(l, r, [a, b](a + b)); +#operator - macro (l: [$N]$T, r: T) => __array_op_scalar(l, r, [a, b](a - b)); +#operator * macro (l: [$N]$T, r: T) => __array_op_scalar(l, r, [a, b](a * b)); +#operator / macro (l: [$N]$T, r: T) => __array_op_scalar(l, r, [a, b](a / b)); + +#operator == macro (l, r: [$N]$T) => intrinsics.wasm.memory_equal(cast(rawptr)l, cast(rawptr)r, N * sizeof T); #operator != macro (l, r: [$N]$T) => !(l == r); -#local __array_math_op :: macro (l, r: [$N]$T, $body: Code) -> [N]T { - res: [N]T; - for 0..N do res[it] = #unquote body(l[it], r[it]); - return res; +__array_op_array :: macro (l, r: [$N]$T, $body: Code) -> [N]T { + res: [N]T; + for 0..N do res[it] = #unquote body(l[it], r[it]); + return res; +} + +__array_op_scalar :: macro (l: [$N]$T, r: T, $body: Code) -> [N]T { + res: [N]T; + for 0..N do res[it] = #unquote body(l[it], r); + return res; } diff --git a/scripts/run_tests.onyx b/scripts/run_tests.onyx index 190ee0ee..87b870ba 100644 --- a/scripts/run_tests.onyx +++ b/scripts/run_tests.onyx @@ -106,7 +106,7 @@ main :: (args) => { sync.mutex_init(&exec_context.failed_tests_mutex); switch runtime.compiler_os { - case .Linux { + case .Linux, .MacOS { exec_context.onyx_cmd = "./dist/bin/onyx"; if settings.debug do exec_context.onyx_cmd = "./bin/onyx-debug"; } @@ -174,7 +174,7 @@ main :: (args) => { for failed_tests { printf(" {}\n", it); } - + os.exit(-1); } else { diff --git a/tests/array_programming b/tests/array_programming new file mode 100644 index 00000000..5cfe51a3 --- /dev/null +++ b/tests/array_programming @@ -0,0 +1,7 @@ +true +true +[ 2, 4, 5, 8 ] +[ 2, 4, 6, 8 ] +true +true +true diff --git a/tests/array_programming.onyx b/tests/array_programming.onyx new file mode 100644 index 00000000..c4f8e859 --- /dev/null +++ b/tests/array_programming.onyx @@ -0,0 +1,25 @@ +#load "core/module" + +use core {*} + +main :: () { + a := i32.[ 1, 2, 3, 4 ]; + b := i32.[ 1, 2, 3, 4 ]; + + println(a == b); + b.z = 2; + println(a != b); + + c := a + b; + println(c); + + d := a * 2; + println(d); + + e := d / 2; + println(e == a); + + f := a - a; + println(f.count == a.count); + println(f == i32.[ 0, 0, 0, 0 ]); +} diff --git a/tests/bugs/array_lengths b/tests/bugs/array_lengths new file mode 100644 index 00000000..1140ff52 --- /dev/null +++ b/tests/bugs/array_lengths @@ -0,0 +1,4 @@ +true +true +true +true diff --git a/tests/bugs/array_lengths.onyx b/tests/bugs/array_lengths.onyx index 62c4e035..4a5e2ada 100644 --- a/tests/bugs/array_lengths.onyx +++ b/tests/bugs/array_lengths.onyx @@ -21,9 +21,9 @@ main :: () { arr1: [cast(i32)(3 * 4 - 5)]f32; arr2: [cast(i32)An_Enum.Count]A_Struct; - assert(arr1.count == (3 * 4 - 5), "invalid count for arr1"); - assert(sizeof(typeof(arr1)) == (3 * 4 - 5) * sizeof(f32), "invalid size for arr1"); + println(arr1.count == (3 * 4 - 5)); + println(sizeof(typeof(arr1)) == (3 * 4 - 5) * sizeof(f32)); - assert(arr2.count == ~~An_Enum.Count, "invalid count for arr2"); - assert(sizeof(typeof(arr2)) == ~~An_Enum.Count * sizeof(A_Struct), "invalid size for arr2"); + println(arr2.count == ~~An_Enum.Count); + println(sizeof(typeof(arr2)) == ~~An_Enum.Count * sizeof(A_Struct)); } -- 2.25.1