-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
192 lines (158 loc) · 6.08 KB
/
lib.rs
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
mod assembler;
mod bootstrapped_interpreter;
mod compiler;
mod runtime;
use anyhow::Result;
use compiler::Compiler;
pub fn compile_interpreter() -> Result<Vec<u8>> {
let mut compiler = Compiler::default();
bootstrapped_interpreter::build(&mut compiler);
compiler.compile()
}
#[cfg(test)]
mod tests {
use super::compile_interpreter;
use super::runtime::InterpreterRuntime;
use anyhow::Result;
fn build_interpreter() -> Result<InterpreterRuntime> {
let binary = compile_interpreter()?;
InterpreterRuntime::new(&binary)
}
#[test]
fn should_parse_string() {
let interpreter = build_interpreter().unwrap();
interpreter.write_input("Hello world!").unwrap();
interpreter.execute("REFILL").unwrap();
interpreter.execute("PARSE-NAME").unwrap();
interpreter.execute("TYPE").unwrap();
assert_eq!(interpreter.read_output().unwrap(), "Hello");
interpreter.execute("PARSE-NAME").unwrap();
interpreter.execute("TYPE").unwrap();
assert_eq!(interpreter.read_output().unwrap(), "world!");
interpreter.execute("PARSE-NAME").unwrap();
interpreter.execute("TYPE").unwrap();
assert_eq!(interpreter.read_output().unwrap(), "");
}
#[test]
fn should_handle_string_equality() {
let interpreter = build_interpreter().unwrap();
let addr1 = 0x500;
let addr2 = 0x600;
interpreter.push_string(addr1, "Fred").unwrap();
interpreter.push_string(addr2, "FRED").unwrap();
interpreter.execute("STR-UPPER-EQ?").unwrap();
assert_eq!(interpreter.pop().unwrap(), -1);
interpreter.push_string(addr1, "Fred").unwrap();
interpreter.push_string(addr2, "George").unwrap();
interpreter.execute("STR-UPPER-EQ?").unwrap();
assert_eq!(interpreter.pop().unwrap(), 0);
}
#[test]
fn should_find_words() {
let interpreter = build_interpreter().unwrap();
let addr1 = 0x500;
interpreter.push_string(addr1, "dup").unwrap();
interpreter.execute("FIND-NAME").unwrap();
let dup_nt = interpreter.pop().unwrap();
assert_ne!(dup_nt, 0);
interpreter.push(dup_nt).unwrap();
interpreter.execute("NAME>STRING").unwrap();
interpreter.execute("TYPE").unwrap();
let dup_str = interpreter.read_output().unwrap();
assert_eq!(dup_str, "DUP");
interpreter.push_string(addr1, "DOOP").unwrap();
interpreter.execute("FIND-NAME").unwrap();
let doop_nt = interpreter.pop().unwrap();
assert_eq!(doop_nt, 0);
}
#[test]
fn should_parse_digits() {
let interpreter = build_interpreter().unwrap();
interpreter.push('6' as i32).unwrap();
interpreter.execute("?DIGIT").unwrap();
assert_eq!(interpreter.pop().unwrap(), -1);
assert_eq!(interpreter.pop().unwrap(), 6);
interpreter.push('4' as i32).unwrap();
interpreter.execute("?DIGIT").unwrap();
assert_eq!(interpreter.pop().unwrap(), -1);
assert_eq!(interpreter.pop().unwrap(), 4);
interpreter.push('a' as i32).unwrap();
interpreter.execute("?DIGIT").unwrap();
assert_eq!(interpreter.pop().unwrap(), 0);
interpreter.push(16).unwrap();
interpreter.execute("BASE").unwrap();
interpreter.execute("!").unwrap();
interpreter.push('a' as i32).unwrap();
interpreter.execute("?DIGIT").unwrap();
assert_eq!(interpreter.pop().unwrap(), -1);
assert_eq!(interpreter.pop().unwrap(), 10);
}
#[test]
fn should_parse_numbers() {
let interpreter = build_interpreter().unwrap();
let addr1 = 0x500;
interpreter.push_string(addr1, "64").unwrap();
interpreter.execute("?NUMBER").unwrap();
assert_eq!(interpreter.pop().unwrap(), -1); // forth true
assert_eq!(interpreter.pop().unwrap(), 64);
}
#[test]
fn should_parse_negative_numbers() {
let interpreter = build_interpreter().unwrap();
let addr1 = 0x500;
interpreter.push_string(addr1, "-64").unwrap();
interpreter.execute("?NUMBER").unwrap();
assert_eq!(interpreter.pop().unwrap(), -1); // forth true
assert_eq!(interpreter.pop().unwrap(), -64);
}
#[test]
fn should_not_parse_numbers_in_wrong_base() {
let interpreter = build_interpreter().unwrap();
let addr1 = 0x500;
interpreter.push_string(addr1, "f0").unwrap();
interpreter.execute("?NUMBER").unwrap();
assert_eq!(interpreter.pop().unwrap(), 0); // forth false
}
#[test]
fn should_parse_hex_literals() {
let interpreter = build_interpreter().unwrap();
let addr1 = 0x500;
interpreter.push(16).unwrap();
interpreter.execute("BASE").unwrap();
interpreter.execute("!").unwrap();
interpreter.push_string(addr1, "f0").unwrap();
interpreter.execute("?NUMBER").unwrap();
assert_eq!(interpreter.pop().unwrap(), -1);
assert_eq!(interpreter.pop().unwrap(), 0xf0);
}
#[test]
fn should_evaluate() {
let interpreter = build_interpreter().unwrap();
let output = interpreter.interpret("2 3 +").unwrap();
// assert expected output
assert_eq!(output, "");
assert_eq!(interpreter.pop().unwrap(), 5);
}
#[test]
fn should_parse_while_interpreting() {
let interpreter = build_interpreter().unwrap();
let output = interpreter.interpret("PARSE-NAME ASS").unwrap();
assert_eq!(output, "");
interpreter.execute("TYPE").unwrap();
assert_eq!(interpreter.read_output().unwrap(), "ASS");
}
#[test]
fn should_emit_output() {
let interpreter = build_interpreter().unwrap();
let output = interpreter
.interpret("110 EMIT 105 EMIT 99 EMIT 101 EMIT")
.unwrap();
assert_eq!(output, "nice");
}
#[test]
fn should_type_output() {
let interpreter = build_interpreter().unwrap();
let output = interpreter.interpret("parse-name k3wl! type").unwrap();
assert_eq!(output, "k3wl!");
}
}