2023-10-06 17:32:58 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use tree_sitter::Node;
|
|
|
|
|
2023-11-30 01:59:58 +00:00
|
|
|
use crate::{AbstractTree, Error, Map, Result, Type, Value, BUILT_IN_FUNCTIONS};
|
2023-10-06 17:32:58 +00:00
|
|
|
|
2023-11-18 01:10:07 +00:00
|
|
|
use super::expression::Expression;
|
2023-10-06 17:32:58 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
2023-11-28 22:54:17 +00:00
|
|
|
pub struct FunctionCall {
|
|
|
|
function: Expression,
|
|
|
|
arguments: Vec<Expression>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FunctionCall {
|
|
|
|
pub fn new(function: Expression, arguments: Vec<Expression>) -> Self {
|
|
|
|
Self {
|
|
|
|
function,
|
|
|
|
arguments,
|
|
|
|
}
|
|
|
|
}
|
2023-10-09 19:54:47 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 17:32:58 +00:00
|
|
|
impl AbstractTree for FunctionCall {
|
2023-11-30 03:54:46 +00:00
|
|
|
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> {
|
2023-10-06 17:32:58 +00:00
|
|
|
debug_assert_eq!("function_call", node.kind());
|
|
|
|
|
2023-11-28 22:54:17 +00:00
|
|
|
let expression_node = node.child(1).unwrap();
|
2023-11-30 03:54:46 +00:00
|
|
|
let function = Expression::from_syntax_node(source, expression_node, context)?;
|
2023-11-28 22:54:17 +00:00
|
|
|
|
2023-10-09 19:54:47 +00:00
|
|
|
let mut arguments = Vec::new();
|
2023-10-06 17:32:58 +00:00
|
|
|
|
2023-11-15 00:38:19 +00:00
|
|
|
for index in 2..node.child_count() - 1 {
|
2023-11-30 03:54:46 +00:00
|
|
|
let child = node.child(index).unwrap();
|
2023-10-06 17:32:58 +00:00
|
|
|
|
2023-11-30 03:54:46 +00:00
|
|
|
if child.is_named() {
|
|
|
|
let expression = Expression::from_syntax_node(source, child, context)?;
|
2023-10-06 17:32:58 +00:00
|
|
|
|
2023-10-11 16:07:30 +00:00
|
|
|
arguments.push(expression);
|
|
|
|
}
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 22:54:17 +00:00
|
|
|
Ok(FunctionCall {
|
|
|
|
function,
|
|
|
|
arguments,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-30 00:23:42 +00:00
|
|
|
fn run(&self, source: &str, context: &Map) -> Result<Value> {
|
2023-11-28 22:54:17 +00:00
|
|
|
let function = if let Expression::Identifier(identifier) = &self.function {
|
|
|
|
let key = identifier.inner();
|
2023-10-31 19:21:13 +00:00
|
|
|
|
2023-11-28 22:54:17 +00:00
|
|
|
for built_in_function in BUILT_IN_FUNCTIONS {
|
|
|
|
if key == built_in_function.name() {
|
|
|
|
let mut arguments = Vec::with_capacity(self.arguments.len());
|
2023-10-31 19:21:13 +00:00
|
|
|
|
2023-11-28 22:54:17 +00:00
|
|
|
for expression in &self.arguments {
|
|
|
|
let value = expression.run(source, context)?;
|
2023-10-31 19:21:13 +00:00
|
|
|
|
2023-11-28 22:54:17 +00:00
|
|
|
arguments.push(value);
|
|
|
|
}
|
2023-10-06 17:32:58 +00:00
|
|
|
|
2023-11-28 22:54:17 +00:00
|
|
|
return built_in_function.run(&arguments);
|
|
|
|
}
|
|
|
|
}
|
2023-10-31 19:21:13 +00:00
|
|
|
|
2023-11-28 22:54:17 +00:00
|
|
|
if let Some(value) = context.variables()?.get(key) {
|
2023-11-18 01:10:07 +00:00
|
|
|
value.as_function().cloned()
|
|
|
|
} else {
|
|
|
|
return Err(Error::FunctionIdentifierNotFound(identifier.clone()));
|
|
|
|
}
|
2023-10-06 17:32:58 +00:00
|
|
|
} else {
|
2023-11-28 22:54:17 +00:00
|
|
|
let expression_run = self.function.run(source, context)?;
|
2023-11-18 01:10:07 +00:00
|
|
|
|
2023-11-28 22:54:17 +00:00
|
|
|
expression_run.as_function().cloned()
|
2023-11-18 01:10:07 +00:00
|
|
|
}?;
|
|
|
|
|
2023-11-05 18:54:29 +00:00
|
|
|
let mut function_context = Map::clone_from(context)?;
|
2023-11-28 22:54:17 +00:00
|
|
|
let parameter_expression_pairs = function.parameters().iter().zip(self.arguments.iter());
|
2023-10-06 21:11:50 +00:00
|
|
|
|
2023-11-30 00:23:42 +00:00
|
|
|
for (identifier, expression) in parameter_expression_pairs {
|
2023-11-27 20:02:08 +00:00
|
|
|
let key = identifier.clone().take_inner();
|
|
|
|
let value = expression.run(source, context)?;
|
2023-10-07 01:00:31 +00:00
|
|
|
|
2023-11-27 20:02:08 +00:00
|
|
|
function_context.variables_mut()?.insert(key, value);
|
2023-10-07 01:00:31 +00:00
|
|
|
}
|
|
|
|
|
2023-11-27 20:02:08 +00:00
|
|
|
function.run(source, &mut function_context)
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
2023-11-30 00:23:42 +00:00
|
|
|
|
2023-11-30 01:59:58 +00:00
|
|
|
fn expected_type(&self, context: &Map) -> Result<Type> {
|
2023-11-30 00:23:42 +00:00
|
|
|
self.function.expected_type(context)
|
|
|
|
}
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|