2023-10-06 17:32:58 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use tree_sitter::Node;
|
|
|
|
|
2023-10-09 19:54:47 +00:00
|
|
|
use crate::{tool::Tool, AbstractTree, Result, Value, VariableMap};
|
2023-10-06 17:32:58 +00:00
|
|
|
|
|
|
|
use super::{expression::Expression, identifier::Identifier};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
|
|
|
pub struct FunctionCall {
|
2023-10-09 19:54:47 +00:00
|
|
|
name: FunctionName,
|
|
|
|
arguments: Vec<Expression>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
|
|
|
pub enum FunctionName {
|
|
|
|
Identifier(Identifier),
|
|
|
|
Tool(Tool),
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AbstractTree for FunctionCall {
|
2023-10-10 17:29:11 +00:00
|
|
|
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
|
2023-10-06 17:32:58 +00:00
|
|
|
debug_assert_eq!("function_call", node.kind());
|
|
|
|
|
2023-10-09 19:54:47 +00:00
|
|
|
let name_node = node.child(1).unwrap();
|
|
|
|
|
|
|
|
let name = match name_node.kind() {
|
|
|
|
"identifier" => {
|
2023-10-10 17:29:11 +00:00
|
|
|
FunctionName::Identifier(Identifier::from_syntax_node(source, name_node)?)
|
2023-10-09 19:54:47 +00:00
|
|
|
}
|
|
|
|
"tool" => {
|
|
|
|
let tool_node = name_node.child(0).unwrap();
|
|
|
|
let tool = match tool_node.kind() {
|
|
|
|
"output" => Tool::Output,
|
|
|
|
"read" => Tool::Read,
|
|
|
|
_ => panic!(""),
|
|
|
|
};
|
|
|
|
|
|
|
|
FunctionName::Tool(tool)
|
|
|
|
}
|
|
|
|
_ => panic!(""),
|
|
|
|
};
|
2023-10-06 17:32:58 +00:00
|
|
|
|
2023-10-09 19:54:47 +00:00
|
|
|
let mut arguments = Vec::new();
|
2023-10-06 17:32:58 +00:00
|
|
|
|
|
|
|
let mut current_index = 2;
|
|
|
|
while current_index < node.child_count() - 1 {
|
|
|
|
let expression_node = node.child(current_index).unwrap();
|
2023-10-10 17:29:11 +00:00
|
|
|
let expression = Expression::from_syntax_node(source, expression_node)?;
|
2023-10-06 17:32:58 +00:00
|
|
|
|
2023-10-09 19:54:47 +00:00
|
|
|
arguments.push(expression);
|
2023-10-06 17:32:58 +00:00
|
|
|
|
|
|
|
current_index += 1;
|
|
|
|
}
|
|
|
|
|
2023-10-09 19:54:47 +00:00
|
|
|
Ok(FunctionCall { name, arguments })
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 17:29:11 +00:00
|
|
|
fn run(&self, source: &str, context: &mut VariableMap) -> Result<Value> {
|
2023-10-09 19:54:47 +00:00
|
|
|
let identifier = match &self.name {
|
|
|
|
FunctionName::Identifier(identifier) => identifier,
|
|
|
|
FunctionName::Tool(tool) => {
|
|
|
|
let value = self
|
|
|
|
.arguments
|
|
|
|
.first()
|
2023-10-10 17:29:11 +00:00
|
|
|
.map(|expression| expression.run(source, context))
|
2023-10-09 19:54:47 +00:00
|
|
|
.unwrap_or(Ok(Value::Empty))?;
|
|
|
|
|
|
|
|
return tool.run(&value);
|
|
|
|
}
|
|
|
|
};
|
2023-10-10 17:29:11 +00:00
|
|
|
let key = identifier.inner();
|
|
|
|
let definition = if let Some(value) = context.get_value(key)? {
|
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-09 19:54:47 +00:00
|
|
|
let id_expr_pairs = definition.identifiers().iter().zip(self.arguments.iter());
|
|
|
|
let mut function_context = context.clone();
|
2023-10-06 21:11:50 +00:00
|
|
|
|
|
|
|
for (identifier, expression) in id_expr_pairs {
|
2023-10-10 21:12:38 +00:00
|
|
|
let key = identifier.inner().clone();
|
|
|
|
let value = expression.run(source, context)?;
|
2023-10-07 01:00:31 +00:00
|
|
|
|
2023-10-09 19:54:47 +00:00
|
|
|
function_context.set_value(key, value)?;
|
2023-10-07 01:00:31 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 17:29:11 +00:00
|
|
|
definition.body().run(source, &mut function_context)
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
}
|