1
0
dust/src/abstract_tree/function_call.rs

157 lines
5.1 KiB
Rust
Raw Normal View History

2023-10-06 13:32:58 -04:00
use serde::{Deserialize, Serialize};
use tree_sitter::Node;
2024-01-01 04:59:27 -05:00
use crate::{AbstractTree, Error, Expression, FunctionExpression, Map, Result, Type, Value};
2023-10-06 13:32:58 -04:00
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
2023-11-28 17:54:17 -05:00
pub struct FunctionCall {
function_expression: FunctionExpression,
2023-11-28 17:54:17 -05:00
arguments: Vec<Expression>,
}
impl FunctionCall {
pub fn new(function_expression: FunctionExpression, arguments: Vec<Expression>) -> Self {
2023-11-28 17:54:17 -05:00
Self {
2023-12-02 02:34:23 -05:00
function_expression,
2023-11-28 17:54:17 -05:00
arguments,
}
}
2023-10-09 15:54:47 -04:00
}
2023-10-06 13:32:58 -04:00
impl AbstractTree for FunctionCall {
2023-11-29 22:54:46 -05:00
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "function_call", node)?;
2023-10-06 13:32:58 -04:00
2023-12-29 20:14:03 -05:00
let function_node = node.child(0).unwrap();
let function_expression =
FunctionExpression::from_syntax_node(source, function_node, context)?;
2023-11-28 17:54:17 -05:00
2023-10-09 15:54:47 -04:00
let mut arguments = Vec::new();
2023-10-06 13:32:58 -04:00
2023-11-14 19:38:19 -05:00
for index in 2..node.child_count() - 1 {
2023-11-29 22:54:46 -05:00
let child = node.child(index).unwrap();
2023-10-06 13:32:58 -04:00
2023-11-29 22:54:46 -05:00
if child.is_named() {
let expression = Expression::from_syntax_node(source, child, context)?;
2023-10-06 13:32:58 -04:00
2023-10-11 12:07:30 -04:00
arguments.push(expression);
}
2023-10-06 13:32:58 -04:00
}
Ok(FunctionCall {
function_expression,
arguments,
})
}
fn check_type(&self, _source: &str, context: &Map) -> Result<()> {
let function_type = self.function_expression.expected_type(context)?;
let mut minimum_parameter_count = 0;
for (index, expression) in self.arguments.iter().enumerate() {
if let Type::Function {
parameter_types, ..
} = &function_type
{
if let Some(r#type) = parameter_types.get(index) {
if let Type::Option(_) = r#type {
} else {
minimum_parameter_count += 1;
}
r#type.check(&expression.expected_type(context)?)?;
}
}
}
if let Type::Function {
2023-12-26 19:33:19 -05:00
parameter_types: _, ..
} = &function_type
{
if self.arguments.len() < minimum_parameter_count {
2023-12-26 19:33:19 -05:00
return Err(Error::ExpectedFunctionArgumentMinimum {
source: "TODO".to_string(),
2023-12-26 19:33:19 -05:00
minumum_expected: minimum_parameter_count,
actual: self.arguments.len(),
});
}
}
Ok(())
2023-11-28 17:54:17 -05:00
}
2023-11-29 19:23:42 -05:00
fn run(&self, source: &str, context: &Map) -> Result<Value> {
2024-01-01 07:46:47 -05:00
let (name, value) = match &self.function_expression {
FunctionExpression::Identifier(identifier) => {
2023-12-02 02:34:23 -05:00
let key = identifier.inner();
let variables = context.variables()?;
2023-12-05 17:40:22 -05:00
2023-12-09 17:15:41 -05:00
if let Some((value, _)) = variables.get(key) {
2024-01-01 07:46:47 -05:00
(Some(key.clone()), value.clone())
2023-12-02 02:34:23 -05:00
} else {
return Err(Error::FunctionIdentifierNotFound(
identifier.inner().clone(),
));
2023-12-02 02:34:23 -05:00
}
}
FunctionExpression::FunctionCall(function_call) => {
2024-01-01 07:46:47 -05:00
(None, function_call.run(source, context)?)
}
2024-01-01 07:46:47 -05:00
FunctionExpression::Value(value_node) => (None, value_node.run(source, context)?),
FunctionExpression::Index(index) => (None, index.run(source, context)?),
FunctionExpression::Yield(r#yield) => (None, r#yield.run(source, context)?),
2023-11-30 11:05:09 -05:00
};
2023-10-06 21:00:31 -04:00
let mut arguments = Vec::with_capacity(self.arguments.len());
for expression in &self.arguments {
let value = expression.run(source, context)?;
arguments.push(value);
}
2024-01-01 07:46:47 -05:00
if let Some(name) = &name {
context.set(name.to_string(), value.clone(), None)?;
}
value
.as_function()
.map_err(|error| {
println!("{name:?}");
error
})?
.call(name, &arguments, source, context)
2023-10-06 13:32:58 -04:00
}
2023-11-29 19:23:42 -05:00
2023-12-05 17:08:22 -05:00
fn expected_type(&self, context: &Map) -> Result<Type> {
2023-12-02 02:34:23 -05:00
match &self.function_expression {
FunctionExpression::Identifier(identifier) => {
2023-12-26 17:52:44 -05: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),
2024-01-01 04:59:27 -05:00
FunctionExpression::Value(value_node) => {
let value_type = value_node.expected_type(context)?;
if let Type::Function { return_type, .. } = value_type {
Ok(*return_type)
} else {
Ok(value_type)
}
}
FunctionExpression::Index(index) => index.expected_type(context),
2023-12-29 21:15:03 -05:00
FunctionExpression::Yield(r#yield) => r#yield.expected_type(context),
2023-12-01 22:16:50 -05:00
}
2023-11-29 19:23:42 -05:00
}
2023-10-06 13:32:58 -04:00
}