From d043f334db7a687e172e0604351f61de51b6d96c Mon Sep 17 00:00:00 2001 From: Jeff Date: Fri, 13 Oct 2023 15:48:48 -0400 Subject: [PATCH] Add more tools --- src/abstract_tree/function_call.rs | 12 +---- src/abstract_tree/tool.rs | 83 +++++++++++++++++++++++++----- tree-sitter-dust | 2 +- 3 files changed, 71 insertions(+), 26 deletions(-) diff --git a/src/abstract_tree/function_call.rs b/src/abstract_tree/function_call.rs index f2bf6c2..7f159ff 100644 --- a/src/abstract_tree/function_call.rs +++ b/src/abstract_tree/function_call.rs @@ -22,23 +22,13 @@ impl AbstractTree for FunctionCall { debug_assert_eq!("function_call", node.kind()); let name_node = node.child(1).unwrap(); - let name = match name_node.kind() { "identifier" => { FunctionName::Identifier(Identifier::from_syntax_node(source, name_node)?) } "tool" => { let tool_node = name_node.child(0).unwrap(); - let tool = match tool_node.kind() { - "assert" => Tool::Assert, - "assert_equal" => Tool::AssertEqual, - "output" => Tool::Output, - "random" => Tool::Random, - "random_integer" => Tool::RandomInteger, - "read" => Tool::Read, - "help" => Tool::Help, - _ => panic!("Tool name not recognized."), - }; + let tool = Tool::new(tool_node.kind())?; FunctionName::Tool(tool) } diff --git a/src/abstract_tree/tool.rs b/src/abstract_tree/tool.rs index 5b2e5df..e78e73b 100644 --- a/src/abstract_tree/tool.rs +++ b/src/abstract_tree/tool.rs @@ -10,13 +10,57 @@ pub enum Tool { Assert, AssertEqual, Output, - Random, Read, Help, + + Raw, + Sh, + Bash, + Fish, + Zsh, + + FromCsv, + ToCsv, + FromJson, + ToJson, + + Random, + RandomFloat, RandomInteger, + RandomString, } impl Tool { + pub fn new(kind: &str) -> Result { + let tool = match kind { + "assert" => Tool::Assert, + "assert_equal" => Tool::AssertEqual, + "output" => Tool::Output, + + "raw" => Tool::Raw, + "sh" => Tool::Sh, + "bash" => Tool::Bash, + "fish" => Tool::Fish, + "zsh" => Tool::Zsh, + + "from_csv" => Tool::FromCsv, + "to_csv" => Tool::ToCsv, + "from_json" => Tool::FromJson, + "to_json" => Tool::ToJson, + + "random" => Tool::Random, + "random_integer" => Tool::RandomInteger, + "random_float" => Tool::RandomFloat, + "random_string" => Tool::RandomString, + + "read" => Tool::Read, + "help" => Tool::Help, + _ => todo!("Tool name not recognized."), + }; + + Ok(tool) + } + pub fn run(&self, values: &[Value]) -> Result { let value = match self { Tool::Assert => { @@ -65,7 +109,22 @@ impl Tool { Value::Empty } + Tool::Read => { + if values.len() != 1 { + return Err(Error::ExpectedToolArgumentAmount { + tool_name: "read", + expected: 1, + actual: values.len(), + }); + } + + let file_contents = read_to_string(values[0].as_string()?)?; + + Value::String(file_contents) + } Tool::Random => todo!(), + Tool::RandomFloat => todo!(), + Tool::RandomString => todo!(), Tool::RandomInteger => { if values.len() == 0 { Value::Integer(random()) @@ -83,19 +142,6 @@ impl Tool { }); } } - Tool::Read => { - if values.len() != 1 { - return Err(Error::ExpectedToolArgumentAmount { - tool_name: "read", - expected: 1, - actual: values.len(), - }); - } - - let file_contents = read_to_string(values[0].as_string()?)?; - - Value::String(file_contents) - } Tool::Help => { if values.len() > 1 { return Err(Error::ExpectedToolArgumentAmount { @@ -131,6 +177,15 @@ impl Tool { Value::Table(help_table) } + Tool::Raw => todo!(), + Tool::Sh => todo!(), + Tool::Bash => todo!(), + Tool::Fish => todo!(), + Tool::Zsh => todo!(), + Tool::FromCsv => todo!(), + Tool::ToCsv => todo!(), + Tool::FromJson => todo!(), + Tool::ToJson => todo!(), }; Ok(value) diff --git a/tree-sitter-dust b/tree-sitter-dust index 7021c7f..b47907e 160000 --- a/tree-sitter-dust +++ b/tree-sitter-dust @@ -1 +1 @@ -Subproject commit 7021c7f7894b54ddf8c46c91477613b3071819a4 +Subproject commit b47907ee26b42990e5170ba9521de5ae66d596ef