add array w/ scalar operations; update tests
authorJudah Caruso <judah@tuta.io>
Tue, 5 Dec 2023 18:30:52 +0000 (11:30 -0700)
committerJudah Caruso <judah@tuta.io>
Tue, 5 Dec 2023 18:30:52 +0000 (11:30 -0700)
core/operations.onyx
scripts/run_tests.onyx
tests/array_programming [new file with mode: 0644]
tests/array_programming.onyx [new file with mode: 0644]
tests/bugs/array_lengths [new file with mode: 0644]
tests/bugs/array_lengths.onyx

index 0d12172db966e143a0f76254ae3d6af408819cec..d91592a1ad5cb1120708598ce00f164d55ef2924 100644 (file)
@@ -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;
 }
index 190ee0ee00665a9300db5407e1e4baaeff7f14f3..87b870ba98f7ad0051b651d1aaa1a21899ed4046 100644 (file)
@@ -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 (file)
index 0000000..5cfe51a
--- /dev/null
@@ -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 (file)
index 0000000..c4f8e85
--- /dev/null
@@ -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 (file)
index 0000000..1140ff5
--- /dev/null
@@ -0,0 +1,4 @@
+true
+true
+true
+true
index 62c4e035ffdac313a09585309d2aa5cf72f7c15d..4a5e2ada796fd309ff269421b07b50b03af87a6a 100644 (file)
@@ -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));
 }