summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
blob: ddb87fdd25e4f6a16e2f63b09124f575e618d34f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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<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]
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<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");
}

#[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(())
}