summaryrefslogtreecommitdiffstats
path: root/tests/forth.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/forth.rs')
-rw-r--r--tests/forth.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/tests/forth.rs b/tests/forth.rs
new file mode 100644
index 0000000..4fae28e
--- /dev/null
+++ b/tests/forth.rs
@@ -0,0 +1,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");
+}