From 70ad08128cd136a87823932d7f67ec0c5887ebd6 Mon Sep 17 00:00:00 2001 From: Jeff Date: Thu, 25 Jan 2024 08:43:21 -0500 Subject: [PATCH] Add command logic --- src/abstract_tree/command.rs | 38 ++++++++++++++++++++++++--------- src/abstract_tree/expression.rs | 10 +++++++-- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/abstract_tree/command.rs b/src/abstract_tree/command.rs index 1f61ddc..df54069 100644 --- a/src/abstract_tree/command.rs +++ b/src/abstract_tree/command.rs @@ -1,30 +1,48 @@ +use std::process; + use serde::{Deserialize, Serialize}; -use crate::{AbstractTree, Error, Format, Identifier, Map, Result, Type, Value}; +use crate::{AbstractTree, Error, Format, Map, Result, Type, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct Command { - binary_name: String, - arguments: Vec, + command_texts: Vec, } impl AbstractTree for Command { - fn from_syntax(node: tree_sitter::Node, source: &str, context: &crate::Map) -> Result { + fn from_syntax(node: tree_sitter::Node, source: &str, _context: &crate::Map) -> Result { Error::expect_syntax_node(source, "command", node)?; - let identifier_node = node.child(1) + + let mut command_texts = Vec::new(); + + for index in 1..node.child_count() { + let text_node = node.child(index).unwrap(); + let text = source[text_node.byte_range()].to_string(); + + command_texts.push(text); + } + + Ok(Command { command_texts }) } - fn run(&self, source: &str, context: &Map) -> Result { - todo!() + fn run(&self, _source: &str, _context: &Map) -> Result { + let output = process::Command::new(self.command_texts.first().unwrap()) + .args(&self.command_texts[1..]) + .spawn()? + .wait_with_output()? + .stdout; + let string = String::from_utf8(output)?; + + Ok(Value::String(string)) } - fn expected_type(&self, context: &Map) -> Result { - todo!() + fn expected_type(&self, _context: &Map) -> Result { + Ok(Type::String) } } impl Format for Command { - fn format(&self, output: &mut String, indent_level: u8) { + fn format(&self, _output: &mut String, _indent_level: u8) { todo!() } } diff --git a/src/abstract_tree/expression.rs b/src/abstract_tree/expression.rs index 507ea2a..b49a536 100644 --- a/src/abstract_tree/expression.rs +++ b/src/abstract_tree/expression.rs @@ -46,10 +46,12 @@ impl AbstractTree for Expression { )?)), "yield" => Expression::Yield(Box::new(Yield::from_syntax(child, source, _context)?)), "new" => Expression::New(New::from_syntax(child, source, _context)?), + "command" => Expression::Command(Command::from_syntax(child, source, _context)?), _ => { return Err(Error::UnexpectedSyntaxNode { - expected: "value, identifier, index, math, logic, function call, new or ->" - .to_string(), + expected: + "value, identifier, index, math, logic, function call, new, context or ->" + .to_string(), actual: child.kind().to_string(), location: child.start_position(), relevant_source: source[child.byte_range()].to_string(), @@ -70,6 +72,7 @@ impl AbstractTree for Expression { Expression::Index(index) => index.check_type(_source, _context), Expression::Yield(r#yield) => r#yield.check_type(_source, _context), Expression::New(new) => new.check_type(_source, _context), + Expression::Command(command) => command.check_type(_source, _context), } } @@ -83,6 +86,7 @@ impl AbstractTree for Expression { Expression::Index(index) => index.run(_source, _context), Expression::Yield(r#yield) => r#yield.run(_source, _context), Expression::New(new) => new.run(_source, _context), + Expression::Command(command) => command.run(_source, _context), } } @@ -96,6 +100,7 @@ impl AbstractTree for Expression { Expression::Index(index) => index.expected_type(_context), Expression::Yield(r#yield) => r#yield.expected_type(_context), Expression::New(new) => new.expected_type(_context), + Expression::Command(command) => command.expected_type(_context), } } } @@ -111,6 +116,7 @@ impl Format for Expression { Expression::Index(index) => index.format(_output, _indent_level), Expression::Yield(r#yield) => r#yield.format(_output, _indent_level), Expression::New(new) => new.format(_output, _indent_level), + Expression::Command(command) => command.format(_output, _indent_level), } } }