diff options
| author | Jonas Maier <jonas@x77.dev> | 2026-05-31 17:12:12 +0200 |
|---|---|---|
| committer | Jonas Maier <jonas@x77.dev> | 2026-05-31 17:12:12 +0200 |
| commit | 08f3af622cc3e7b3f85a60c6ffe83d9d70e9dc02 (patch) | |
| tree | 5b2b7d303fc88023c67e6693194b072ac40f9630 /src/run | |
| parent | 7679654c5710d08f626f164731b9b929b93957d8 (diff) | |
| download | pish-08f3af622cc3e7b3f85a60c6ffe83d9d70e9dc02.tar.gz | |
basic case statement
Diffstat (limited to 'src/run')
| -rw-r--r-- | src/run/mod.rs | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/run/mod.rs b/src/run/mod.rs index a2a6cb1..f86278d 100644 --- a/src/run/mod.rs +++ b/src/run/mod.rs @@ -26,6 +26,7 @@ pub enum ExecError { Parse(crate::parse::ParseError), Break, Continue, + NoCaseMatch, } impl ExecError { @@ -71,6 +72,7 @@ impl ExecError { ExecError::Parse(pe) => format!("parse error: {pe:?}"), ExecError::Break => "break: only useful in loops".to_string(), ExecError::Continue => "continue: only useful in loops".to_string(), + ExecError::NoCaseMatch => "case: no pattern matched".to_string(), } } } @@ -356,6 +358,7 @@ impl Executor { Ast::Pipes(pipes) => self.execute_pipeline(pipes, stdin, stdout), Ast::If(cond) => self.execute_if(cond, stdin, stdout), Ast::While(w) => self.execute_while(w, stdin, stdout), + Ast::Case(c) => self.execute_case(c, stdin, stdout), } } @@ -446,6 +449,21 @@ impl Executor { SpawnedCmd::Fun(handle) } + fn execute_case( + &mut self, + c: parse::Case<PostExpansion>, + stdin: InputReader, + stdout: OutputWriter, + ) -> SpawnedCmd { + for branch in c.branches.into_iter() { + // TODO: regex case patterns + if branch.pattern == c.discriminant { + return self.execute_block(branch.block, stdin, stdout); + } + } + SpawnedCmd::Joined(Err(ExecError::NoCaseMatch)) + } + pub fn execute_script( &mut self, s: parse::Script, |
