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 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<String>,
command_texts: Vec<String>,
}
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)?;
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<Value> {
todo!()
fn run(&self, _source: &str, _context: &Map) -> Result<Value> {
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> {
todo!()
fn expected_type(&self, _context: &Map) -> Result<Type> {
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!()
}
}

View File

@ -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),
}
}
}