2023-10-06 17:32:58 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use tree_sitter::Node;
|
|
|
|
|
2023-12-05 22:08:22 +00:00
|
|
|
use crate::{AbstractTree, Error, Map, Result, Type, Value, ValueNode, 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 {
|
2023-12-02 07:34:23 +00:00
|
|
|
function_expression: Expression,
|
2023-11-28 22:54:17 +00:00
|
|
|
arguments: Vec<Expression>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FunctionCall {
|
2023-12-02 07:34:23 +00:00
|
|
|
pub fn new(function_expression: Expression, arguments: Vec<Expression>) -> Self {
|
2023-11-28 22:54:17 +00:00
|
|
|
Self {
|
2023-12-02 07:34:23 +00:00
|
|
|
function_expression,
|
2023-11-28 22:54:17 +00:00
|
|
|
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-12-02 07:34:23 +00:00
|
|
|
let expression_node = node.child(1).unwrap();
|
|
|
|
let function_expression = 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-12-05 22:08:22 +00:00
|
|
|
let function_type = function_expression.expected_type(context)?;
|
2023-12-05 21:42:11 +00:00
|
|
|
|
|
|
|
if let Type::Function {
|
|
|
|
parameter_types,
|
2023-12-05 22:40:22 +00:00
|
|
|
return_type: _,
|
2023-12-05 21:42:11 +00:00
|
|
|
} = function_type
|
|
|
|
{
|
|
|
|
let argument_type_pairs = arguments.iter().zip(parameter_types.iter());
|
|
|
|
|
|
|
|
for (argument, r#type) in argument_type_pairs {
|
|
|
|
let argument_type = argument.expected_type(context)?;
|
|
|
|
|
2023-12-05 22:40:22 +00:00
|
|
|
if let Type::Function { return_type, .. } = argument_type {
|
|
|
|
r#type.check(&return_type, context, node, source)?;
|
|
|
|
} else {
|
|
|
|
r#type.check(&argument_type, context, node, source)?;
|
|
|
|
}
|
2023-12-05 21:42:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-02 03:16:50 +00:00
|
|
|
let function_call = FunctionCall {
|
2023-12-02 07:34:23 +00:00
|
|
|
function_expression,
|
2023-11-28 22:54:17 +00:00
|
|
|
arguments,
|
2023-12-02 03:16:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(function_call)
|
2023-11-28 22:54:17 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 00:23:42 +00:00
|
|
|
fn run(&self, source: &str, context: &Map) -> Result<Value> {
|
2023-12-02 07:34:23 +00:00
|
|
|
let value = match &self.function_expression {
|
|
|
|
Expression::Value(value_node) => value_node.run(source, context)?,
|
|
|
|
Expression::Identifier(identifier) => {
|
|
|
|
let key = identifier.inner();
|
2023-10-31 19:21:13 +00:00
|
|
|
|
2023-12-02 07:34:23 +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-12-02 07:34:23 +00:00
|
|
|
for expression in &self.arguments {
|
|
|
|
let value = expression.run(source, context)?;
|
2023-10-31 19:21:13 +00:00
|
|
|
|
2023-12-02 07:34:23 +00:00
|
|
|
arguments.push(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return built_in_function.run(&arguments, context);
|
|
|
|
}
|
2023-11-28 22:54:17 +00:00
|
|
|
}
|
2023-10-31 19:21:13 +00:00
|
|
|
|
2023-12-02 07:34:23 +00:00
|
|
|
let variables = context.variables()?;
|
2023-12-05 22:40:22 +00:00
|
|
|
|
2023-12-09 22:15:41 +00:00
|
|
|
if let Some((value, _)) = variables.get(key) {
|
2023-12-02 07:34:23 +00:00
|
|
|
value.clone()
|
|
|
|
} else {
|
|
|
|
return Err(Error::FunctionIdentifierNotFound(identifier.clone()));
|
|
|
|
}
|
2023-11-18 01:10:07 +00:00
|
|
|
}
|
2023-12-02 07:34:23 +00:00
|
|
|
Expression::Index(index) => index.run(source, context)?,
|
|
|
|
Expression::Math(math) => math.run(source, context)?,
|
|
|
|
Expression::Logic(logic) => logic.run(source, context)?,
|
|
|
|
Expression::FunctionCall(function_call) => function_call.run(source, context)?,
|
|
|
|
Expression::Yield(r#yield) => r#yield.run(source, context)?,
|
2023-11-30 16:05:09 +00:00
|
|
|
};
|
2023-10-07 01:00:31 +00:00
|
|
|
|
2023-12-02 07:34:23 +00:00
|
|
|
value.as_function()?.call(&self.arguments, source, context)
|
2023-10-06 17:32:58 +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-02 07:34:23 +00:00
|
|
|
match &self.function_expression {
|
|
|
|
Expression::Value(value_node) => {
|
|
|
|
if let ValueNode::Function(function) = value_node {
|
2023-12-05 21:42:11 +00:00
|
|
|
let return_type = function.return_type()?.clone();
|
|
|
|
|
2023-12-05 22:08:22 +00:00
|
|
|
Ok(return_type)
|
2023-12-02 07:34:23 +00:00
|
|
|
} else {
|
|
|
|
value_node.expected_type(context)
|
|
|
|
}
|
|
|
|
}
|
2023-12-05 22:08:22 +00:00
|
|
|
Expression::Identifier(identifier) => identifier.expected_type(context),
|
2023-12-02 07:34:23 +00:00
|
|
|
Expression::Index(index) => index.expected_type(context),
|
|
|
|
Expression::Math(math) => math.expected_type(context),
|
|
|
|
Expression::Logic(logic) => logic.expected_type(context),
|
|
|
|
Expression::FunctionCall(function_call) => function_call.expected_type(context),
|
|
|
|
Expression::Yield(r#yield) => r#yield.expected_type(context),
|
2023-12-02 03:16:50 +00:00
|
|
|
}
|
2023-11-30 00:23:42 +00:00
|
|
|
}
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
2023-11-30 14:48:56 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::{evaluate, Value};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn evaluate_function_call() {
|
|
|
|
assert_eq!(
|
|
|
|
evaluate(
|
|
|
|
"
|
2023-12-05 21:42:11 +00:00
|
|
|
foobar <(str) -> str> = fn |message| { message }
|
2023-11-30 14:48:56 +00:00
|
|
|
(foobar 'Hiya')
|
|
|
|
",
|
|
|
|
),
|
|
|
|
Ok(Value::String("Hiya".to_string()))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-12-05 21:42:11 +00:00
|
|
|
#[test]
|
|
|
|
fn evaluate_callback() {
|
|
|
|
assert_eq!(
|
|
|
|
evaluate(
|
|
|
|
"
|
|
|
|
foobar <(() -> str) -> str> = fn |cb| {
|
|
|
|
(cb)
|
|
|
|
}
|
|
|
|
|
|
|
|
(foobar fn || { 'Hiya' })
|
|
|
|
",
|
|
|
|
),
|
|
|
|
Ok(Value::String("Hiya".to_string()))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-30 14:48:56 +00:00
|
|
|
#[test]
|
|
|
|
fn evaluate_built_in_function_call() {
|
|
|
|
assert_eq!(evaluate("(output 'Hiya')"), Ok(Value::Empty));
|
|
|
|
}
|
|
|
|
}
|