diff --git a/src/abstract_tree/expression.rs b/src/abstract_tree/expression.rs index 7eabc98..2c5bbe4 100644 --- a/src/abstract_tree/expression.rs +++ b/src/abstract_tree/expression.rs @@ -15,9 +15,9 @@ pub enum Expression { Index(Box), Math(Box), Logic(Box), - FunctionCall(FunctionCall), + FunctionCall(Box), Tool(Box), - Yield(Yield), + Yield(Box), } impl AbstractTree for Expression { @@ -37,10 +37,10 @@ impl AbstractTree for Expression { "math" => Expression::Math(Box::new(Math::from_syntax_node(source, child)?)), "logic" => Expression::Logic(Box::new(Logic::from_syntax_node(source, child)?)), "function_call" => { - Expression::FunctionCall(FunctionCall::from_syntax_node(source, child)?) + Expression::FunctionCall(Box::new(FunctionCall::from_syntax_node(source, child)?)) } "tool" => Expression::Tool(Box::new(BuiltInFunction::from_syntax_node(source, child)?)), - "yield" => Expression::Yield(Yield::from_syntax_node(source, child)?), + "yield" => Expression::Yield(Box::new(Yield::from_syntax_node(source, child)?)), _ => { return Err(Error::UnexpectedSyntaxNode { expected: "value, identifier, index, math, logic, function_call or yield", diff --git a/src/abstract_tree/function_call.rs b/src/abstract_tree/function_call.rs index de6cd5a..68f547b 100644 --- a/src/abstract_tree/function_call.rs +++ b/src/abstract_tree/function_call.rs @@ -3,13 +3,13 @@ use tree_sitter::Node; use crate::{AbstractTree, BuiltInFunction, Error, Map, Result, Value}; -use super::{expression::Expression, identifier::Identifier}; +use super::expression::Expression; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub enum FunctionCall { BuiltIn(Box), ContextDefined { - name: Identifier, + name: Expression, arguments: Vec, }, } @@ -36,12 +36,9 @@ impl AbstractTree for FunctionCall { FunctionCall::BuiltIn(Box::new(function)) } else { - let identifier = Identifier::from_syntax_node(source, function_node)?; + let name = Expression::from_syntax_node(source, function_node)?; - FunctionCall::ContextDefined { - name: identifier, - arguments, - } + FunctionCall::ContextDefined { name, arguments } }; Ok(function_call) @@ -53,11 +50,18 @@ impl AbstractTree for FunctionCall { FunctionCall::ContextDefined { name, arguments } => (name, arguments), }; - let definition = if let Some(value) = context.variables()?.get(name.inner()) { - value.as_function().cloned()? + let definition = if let Expression::Identifier(identifier) = name { + if let Some(value) = context.variables()?.get(identifier.inner()) { + value.as_function().cloned() + } else { + return Err(Error::FunctionIdentifierNotFound(identifier.clone())); + } } else { - return Err(Error::FunctionIdentifierNotFound(name.clone())); - }; + let name_run = name.run(source, context)?; + + name_run.as_function().cloned() + }?; + let mut function_context = Map::clone_from(context)?; if let Some(parameters) = definition.identifiers() { diff --git a/src/abstract_tree/mod.rs b/src/abstract_tree/mod.rs index 0a08a30..ab88099 100644 --- a/src/abstract_tree/mod.rs +++ b/src/abstract_tree/mod.rs @@ -26,6 +26,7 @@ pub mod remove; pub mod select; pub mod statement; pub mod transform; +pub mod r#use; pub mod value_node; pub mod r#while; pub mod r#yield; @@ -33,8 +34,8 @@ pub mod r#yield; pub use { assignment::*, block::*, built_in_function::*, expression::*, filter::*, find::*, function_call::*, identifier::*, if_else::*, index::*, index_assignment::IndexAssignment, - insert::*, logic::*, math::*, r#for::*, r#match::*, r#while::*, r#yield::*, remove::*, - select::*, statement::*, transform::*, value_node::*, + insert::*, logic::*, math::*, r#for::*, r#match::*, r#use::*, r#while::*, r#yield::*, + remove::*, select::*, statement::*, transform::*, value_node::*, }; use tree_sitter::Node; diff --git a/src/abstract_tree/statement.rs b/src/abstract_tree/statement.rs index b37d492..4ed98c5 100644 --- a/src/abstract_tree/statement.rs +++ b/src/abstract_tree/statement.rs @@ -3,7 +3,7 @@ use tree_sitter::Node; use crate::{ AbstractTree, Assignment, Block, Error, Expression, Filter, Find, For, IfElse, IndexAssignment, - Insert, Map, Match, Remove, Result, Select, Transform, Value, While, + Insert, Map, Match, Remove, Result, Select, Transform, Use, Value, While, }; /// Abstract representation of a statement. @@ -21,6 +21,7 @@ pub enum Statement { Filter(Box), Find(Box), Remove(Box), + Use(Use), Select(Box