1
0
dust/src/abstract_tree/assignment.rs

116 lines
4.2 KiB
Rust
Raw Normal View History

2023-10-06 17:32:58 +00:00
use serde::{Deserialize, Serialize};
use tree_sitter::Node;
2023-11-27 15:27:44 +00:00
use crate::{AbstractTree, Error, Identifier, Map, Result, Statement, Type, 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-27 15:27:44 +00:00
r#type: Option<Type>,
2023-10-09 19:54:47 +00:00
operator: AssignmentOperator,
2023-10-06 17:32:58 +00:00
statement: Statement,
}
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 {
2023-10-10 17:29:11 +00:00
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
2023-11-15 01:41:57 +00:00
Error::expect_syntax_node(source, "assignment", node)?;
2023-11-27 15:27:44 +00:00
let identifier_node = node.child_by_field_name("identifier").unwrap();
2023-10-10 17:29:11 +00:00
let identifier = Identifier::from_syntax_node(source, identifier_node)?;
2023-10-06 17:32:58 +00:00
2023-11-27 15:27:44 +00:00
let type_node = node.child_by_field_name("type");
let r#type = if let Some(type_node) = type_node {
Some(Type::from_syntax_node(source, type_node)?)
} else {
None
};
let operator_node = node
.child_by_field_name("assignment_operator")
.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-10-09 19:54:47 +00:00
expected: "=, += or -=",
actual: operator_node.kind(),
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
})
}
};
2023-11-27 15:27:44 +00:00
let statement_node = node.child_by_field_name("statement").unwrap();
2023-10-10 17:29:11 +00:00
let statement = Statement::from_syntax_node(source, statement_node)?;
2023-10-06 17:32:58 +00:00
Ok(Assignment {
identifier,
2023-11-27 15:27:44 +00:00
r#type,
2023-10-09 19:54:47 +00:00
operator,
2023-10-06 17:32:58 +00:00
statement,
})
}
2023-10-25 20:44:50 +00:00
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
2023-10-10 21:12:38 +00:00
let key = self.identifier.inner().clone();
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-11-16 02:35:40 +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 {
Value::Empty
2023-10-09 19:54:47 +00:00
}
}
AssignmentOperator::MinusEqual => {
2023-11-16 02:35:40 +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 {
Value::Empty
2023-10-09 19:54:47 +00:00
}
}
2023-10-23 20:12:43 +00:00
AssignmentOperator::Equal => value,
};
2023-10-06 17:32:58 +00:00
2023-11-27 15:27:44 +00:00
let expected_type = self.r#type.as_ref().unwrap_or(&Type::Any);
match (expected_type, new_value.r#type()) {
(Type::Any, _)
| (Type::Boolean, Type::Boolean)
| (Type::Float, Type::Float)
| (Type::Function, Type::Function)
| (Type::Integer, Type::Integer)
| (Type::List, Type::List)
| (Type::Map, Type::Map)
| (Type::String, Type::String)
| (Type::Table, Type::Table) => {}
(Type::Boolean, _) => return Err(Error::ExpectedBoolean { actual: new_value }),
(Type::Float, _) => return Err(Error::ExpectedFloat { actual: new_value }),
(Type::Function, _) => return Err(Error::ExpectedFunction { actual: new_value }),
(Type::Integer, _) => return Err(Error::ExpectedInteger { actual: new_value }),
(Type::List, _) => return Err(Error::ExpectedList { actual: new_value }),
(Type::Map, _) => return Err(Error::ExpectedMap { actual: new_value }),
(Type::String, _) => return Err(Error::ExpectedString { actual: new_value }),
(Type::Table, _) => return Err(Error::ExpectedTable { actual: new_value }),
}
2023-11-16 02:35:40 +00:00
context.variables_mut()?.insert(key, new_value);
2023-10-06 17:32:58 +00:00
Ok(Value::Empty)
}
}