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

230 lines
7.9 KiB
Rust
Raw Normal View History

use std::fmt::{self, Display, Formatter};
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-26 20:24:41 +00:00
use super::{AbstractNode, Evaluation, Statement, Type, 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:50:06 +00:00
constructor: Option<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:50:06 +00:00
constructor: Option<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),
}
}
}
2024-06-22 00:59:38 +00:00
impl AbstractNode for Assignment {
fn define_types(&self, context: &Context) -> Result<(), ValidationError> {
2024-06-24 04:47:11 +00:00
self.statement.define_types(context)?;
2024-06-24 05:41:16 +00:00
if let Some(constructor) = &self.constructor {
let r#type = constructor.construct(&context)?;
context.set_type(self.identifier.node.clone(), r#type.clone())?;
} else if let Some(r#type) = self.statement.expected_type(context)? {
context.set_type(self.identifier.node.clone(), r#type)?;
2024-06-22 03:37:25 +00:00
} else {
2024-06-22 00:59:38 +00:00
return Err(ValidationError::CannotAssignToNone(
2024-06-26 20:24:41 +00:00
self.statement.last_evaluated_statement().position(),
2024-06-22 00:59:38 +00:00
));
2024-06-22 03:37:25 +00:00
};
2024-06-22 00:59:38 +00:00
Ok(())
}
fn validate(&self, context: &Context, manage_memory: bool) -> Result<(), ValidationError> {
2024-06-24 06:26:19 +00:00
self.statement.validate(context, manage_memory)?;
let statement_type = self.statement.expected_type(context)?;
2024-06-24 06:58:19 +00:00
if statement_type.is_none() {
return Err(ValidationError::CannotAssignToNone(
self.statement.last_evaluated_statement().position(),
));
}
2024-06-24 06:26:19 +00:00
if let (Some(expected_type_constructor), Some(actual_type)) =
(&self.constructor, statement_type)
{
let expected_type = expected_type_constructor.construct(context)?;
expected_type
.check(&actual_type)
.map_err(|conflict| ValidationError::TypeCheck {
conflict,
actual_position: self.statement.last_evaluated_statement().position(),
expected_position: Some(expected_type_constructor.position()),
})?;
}
Ok(())
}
2024-06-22 00:59:38 +00:00
fn evaluate(
2024-06-17 14:10:06 +00:00
self,
2024-06-22 00:59:38 +00:00
context: &Context,
2024-06-17 14:10:06 +00:00
manage_memory: bool,
2024-06-21 22:28:12 +00:00
) -> Result<Option<Evaluation>, RuntimeError> {
2024-06-22 03:37:25 +00:00
let evaluation = self.statement.evaluate(context, manage_memory)?;
2024-06-19 06:32:17 +00:00
let right = match evaluation {
2024-06-21 22:28:12 +00:00
Some(Evaluation::Return(value)) => value,
evaluation => return Ok(evaluation),
2024-03-08 17:24:11 +00:00
};
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-22 03:37:25 +00:00
Ok(None)
2024-02-25 18:49:26 +00:00
}
2024-06-22 00:59:38 +00:00
2024-06-22 03:37:25 +00:00
fn expected_type(&self, _: &Context) -> Result<Option<Type>, ValidationError> {
2024-06-22 00:59:38 +00:00
Ok(None)
}
2024-02-25 18:49:26 +00:00
}
impl Display for Assignment {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let Assignment {
identifier,
constructor,
operator,
statement,
} = self;
write!(f, "{} ", identifier.node)?;
if let Some(constructor) = constructor {
write!(f, ": {constructor} ")?;
}
match operator {
AssignmentOperator::Assign => write!(f, "="),
AssignmentOperator::AddAssign => write!(f, "+="),
AssignmentOperator::SubAssign => write!(f, "-="),
}?;
write!(f, " {statement}")
}
}