summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rwxr-xr-xsrc/lib.rs31
1 files changed, 19 insertions, 12 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 60a406f..487a5ce 100755
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,5 +1,4 @@
use log::{Level, error, info};
-use console_log;
use wasm_bindgen::prelude::*;
pub mod forth;
@@ -25,11 +24,11 @@ impl ExportedByteCode {
fn tr_op(op: &forth::vm::OpCode) -> String {
use forth::vm::OpCode::*;
let s = match op {
- If(t, None) => format!("If({}, none)", t),
- If(t, Some(f)) => format!("If({}, {})", t, f),
- TIf(t, None) => format!("TIf({}, none)", t),
- TIf(t, Some(f)) => format!("TIf({}, {})", t, f),
- other => format!("{:?}", other),
+ If(t, None) => format!("If({t}, none)"),
+ If(t, Some(f)) => format!("If({t}, {f})"),
+ TIf(t, None) => format!("TIf({t}, none)"),
+ TIf(t, Some(f)) => format!("TIf({t}, {f})"),
+ other => format!("{other:?}"),
};
s.to_string()
}
@@ -47,6 +46,10 @@ impl ExportedByteCode {
self.0.len()
}
+ pub fn is_empty(&self) -> bool {
+ self.0.is_empty()
+ }
+
pub fn at(&self, offset: usize) -> String {
self.0[offset].clone()
}
@@ -86,6 +89,10 @@ impl ExportedWordAnnotations {
self.0.len()
}
+ pub fn is_empty(&self) -> bool {
+ self.0.is_empty()
+ }
+
pub fn at(&self, offset: usize) -> ExportedAnnotation {
self.0[offset].clone()
}
@@ -104,15 +111,15 @@ impl ExportedVM {
}
pub fn compile(&mut self, text: &str) -> bool {
- let mut comp = forth::compiler::Compiler::new(&text);
+ let mut comp = forth::compiler::Compiler::new(text);
if let Err(e) = comp.compile() {
- error!("couldn't compile program text: {:?}", e);
+ error!("couldn't compile program text: {e:?}");
return false
}
self.annos =
comp.annotations.iter()
.map(|word_anno| -> ExportedWordAnnotations {
- word_anno.into_iter().map(|a| a.into()).collect()
+ word_anno.iter().map(|a| a.into()).collect()
})
.collect();
let vm = forth::vm::VM::new(comp.wordlist);
@@ -124,7 +131,7 @@ impl ExportedVM {
let Some(vm) = &mut self.vm else {
return Err("no vm".to_string())
};
- vm.tick().or_else(|err| Err(format!("runtime error: {:?}", err)))
+ vm.tick().map_err(|err| format!("runtime error: {err:?}"))
}
pub fn run(&mut self) -> Result<usize, String> {
@@ -134,7 +141,7 @@ impl ExportedVM {
vm.ip.word = 0;
vm.ip.offset = 0;
- vm.run().or_else(|err| Err(format!("runtime error: {:?}", err)))
+ vm.run().map_err(|err| format!("runtime error: {err:?}"))
}
pub fn stack(&self) -> Vec<i32> {
@@ -142,7 +149,7 @@ impl ExportedVM {
return vec![]
};
- return vm.stack.0.clone()
+ vm.stack.0.clone()
}
pub fn wordlist(&self) -> Vec<ExportedByteCode> {