1
0
dust/src/abstract_tree/index_assignment.rs

98 lines
3.2 KiB
Rust
Raw Normal View History

2023-11-05 18:54:29 +00:00
use serde::{Deserialize, Serialize};
use tree_sitter::Node;
2023-12-05 22:08:22 +00:00
use crate::{AbstractTree, Error, Index, Map, Result, Statement, Type, Value};
2023-11-05 18:54:29 +00:00
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct IndexAssignment {
index: Index,
operator: AssignmentOperator,
statement: Statement,
}
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub enum AssignmentOperator {
Equal,
PlusEqual,
MinusEqual,
}
impl AbstractTree for IndexAssignment {
2023-11-30 03:54:46 +00:00
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> {
2023-11-15 01:41:57 +00:00
Error::expect_syntax_node(source, "index_assignment", node)?;
2023-11-05 18:54:29 +00:00
let index_node = node.child(0).unwrap();
2023-11-30 03:54:46 +00:00
let index = Index::from_syntax_node(source, index_node, context)?;
2023-11-05 18:54:29 +00:00
let operator_node = node.child(1).unwrap().child(0).unwrap();
let operator = match operator_node.kind() {
"=" => AssignmentOperator::Equal,
"+=" => AssignmentOperator::PlusEqual,
"-=" => AssignmentOperator::MinusEqual,
_ => {
return Err(Error::UnexpectedSyntaxNode {
expected: "=, += or -=",
actual: operator_node.kind(),
location: operator_node.start_position(),
relevant_source: source[operator_node.byte_range()].to_string(),
})
}
};
let statement_node = node.child(2).unwrap();
2023-11-30 03:54:46 +00:00
let statement = Statement::from_syntax_node(source, statement_node, context)?;
2023-11-05 18:54:29 +00:00
Ok(IndexAssignment {
index,
operator,
statement,
})
}
2023-11-30 00:23:42 +00:00
fn run(&self, source: &str, context: &Map) -> Result<Value> {
2023-11-15 01:41:57 +00:00
let index_collection = self.index.collection.run(source, context)?;
let index_context = index_collection.as_map().unwrap_or(&context);
let index_key = if let crate::Expression::Identifier(identifier) = &self.index.index {
identifier.inner()
} else {
return Err(Error::VariableIdentifierNotFound(
self.index.index.run(source, context)?.to_string(),
));
};
let value = self.statement.run(source, &mut context.clone())?;
let new_value = match self.operator {
AssignmentOperator::PlusEqual => {
2023-12-09 22:15:41 +00:00
if let Some((mut previous_value, _)) =
index_context.variables()?.get(index_key).cloned()
2023-11-15 01:41:57 +00:00
{
previous_value += value;
previous_value
} else {
Value::Option(None)
2023-11-15 01:41:57 +00:00
}
}
AssignmentOperator::MinusEqual => {
2023-12-09 22:15:41 +00:00
if let Some((mut previous_value, _)) =
index_context.variables()?.get(index_key).cloned()
2023-11-15 01:41:57 +00:00
{
previous_value -= value;
previous_value
} else {
Value::Option(None)
2023-11-15 01:41:57 +00:00
}
}
AssignmentOperator::Equal => value,
};
index_context.set(index_key.clone(), new_value, None)?;
2023-11-05 18:54:29 +00:00
Ok(Value::Option(None))
2023-11-05 18:54:29 +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-11-05 18:54:29 +00:00
}