summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock12
-rw-r--r--Cargo.toml2
-rw-r--r--index.html15
-rw-r--r--main.css5
-rw-r--r--main.mjs24
-rw-r--r--src/forth/interp.rs4
-rwxr-xr-xsrc/lib.rs24
7 files changed, 71 insertions, 15 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 88c32de..ef827e2 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6,6 +6,8 @@ version = 4
name = "automathon"
version = "0.1.0"
dependencies = [
+ "console_log",
+ "log",
"wasm-bindgen",
"web-sys",
]
@@ -23,6 +25,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268"
[[package]]
+name = "console_log"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "be8aed40e4edbf4d3b4431ab260b63fdc40f5780a4766824329ea0f1eefe3c0f"
+dependencies = [
+ "log",
+ "web-sys",
+]
+
+[[package]]
name = "js-sys"
version = "0.3.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index 6dbff64..c438aa0 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,6 +7,8 @@ edition = "2024"
crate-type = ["cdylib"]
[dependencies]
+console_log = "1.0"
+log = "0.4"
wasm-bindgen = "0.2"
[dependencies.web-sys]
diff --git a/index.html b/index.html
index c985df4..16b132f 100644
--- a/index.html
+++ b/index.html
@@ -7,6 +7,21 @@
<body>
<h1>automathon</h1>
+ <textarea>
+: fac
+ dup
+ 1 > if
+ dup 1 - fac *
+ then
+ ;
+5 fac
+ </textarea>
+ <button id='compile'>compile</button>
+ <button id='tick'>tick</button>
+ <button id='run'>run</button>
+ <div id='datastack'></div>
+ <div id='callstack'></div>
+ <div id='wordlist'></div>
<canvas id='arena' width='512' height='512'>no canvas!</canvas>
<script src='./main.mjs' type='module'></script>
</body>
diff --git a/main.css b/main.css
index a07613e..c6603e1 100644
--- a/main.css
+++ b/main.css
@@ -1,3 +1,8 @@
canvas {
border: 1px solid greenyellow;
}
+
+textarea {
+ width: 25em;
+ height: 25ex;
+}
diff --git a/main.mjs b/main.mjs
index 63f277e..2df434f 100644
--- a/main.mjs
+++ b/main.mjs
@@ -1,22 +1,22 @@
import init from './pkg/automathon.js';
async function loaded() {
- console.debug('run');
+ console.debug('running init');
const mod = await init();
console.debug('init done', mod);
- window.calculate = _ => {
- console.debug('calc');
- const inp1 = document.getElementById('number-input1').value;
- const inp2 = document.getElementById('number-input2').value;
- const res = mod.add(parseInt(inp1), parseInt(inp2));
- document.getElementById('result').textContent = res;
- }
-
- window.testalert = _ => {
- console.debug('testalert');
+ document.querySelector("#compile").onclick = e => {
+ console.debug("compile clicked", e);
+ mod.compile();
+ };
+ document.querySelector("#tick").onclick = e => {
+ console.debug("tick clicked", e);
+ mod.tick();
+ };
+ document.querySelector("#run").onclick = e => {
+ console.debug("run clicked", e);
mod.run();
- }
+ };
}
document.addEventListener('DOMContentLoaded', loaded);
diff --git a/src/forth/interp.rs b/src/forth/interp.rs
index f1af4ae..c6dcb72 100644
--- a/src/forth/interp.rs
+++ b/src/forth/interp.rs
@@ -1,3 +1,5 @@
+use log::debug;
+
use std::ops::Index;
#[derive(Clone, Debug, PartialEq)]
@@ -106,7 +108,7 @@ impl Interp {
let bc = &self.wordlist.0[self.ip.word];
match bc[self.ip.offset] {
OpCode::Num(n) => self.stack.0.push(n),
- OpCode::Str(start, end) => eprintln!("got str: {} to {}", start, end),
+ OpCode::Str(start, end) => debug!("got str: {} to {}", start, end),
OpCode::Add => {
let n1 = self.stack.0.pop().ok_or(RuntimeError::StackUnderflow)?;
let n2 = self.stack.0.pop().ok_or(RuntimeError::StackUnderflow)?;
diff --git a/src/lib.rs b/src/lib.rs
index da1469b..716ad09 100755
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,3 +1,5 @@
+use log::{Level, debug, info};
+use console_log;
use wasm_bindgen::prelude::*;
mod forth;
@@ -8,8 +10,26 @@ extern {
fn debug(s: &str);
}
+#[wasm_bindgen]
+pub fn compile() {
+ info!("compiling code");
+}
+
+#[wasm_bindgen]
+pub fn tick() {
+ info!("executing single instruction");
+}
+
+#[wasm_bindgen]
+pub fn run() {
+ info!("running to completion");
+}
+
#[wasm_bindgen(start)]
-pub fn run() -> Result<(), JsValue> {
+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`");
@@ -18,7 +38,7 @@ pub fn run() -> Result<(), JsValue> {
val.set_text_content(Some("hi there"));
body.append_child(&val)?;
- debug("done in rust's run");
+ debug!("done in rust's run");
Ok(())
}