dust/src/abstract_tree/assignment.rs

220 lines
6.9 KiB
Rust
Raw Normal View History

2023-10-06 17:32:58 +00:00
use serde::{Deserialize, Serialize};
use tree_sitter::Node;
2023-11-30 10:40:39 +00:00
use crate::{
AbstractTree, Error, Function, Identifier, Map, Result, Statement, 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,
}
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-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, "assignment", node)?;
2023-11-27 15:27:44 +00:00
let identifier_node = node.child_by_field_name("identifier").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
2023-11-27 15:27:44 +00:00
let type_node = node.child_by_field_name("type");
2023-11-30 01:59:58 +00:00
let type_definition = if let Some(type_node) = type_node {
2023-11-30 05:57:15 +00:00
Some(TypeDefinition::from_syntax_node(
source, type_node, context,
)?)
2023-11-27 15:27:44 +00:00
} 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-11-30 03:54:46 +00:00
let statement = Statement::from_syntax_node(source, statement_node, context)?;
2023-11-30 10:40:39 +00:00
if let Some(type_definition) = &type_definition {
let statement_type = statement.expected_type(context)?;
match operator {
AssignmentOperator::Equal => {
type_definition.abstract_check(
&statement_type,
context,
statement_node,
source,
)?;
}
AssignmentOperator::PlusEqual => {
let identifier_type = identifier.expected_type(context)?;
type_definition.abstract_check(
&identifier_type,
context,
type_node.unwrap(),
source,
)?;
let type_definition = if let Type::List(item_type) = type_definition.inner() {
TypeDefinition::new(item_type.as_ref().clone())
} else {
type_definition.clone()
};
type_definition.abstract_check(
&statement_type,
context,
statement_node,
source,
)?;
}
AssignmentOperator::MinusEqual => todo!(),
}
}
2023-10-06 17:32:58 +00:00
Ok(Assignment {
identifier,
2023-11-30 01:59:58 +00:00
type_definition,
2023-10-09 19:54:47 +00:00
operator,
2023-10-06 17:32:58 +00:00
statement,
})
}
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-11-27 22:53:12 +00:00
if let Some(mut previous_value) = context.variables()?.get(key).cloned() {
2023-11-30 07:09:55 +00:00
if let Ok(list) = previous_value.as_list() {
2023-11-30 10:40:39 +00:00
let item_type = if let Some(type_defintion) = &self.type_definition {
if let Type::List(item_type) = type_defintion.inner() {
TypeDefinition::new(item_type.as_ref().clone())
} else {
TypeDefinition::new(Type::Empty)
}
} else if let Some(first) = list.items().first() {
2023-11-30 07:09:55 +00:00
first.r#type(context)?
} else {
TypeDefinition::new(Type::Any)
};
2023-11-30 10:40:39 +00:00
item_type.runtime_check(&value.r#type(context)?, context)?;
2023-11-30 07:09:55 +00:00
}
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-11-27 22:53:12 +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-11-30 10:40:39 +00:00
let new_value = if let Some(type_definition) = &self.type_definition {
let new_value_type = new_value.r#type(context)?;
type_definition.runtime_check(&new_value_type, context)?;
2023-11-30 05:57:15 +00:00
2023-11-30 10:40:39 +00:00
if let Value::Function(function) = new_value {
Value::Function(Function::new(
function.parameters().clone(),
function.body().clone(),
))
} else {
new_value
2023-11-30 07:09:55 +00:00
}
2023-11-30 10:40:39 +00:00
} else {
new_value
2023-11-30 07:09:55 +00:00
};
2023-11-30 03:02:55 +00:00
2023-11-27 22:53:12 +00:00
context.variables_mut()?.insert(key.clone(), new_value);
2023-10-06 17:32:58 +00:00
Ok(Value::Empty)
}
2023-11-30 00:23:42 +00:00
2023-11-30 05:57:15 +00:00
fn expected_type(&self, _context: &Map) -> Result<TypeDefinition> {
Ok(TypeDefinition::new(Type::Empty))
2023-11-30 00:23:42 +00:00
}
2023-10-06 17:32:58 +00:00
}
2023-11-30 10:40:39 +00:00
#[cfg(test)]
mod tests {
use crate::{evaluate, List, Value};
#[test]
fn simple_assignment() {
let test = evaluate("x = 1 x").unwrap();
assert_eq!(Value::Integer(1), test);
}
#[test]
fn simple_assignment_with_type() {
let test = evaluate("x <int> = 1 x").unwrap();
assert_eq!(Value::Integer(1), test);
}
#[test]
fn list_add_assign() {
let test = evaluate(
"
x <list int> = []
x += 1
x
",
)
.unwrap();
assert_eq!(Value::List(List::with_items(vec![Value::Integer(1)])), test);
}
#[test]
fn function_assignment() {
let test = evaluate(
"
foobar <fn str -> str> = |text| { 'hi' }
foobar
",
)
.unwrap();
assert_eq!(Value::String("hi".to_string()), test);
}
}