From 39c9b26a8ab10950b2d26c5a6fd2dbe5189fb50c Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 8 Jan 2022 18:16:56 -0500 Subject: implement if --- src/shell/history/mod.rs | 60 +++++++++++++++++++++++++++++++++++++++++++----- 1 file 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, +} + +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>, @@ -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; -- cgit v1.2.3-54-g00ecf