dust/dust-lang/src/abstract_tree/math.rs

259 lines
11 KiB
Rust
Raw Normal View History

2024-03-07 11:33:54 +00:00
use crate::{
context::Context,
error::{RuntimeError, ValidationError},
value::ValueInner,
Value,
};
use super::{AbstractNode, Action, Expression, SourcePosition, Type, WithPosition};
2024-03-07 11:33:54 +00:00
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
2024-03-08 21:14:47 +00:00
pub enum Math {
2024-03-17 11:31:45 +00:00
Add(WithPosition<Expression>, WithPosition<Expression>),
Subtract(WithPosition<Expression>, WithPosition<Expression>),
Multiply(WithPosition<Expression>, WithPosition<Expression>),
Divide(WithPosition<Expression>, WithPosition<Expression>),
Modulo(WithPosition<Expression>, WithPosition<Expression>),
2024-03-07 11:33:54 +00:00
}
impl AbstractNode for Math {
2024-03-07 11:33:54 +00:00
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
match self {
Math::Add(left, _)
| Math::Subtract(left, _)
| Math::Multiply(left, _)
| Math::Divide(left, _)
2024-03-17 10:26:12 +00:00
| Math::Modulo(left, _) => left.node.expected_type(_context),
2024-03-07 11:33:54 +00:00
}
}
fn validate(&self, context: &Context) -> Result<(), ValidationError> {
match self {
Math::Add(left, right)
| Math::Subtract(left, right)
| Math::Multiply(left, right)
| Math::Divide(left, right)
| Math::Modulo(left, right) => {
2024-03-17 10:26:12 +00:00
let left_type = left.node.expected_type(context)?;
let right_type = right.node.expected_type(context)?;
2024-03-07 11:33:54 +00:00
2024-03-18 09:39:09 +00:00
if let Type::Integer | Type::Float = left_type {
if let Type::Integer | Type::Float = right_type {
Ok(())
} else {
Err(ValidationError::ExpectedIntegerOrFloat(right.position))
}
} else {
Err(ValidationError::ExpectedIntegerOrFloat(left.position))
2024-03-07 11:33:54 +00:00
}
}
}
}
2024-03-08 17:24:11 +00:00
fn run(self, _context: &Context) -> Result<Action, RuntimeError> {
2024-03-18 09:39:09 +00:00
let run_and_expect_value =
|expression: Expression, position: SourcePosition| -> Result<Value, RuntimeError> {
let action = expression.run(_context)?;
let value = if let Action::Return(value) = action {
value
} else {
return Err(RuntimeError::ValidationFailure(
ValidationError::InterpreterExpectedReturn(position),
));
};
Ok(value)
};
2024-03-08 17:24:11 +00:00
let value = match self {
2024-03-07 11:33:54 +00:00
Math::Add(left, right) => {
2024-03-18 09:39:09 +00:00
let left_value = run_and_expect_value(left.node, left.position)?;
let right_value = run_and_expect_value(right.node, right.position)?;
match (left_value.inner().as_ref(), right_value.inner().as_ref()) {
(ValueInner::Integer(left), ValueInner::Integer(right)) => {
let sum = left.saturating_add(*right);
Value::integer(sum)
}
(ValueInner::Float(left), ValueInner::Float(right)) => {
let sum = left + right;
Value::float(sum)
}
(ValueInner::Float(left), ValueInner::Integer(right)) => {
let sum = left + *right as f64;
Value::float(sum)
}
(ValueInner::Integer(left), ValueInner::Float(right)) => {
let sum = *left as f64 + right;
2024-03-07 11:33:54 +00:00
2024-03-18 09:39:09 +00:00
Value::float(sum)
}
(ValueInner::Integer(_) | ValueInner::Float(_), _) => {
return Err(RuntimeError::ValidationFailure(
ValidationError::ExpectedIntegerOrFloat(right.position),
))
}
_ => {
return Err(RuntimeError::ValidationFailure(
ValidationError::ExpectedIntegerOrFloat(left.position),
))
}
}
2024-03-07 11:33:54 +00:00
}
2024-03-07 17:33:30 +00:00
Math::Subtract(left, right) => {
2024-03-18 09:39:09 +00:00
let left_value = run_and_expect_value(left.node, left.position)?;
let right_value = run_and_expect_value(right.node, right.position)?;
match (left_value.inner().as_ref(), right_value.inner().as_ref()) {
(ValueInner::Integer(left), ValueInner::Integer(right)) => {
let difference = left.saturating_sub(*right);
Value::integer(difference)
}
(ValueInner::Float(left), ValueInner::Float(right)) => {
let difference = left - right;
Value::float(difference)
}
(ValueInner::Float(left), ValueInner::Integer(right)) => {
let difference = left - *right as f64;
Value::float(difference)
}
(ValueInner::Integer(left), ValueInner::Float(right)) => {
let difference = *left as f64 - right;
2024-03-07 17:33:30 +00:00
2024-03-18 09:39:09 +00:00
Value::float(difference)
}
(ValueInner::Integer(_) | ValueInner::Float(_), _) => {
return Err(RuntimeError::ValidationFailure(
ValidationError::ExpectedIntegerOrFloat(right.position),
))
}
_ => {
return Err(RuntimeError::ValidationFailure(
ValidationError::ExpectedIntegerOrFloat(left.position),
))
}
}
2024-03-07 17:33:30 +00:00
}
2024-03-07 11:33:54 +00:00
Math::Multiply(left, right) => {
2024-03-18 09:39:09 +00:00
let left_value = run_and_expect_value(left.node, left.position)?;
let right_value = run_and_expect_value(right.node, right.position)?;
2024-03-07 11:33:54 +00:00
2024-03-18 09:39:09 +00:00
match (left_value.inner().as_ref(), right_value.inner().as_ref()) {
(ValueInner::Integer(left), ValueInner::Integer(right)) => {
let product = left.saturating_mul(*right);
Value::integer(product)
}
(ValueInner::Float(left), ValueInner::Float(right)) => {
let product = left * right;
Value::float(product)
}
(ValueInner::Float(left), ValueInner::Integer(right)) => {
let product = left * *right as f64;
Value::float(product)
}
(ValueInner::Integer(left), ValueInner::Float(right)) => {
let product = *left as f64 * right;
Value::float(product)
}
(ValueInner::Integer(_) | ValueInner::Float(_), _) => {
return Err(RuntimeError::ValidationFailure(
ValidationError::ExpectedIntegerOrFloat(right.position).into(),
))
}
_ => {
return Err(RuntimeError::ValidationFailure(
ValidationError::ExpectedIntegerOrFloat(left.position),
))
}
2024-03-07 11:33:54 +00:00
}
}
2024-03-07 17:33:30 +00:00
Math::Divide(left, right) => {
2024-03-18 09:39:09 +00:00
let left_value = run_and_expect_value(left.node, left.position)?;
let right_value = run_and_expect_value(right.node, right.position)?;
2024-03-07 17:33:30 +00:00
2024-03-18 09:39:09 +00:00
match (left_value.inner().as_ref(), right_value.inner().as_ref()) {
(ValueInner::Integer(left), ValueInner::Integer(right)) => {
let quotient = left.saturating_div(*right);
Value::integer(quotient)
}
(ValueInner::Float(left), ValueInner::Float(right)) => {
let quotient = left / right;
Value::float(quotient)
}
(ValueInner::Float(left), ValueInner::Integer(right)) => {
let quotient = left / *right as f64;
Value::float(quotient)
}
(ValueInner::Integer(left), ValueInner::Float(right)) => {
let quotient = *left as f64 / right;
Value::float(quotient)
}
(ValueInner::Integer(_) | ValueInner::Float(_), _) => {
return Err(RuntimeError::ValidationFailure(
ValidationError::ExpectedIntegerOrFloat(right.position).into(),
))
}
_ => {
return Err(RuntimeError::ValidationFailure(
ValidationError::ExpectedIntegerOrFloat(left.position),
))
}
2024-03-07 17:33:30 +00:00
}
}
Math::Modulo(left, right) => {
2024-03-18 09:39:09 +00:00
let left_value = run_and_expect_value(left.node, left.position)?;
let right_value = run_and_expect_value(right.node, right.position)?;
2024-03-07 17:33:30 +00:00
2024-03-18 09:39:09 +00:00
match (left_value.inner().as_ref(), right_value.inner().as_ref()) {
(ValueInner::Integer(left), ValueInner::Integer(right)) => {
let remainder = left % right;
Value::integer(remainder)
}
(ValueInner::Float(left), ValueInner::Float(right)) => {
let remainder = left % right;
Value::float(remainder)
}
(ValueInner::Float(left), ValueInner::Integer(right)) => {
let remainder = left % *right as f64;
Value::float(remainder)
}
(ValueInner::Integer(left), ValueInner::Float(right)) => {
let remainder = *left as f64 % right;
Value::float(remainder)
}
(ValueInner::Integer(_) | ValueInner::Float(_), _) => {
return Err(RuntimeError::ValidationFailure(
ValidationError::ExpectedIntegerOrFloat(right.position).into(),
))
}
_ => {
return Err(RuntimeError::ValidationFailure(
ValidationError::ExpectedIntegerOrFloat(left.position),
))
}
2024-03-07 17:33:30 +00:00
}
}
2024-03-08 17:24:11 +00:00
};
Ok(Action::Return(value))
2024-03-07 11:33:54 +00:00
}
}