From 7ee7a083aed936bc0cfeac99e2ff5fc96202ddd1 Mon Sep 17 00:00:00 2001 From: Jeff Date: Thu, 7 Mar 2024 12:33:30 -0500 Subject: [PATCH] Add more math for integers --- src/abstract_tree/math.rs | 45 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/src/abstract_tree/math.rs b/src/abstract_tree/math.rs index d22a54c..b6fef36 100644 --- a/src/abstract_tree/math.rs +++ b/src/abstract_tree/math.rs @@ -64,7 +64,20 @@ impl<'src> AbstractTree for Math<'src> { )) } } - Math::Subtract(_, _) => todo!(), + Math::Subtract(left, right) => { + let left_value = left.run(context)?; + let right_value = right.run(context)?; + + if let (ValueInner::Integer(left), ValueInner::Integer(right)) = + (left_value.inner().as_ref(), right_value.inner().as_ref()) + { + Ok(Value::integer(left - right)) + } else { + Err(RuntimeError::ValidationFailure( + ValidationError::ExpectedIntegerOrFloat, + )) + } + } Math::Multiply(left, right) => { let left_value = left.run(context)?; let right_value = right.run(context)?; @@ -79,8 +92,34 @@ impl<'src> AbstractTree for Math<'src> { )) } } - Math::Divide(_, _) => todo!(), - Math::Modulo(_, _) => todo!(), + Math::Divide(left, right) => { + let left_value = left.run(context)?; + let right_value = right.run(context)?; + + if let (ValueInner::Integer(left), ValueInner::Integer(right)) = + (left_value.inner().as_ref(), right_value.inner().as_ref()) + { + Ok(Value::integer(left / right)) + } else { + Err(RuntimeError::ValidationFailure( + ValidationError::ExpectedIntegerOrFloat, + )) + } + } + Math::Modulo(left, right) => { + let left_value = left.run(context)?; + let right_value = right.run(context)?; + + if let (ValueInner::Integer(left), ValueInner::Integer(right)) = + (left_value.inner().as_ref(), right_value.inner().as_ref()) + { + Ok(Value::integer(left % right)) + } else { + Err(RuntimeError::ValidationFailure( + ValidationError::ExpectedIntegerOrFloat, + )) + } + } } } }