diff options
| -rw-r--r-- | main.mjs | 21 | ||||
| -rw-r--r-- | src/forth/interp.rs | 6 | ||||
| -rwxr-xr-x | src/forth/mod.rs | 4 | ||||
| -rw-r--r-- | src/forth/parser.rs | 5 | ||||
| -rwxr-xr-x | src/lib.rs | 23 |
5 files changed, 29 insertions, 30 deletions
@@ -1,21 +1,22 @@ -import init from './pkg/automathon.js'; +import init, { compile, run, tick } from './pkg/automathon.js'; async function loaded() { console.debug('running init'); const mod = await init(); console.debug('init done', mod); - document.querySelector("#compile").onclick = e => { - console.debug("compile clicked", e); - mod.compile(); + document.querySelector('#compile').onclick = e => { + console.debug('compile clicked', e); + let text = document.querySelector('textarea').textContent; + compile(text); }; - document.querySelector("#tick").onclick = e => { - console.debug("tick clicked", e); - mod.tick(); + document.querySelector('#tick').onclick = e => { + console.debug('tick clicked', e); + tick(); }; - document.querySelector("#run").onclick = e => { - console.debug("run clicked", e); - mod.run(); + document.querySelector('#run').onclick = e => { + console.debug('run clicked', e); + run(); }; } diff --git a/src/forth/interp.rs b/src/forth/interp.rs index c6dcb72..b34ed31 100644 --- a/src/forth/interp.rs +++ b/src/forth/interp.rs @@ -66,15 +66,15 @@ pub(super) struct DataStack(pub(super) Vec<i32>); pub(super) struct CallStack(pub(super) Vec<InstructionPointer>); #[derive(Clone, Debug)] -pub(super) struct WordList(pub(super) Vec<ByteCode>); +pub struct WordList(pub(super) Vec<ByteCode>); #[derive(Debug)] pub struct Interp { // todo: don't be pub, probably - pub stack: DataStack, + pub(super) stack: DataStack, callstack: CallStack, // todo: don't be pub - pub wordlist: WordList, + pub(super) wordlist: WordList, ip: InstructionPointer, } diff --git a/src/forth/mod.rs b/src/forth/mod.rs index 5792148..063b40e 100755 --- a/src/forth/mod.rs +++ b/src/forth/mod.rs @@ -1,5 +1,5 @@ -mod interp; -mod parser; +pub mod interp; +pub mod parser; #[cfg(test)] mod tests { diff --git a/src/forth/parser.rs b/src/forth/parser.rs index 55ad64b..ac6715d 100644 --- a/src/forth/parser.rs +++ b/src/forth/parser.rs @@ -1,5 +1,7 @@ use super::interp::{ByteCode, OpCode, WordList}; +use log::debug; + use std::collections::HashMap; use std::iter::{Enumerate, Iterator}; use std::str::Chars; @@ -45,7 +47,7 @@ pub struct Parser<'a> { // todo: don't be pub, have a method to extract a wordlist pub wordlist: WordList, // catalog of word to word index in `wordlist` - wordalog: WordCatalog<'a>, + pub wordalog: WordCatalog<'a>, // holds a stack of indices into `wordlist` that are currently // being defined, with the top of stack being the most recent // definition. @@ -79,6 +81,7 @@ impl<'a> Parser<'a> { self.enumerator.by_ref() .find(|(_i, c)| c.is_whitespace())?; let word = &self.text[start..end]; + debug!("Parser::next_word → ‘{}’ ({} → {})", word, start, end); Some((word, start, end)) } @@ -2,7 +2,7 @@ use log::{Level, debug, info}; use console_log; use wasm_bindgen::prelude::*; -mod forth; +pub mod forth; #[wasm_bindgen] extern { @@ -11,8 +11,13 @@ extern { } #[wasm_bindgen] -pub fn compile() { - info!("compiling code"); +pub fn compile(text: &str) { + info!("compiling code: `{}`", text); + let mut p = forth::parser::Parser::new(&text); + p.parse().expect("couldn't parse text"); + debug!("wordlist: {:?}", &p.wordlist); + let interp = forth::interp::Interp::new(p.wordlist); + debug!("interp: {:?}", interp); } #[wasm_bindgen] @@ -28,17 +33,7 @@ pub fn run() { #[wasm_bindgen(start)] pub fn init() -> Result<(), JsValue> { console_log::init_with_level(Level::Debug).expect("couldn't init console log"); - debug!("starting run"); - - let window = web_sys::window().expect("no global `window` exists"); - let document = window.document().expect("should have `document` on window"); - let body = document.body().expect("document should have `body`"); - - let val = document.create_element("p")?; - val.set_text_content(Some("hi there")); - body.append_child(&val)?; - - debug!("done in rust's run"); + info!("wasm init"); Ok(()) } |
