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

214 lines
7.7 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
use super::{
2024-06-19 06:32:17 +00:00
AbstractNode, Evaluation, ExpectedType, Expression, 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),
}
}
}
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
if let Type::None = statement_type {
return Err(ValidationError::CannotAssignToNone(
self.statement.position(),
));
}
2024-06-19 06:32:17 +00:00
if let (Some(constructor), Statement::Expression(Expression::FunctionCall(function_call))) =
(&self.constructor, self.statement.as_ref())
{
let declared_type = constructor.clone().construct(context)?;
let function_type = function_call.node.function().expected_type(context)?;
if let Type::Function {
return_type,
type_parameters: Some(type_parameters),
..
} = function_type
{
if let Type::Generic { identifier, .. } = *return_type {
2024-06-19 06:32:17 +00:00
let returned_parameter = type_parameters
.into_iter()
.find(|parameter| parameter == &identifier);
if let Some(parameter) = returned_parameter {
context.set_type(parameter, declared_type)?;
}
}
} else {
return Err(ValidationError::ExpectedFunction {
actual: function_type,
position: function_call.position,
});
}
} else if let Some(constructor) = &self.constructor {
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:50:06 +00:00
expected_position: Some(constructor.position()),
2024-06-17 14:10:06 +00:00
})?;
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> {
2024-06-19 06:32:17 +00:00
let evaluation = self.statement.evaluate(context, manage_memory)?;
let right = match evaluation {
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
}
}