surely this is not computer science

parse-tree
Jordan Orelli 2 years ago
parent 0c0bd77eea
commit 869cc68abd

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

@ -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");
}
}
/*
*/

@ -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<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> {
match cmd.as_str() {
"pwd" => {

Loading…
Cancel
Save