From 869cc68abd5a25f9981a6d452d3bf49cd326293b Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 2 Apr 2023 23:04:24 -0500 Subject: [PATCH] surely this is not computer science --- src/main.rs | 25 ++++--------------------- src/parse.rs | 10 ---------- src/shell.rs | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 31 deletions(-) diff --git a/src/main.rs b/src/main.rs index feeb3a6..db6a973 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,27 +63,10 @@ fn main() -> Result<()> { info!("◇ {}", s); let tree = parse::parse(&s)?; debug!(" {:?}", tree); - let parts: Vec<&str> = s.split_whitespace().collect(); - - if parts.len() > 0 { - let cmd = parts[0].to_string(); - let args = if parts.len() > 1 { - parts[1..].to_vec() - } else { - vec![] - }; - match shell.eval(cmd.clone(), args.clone()) { - Ok(true) => debug!(" ok"), - Ok(false) => warn!(" fail"), - Err(e) => { - error!("▷ {} {} ● {}", cmd, args.join(" "), e); - println!("error: {}", e); - } - } - // Some commands don't leave the terminal in a clean state, so we use reset - // to ensure that our input and output modes are what we expect them to be. - shell.reset()?; - } + shell.exec(tree)?; + // Some commands don't leave the terminal in a clean state, so we use reset + // to ensure that our input and output modes are what we expect them to be. + shell.reset()?; prompt.print(&mut shell.output)?; continue; } diff --git a/src/parse.rs b/src/parse.rs index e9acd8c..8589c31 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -1,6 +1,4 @@ -use crate::shell::Shell; use log::debug; -use std::fmt; use thiserror::Error; #[derive(Debug, Error)] @@ -244,11 +242,3 @@ mod tests { ident("one") Pipe ident("two"); } } - -/* - - - - - -*/ diff --git a/src/shell.rs b/src/shell.rs index f071765..190e521 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -2,6 +2,7 @@ use crate::{ ext::{Command, Echo, Printenv, Tail, Which}, input, line::Line, + parse::Node, log::*, output, // parse::parse, @@ -96,6 +97,19 @@ impl Shell { } } + pub fn exec(&mut self, mut node: Node) -> Result { + loop { + match node { + Node::Empty => break, + Node::Command{name, args} => { + self.eval(name, args.iter().map(|s| s.as_str()).collect())?; + node = Node::Empty; + }, + } + } + Ok(true) + } + pub fn eval(&mut self, cmd: String, args: Vec<&str>) -> Result { match cmd.as_str() { "pwd" => {