1
0

Add command logic

This commit is contained in:
Jeff 2024-01-25 08:43:21 -05:00
parent 5bdb9f116f
commit 70ad08128c
2 changed files with 36 additions and 12 deletions

View File

@ -1,30 +1,48 @@
use std::process;
use serde::{Deserialize, Serialize}; 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)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Command { pub struct Command {
binary_name: String, command_texts: Vec<String>,
arguments: Vec<String>,
} }
impl AbstractTree for Command { impl AbstractTree for Command {
fn from_syntax(node: tree_sitter::Node, source: &str, context: &crate::Map) -> Result<Self> { fn from_syntax(node: tree_sitter::Node, source: &str, _context: &crate::Map) -> Result<Self> {
Error::expect_syntax_node(source, "command", node)?; 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);
} }
fn run(&self, source: &str, context: &Map) -> Result<Value> { Ok(Command { command_texts })
todo!()
} }
fn expected_type(&self, context: &Map) -> Result<Type> { fn run(&self, _source: &str, _context: &Map) -> Result<Value> {
todo!() 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<Type> {
Ok(Type::String)
} }
} }
impl Format for Command { impl Format for Command {
fn format(&self, output: &mut String, indent_level: u8) { fn format(&self, _output: &mut String, _indent_level: u8) {
todo!() todo!()
} }
} }

View File

@ -46,9 +46,11 @@ impl AbstractTree for Expression {
)?)), )?)),
"yield" => Expression::Yield(Box::new(Yield::from_syntax(child, source, _context)?)), "yield" => Expression::Yield(Box::new(Yield::from_syntax(child, source, _context)?)),
"new" => Expression::New(New::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 { return Err(Error::UnexpectedSyntaxNode {
expected: "value, identifier, index, math, logic, function call, new or ->" expected:
"value, identifier, index, math, logic, function call, new, context or ->"
.to_string(), .to_string(),
actual: child.kind().to_string(), actual: child.kind().to_string(),
location: child.start_position(), location: child.start_position(),
@ -70,6 +72,7 @@ impl AbstractTree for Expression {
Expression::Index(index) => index.check_type(_source, _context), Expression::Index(index) => index.check_type(_source, _context),
Expression::Yield(r#yield) => r#yield.check_type(_source, _context), Expression::Yield(r#yield) => r#yield.check_type(_source, _context),
Expression::New(new) => new.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::Index(index) => index.run(_source, _context),
Expression::Yield(r#yield) => r#yield.run(_source, _context), Expression::Yield(r#yield) => r#yield.run(_source, _context),
Expression::New(new) => new.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::Index(index) => index.expected_type(_context),
Expression::Yield(r#yield) => r#yield.expected_type(_context), Expression::Yield(r#yield) => r#yield.expected_type(_context),
Expression::New(new) => new.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::Index(index) => index.format(_output, _indent_level),
Expression::Yield(r#yield) => r#yield.format(_output, _indent_level), Expression::Yield(r#yield) => r#yield.format(_output, _indent_level),
Expression::New(new) => new.format(_output, _indent_level), Expression::New(new) => new.format(_output, _indent_level),
Expression::Command(command) => command.format(_output, _indent_level),
} }
} }
} }