use log::{Level, debug, info}; use console_log; use wasm_bindgen::prelude::*; pub mod forth; #[wasm_bindgen] pub struct ExportedInstructionPointer { pub word: usize, pub offset: usize, } #[wasm_bindgen] pub struct ExportedByteCode(Vec); #[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] 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] pub fn tick() { info!("executing single instruction"); } #[wasm_bindgen] pub fn ip() -> ExportedInstructionPointer { ExportedInstructionPointer { word: 0, offset: 0, } } #[wasm_bindgen] pub fn wordlist() -> Vec { 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"); } #[wasm_bindgen(start)] pub fn init() -> Result<(), JsValue> { console_log::init_with_level(Level::Debug).expect("couldn't init console log"); info!("wasm init"); Ok(()) }