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

182 lines
6.5 KiB
Rust
Raw Normal View History

2024-06-04 18:47:15 +00:00
use serde::{Deserialize, Serialize};
use crate::{
error::{RuntimeError, ValidationError},
2024-03-25 04:16:55 +00:00
identifier::Identifier,
2024-03-18 09:39:09 +00:00
value::ValueInner,
Context, Value,
};
2024-02-25 18:49:26 +00:00
2024-06-17 14:10:06 +00:00
use super::{AbstractNode, Evaluation, ExpectedType, Statement, TypeConstructor, WithPosition};
2024-02-25 18:49:26 +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 struct Assignment {
2024-03-18 07:24:41 +00:00
identifier: WithPosition<Identifier>,
2024-06-17 14:10:06 +00:00
constructor: Option<WithPosition<TypeConstructor>>,
operator: AssignmentOperator,
2024-03-25 04:16:55 +00:00
statement: Box<Statement>,
2024-02-25 18:49:26 +00:00
}
2024-06-04 18:47:15 +00:00
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum AssignmentOperator {
Assign,
AddAssign,
SubAssign,
}
2024-03-08 21:14:47 +00:00
impl Assignment {
pub fn new(
2024-03-18 07:24:41 +00:00
identifier: WithPosition<Identifier>,
2024-06-17 14:10:06 +00:00
constructor: Option<WithPosition<TypeConstructor>>,
operator: AssignmentOperator,
2024-03-25 04:16:55 +00:00
statement: Statement,
) -> Self {
2024-02-25 18:49:26 +00:00
Self {
identifier,
2024-06-17 14:10:06 +00:00
constructor,
operator,
2024-02-25 18:49:26 +00:00
statement: Box::new(statement),
}
}
}
impl AbstractNode for Assignment {
2024-04-22 12:25:20 +00:00
fn validate(&self, context: &mut Context, manage_memory: bool) -> Result<(), ValidationError> {
2024-03-25 04:16:55 +00:00
let statement_type = self.statement.expected_type(context)?;
2024-03-17 04:49:01 +00:00
2024-03-17 11:31:45 +00:00
if let Some(WithPosition {
2024-06-17 14:10:06 +00:00
node: constructor,
2024-03-17 04:49:01 +00:00
position: expected_position,
2024-06-17 14:10:06 +00:00
}) = &self.constructor
2024-03-17 04:49:01 +00:00
{
2024-06-17 14:10:06 +00:00
let r#type = constructor.clone().construct(&context)?;
r#type
.check(&statement_type)
.map_err(|conflict| ValidationError::TypeCheck {
2024-03-17 04:49:01 +00:00
conflict,
2024-03-25 04:16:55 +00:00
actual_position: self.statement.position(),
2024-06-17 14:10:06 +00:00
expected_position: Some(expected_position.clone()),
})?;
2024-03-06 23:15:25 +00:00
2024-06-17 14:10:06 +00:00
context.set_type(self.identifier.node.clone(), r#type.clone())?;
2024-03-06 23:15:25 +00:00
} else {
2024-06-16 07:12:04 +00:00
context.set_type(self.identifier.node.clone(), statement_type)?;
2024-03-06 17:15:03 +00:00
}
2024-04-22 11:56:03 +00:00
self.statement.validate(context, manage_memory)?;
2024-03-11 18:49:44 +00:00
2024-03-06 17:15:03 +00:00
Ok(())
}
2024-06-17 14:10:06 +00:00
fn evaluate(
self,
context: &mut Context,
manage_memory: bool,
) -> Result<Evaluation, RuntimeError> {
let action = self.statement.evaluate(context, manage_memory)?;
2024-03-18 09:39:09 +00:00
let right = match action {
2024-06-17 14:10:06 +00:00
Evaluation::Return(value) => value,
2024-03-08 17:24:11 +00:00
r#break => return Ok(r#break),
};
2024-02-25 18:49:26 +00:00
match self.operator {
AssignmentOperator::Assign => {
2024-06-16 07:12:04 +00:00
context.set_value(self.identifier.node, right)?;
}
AssignmentOperator::AddAssign => {
2024-04-22 11:56:03 +00:00
let left_option = if manage_memory {
2024-06-16 07:12:04 +00:00
context.use_value(&self.identifier.node)?
2024-04-22 11:56:03 +00:00
} else {
2024-06-16 07:12:04 +00:00
context.get_value(&self.identifier.node)?
2024-04-22 11:56:03 +00:00
};
if let Some(left) = left_option {
2024-03-18 09:39:09 +00:00
let new_value = match (left.inner().as_ref(), right.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;
Value::float(sum)
}
_ => {
return Err(RuntimeError::ValidationFailure(
ValidationError::ExpectedIntegerOrFloat(self.identifier.position),
))
}
};
2024-06-16 07:12:04 +00:00
context.set_value(self.identifier.node, new_value)?;
} else {
return Err(RuntimeError::ValidationFailure(
2024-03-25 04:16:55 +00:00
ValidationError::VariableNotFound {
2024-06-16 07:12:04 +00:00
identifier: self.identifier.node,
2024-03-25 04:16:55 +00:00
position: self.identifier.position,
},
));
}
}
AssignmentOperator::SubAssign => {
2024-04-22 11:56:03 +00:00
let left_option = if manage_memory {
2024-06-16 07:12:04 +00:00
context.use_value(&self.identifier.node)?
2024-04-22 11:56:03 +00:00
} else {
2024-06-16 07:12:04 +00:00
context.get_value(&self.identifier.node)?
2024-04-22 11:56:03 +00:00
};
if let Some(left) = left_option {
2024-03-18 09:39:09 +00:00
let new_value = match (left.inner().as_ref(), right.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;
Value::float(difference)
}
_ => {
return Err(RuntimeError::ValidationFailure(
ValidationError::ExpectedIntegerOrFloat(self.identifier.position),
))
}
};
2024-06-16 07:12:04 +00:00
context.set_value(self.identifier.node, new_value)?;
} else {
return Err(RuntimeError::ValidationFailure(
2024-03-25 04:16:55 +00:00
ValidationError::VariableNotFound {
2024-06-16 07:12:04 +00:00
identifier: self.identifier.node,
2024-03-25 04:16:55 +00:00
position: self.identifier.position,
},
));
}
}
}
2024-02-25 18:49:26 +00:00
2024-06-17 14:10:06 +00:00
Ok(Evaluation::None)
2024-02-25 18:49:26 +00:00
}
}