1
0
dust/src/abstract_tree/function_call.rs

67 lines
2.0 KiB
Rust
Raw Normal View History

2023-10-06 17:32:58 +00:00
use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{AbstractTree, Result, Value, VariableMap};
use super::{expression::Expression, identifier::Identifier};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct FunctionCall {
identifier: Identifier,
expressions: Vec<Expression>,
}
impl AbstractTree for FunctionCall {
fn from_syntax_node(node: Node, source: &str) -> Result<Self> {
debug_assert_eq!("function_call", node.kind());
let identifier_node = node.child(1).unwrap();
let identifier = Identifier::from_syntax_node(identifier_node, source)?;
let mut expressions = Vec::new();
let mut current_index = 2;
while current_index < node.child_count() - 1 {
let expression_node = node.child(current_index).unwrap();
let expression = Expression::from_syntax_node(expression_node, source)?;
expressions.push(expression);
current_index += 1;
}
Ok(FunctionCall {
identifier,
expressions,
})
}
fn run(&self, context: &mut VariableMap) -> Result<Value> {
let identifier = &self.identifier;
let definition = if let Some(value) = context.get_value(identifier.inner())? {
2023-10-06 21:11:50 +00:00
value.as_function().cloned()?
2023-10-06 17:32:58 +00:00
} else {
return Err(crate::Error::FunctionIdentifierNotFound(identifier.clone()));
};
2023-10-06 21:11:50 +00:00
let id_expr_pairs = definition.identifiers().iter().zip(self.expressions.iter());
for (identifier, expression) in id_expr_pairs {
let key = identifier.clone().take_inner();
let value = expression.run(context)?;
context.set_value(key, value)?;
}
let mut results = Vec::with_capacity(self.expressions.len());
for statement in definition.statements() {
let result = statement.run(context)?;
results.push(result);
}
Ok(Value::List(results))
2023-10-06 17:32:58 +00:00
}
}