2023-10-06 17:32:58 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2024-01-06 03:26:37 +00:00
|
|
|
use crate::{
|
|
|
|
AbstractTree, Error, Identifier, Map, Result, Statement, SyntaxNode, SyntaxPosition, Type,
|
|
|
|
TypeDefinition, Value,
|
|
|
|
};
|
2023-10-06 17:32:58 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
|
|
|
pub struct Assignment {
|
|
|
|
identifier: Identifier,
|
2023-11-30 05:57:15 +00:00
|
|
|
type_definition: Option<TypeDefinition>,
|
2023-10-09 19:54:47 +00:00
|
|
|
operator: AssignmentOperator,
|
2023-10-06 17:32:58 +00:00
|
|
|
statement: Statement,
|
2024-01-06 03:26:37 +00:00
|
|
|
syntax_position: SyntaxPosition,
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 19:54:47 +00:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
|
|
|
pub enum AssignmentOperator {
|
|
|
|
Equal,
|
|
|
|
PlusEqual,
|
|
|
|
MinusEqual,
|
|
|
|
}
|
|
|
|
|
2023-10-06 17:32:58 +00:00
|
|
|
impl AbstractTree for Assignment {
|
2024-01-06 03:26:37 +00:00
|
|
|
fn from_syntax_node(source: &str, syntax_node: SyntaxNode, context: &Map) -> Result<Self> {
|
|
|
|
Error::expect_syntax_node(source, "assignment", syntax_node)?;
|
2023-11-15 01:41:57 +00:00
|
|
|
|
2024-01-06 03:26:37 +00:00
|
|
|
let child_count = syntax_node.child_count();
|
2023-11-30 14:30:25 +00:00
|
|
|
|
2024-01-06 03:26:37 +00:00
|
|
|
let identifier_node = syntax_node.child(0).unwrap();
|
2023-11-30 03:54:46 +00:00
|
|
|
let identifier = Identifier::from_syntax_node(source, identifier_node, context)?;
|
2023-10-06 17:32:58 +00:00
|
|
|
|
2024-01-06 03:26:37 +00:00
|
|
|
let type_node = syntax_node.child(1).unwrap();
|
2023-12-29 19:35:52 +00:00
|
|
|
let type_definition = if type_node.kind() == "type_definition" {
|
|
|
|
Some(TypeDefinition::from_syntax_node(
|
|
|
|
source, type_node, context,
|
|
|
|
)?)
|
|
|
|
} else {
|
2023-12-30 03:39:50 +00:00
|
|
|
None
|
2023-11-27 15:27:44 +00:00
|
|
|
};
|
|
|
|
|
2024-01-06 03:26:37 +00:00
|
|
|
let operator_node = syntax_node
|
|
|
|
.child(child_count - 2)
|
|
|
|
.unwrap()
|
|
|
|
.child(0)
|
|
|
|
.unwrap();
|
2023-10-09 19:54:47 +00:00
|
|
|
let operator = match operator_node.kind() {
|
|
|
|
"=" => AssignmentOperator::Equal,
|
|
|
|
"+=" => AssignmentOperator::PlusEqual,
|
|
|
|
"-=" => AssignmentOperator::MinusEqual,
|
|
|
|
_ => {
|
2023-10-10 17:29:11 +00:00
|
|
|
return Err(Error::UnexpectedSyntaxNode {
|
2023-12-30 07:04:39 +00:00
|
|
|
expected: "=, += or -=".to_string(),
|
|
|
|
actual: operator_node.kind().to_string(),
|
2023-10-09 19:54:47 +00:00
|
|
|
location: operator_node.start_position(),
|
2023-10-10 17:29:11 +00:00
|
|
|
relevant_source: source[operator_node.byte_range()].to_string(),
|
2023-10-09 19:54:47 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-06 03:26:37 +00:00
|
|
|
let statement_node = syntax_node.child(child_count - 1).unwrap();
|
2023-11-30 03:54:46 +00:00
|
|
|
let statement = Statement::from_syntax_node(source, statement_node, context)?;
|
2023-12-13 20:47:41 +00:00
|
|
|
let statement_type = statement.expected_type(context)?;
|
2023-11-30 03:54:46 +00:00
|
|
|
|
2024-01-04 00:57:06 +00:00
|
|
|
let variable_key = identifier.inner().clone();
|
|
|
|
let variable_type = if let Some(definition) = &type_definition {
|
|
|
|
definition.inner().clone()
|
|
|
|
} else if let Some((_, r#type)) = context.variables()?.get(identifier.inner()) {
|
|
|
|
r#type.clone()
|
|
|
|
} else {
|
|
|
|
statement_type
|
|
|
|
};
|
|
|
|
|
|
|
|
context.set(variable_key, Value::none(), Some(variable_type))?;
|
|
|
|
|
|
|
|
Ok(Assignment {
|
|
|
|
identifier,
|
|
|
|
type_definition,
|
|
|
|
operator,
|
|
|
|
statement,
|
2024-01-06 03:26:37 +00:00
|
|
|
syntax_position: syntax_node.range().into(),
|
2024-01-04 00:57:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-06 03:26:37 +00:00
|
|
|
fn check_type(&self, source: &str, context: &Map) -> Result<()> {
|
2024-01-04 00:57:06 +00:00
|
|
|
let statement_type = self.statement.expected_type(context)?;
|
|
|
|
|
|
|
|
if let Some(type_definition) = &self.type_definition {
|
|
|
|
match self.operator {
|
2023-11-30 10:40:39 +00:00
|
|
|
AssignmentOperator::Equal => {
|
2024-01-06 03:26:37 +00:00
|
|
|
type_definition
|
|
|
|
.inner()
|
|
|
|
.check(&statement_type)
|
|
|
|
.map_err(|error| error.at_source_position(source, self.syntax_position))?;
|
2023-11-30 10:40:39 +00:00
|
|
|
}
|
|
|
|
AssignmentOperator::PlusEqual => {
|
2023-12-02 07:34:23 +00:00
|
|
|
if let Type::List(item_type) = type_definition.inner() {
|
2024-01-04 00:57:06 +00:00
|
|
|
item_type.check(&statement_type)?;
|
2023-11-30 10:40:39 +00:00
|
|
|
} else {
|
2023-12-15 22:27:29 +00:00
|
|
|
type_definition
|
|
|
|
.inner()
|
2024-01-06 03:26:37 +00:00
|
|
|
.check(&self.identifier.expected_type(context)?)
|
|
|
|
.map_err(|error| {
|
|
|
|
error.at_source_position(source, 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 {
|
2023-12-22 20:02:22 +00:00
|
|
|
AssignmentOperator::Equal => {}
|
|
|
|
AssignmentOperator::PlusEqual => {
|
2024-01-04 00:57:06 +00:00
|
|
|
if let Type::List(item_type) = self.identifier.expected_type(context)? {
|
2024-01-06 03:26:37 +00:00
|
|
|
item_type.check(&statement_type).map_err(|error| {
|
|
|
|
error.at_source_position(source, self.syntax_position)
|
|
|
|
})?;
|
2023-12-22 20:02:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
AssignmentOperator::MinusEqual => todo!(),
|
2023-12-17 00:40:14 +00:00
|
|
|
}
|
2023-11-30 10:40:39 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 03:26:37 +00:00
|
|
|
self.statement.check_type(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
|
|
|
}
|
|
|
|
|
2023-11-30 00:23:42 +00:00
|
|
|
fn run(&self, source: &str, context: &Map) -> Result<Value> {
|
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 => {
|
2023-12-09 22:15:41 +00:00
|
|
|
if let Some((mut previous_value, _)) = context.variables()?.get(key).cloned() {
|
2023-10-23 20:12:43 +00:00
|
|
|
previous_value += value;
|
|
|
|
previous_value
|
|
|
|
} else {
|
2023-11-27 22:53:12 +00:00
|
|
|
return Err(Error::VariableIdentifierNotFound(key.clone()));
|
2023-10-09 19:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
AssignmentOperator::MinusEqual => {
|
2023-12-09 22:15:41 +00:00
|
|
|
if let Some((mut previous_value, _)) = context.variables()?.get(key).cloned() {
|
2023-10-23 20:12:43 +00:00
|
|
|
previous_value -= value;
|
|
|
|
previous_value
|
|
|
|
} else {
|
2023-11-27 22:53:12 +00:00
|
|
|
return Err(Error::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
|
|
|
|
2023-12-09 23:50:17 +00:00
|
|
|
if let Some(type_defintion) = &self.type_definition {
|
|
|
|
context.set(key.clone(), new_value, Some(type_defintion.inner().clone()))?;
|
|
|
|
} else {
|
|
|
|
context.set(key.clone(), new_value, None)?;
|
|
|
|
}
|
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
|
|
|
|
2023-12-05 22:08:22 +00:00
|
|
|
fn expected_type(&self, _context: &Map) -> Result<Type> {
|
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
|
|
|
}
|