dust/src/abstract_tree/function_call.rs

165 lines
5.7 KiB
Rust
Raw Normal View History

2023-10-06 17:32:58 +00:00
use serde::{Deserialize, Serialize};
use tree_sitter::Node;
2023-12-26 22:52:44 +00:00
use crate::{
AbstractTree, Error, Expression, FunctionExpression, Map, Result, Type, Value,
BUILT_IN_FUNCTIONS,
2023-12-26 22:52:44 +00:00
};
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: FunctionExpression,
2023-11-28 22:54:17 +00:00
arguments: Vec<Expression>,
}
impl FunctionCall {
pub fn new(function_expression: FunctionExpression, 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> {
Error::expect_syntax_node(source, "function_call", node)?;
2023-10-06 17:32:58 +00:00
2023-12-30 01:14:03 +00:00
let function_node = node.child(0).unwrap();
let function_expression =
FunctionExpression::from_syntax_node(source, function_node, context)?;
let function_type = function_expression.expected_type(context)?;
2023-11-28 22:54:17 +00:00
2023-12-27 00:33:19 +00:00
let mut minimum_parameter_count = 0;
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)?;
let expression_type = expression.expected_type(context)?;
let argument_index = arguments.len();
if let Type::Function {
parameter_types, ..
} = &function_type
{
if let Some(r#type) = parameter_types.get(argument_index) {
2023-12-27 00:33:19 +00:00
if let Type::Option(_) = r#type {
} else {
minimum_parameter_count += 1;
}
r#type
.check(&expression_type)
.map_err(|error| error.at_node(child, source))?;
}
}
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
}
if let Type::Function {
2023-12-27 00:33:19 +00:00
parameter_types: _, ..
} = &function_type
{
2023-12-27 00:33:19 +00:00
if arguments.len() < minimum_parameter_count {
return Err(Error::ExpectedFunctionArgumentMinimum {
source: source[function_node.byte_range()].to_string(),
2023-12-27 00:33:19 +00:00
minumum_expected: minimum_parameter_count,
actual: arguments.len(),
});
}
}
2023-12-12 23:21:16 +00:00
Ok(FunctionCall {
2023-12-02 07:34:23 +00:00
function_expression,
2023-11-28 22:54:17 +00:00
arguments,
2023-12-12 23:21:16 +00:00
})
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 {
FunctionExpression::Identifier(identifier) => {
2023-12-02 07:34:23 +00:00
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.inner().clone(),
));
2023-12-02 07:34:23 +00:00
}
}
FunctionExpression::FunctionCall(function_call) => {
function_call.run(source, context)?
}
FunctionExpression::Value(value_node) => value_node.run(source, context)?,
FunctionExpression::Index(index) => index.run(source, context)?,
2023-12-30 02:15:03 +00:00
FunctionExpression::Yield(r#yield) => r#yield.run(source, context)?,
2023-11-30 16:05:09 +00:00
};
2023-10-07 01:00:31 +00:00
let mut arguments = Vec::with_capacity(self.arguments.len());
for expression in &self.arguments {
let value = expression.run(source, context)?;
arguments.push(value);
}
2023-12-22 21:12:41 +00:00
value.as_function()?.call(&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 {
FunctionExpression::Identifier(identifier) => {
for built_in_function in BUILT_IN_FUNCTIONS {
if identifier.inner() == built_in_function.name() {
if let Type::Function {
parameter_types: _,
return_type,
} = built_in_function.r#type()
{
return Ok(*return_type);
}
}
}
2023-12-26 22:52:44 +00:00
let identifier_type = identifier.expected_type(context)?;
if let Type::Function {
parameter_types: _,
return_type,
} = &identifier_type
{
Ok(*return_type.clone())
} else {
Ok(identifier_type)
}
}
FunctionExpression::FunctionCall(function_call) => function_call.expected_type(context),
FunctionExpression::Value(value_node) => value_node.expected_type(context),
FunctionExpression::Index(index) => index.expected_type(context),
2023-12-30 02:15:03 +00:00
FunctionExpression::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
}