summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2022-01-08 18:16:56 -0500
committerJesse Luehrs <doy@tozt.net>2022-01-08 18:16:56 -0500
commit39c9b26a8ab10950b2d26c5a6fd2dbe5189fb50c (patch)
treea2bc5480c42c0dac5a6bfb2cd9c8e8c77addbf08
parent879ff29a10b07194b8240aa0a7d4cb19cf397801 (diff)
downloadnbsh-39c9b26a8ab10950b2d26c5a6fd2dbe5189fb50c.tar.gz
nbsh-39c9b26a8ab10950b2d26c5a6fd2dbe5189fb50c.zip
implement if
-rw-r--r--src/shell/history/mod.rs60
1 files changed, 54 insertions, 6 deletions
diff --git a/src/shell/history/mod.rs b/src/shell/history/mod.rs
index f3b755e..1eb98c7 100644
--- a/src/shell/history/mod.rs
+++ b/src/shell/history/mod.rs
@@ -268,6 +268,39 @@ impl std::iter::DoubleEndedIterator for VisibleEntries {
}
}
+struct Stack {
+ frames: Vec<Frame>,
+}
+
+impl Stack {
+ fn new() -> Self {
+ Self { frames: vec![] }
+ }
+
+ fn push(&mut self, frame: Frame) {
+ self.frames.push(frame);
+ }
+
+ fn pop(&mut self) -> Frame {
+ self.frames.pop().unwrap()
+ }
+
+ fn should_execute(&self) -> bool {
+ for frame in &self.frames {
+ if let Frame::If(false) = frame {
+ return false;
+ }
+ }
+ true
+ }
+}
+
+enum Frame {
+ If(bool),
+ While,
+ For,
+}
+
fn run_commands(
ast: crate::parse::ast::Commands,
entry: async_std::sync::Arc<async_std::sync::Mutex<Entry>>,
@@ -330,14 +363,21 @@ fn run_commands(
let commands = ast.commands();
let mut pc = 0;
+ let mut stack = Stack::new();
while pc < commands.len() {
match &commands[pc] {
crate::parse::ast::Command::Pipeline(pipeline) => {
- run_pipeline!(pipeline);
+ if stack.should_execute() {
+ run_pipeline!(pipeline);
+ }
pc += 1;
}
- crate::parse::ast::Command::If(_pipeline) => {
- todo!();
+ crate::parse::ast::Command::If(pipeline) => {
+ if stack.should_execute() {
+ run_pipeline!(pipeline);
+ }
+ stack.push(Frame::If(env.latest_status().success()));
+ pc += 1;
}
crate::parse::ast::Command::While(_pipeline) => {
todo!();
@@ -345,9 +385,17 @@ fn run_commands(
crate::parse::ast::Command::For(_var, _pipeline) => {
todo!();
}
- crate::parse::ast::Command::End => {
- todo!();
- }
+ crate::parse::ast::Command::End => match stack.pop() {
+ Frame::If(_) => {
+ pc += 1;
+ }
+ Frame::While => {
+ todo!()
+ }
+ Frame::For => {
+ todo!()
+ }
+ },
}
}
entry.lock_arc().await.finish(env, event_w).await;