aboutsummaryrefslogtreecommitdiffstats
path: root/src/run
diff options
context:
space:
mode:
authorJonas Maier <jonas@x77.dev>2026-05-31 19:21:44 +0200
committerJonas Maier <jonas@x77.dev>2026-05-31 19:21:44 +0200
commit81759dd51eb1f6f9a7dc8af9b2b8126ff7dfab24 (patch)
treeaf451a170b30e05148088d477b725023d02c505d /src/run
parent08f3af622cc3e7b3f85a60c6ffe83d9d70e9dc02 (diff)
downloadpish-81759dd51eb1f6f9a7dc8af9b2b8126ff7dfab24.tar.gz
regex based case statements
Diffstat (limited to 'src/run')
-rw-r--r--src/run/builtin.rs31
-rw-r--r--src/run/mod.rs7
2 files changed, 35 insertions, 3 deletions
diff --git a/src/run/builtin.rs b/src/run/builtin.rs
index fab7565..6f7cc5d 100644
--- a/src/run/builtin.rs
+++ b/src/run/builtin.rs
@@ -1098,7 +1098,36 @@ mod dbg {
Ok(())
}
}
+
+ #[derive(Copy, Clone)]
+ pub struct case_match;
+ impl Builtin for case_match {
+ fn name(&self) -> &str {
+ "case_match"
+ }
+
+ fn io(
+ &self,
+ _session: Arc<Mutex<Session>>,
+ args: &[BString],
+ _stdin: &mut dyn Read,
+ stdout: &mut dyn Write,
+ ) -> Result {
+ let regex = match crate::parse::regex::Pattern::parse_from_bytes(&args[0]) {
+ Ok(r) => r,
+ Err(e) => {
+ writeln!(stdout, "not a valid regex: {e:?}")?;
+ return Err(Error::Exit(1));
+ },
+ };
+
+ let compiled = regex.compile();
+ writeln!(stdout, "{compiled:?}")?;
+
+ Ok(())
+ }
+ }
}
#[cfg(debug_assertions)]
-pub use dbg::{debug, re};
+pub use dbg::{debug, re, case_match};
diff --git a/src/run/mod.rs b/src/run/mod.rs
index f86278d..c3ceb76 100644
--- a/src/run/mod.rs
+++ b/src/run/mod.rs
@@ -456,8 +456,9 @@ impl Executor {
stdout: OutputWriter,
) -> SpawnedCmd {
for branch in c.branches.into_iter() {
- // TODO: regex case patterns
- if branch.pattern == c.discriminant {
+ // TODO: do not compile every time
+ let compiled = branch.pattern.compile();
+ if compiled.matches(&c.discriminant) {
return self.execute_block(branch.block, stdin, stdout);
}
}
@@ -732,6 +733,8 @@ const BUILTINS: &[&'static dyn BuiltinClone] = &[
&builtin::logo,
&builtin::export,
&builtin::pish_theme,
+ #[cfg(debug_assertions)]
+ &builtin::case_match,
];
pub fn builtin_map() -> HashMap<BString, &'static dyn BuiltinClone> {