surely this is not computer science

parse-tree
Jordan Orelli 1 year ago
parent 0c0bd77eea
commit 869cc68abd

@ -63,27 +63,10 @@ fn main() -> Result<()> {
info!("◇ {}", s); info!("◇ {}", s);
let tree = parse::parse(&s)?; let tree = parse::parse(&s)?;
debug!(" {:?}", tree); debug!(" {:?}", tree);
let parts: Vec<&str> = s.split_whitespace().collect(); shell.exec(tree)?;
// Some commands don't leave the terminal in a clean state, so we use reset
if parts.len() > 0 { // to ensure that our input and output modes are what we expect them to be.
let cmd = parts[0].to_string(); shell.reset()?;
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()?;
}
prompt.print(&mut shell.output)?; prompt.print(&mut shell.output)?;
continue; continue;
} }

@ -1,6 +1,4 @@
use crate::shell::Shell;
use log::debug; use log::debug;
use std::fmt;
use thiserror::Error; use thiserror::Error;
#[derive(Debug, Error)] #[derive(Debug, Error)]
@ -244,11 +242,3 @@ mod tests {
ident("one") Pipe ident("two"); ident("one") Pipe ident("two");
} }
} }
/*
*/

@ -2,6 +2,7 @@ use crate::{
ext::{Command, Echo, Printenv, Tail, Which}, ext::{Command, Echo, Printenv, Tail, Which},
input, input,
line::Line, line::Line,
parse::Node,
log::*, log::*,
output, output,
// parse::parse, // parse::parse,
@ -96,6 +97,19 @@ impl Shell {
} }
} }
pub fn exec(&mut self, mut node: Node) -> Result<bool> {
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<bool> { pub fn eval(&mut self, cmd: String, args: Vec<&str>) -> Result<bool> {
match cmd.as_str() { match cmd.as_str() {
"pwd" => { "pwd" => {

Loading…
Cancel
Save