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
|
use std::collections::HashMap;
use std::ops::Index;
#[derive(Clone, Debug, PartialEq)]
pub enum OpCode {
Num(i32),
Str(usize, usize),
WordI(usize),
Word(&'static str),
Add,
Sub,
Ret,
}
#[derive(Debug)]
pub(super) struct ByteCode(pub(super) Vec<OpCode>);
impl ByteCode {
pub fn len(&self) -> usize {
self.0.len()
}
}
impl Index<usize> for ByteCode {
type Output = OpCode;
fn index(&self, index: usize) -> &Self::Output {
&self.0[index]
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub(super) struct InstructionPointer(pub(super) usize);
#[derive(Debug)]
pub(super) struct DataStack(pub(super) Vec<i32>);
#[derive(Debug)]
pub(super) struct CallStack(pub(super) Vec<InstructionPointer>);
#[derive(Debug)]
pub(super) struct WordList(pub(super) HashMap<&'static str, InstructionPointer>);
#[derive(Debug)]
pub(super) struct WordCatalog<'a>(pub(super) HashMap<&'a str, usize>);
#[derive(Debug)]
pub struct Interp {
stack: DataStack,
callstack: CallStack,
wordlist: WordList,
bytecode: ByteCode,
ip: InstructionPointer,
}
#[derive(Debug)]
pub enum RuntimeError {
StackUnderflow,
UndefinedWord,
}
impl Interp {
pub fn new() -> Self {
Self {
stack: DataStack(Vec::new()),
callstack: CallStack(Vec::new()),
wordlist: WordList(HashMap::new()),
bytecode: ByteCode(Vec::new()),
ip: InstructionPointer(0),
}
}
pub fn tick(&mut self) -> Result<(), RuntimeError> {
match self.bytecode[self.ip.0] {
OpCode::Num(n) => self.stack.0.push(n),
OpCode::Str(start, end) => eprintln!("got str: {} to {}", start, end),
OpCode::Add => {
let n1 = self.stack.0.pop().ok_or(RuntimeError::StackUnderflow)?;
let n2 = self.stack.0.pop().ok_or(RuntimeError::StackUnderflow)?;
self.stack.0.push(n2 + n1);
},
OpCode::Sub => {
let n1 = self.stack.0.pop().ok_or(RuntimeError::StackUnderflow)?;
let n2 = self.stack.0.pop().ok_or(RuntimeError::StackUnderflow)?;
self.stack.0.push(n2 - n1);
},
OpCode::Ret => {
let ip = self.callstack.0.pop().ok_or(RuntimeError::StackUnderflow)?;
self.ip = ip;
},
OpCode::Word(w) => {
let ip = self.wordlist.0.get(w).ok_or(RuntimeError::UndefinedWord)?;
self.callstack.0.push(self.ip);
self.ip.0 = ip.0 - 1; // we auto-increment at the end
},
OpCode::WordI(i) => {
eprintln!("should jump to word based on index {}", i);
}
}
self.ip.0 += 1;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use super::OpCode;
#[test]
fn simple_ticks() -> Result<(), RuntimeError> {
let mut interp = Interp::new();
interp.bytecode = ByteCode(vec![OpCode::Num(2), OpCode::Num(3), OpCode::Add]);
interp.tick()?;
assert_eq!(interp.ip.0, 1);
assert_eq!(interp.stack.0.len(), 1);
assert_eq!(interp.stack.0[0], 2, "first argument");
interp.tick()?;
assert_eq!(interp.ip.0, 2);
assert_eq!(interp.stack.0.len(), 2);
assert_eq!(interp.stack.0[1], 3, "second argument");
interp.tick()?;
assert_eq!(interp.ip.0, 3);
assert_eq!(interp.stack.0.len(), 1);
assert_eq!(interp.stack.0[0], 5, "result of addition");
Ok(())
}
#[test]
fn custom_word() -> Result<(), RuntimeError> {
let mut interp = Interp::new();
interp.bytecode = ByteCode(vec![
OpCode::Num(2), OpCode::Num(3), OpCode::Word("sub"), OpCode::Num(-2), OpCode::Add,
// "sub" definition
OpCode::Sub, OpCode::Ret,
]);
// 5 is offset of w
interp.wordlist.0.insert("sub", InstructionPointer(5));
interp.tick()?;
assert_eq!(interp.ip.0, 1);
assert_eq!(interp.stack.0.len(), 1);
assert_eq!(interp.stack.0[0], 2, "first argument");
interp.tick()?;
assert_eq!(interp.ip.0, 2);
assert_eq!(interp.stack.0.len(), 2);
assert_eq!(interp.stack.0[1], 3, "second argument");
interp.tick()?; // call sub
assert_eq!(interp.ip.0, 5);
assert_eq!(interp.stack.0.len(), 2);
interp.tick()?; // -
assert_eq!(interp.ip.0, 6);
assert_eq!(interp.stack.0.len(), 1);
interp.tick()?; // ret
assert_eq!(interp.ip.0, 3);
assert_eq!(interp.stack.0.len(), 1);
assert_eq!(interp.stack.0[0], -1, "result of sub word");
interp.tick()?; // 2
assert_eq!(interp.ip.0, 4);
assert_eq!(interp.stack.0.len(), 2);
assert_eq!(interp.stack.0[1], -2, "post sub arg");
interp.tick()?; // -
assert_eq!(interp.ip.0, 5);
assert_eq!(interp.stack.0.len(), 1);
assert_eq!(interp.stack.0[0], -3, "add opcode result");
Ok(())
}
}
|