1
0

Implement output tool

This commit is contained in:
Jeff 2023-10-21 13:47:08 -04:00
parent 02b237b11b
commit b123b90298
3 changed files with 59 additions and 2 deletions

View File

@ -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<Math>),
Logic(Box<Logic>),
FunctionCall(FunctionCall),
Tool(Box<Tool>),
}
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),
}
}
}

View File

@ -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;

51
src/abstract_tree/tool.rs Normal file
View File

@ -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<Expression>),
}
impl AbstractTree for Tool {
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
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<Value> {
match self {
Tool::Output(expressions) => {
for expression in expressions {
let value = expression.run(source, context)?;
println!("{value}");
}
Ok(Value::Empty)
}
}
}
}