summaryrefslogtreecommitdiffstats
path: root/tests/forth.rs
blob: 4fae28e07b10181dc1116383a9e757e8cd343f5a (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
use automathon::forth::{
    compiler::Compiler,
    vm::{WordList, VM},
};

fn compiler_for(text: &str) -> Compiler {
    let mut p = Compiler::new(text);
    p.compile().expect("badparse");
    p
}

fn eprintwordlist(wordlist: &WordList) {
    for i in 0..wordlist.0.len() {
        eprintln!("wordlist[{}]: {:?}", i, wordlist.0[i]);
    }
}

#[test]
fn factorial() {
    let prog = ": fac dup 1 > if dup 1 - fac * then ; 5 fac\n";
    let comp = compiler_for(prog);
    eprintwordlist(&comp.wordlist);
    let mut vm = VM::new(comp.wordlist);
    vm.run().expect("should run to completion");
    eprintln!("stack: {:?}", vm.stack);
    assert_eq!(vm.stack.0.len(), 1, "factorial result stack len");
    assert_eq!(vm.stack.0[0], 120, "factorial result value");
}