aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJonas Maier <>2026-03-07 15:42:50 +0100
committerJonas Maier <>2026-03-07 15:42:50 +0100
commit56a1548c9a10a2bfc6ddda0e0d28ae0087aa193f (patch)
tree714b9e71de352430612a18960c2925b35d9319d8 /src
parenta4c61ccccc5a83a8d9506d996fd695104ef14e2d (diff)
downloadpish-56a1548c9a10a2bfc6ddda0e0d28ae0087aa193f.tar.gz
reorganize towards function execution
Diffstat (limited to 'src')
-rw-r--r--src/main.rs5
-rw-r--r--src/run/mod.rs50
2 files changed, 26 insertions, 29 deletions
diff --git a/src/main.rs b/src/main.rs
index 1dd86bc..7223e26 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -26,7 +26,6 @@ use raw::*;
use crate::cursor::{Direction, move_cursor};
use crate::parse::{Ast, PreExpansion};
-use crate::run::CommandDispatch;
macro_rules! print {
($($x:tt)*) => {{
@@ -62,8 +61,8 @@ pub struct Session {
raw: ScopedRawMode,
line: LineBuf,
history: Vec<BString>,
- dispatch: CommandDispatch,
prev_path: BString,
+ builtins: HashMap<BString, &'static dyn run::Builtin>,
vars: HashMap<BString, BString>,
funs: HashMap<BString, Ast<PreExpansion>>,
@@ -203,7 +202,7 @@ fn event_loop() {
raw,
line: LineBuf::new(),
history: Vec::new(),
- dispatch: CommandDispatch::new(),
+ builtins: run::builtin_map(),
prev_path: vec![b'.'],
history_visit: 0,
vars: HashMap::new(),
diff --git a/src/run/mod.rs b/src/run/mod.rs
index f32007e..3b48f75 100644
--- a/src/run/mod.rs
+++ b/src/run/mod.rs
@@ -66,7 +66,7 @@ impl Executor {
(None, None)
};
- let dc = self.se.lock().unwrap().dispatch.get(&cmd.cmd[..]);
+ let dc = get_command_kind(&self.se.lock().unwrap(), &cmd.cmd[..]);
match dc {
CommandKind::Path(path) => {
@@ -147,6 +147,10 @@ impl Executor {
threads.push(handle);
}
+
+ CommandKind::Fun(ast) => {
+ todo!()
+ }
}
prev_reader = reader;
@@ -346,36 +350,30 @@ const BUILTINS: &[&'static dyn Builtin] = &[
&builtin::parse,
];
-pub struct CommandDispatch {
- map: HashMap<BString, CommandKind>,
-}
-
-impl CommandDispatch {
- pub fn new() -> Self {
- let mut map = HashMap::new();
-
- // builtins
- for &b in BUILTINS {
- map.insert(b.name().as_bytes().to_vec(), CommandKind::Builtin(b));
- }
-
- Self { map }
- }
-
- fn get(&self, cmd: &bstr) -> CommandKind {
- let path_cmd = CommandKind::Path(PathBuf::from(OsStr::from_bytes(cmd)));
- if cmd.contains(&b'/') {
- path_cmd
- } else if let Some(cmd) = self.map.get(cmd) {
- cmd.clone()
- } else {
- path_cmd
- }
+pub fn builtin_map() -> HashMap<BString, &'static dyn Builtin> {
+ let mut map = HashMap::new();
+ for &b in BUILTINS {
+ map.insert(b.name().as_bytes().to_vec(), b);
}
+ map
}
#[derive(Clone)]
pub enum CommandKind {
Builtin(&'static dyn Builtin),
+ Fun(Ast<PreExpansion>),
Path(PathBuf),
}
+
+pub fn get_command_kind(se: &Session, cmd: &bstr) -> CommandKind {
+ let path_cmd = CommandKind::Path(PathBuf::from(OsStr::from_bytes(cmd)));
+ if cmd.contains(&b'/') {
+ path_cmd
+ } else if let Some(fun) = se.funs.get(cmd) {
+ CommandKind::Fun(fun.clone())
+ } else if let Some(builtin) = se.builtins.get(cmd) {
+ CommandKind::Builtin(builtin.clone())
+ } else {
+ path_cmd
+ }
+}