From: Judah Caruso Date: Tue, 5 Dec 2023 17:57:47 +0000 (-0700) Subject: move builtin operator overloads into separate file; add array math operators X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=094fd7c9ba207a420db79dac107f8a038c483454;p=onyx.git move builtin operator overloads into separate file; add array math operators --- diff --git a/core/builtin.onyx b/core/builtin.onyx index eed4ad27..0a18e339 100644 --- a/core/builtin.onyx +++ b/core/builtin.onyx @@ -577,12 +577,8 @@ package_id :: #distinct u32 any_package :: cast(package_id) 0 // -// Allow for comparing `package_id`s -#operator == macro (p1, p2: package_id) => cast(u32) p1 == cast(u32) p2; -#operator != macro (p1, p2: package_id) => cast(u32) p1 != cast(u32) p2; - -#operator == macro (l, r: [$N]$T) => core.intrinsics.wasm.memory_equal(cast(rawptr)l, cast(rawptr)r, N * sizeof T); -#operator != macro (l, r: [$N]$T) => !(l == r); +// Load builtin operator overloads. +#load "./operations.onyx" // // DEPRECATED THINGS diff --git a/core/operations.onyx b/core/operations.onyx new file mode 100644 index 00000000..6e203f65 --- /dev/null +++ b/core/operations.onyx @@ -0,0 +1,28 @@ +package builtin + +// +// This file contains builtin operator overloads. +// It's in a separate file because we need to defer resolution of some definitions +// until certain types are defined (namely builtin code used for math operators). +// + +// +// Allows for comparing `package_id`s +#operator == macro (p1, p2: package_id) => cast(u32) p1 == cast(u32) p2; +#operator != macro (p1, p2: package_id) => cast(u32) p1 != cast(u32) p2; + +// +// Allows for basic array programming support +#operator + (l, r: [$N]$T) => __array_math_op(l, r, [a, b](a + b)); +#operator - (l, r: [$N]$T) => __array_math_op(l, r, [a, b](a - b)); +#operator * (l, r: [$N]$T) => __array_math_op(l, r, [a, b](a * b)); +#operator / (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); +#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; +}