From b123b902988022bd8eff48e1c2f6f77a79fd45f3 Mon Sep 17 00:00:00 2001 From: Jeff Date: Sat, 21 Oct 2023 13:47:08 -0400 Subject: [PATCH] Implement output tool --- src/abstract_tree/expression.rs | 7 ++++- src/abstract_tree/mod.rs | 3 +- src/abstract_tree/tool.rs | 51 +++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 src/abstract_tree/tool.rs diff --git a/src/abstract_tree/expression.rs b/src/abstract_tree/expression.rs index f74858d..7f5cbe7 100644 --- a/src/abstract_tree/expression.rs +++ b/src/abstract_tree/expression.rs @@ -1,7 +1,9 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{value_node::ValueNode, AbstractTree, Error, Identifier, Result, Value, VariableMap}; +use crate::{ + value_node::ValueNode, AbstractTree, Error, Identifier, Result, Tool, Value, VariableMap, +}; use super::{function_call::FunctionCall, logic::Logic, math::Math}; @@ -12,6 +14,7 @@ pub enum Expression { Math(Box), Logic(Box), FunctionCall(FunctionCall), + Tool(Box), } impl AbstractTree for Expression { @@ -28,6 +31,7 @@ impl AbstractTree for Expression { "function_call" => { Expression::FunctionCall(FunctionCall::from_syntax_node(source, child)?) } + "tool" => Expression::Tool(Box::new(Tool::from_syntax_node(source, child)?)), _ => continue, }; @@ -51,6 +55,7 @@ impl AbstractTree for Expression { Expression::Math(math) => math.run(source, context), Expression::Logic(logic) => logic.run(source, context), Expression::FunctionCall(function_call) => function_call.run(source, context), + Expression::Tool(tool) => tool.run(source, context), } } } diff --git a/src/abstract_tree/mod.rs b/src/abstract_tree/mod.rs index bfddbcb..d194891 100644 --- a/src/abstract_tree/mod.rs +++ b/src/abstract_tree/mod.rs @@ -22,6 +22,7 @@ pub mod math; pub mod remove; pub mod select; pub mod statement; +pub mod tool; pub mod transform; pub mod value_node; pub mod r#while; @@ -29,7 +30,7 @@ pub mod r#while; pub use { assignment::*, expression::*, filter::*, find::*, function_call::*, identifier::*, if_else::*, item::*, logic::*, math::*, r#async::*, r#for::*, r#match::*, r#while::*, remove::*, select::*, - statement::*, transform::*, + statement::*, tool::*, transform::*, value_node::*, }; use tree_sitter::Node; diff --git a/src/abstract_tree/tool.rs b/src/abstract_tree/tool.rs new file mode 100644 index 0000000..0bffcc0 --- /dev/null +++ b/src/abstract_tree/tool.rs @@ -0,0 +1,51 @@ +use serde::{Deserialize, Serialize}; +use tree_sitter::Node; + +use crate::{AbstractTree, Error, Expression, Result, Value, VariableMap}; + +#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] +pub enum Tool { + Output(Vec), +} + +impl AbstractTree for Tool { + fn from_syntax_node(source: &str, node: Node) -> Result { + let mut expressions = Vec::new(); + + for index in 2..node.child_count() - 1 { + let expression_node = node.child(index).unwrap(); + let expression = Expression::from_syntax_node(source, expression_node)?; + + expressions.push(expression); + } + + let tool_node = node.child(1).unwrap(); + let tool = match tool_node.kind() { + "output" => Tool::Output(expressions), + _ => { + return Err(Error::UnexpectedSyntaxNode { + expected: "output", + actual: tool_node.kind(), + location: tool_node.start_position(), + relevant_source: source[tool_node.byte_range()].to_string(), + }) + } + }; + + Ok(tool) + } + + fn run(&self, source: &str, context: &mut VariableMap) -> Result { + match self { + Tool::Output(expressions) => { + for expression in expressions { + let value = expression.run(source, context)?; + + println!("{value}"); + } + + Ok(Value::Empty) + } + } + } +}