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

322 lines
13 KiB
Rust
Raw Normal View History

2024-06-04 18:47:15 +00:00
use serde::{Deserialize, Serialize};
2024-03-07 11:33:54 +00:00
use crate::{
context::Context,
error::{RuntimeError, ValidationError},
value::ValueInner,
Value,
};
2024-06-17 14:10:06 +00:00
use super::{AbstractNode, Evaluation, ExpectedType, Expression, SourcePosition, Type};
2024-03-07 11:33:54 +00:00
2024-06-04 18:47:15 +00:00
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
2024-03-08 21:14:47 +00:00
pub enum Math {
2024-06-17 14:10:06 +00:00
Add(Expression, Expression),
Subtract(Expression, Expression),
Multiply(Expression, Expression),
Divide(Expression, Expression),
Modulo(Expression, Expression),
2024-03-07 11:33:54 +00:00
}
impl AbstractNode for Math {
2024-04-22 12:25:20 +00:00
fn validate(&self, context: &mut Context, _manage_memory: bool) -> Result<(), ValidationError> {
2024-03-07 11:33:54 +00:00
match self {
2024-03-23 21:51:40 +00:00
Math::Add(left, right) => {
2024-03-25 04:16:55 +00:00
let left_position = left.position();
let left_type = left.expected_type(context)?;
2024-03-23 21:51:40 +00:00
if let Type::Integer | Type::Float | Type::String = left_type {
2024-03-25 04:16:55 +00:00
let right_position = right.position();
let right_type = right.expected_type(context)?;
2024-03-23 21:51:40 +00:00
if let Type::Integer | Type::Float | Type::String = right_type {
Ok(())
} else {
Err(ValidationError::ExpectedIntegerFloatOrString {
actual: right_type,
2024-03-25 04:16:55 +00:00
position: right_position,
2024-03-23 21:51:40 +00:00
})
}
} else {
Err(ValidationError::ExpectedIntegerFloatOrString {
actual: left_type,
2024-03-25 04:16:55 +00:00
position: left_position,
2024-03-23 21:51:40 +00:00
})
}
}
Math::Subtract(left, right)
2024-03-07 11:33:54 +00:00
| Math::Multiply(left, right)
| Math::Divide(left, right)
| Math::Modulo(left, right) => {
2024-03-25 04:16:55 +00:00
let left_position = left.position();
let left_type = left.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 {
2024-03-25 04:16:55 +00:00
let right_position = right.position();
let right_type = right.expected_type(context)?;
2024-03-18 09:39:09 +00:00
if let Type::Integer | Type::Float = right_type {
Ok(())
} else {
2024-03-25 04:16:55 +00:00
Err(ValidationError::ExpectedIntegerOrFloat(right_position))
2024-03-18 09:39:09 +00:00
}
} else {
2024-03-25 04:16:55 +00:00
Err(ValidationError::ExpectedIntegerOrFloat(left_position))
2024-03-07 11:33:54 +00:00
}
}
}
}
2024-06-17 14:10:06 +00:00
fn evaluate(
self,
_context: &mut Context,
_clear_variables: bool,
) -> Result<Evaluation, RuntimeError> {
let run_and_expect_value =
|position: SourcePosition, expression: Expression| -> Result<Value, RuntimeError> {
let action = expression.evaluate(&mut _context.clone(), _clear_variables)?;
let value = if let Evaluation::Return(value) = action {
value
} else {
return Err(RuntimeError::ValidationFailure(
ValidationError::InterpreterExpectedReturn(position),
));
};
2024-03-18 09:39:09 +00:00
2024-06-17 14:10:06 +00:00
Ok(value)
};
2024-06-16 07:12:04 +00:00
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-25 04:16:55 +00:00
let left_position = left.position();
let left_value = run_and_expect_value(left_position, left)?;
let right_position = right.position();
let right_value = run_and_expect_value(right_position, right)?;
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 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)
}
2024-03-23 21:51:40 +00:00
(ValueInner::String(left), ValueInner::String(right)) => {
let mut concatenated = String::with_capacity(left.len() + right.len());
concatenated.extend(left.chars().chain(right.chars()));
Value::string(concatenated)
}
2024-03-18 09:39:09 +00:00
(ValueInner::Integer(_) | ValueInner::Float(_), _) => {
return Err(RuntimeError::ValidationFailure(
2024-03-25 04:16:55 +00:00
ValidationError::ExpectedIntegerOrFloat(right_position),
2024-03-18 09:39:09 +00:00
))
}
_ => {
return Err(RuntimeError::ValidationFailure(
2024-03-25 04:16:55 +00:00
ValidationError::ExpectedIntegerOrFloat(left_position),
2024-03-18 09:39:09 +00:00
))
}
}
2024-03-07 11:33:54 +00:00
}
2024-03-07 17:33:30 +00:00
Math::Subtract(left, right) => {
2024-03-25 04:16:55 +00:00
let left_position = left.position();
let left_value = run_and_expect_value(left_position, left)?;
let right_position = right.position();
let right_value = run_and_expect_value(right_position, right)?;
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 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(
2024-03-25 04:16:55 +00:00
ValidationError::ExpectedIntegerOrFloat(right_position),
2024-03-18 09:39:09 +00:00
))
}
_ => {
return Err(RuntimeError::ValidationFailure(
2024-03-25 04:16:55 +00:00
ValidationError::ExpectedIntegerOrFloat(left_position),
2024-03-18 09:39:09 +00:00
))
}
}
2024-03-07 17:33:30 +00:00
}
2024-03-07 11:33:54 +00:00
Math::Multiply(left, right) => {
2024-03-25 04:16:55 +00:00
let left_position = left.position();
let left_value = run_and_expect_value(left_position, left)?;
let right_position = right.position();
let right_value = run_and_expect_value(right_position, right)?;
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(
2024-03-25 04:16:55 +00:00
ValidationError::ExpectedIntegerOrFloat(right_position).into(),
2024-03-18 09:39:09 +00:00
))
}
_ => {
return Err(RuntimeError::ValidationFailure(
2024-03-25 04:16:55 +00:00
ValidationError::ExpectedIntegerOrFloat(left_position),
2024-03-18 09:39:09 +00:00
))
}
2024-03-07 11:33:54 +00:00
}
}
2024-03-07 17:33:30 +00:00
Math::Divide(left, right) => {
2024-03-25 04:16:55 +00:00
let left_position = left.position();
let left_value = run_and_expect_value(left_position, left)?;
let right_position = right.position();
let right_value = run_and_expect_value(right_position, right)?;
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(
2024-03-25 04:16:55 +00:00
ValidationError::ExpectedIntegerOrFloat(right_position).into(),
2024-03-18 09:39:09 +00:00
))
}
_ => {
return Err(RuntimeError::ValidationFailure(
2024-03-25 04:16:55 +00:00
ValidationError::ExpectedIntegerOrFloat(left_position),
2024-03-18 09:39:09 +00:00
))
}
2024-03-07 17:33:30 +00:00
}
}
Math::Modulo(left, right) => {
2024-03-25 04:16:55 +00:00
let left_position = left.position();
let left_value = run_and_expect_value(left_position, left)?;
let right_position = right.position();
let right_value = run_and_expect_value(right_position, right)?;
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(
2024-03-25 04:16:55 +00:00
ValidationError::ExpectedIntegerOrFloat(right_position).into(),
2024-03-18 09:39:09 +00:00
))
}
_ => {
return Err(RuntimeError::ValidationFailure(
2024-03-25 04:16:55 +00:00
ValidationError::ExpectedIntegerOrFloat(left_position),
2024-03-18 09:39:09 +00:00
))
}
2024-03-07 17:33:30 +00:00
}
}
2024-03-08 17:24:11 +00:00
};
2024-06-17 14:10:06 +00:00
Ok(Evaluation::Return(value))
2024-03-07 11:33:54 +00:00
}
}
2024-06-16 07:12:04 +00:00
impl ExpectedType for Math {
fn expected_type(&self, _context: &mut Context) -> Result<Type, ValidationError> {
match self {
Math::Add(left, right)
| Math::Subtract(left, right)
| Math::Multiply(left, right)
| Math::Divide(left, right)
| Math::Modulo(left, right) => {
let left_type = left.expected_type(_context)?;
let right_type = right.expected_type(_context)?;
if let Type::Float = left_type {
return Ok(Type::Float);
}
if let Type::Float = right_type {
return Ok(Type::Float);
}
Ok(left_type)
}
}
}
}