From 0753ea2b9e10b0f5030b05e84cd8490cc9ec7b34 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Sat, 23 Aug 2025 13:05:07 -0400 Subject: highlight running code --- src/lib.rs | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 32a8ffc..840a86c 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,6 +10,12 @@ pub struct ExportedInstructionPointer { pub offset: usize, } +impl From<&forth::interp::InstructionPointer> for ExportedInstructionPointer { + fn from(ip: &forth::interp::InstructionPointer) -> Self { + Self { word: ip.word, offset: ip.offset } + } +} + // wasm can't wrap Vec>, so we need a custom type // - 23-aug-2025 #[wasm_bindgen] @@ -67,8 +73,12 @@ impl ExportedInterp { true } - pub fn tick(&mut self) { + pub fn tick(&mut self) -> Result { + let Some(interp) = &mut self.i else { + return Err("no interpreter".to_string()) + }; info!("executing single instruction"); + interp.tick().or_else(|err| Err(format!("runtime error: {:?}", err))) } pub fn run(&mut self) -> Result { @@ -98,15 +108,21 @@ impl ExportedInterp { interp.wordlist.iter().map(|bc| ExportedByteCode::from_bc(bc)).collect() } + pub fn callstack(&self) -> Vec { + let Some(interp) = &self.i else { + return vec![] + }; + info!("callstack: ‘{:?}’", interp.callstack); + interp.callstack.0.iter() + .map(|ip| ip.into()) + .collect() + } + pub fn ip(&self) -> ExportedInstructionPointer { let Some(interp) = self.i.as_ref() else { return ExportedInstructionPointer { word: 0, offset: 0 } }; - - ExportedInstructionPointer { - word: interp.ip.word, - offset: interp.ip.offset, - } + (&interp.ip).into() } } -- cgit v1.3