diff --git a/crates/oyster/src/main.rs b/crates/oyster/src/main.rs index 84b2f61..8473cc7 100644 --- a/crates/oyster/src/main.rs +++ b/crates/oyster/src/main.rs @@ -9,13 +9,8 @@ fn main() { shell.builtins_mut().add_defaults(); let mut exit_code = Status::SUCCESS; - loop { + while shell.is_running() { let prog = readline("> ").unwrap(); - - if prog.trim() == "exit" { - break; - } - let ast = Code::try_from(prog.as_ref()).unwrap(); exit_code = shell.run(&ast).unwrap(); } diff --git a/crates/oyster_runtime/src/builtins/mod.rs b/crates/oyster_runtime/src/builtins/mod.rs index c8fec4e..bac3ad3 100644 --- a/crates/oyster_runtime/src/builtins/mod.rs +++ b/crates/oyster_runtime/src/builtins/mod.rs @@ -12,6 +12,11 @@ pub struct Builtin { pub fun: fn(shell: &mut Shell, args: &[Cow]), } +#[builtin(description = "exit the shell", nofork)] +pub fn exit(shell: &mut Shell, _args: &[Cow]) { + shell.is_running = false; +} + #[builtin(description = "prints help for different builtins")] pub fn help(shell: &mut Shell, _args: &[Cow]) { println!( @@ -48,6 +53,7 @@ impl BuiltinMap { /// Add default builtins. pub fn add_defaults(&mut self) { + self.add(exit); self.add(help); } } diff --git a/crates/oyster_runtime/src/lib.rs b/crates/oyster_runtime/src/lib.rs index d2d78f0..b301371 100644 --- a/crates/oyster_runtime/src/lib.rs +++ b/crates/oyster_runtime/src/lib.rs @@ -66,6 +66,7 @@ pub enum RuntimeError { } pub struct Shell { + is_running: bool, builtins: BuiltinMap, } @@ -79,10 +80,15 @@ impl Shell { } Ok(Shell { + is_running: true, builtins: Default::default(), }) } + pub fn is_running(&self) -> bool { + self.is_running + } + pub fn run<'a>(&mut self, code: &'a ast::Code) -> Result { let mut last_status = Status::SUCCESS;