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