diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/forth/interp.rs | 7 | ||||
| -rwxr-xr-x | src/lib.rs | 20 |
2 files changed, 21 insertions, 6 deletions
diff --git a/src/forth/interp.rs b/src/forth/interp.rs index e41f0d9..512c022 100644 --- a/src/forth/interp.rs +++ b/src/forth/interp.rs @@ -216,9 +216,10 @@ impl Interp { Ok(self.ip.offset < self.wordlist.0[self.ip.word].len()) } - pub fn run(&mut self) -> Result<(), RuntimeError> { - while self.tick()? {} - Ok(()) + pub fn run(&mut self) -> Result<usize, RuntimeError> { + let mut count = 0; + while self.tick()? { count += 1 } + Ok(count) } } @@ -71,12 +71,26 @@ impl ExportedInterp { info!("executing single instruction"); } - pub fn run(&mut self) { - info!("running to completion"); + pub fn run(&mut self) -> Result<usize, String> { + let Some(interp) = &mut self.i else { + return Err("no interpreter".to_string()) + }; + + interp.ip.word = 0; + interp.ip.offset = 0; + interp.run().or_else(|err| Err(format!("runtime error: {:?}", err))) + } + + pub fn stack(&self) -> Vec<i32> { + let Some(interp) = &self.i else { + return vec![] + }; + + return interp.stack.0.clone() } pub fn wordlist(&self) -> Vec<ExportedByteCode> { - let Some(interp) = self.i.as_ref() else { + let Some(interp) = &self.i else { return vec![] }; |
