1
0
dust/src/abstract_tree/assignment.rs

181 lines
6.5 KiB
Rust
Raw Normal View History

2023-10-06 17:32:58 +00:00
use serde::{Deserialize, Serialize};
use crate::{
2024-02-10 23:29:11 +00:00
context::Context,
2024-01-31 18:51:48 +00:00
error::{RuntimeError, SyntaxError, ValidationError},
AbstractTree, AssignmentOperator, Format, Identifier, SourcePosition, Statement, SyntaxNode,
Type, TypeSpecification, Value,
};
2023-10-06 17:32:58 +00:00
2024-01-30 23:13:30 +00:00
/// Variable assignment, including add-assign and subtract-assign operations.
2023-10-06 17:32:58 +00:00
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Assignment {
identifier: Identifier,
type_specification: Option<TypeSpecification>,
2023-10-09 19:54:47 +00:00
operator: AssignmentOperator,
2023-10-06 17:32:58 +00:00
statement: Statement,
2024-02-01 00:07:18 +00:00
syntax_position: SourcePosition,
2023-10-06 17:32:58 +00:00
}
impl AbstractTree for Assignment {
2024-01-31 18:51:48 +00:00
fn from_syntax(
syntax_node: SyntaxNode,
source: &str,
2024-02-10 23:29:11 +00:00
context: &Context,
2024-01-31 18:51:48 +00:00
) -> Result<Self, SyntaxError> {
2024-02-01 00:35:27 +00:00
SyntaxError::expect_syntax_node(source, "assignment", syntax_node)?;
2023-11-15 01:41:57 +00:00
let child_count = syntax_node.child_count();
2023-11-30 14:30:25 +00:00
let identifier_node = syntax_node.child(0).unwrap();
2024-01-10 20:03:52 +00:00
let identifier = Identifier::from_syntax(identifier_node, source, context)?;
2023-10-06 17:32:58 +00:00
let type_node = syntax_node.child(1).unwrap();
let type_specification = if type_node.kind() == "type_specification" {
Some(TypeSpecification::from_syntax(type_node, source, context)?)
2023-12-29 19:35:52 +00:00
} else {
2023-12-30 03:39:50 +00:00
None
2023-11-27 15:27:44 +00:00
};
2024-01-06 13:11:09 +00:00
let operator_node = syntax_node.child(child_count - 2).unwrap();
2024-01-10 20:03:52 +00:00
let operator = AssignmentOperator::from_syntax(operator_node, source, context)?;
2023-10-09 19:54:47 +00:00
let statement_node = syntax_node.child(child_count - 1).unwrap();
2024-01-10 20:03:52 +00:00
let statement = Statement::from_syntax(statement_node, source, context)?;
2024-01-04 00:57:06 +00:00
Ok(Assignment {
identifier,
type_specification,
2024-01-04 00:57:06 +00:00
operator,
statement,
syntax_position: syntax_node.range().into(),
2024-01-04 00:57:06 +00:00
})
}
2024-02-10 23:29:11 +00:00
fn validate(&self, source: &str, context: &Context) -> Result<(), ValidationError> {
2024-02-01 01:52:34 +00:00
if let AssignmentOperator::Equal = self.operator {
let key = self.identifier.inner().clone();
let r#type = if let Some(definition) = &self.type_specification {
definition.inner().clone()
} else {
self.statement.expected_type(context)?
};
context.set_type(key, r#type)?;
}
if let Some(type_specification) = &self.type_specification {
2024-01-04 00:57:06 +00:00
match self.operator {
2023-11-30 10:40:39 +00:00
AssignmentOperator::Equal => {
2024-02-01 00:07:18 +00:00
let expected = type_specification.inner();
let actual = self.statement.expected_type(context)?;
if !expected.accepts(&actual) {
return Err(ValidationError::TypeCheck {
expected: expected.clone(),
actual,
position: self.syntax_position,
});
}
2023-11-30 10:40:39 +00:00
}
AssignmentOperator::PlusEqual => {
2024-02-01 00:07:18 +00:00
if let Type::List(expected) = type_specification.inner() {
let actual = self.identifier.expected_type(context)?;
if !expected.accepts(&actual) {
return Err(ValidationError::TypeCheck {
expected: expected.as_ref().clone(),
actual,
position: self.syntax_position,
});
}
2023-11-30 10:40:39 +00:00
} else {
2024-02-01 00:07:18 +00:00
let expected = type_specification.inner();
let actual = self.identifier.expected_type(context)?;
if !expected.accepts(&actual) {
return Err(ValidationError::TypeCheck {
expected: expected.clone(),
actual,
position: self.syntax_position,
});
}
2023-12-02 07:34:23 +00:00
}
2023-11-30 10:40:39 +00:00
}
AssignmentOperator::MinusEqual => todo!(),
}
2023-12-17 00:40:14 +00:00
} else {
2024-01-04 00:57:06 +00:00
match self.operator {
2024-01-17 15:21:00 +00:00
AssignmentOperator::Equal => {}
AssignmentOperator::PlusEqual => {
2024-02-01 00:07:18 +00:00
if let Type::List(expected) = self.identifier.expected_type(context)? {
let actual = self.statement.expected_type(context)?;
if !expected.accepts(&actual) {
return Err(ValidationError::TypeCheck {
expected: expected.as_ref().clone(),
actual,
position: self.syntax_position,
});
}
}
}
AssignmentOperator::MinusEqual => todo!(),
2023-12-17 00:40:14 +00:00
}
2023-11-30 10:40:39 +00:00
}
2024-02-01 01:52:34 +00:00
self.statement.validate(source, context)?;
2023-12-17 01:17:38 +00:00
2024-01-04 00:57:06 +00:00
Ok(())
2023-10-06 17:32:58 +00:00
}
2024-02-11 00:31:47 +00:00
fn run(&self, source: &str, context: &Context) -> Result<Value, RuntimeError> {
2023-11-27 22:53:12 +00:00
let key = self.identifier.inner();
2023-10-23 20:12:43 +00:00
let value = self.statement.run(source, context)?;
2023-10-09 19:54:47 +00:00
2023-10-23 20:12:43 +00:00
let new_value = match self.operator {
2023-10-09 19:54:47 +00:00
AssignmentOperator::PlusEqual => {
2024-02-11 00:31:47 +00:00
if let Some(mut previous_value) = context.get_value(key)? {
2023-10-23 20:12:43 +00:00
previous_value += value;
previous_value
} else {
2024-02-01 00:07:18 +00:00
return Err(RuntimeError::VariableIdentifierNotFound(key.clone()));
2023-10-09 19:54:47 +00:00
}
}
AssignmentOperator::MinusEqual => {
2024-02-11 00:31:47 +00:00
if let Some(mut previous_value) = context.get_value(key)? {
2023-10-23 20:12:43 +00:00
previous_value -= value;
previous_value
} else {
2024-02-01 00:07:18 +00:00
return Err(RuntimeError::VariableIdentifierNotFound(key.clone()));
2023-10-09 19:54:47 +00:00
}
}
2023-11-30 10:40:39 +00:00
AssignmentOperator::Equal => value,
};
2023-10-06 17:32:58 +00:00
2024-02-11 00:31:47 +00:00
context.set_value(key.clone(), new_value)?;
2023-10-06 17:32:58 +00:00
2023-12-31 14:14:43 +00:00
Ok(Value::none())
2023-10-06 17:32:58 +00:00
}
2023-11-30 00:23:42 +00:00
2024-02-11 00:31:47 +00:00
fn expected_type(&self, _context: &Context) -> Result<Type, ValidationError> {
2023-12-26 22:19:12 +00:00
Ok(Type::None)
2023-11-30 00:23:42 +00:00
}
2023-10-06 17:32:58 +00:00
}
2024-01-06 10:00:36 +00:00
2024-01-06 13:11:09 +00:00
impl Format for Assignment {
fn format(&self, output: &mut String, indent_level: u8) {
self.identifier.format(output, indent_level);
2024-01-06 10:00:36 +00:00
if let Some(type_specification) = &self.type_specification {
type_specification.format(output, indent_level);
2024-01-06 10:00:36 +00:00
}
2024-01-10 20:07:27 +00:00
output.push(' ');
2024-01-06 13:11:09 +00:00
self.operator.format(output, indent_level);
2024-01-10 20:07:27 +00:00
output.push(' ');
2024-01-06 10:00:36 +00:00
2024-01-06 13:53:31 +00:00
self.statement.format(output, 0);
2024-01-06 10:00:36 +00:00
}
}