summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--main.mjs5
-rw-r--r--src/forth/interp.rs18
-rwxr-xr-xsrc/lib.rs57
3 files changed, 67 insertions, 13 deletions
diff --git a/main.mjs b/main.mjs
index c07dccd..a555a60 100644
--- a/main.mjs
+++ b/main.mjs
@@ -1,4 +1,4 @@
-import init, { compile, run, tick } from './pkg/automathon.js';
+import init, { compile, run, tick, wordlist, ip, interp } from './pkg/automathon.js';
async function loaded() {
console.debug('running init');
@@ -13,6 +13,9 @@ async function loaded() {
document.querySelector('#tick').onclick = e => {
console.debug('tick clicked', e);
tick();
+ console.debug('result of ip', ip());
+ console.debug('result of wordlist', wordlist());
+ console.debug('result of interp', interp());
};
document.querySelector('#run').onclick = e => {
console.debug('run clicked', e);
diff --git a/src/forth/interp.rs b/src/forth/interp.rs
index b34ed31..cacc5e3 100644
--- a/src/forth/interp.rs
+++ b/src/forth/interp.rs
@@ -28,7 +28,7 @@ pub enum OpCode {
}
#[derive(Clone, Debug)]
-pub(super) struct ByteCode(pub(super) Vec<OpCode>);
+pub struct ByteCode(pub Vec<OpCode>);
impl ByteCode {
pub fn len(&self) -> usize {
@@ -45,9 +45,9 @@ impl Index<usize> for ByteCode {
}
#[derive(Debug, Copy, Clone, PartialEq)]
-pub(super) struct InstructionPointer {
- pub(super) word: usize,
- pub(super) offset: usize,
+pub struct InstructionPointer {
+ pub word: usize,
+ pub offset: usize,
}
impl InstructionPointer {
@@ -60,21 +60,21 @@ impl InstructionPointer {
}
#[derive(Debug)]
-pub(super) struct DataStack(pub(super) Vec<i32>);
+pub struct DataStack(pub Vec<i32>);
#[derive(Debug)]
-pub(super) struct CallStack(pub(super) Vec<InstructionPointer>);
+pub struct CallStack(pub Vec<InstructionPointer>);
#[derive(Clone, Debug)]
-pub struct WordList(pub(super) Vec<ByteCode>);
+pub struct WordList(pub Vec<ByteCode>);
#[derive(Debug)]
pub struct Interp {
// todo: don't be pub, probably
- pub(super) stack: DataStack,
+ pub stack: DataStack,
callstack: CallStack,
// todo: don't be pub
- pub(super) wordlist: WordList,
+ pub wordlist: WordList,
ip: InstructionPointer,
}
diff --git a/src/lib.rs b/src/lib.rs
index f4fc294..ddb87fd 100755
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -5,9 +5,23 @@ use wasm_bindgen::prelude::*;
pub mod forth;
#[wasm_bindgen]
-extern {
- #[wasm_bindgen(js_namespace = console)]
- fn debug(s: &str);
+pub struct ExportedInstructionPointer {
+ pub word: usize,
+ pub offset: usize,
+}
+
+#[wasm_bindgen]
+pub struct ExportedByteCode(Vec<String>);
+
+#[wasm_bindgen]
+impl ExportedByteCode {
+ pub fn len(&self) -> usize {
+ self.0.len()
+ }
+
+ pub fn at(&self, index: usize) -> String {
+ self.0[index].clone()
+ }
}
#[wasm_bindgen]
@@ -26,6 +40,43 @@ pub fn tick() {
}
#[wasm_bindgen]
+pub fn ip() -> ExportedInstructionPointer {
+ ExportedInstructionPointer {
+ word: 0,
+ offset: 0,
+ }
+}
+
+#[wasm_bindgen]
+pub fn wordlist() -> Vec<ExportedByteCode> {
+ vec![
+ ExportedByteCode(vec!["NOP".to_string(), "2".to_string(), "DUP".to_string()]),
+ ]
+}
+
+#[wasm_bindgen]
+pub struct ExportedInterp {
+ i: forth::interp::Interp,
+}
+
+#[wasm_bindgen]
+impl ExportedInterp {
+ fn from_interp(i: forth::interp::Interp) -> Self {
+ Self { i }
+ }
+
+ pub fn foo(&self) {
+ info!("in interp::foo: {:?}", self.i.wordlist);
+ }
+}
+
+#[wasm_bindgen]
+pub fn interp() -> ExportedInterp {
+ let i = forth::interp::Interp::new(forth::interp::WordList(vec![]));
+ ExportedInterp::from_interp(i)
+}
+
+#[wasm_bindgen]
pub fn run() {
info!("running to completion");
}