diff --git a/src/renderer/math.rs b/src/renderer/math.rs index 0c4456d..cc35f7f 100644 --- a/src/renderer/math.rs +++ b/src/renderer/math.rs @@ -52,7 +52,21 @@ impl Sub for MathNumber { type Output = Option; fn sub(self, other: MathNumber) -> Self::Output { - todo!() + match (self, other) { + (MathNumber::Failure, _) | (_, MathNumber::Failure) => None, + (MathNumber::Integer(self_num), MathNumber::Integer(other_num)) => { + math_ints(self_num, other_num, std::ops::Sub::sub) + } + (MathNumber::Decimal(self_num), MathNumber::Decimal(other_num)) => { + Some(OwnedLiteral::LFloat(self_num - other_num)) + } + (MathNumber::Integer(self_num), MathNumber::Decimal(other_num)) => { + Some(OwnedLiteral::LFloat((self_num as f64) - other_num)) + } + (MathNumber::Decimal(self_num), MathNumber::Integer(other_num)) => { + Some(OwnedLiteral::LFloat(self_num - (other_num as f64))) + } + } } } @@ -60,7 +74,21 @@ impl Mul for MathNumber { type Output = Option; fn mul(self, other: MathNumber) -> Self::Output { - todo!() + match (self, other) { + (MathNumber::Failure, _) | (_, MathNumber::Failure) => None, + (MathNumber::Integer(self_num), MathNumber::Integer(other_num)) => { + math_ints(self_num, other_num, std::ops::Mul::mul) + } + (MathNumber::Decimal(self_num), MathNumber::Decimal(other_num)) => { + Some(OwnedLiteral::LFloat(self_num * other_num)) + } + (MathNumber::Integer(self_num), MathNumber::Decimal(other_num)) => { + Some(OwnedLiteral::LFloat((self_num as f64) * other_num)) + } + (MathNumber::Decimal(self_num), MathNumber::Integer(other_num)) => { + Some(OwnedLiteral::LFloat(self_num * (other_num as f64))) + } + } } } @@ -68,7 +96,21 @@ impl Div for MathNumber { type Output = Option; fn div(self, other: MathNumber) -> Self::Output { - todo!() + match (self, other) { + (MathNumber::Failure, _) | (_, MathNumber::Failure) => None, + (MathNumber::Integer(self_num), MathNumber::Integer(other_num)) => { + Some(OwnedLiteral::LFloat((self_num as f64) / (other_num as f64))) + } + (MathNumber::Decimal(self_num), MathNumber::Decimal(other_num)) => { + Some(OwnedLiteral::LFloat(self_num / other_num)) + } + (MathNumber::Integer(self_num), MathNumber::Decimal(other_num)) => { + Some(OwnedLiteral::LFloat((self_num as f64) / other_num)) + } + (MathNumber::Decimal(self_num), MathNumber::Integer(other_num)) => { + Some(OwnedLiteral::LFloat(self_num / (other_num as f64))) + } + } } } @@ -76,7 +118,21 @@ impl Rem for MathNumber { type Output = Option; fn rem(self, other: MathNumber) -> Self::Output { - todo!() + match (self, other) { + (MathNumber::Failure, _) | (_, MathNumber::Failure) => None, + (MathNumber::Integer(self_num), MathNumber::Integer(other_num)) => { + math_ints(self_num, other_num, std::ops::Rem::rem) + } + (MathNumber::Decimal(self_num), MathNumber::Decimal(other_num)) => { + Some(OwnedLiteral::LFloat(self_num % other_num)) + } + (MathNumber::Integer(self_num), MathNumber::Decimal(other_num)) => { + Some(OwnedLiteral::LFloat((self_num as f64) % other_num)) + } + (MathNumber::Decimal(self_num), MathNumber::Integer(other_num)) => { + Some(OwnedLiteral::LFloat(self_num % (other_num as f64))) + } + } } }