Operator overloading for + - * / % & | ^ << >> ~ == !=

This commit is contained in:
Christoffer Lerno
2025-04-11 18:46:22 +02:00
parent 28fc03c376
commit 0f2d425297
16 changed files with 601 additions and 122 deletions

View File

@@ -0,0 +1,47 @@
module operator_overload;
import std;
struct Abc
{ int a; }
int b;
fn bool Abc.neq(self, Abc abc) @operator(!=) => self.a != abc.a;
fn bool Abc.eq(self, Abc abc) @operator(==) => self.a == abc.a;
fn Abc Abc.plus(self, Abc abc) @operator(+) => { self.a + abc.a };
fn Abc Abc.plus2(self, int i) @operator(+) => { self.a + i };
fn Abc Abc.minus2(self, int i) @operator(-) => { self.a - i };
fn Abc int.plus_abc(self, Abc abc) @operator(+) => { self + abc.a };
fn int int.mul_abc(self, Abc abc) @operator(*) => self * abc.a;
fn Abc Abc.negate(self) @operator(~) => { ~self.a };
fn Abc Abc.negate2(self) @operator(-) => { -self.a };
fn Abc Abc.shr2(self, int x) @operator(>>) => { self.a >> x };
fn Abc Abc.shl2(self, int x) @operator(<<) @local { b++; return { self.a << x }; }
fn Abc Abc.shl(self, Abc x) @operator(<<) => { self.a << x.a };
fn Abc Abc.div(self, Abc abc) @operator(/) => { self.a / abc.a };
fn Abc Abc.rem(self, Abc abc) @operator(%) => { self.a % abc.a };
fn Abc Abc.xor(self, Abc abc) @operator(^) => { self.a ^ abc.a };
fn Abc Abc.or(self, Abc abc) @operator(|) => { self.a | abc.a };
fn Abc Abc.and(self, Abc abc) @operator(&) => { self.a & abc.a };
fn void test_struct_overload() @test
{
Abc x = { 2 };
Abc y = { 3 };
assert(x != y);
assert(x == x);
assert(x + 2 == { 4 });
assert(x - 2 == { 0 });
assert(2 + x == { 4 });
assert(3 * x == 6);
assert(~x == { -3 });
assert(-x == { -2 });
assert(+x == { 2 });
assert(y >> 1 == { 1 });
assert(y << x == { 12 });
assert(x << 4 == { 32 });
assert((Abc) { 123 } / x == { 61 });
assert((Abc) { 123 } % x == { 1 });
assert(x ^ y == { 1 });
assert(x | y == { 3 });
assert(x & y == { 2 });
}

View File

@@ -8,6 +8,7 @@ fn void test_mat4()
Matrix4 mat2 = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
Matrix4 calc = mat.mul(mat2);
assert(calc.m == mat.m);
assert(mat * mat2 == mat);
Matrix4 translated = mat.translate({0.0, 0.0, 0.0});
assert(translated.m == mat.m);
@@ -19,6 +20,7 @@ fn void test_mat4()
Matrix4 calc = mat.mul(mat2);
Matrix4 value = { 56, 46, 36, 26, 152, 126, 100, 74, 56, 46, 36, 26, 152, 126, 100, 74 };
assert(calc.m == value.m);
assert(mat * mat2 == value);
};
{