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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
use super::interp::{ByteCode, OpCode, WordList};
use std::collections::HashMap;
use std::iter::{Enumerate, Iterator};
use std::str::Chars;
#[derive(Debug)]
pub enum ParseError {
EOF,
DefStackEmpty,
MissingIf,
MissingQuote,
UnknownWord(String),
}
impl std::fmt::Display for ParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::EOF => write!(f, "premature end-of-file"),
Self::DefStackEmpty => write!(f, "def stack empty"),
Self::MissingIf => write!(f, "missing if"),
Self::MissingQuote => write!(f, "missing ending quote"),
Self::UnknownWord(word) => write!(f, "unknown word: {}", word),
}
}
}
impl std::error::Error for ParseError {}
type ParseResult<T> = Result<T, ParseError>;
// todo: the annotations should be directly tied to the wordlist so
// they can't get out of sync.
#[derive(Debug)]
pub struct Annotation {
// (start, end) index in program text
pub loc: (usize, usize),
}
#[derive(Debug)]
pub struct WordCatalog<'a>(pub(super) HashMap<&'a str, usize>);
#[derive(Debug)]
pub enum IfClauses {
True(usize),
TrueFalse(usize, usize),
}
#[derive(Debug)]
pub struct Parser<'a> {
text: &'a str,
enumerator: Enumerate<Chars<'a>>,
// list of all word definitions, in the order defined. the main
// routine is always in the first entry.
// todo: don't be pub, have a method to extract a wordlist
pub wordlist: WordList,
pub annotations: Vec<Vec<Annotation>>,
// catalog of word to word index in `wordlist`
pub wordalog: WordCatalog<'a>,
// holds a stack of indices into `wordlist` that are currently
// being defined, with the top of stack being the most recent
// definition.
defstack: Vec<usize>,
// number of clauses currently being defined for a particular ‘if’
// construct. a stack is needed in order to allow for nesting.
if_stack: Vec<(IfClauses, Annotation)>,
}
impl<'a> Parser<'a> {
pub fn new(text: &'a str) -> Self {
let mut wl = vec![];
// main routine is always the first entry.
wl.push(ByteCode(vec![]));
Self {
text,
enumerator: text.chars().enumerate(),
wordlist: WordList(wl),
annotations: vec![vec![]],
wordalog: WordCatalog(HashMap::new()),
defstack: vec![],
if_stack: vec![],
}
}
// pull the next, whitespace-delimited word off the input stream.
fn next_word(&mut self) -> Option<(&'a str, usize, usize)> {
let (start, _c) =
self.enumerator.by_ref()
.find(|(_i, c)| !c.is_whitespace())?;
let (end, _c) =
self.enumerator.by_ref()
.find(|(_i, c)| c.is_whitespace())?;
let word = &self.text[start..end];
Some((word, start, end))
}
// currently ‘active’ bytecode being built.
fn bc_mut(&mut self) -> &mut ByteCode {
let word_index = self.defstack.last().unwrap_or(&0);
&mut self.wordlist.0[*word_index]
}
fn bc(&self) -> &ByteCode {
let word_index = self.defstack.last().unwrap_or(&0);
&self.wordlist.0[*word_index]
}
fn anno_mut(&mut self) -> &mut Vec<Annotation> {
let word_index = self.defstack.last().unwrap_or(&0);
&mut self.annotations[*word_index]
}
// push `op` onto the currently building bytecode, as determined
// by the top of the `namestack`.
fn bc_push(&mut self, op: OpCode, anno: Annotation) {
self.bc_mut().0.push(op);
self.anno_mut().push(anno);
}
pub fn parse(&mut self) -> ParseResult<()> {
while let Some((word, start, end)) = self.next_word() {
let anno = Annotation { loc: (start, end) };
if let Ok(i) = word.parse::<i32>() {
self.bc_push(OpCode::Num(i), anno);
} else if let Some(i) = self.wordalog.0.get(word) {
self.bc_push(OpCode::Call(*i), anno);
} else {
match word {
r#"s""# => {
let (s_end, _) =
self.enumerator
.find(|(_i, c)| return *c == '"')
.ok_or(ParseError::MissingQuote)?;
self.bc_push(OpCode::Str(end+1, s_end), anno);
},
":" => {
let (name, _, _) = self.next_word().ok_or(ParseError::EOF)?;
self.wordalog.0.insert(name, self.wordlist.0.len());
self.defstack.push(self.wordlist.0.len());
self.wordlist.0.push(ByteCode(vec![]));
self.annotations.push(vec![]);
},
";" => {
match self.bc().0.last() {
Some(OpCode::Call(i)) => {
let k = *i;
self.bc_mut().0.pop();
self.bc_mut().0.push(OpCode::TCall(k));
},
Some(OpCode::If(t, f)) => {
let t = *t;
let f = *f;
self.bc_mut().0.pop();
self.bc_mut().0.push(OpCode::TIf(t, f));
// technically only needed if ‘f’ is None, but whatever.
self.bc_push(OpCode::Ret, anno);
},
_ => self.bc_push(OpCode::Ret, anno),
}
self.defstack.pop().ok_or(ParseError::DefStackEmpty)?;
},
"if" => {
let i = self.wordlist.0.len();
self.wordlist.0.push(ByteCode(vec![]));
self.annotations.push(vec![]);
self.defstack.push(i);
self.if_stack.push((IfClauses::True(i), anno));
},
"else" => {
self.bc_push(OpCode::Ret, anno);
self.defstack.pop();
let i = self.wordlist.0.len();
self.wordlist.0.push(ByteCode(vec![]));
self.annotations.push(vec![]);
self.defstack.push(i);
let (true_clause, anno) = match self.if_stack.pop() {
None => return Err(ParseError::MissingIf),
Some((IfClauses::TrueFalse(_, _), _)) => return Err(ParseError::MissingIf),
Some((IfClauses::True(cl), anno)) => (cl, anno),
};
self.if_stack.push((IfClauses::TrueFalse(true_clause, i), anno));
},
"then" => {
self.bc_push(OpCode::Ret, anno);
self.defstack.pop();
match self.if_stack.pop() {
None => return Err(ParseError::MissingIf),
Some((IfClauses::True(true_clause), anno)) => {
self.bc_push(OpCode::If(true_clause, None), anno);
},
Some((IfClauses::TrueFalse(true_clause, false_clause), anno)) => {
self.bc_push(OpCode::If(true_clause, Some(false_clause)), anno);
}
}
},
"+" => self.bc_push(OpCode::Add, anno),
"-" => self.bc_push(OpCode::Sub, anno),
"*" => self.bc_push(OpCode::Mul, anno),
"/" => self.bc_push(OpCode::Div, anno),
"dup" => self.bc_push(OpCode::Dup, anno),
"drop" => self.bc_push(OpCode::Drop, anno),
"=" => self.bc_push(OpCode::EQ, anno),
">" => self.bc_push(OpCode::GT, anno),
">=" => self.bc_push(OpCode::GTE, anno),
"<" => self.bc_push(OpCode::LT, anno),
"<=" => self.bc_push(OpCode::LTE, anno),
other => return Err(ParseError::UnknownWord(String::from(other))),
}
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use super::super::interp::OpCode;
fn parser_for(text: &str) -> Parser {
let mut p = Parser::new(text);
p.parse().expect("badparse");
p
}
fn eprintwordlist(wordlist: &WordList) {
for i in 0..wordlist.0.len() {
eprintln!("wordlist[{}]: {:?}", i, wordlist.0[i]);
}
}
#[test]
fn literal_num() {
let p = parser_for("1\n");
let main = &p.wordlist.0[0];
assert_eq!(main.len(), 1);
assert_eq!(main[0], OpCode::Num(1));
}
#[test]
fn literal_string() {
let p = parser_for(r#"s" hello there""#);
let main = &p.wordlist.0[0];
assert_eq!(main.len(), 1);
assert_eq!(main[0], OpCode::Str(3, 14));
}
#[test]
fn add_opcode() {
let p = parser_for("+\n");
let main = &p.wordlist.0[0];
assert_eq!(main.len(), 1);
assert_eq!(main[0], OpCode::Add);
}
#[test]
fn sub_opcode() {
let p = parser_for("-\n");
let main = &p.wordlist.0[0];
assert_eq!(main.len(), 1);
assert_eq!(main[0], OpCode::Sub);
}
#[test]
fn def_word() {
let p = parser_for(": add2 2 + ; 3 add2\n");
let main = &p.wordlist.0[0];
let add2_index = p.wordalog.0.get("add2").expect("add2 has entry in wordlist");
let add2 = &p.wordlist.0[*add2_index];
assert_eq!(main.len(), 2);
assert_eq!(main[0], OpCode::Num(3));
assert_eq!(main[1], OpCode::Call(*add2_index));
assert_eq!(add2.len(), 3);
assert_eq!(add2[0], OpCode::Num(2));
assert_eq!(add2[1], OpCode::Add);
assert_eq!(add2[2], OpCode::Ret);
}
#[test]
fn if_then() {
let p = parser_for("if 1 2 + then\n");
eprintwordlist(&p.wordlist);
assert_eq!(p.wordlist.0.len(), 2);
let main = &p.wordlist.0[0];
assert_eq!(main.len(), 1);
assert_eq!(main[0], OpCode::If(1, None));
let tr = &p.wordlist.0[1];
assert_eq!(tr.len(), 4);
assert_eq!(tr[0], OpCode::Num(1));
assert_eq!(tr[1], OpCode::Num(2));
assert_eq!(tr[2], OpCode::Add);
assert_eq!(tr[3], OpCode::Ret);
}
#[test]
fn if_else_then() {
let p = parser_for("if 1 2 + else 1 2 - then\n");
eprintwordlist(&p.wordlist);
assert_eq!(p.wordlist.0.len(), 3, "wordlist length");
let main = &p.wordlist.0[0];
assert_eq!(main.len(), 1);
assert_eq!(main[0], OpCode::If(1, Some(2)));
let tr = &p.wordlist.0[1];
assert_eq!(tr.len(), 4);
assert_eq!(tr[0], OpCode::Num(1));
assert_eq!(tr[1], OpCode::Num(2));
assert_eq!(tr[2], OpCode::Add);
assert_eq!(tr[3], OpCode::Ret);
let fl = &p.wordlist.0[2];
assert_eq!(fl.len(), 4);
assert_eq!(fl[0], OpCode::Num(1));
assert_eq!(fl[1], OpCode::Num(2));
assert_eq!(fl[2], OpCode::Sub);
assert_eq!(fl[3], OpCode::Ret);
}
#[test]
fn tail_call() {
let p = parser_for(": first + ; : second 3 first ; 1 second\n");
assert_eq!(p.wordlist.0.len(), 3, "wordlist length");
eprintwordlist(&p.wordlist);
}
#[test]
fn tail_if() {
let p = parser_for(": t if 1 2 + then ; t\n");
eprintwordlist(&p.wordlist);
assert_eq!(p.wordlist.0.len(), 3);
let main = &p.wordlist.0[0];
assert_eq!(main.len(), 1);
assert_eq!(main[0], OpCode::Call(1));
let t = &p.wordlist.0[1];
assert_eq!(t.len(), 2);
assert_eq!(t[0], OpCode::TIf(2, None));
assert_eq!(t[1], OpCode::Ret);
let tr = &p.wordlist.0[2];
assert_eq!(tr.len(), 4);
assert_eq!(tr[0], OpCode::Num(1));
assert_eq!(tr[1], OpCode::Num(2));
assert_eq!(tr[2], OpCode::Add);
assert_eq!(tr[3], OpCode::Ret);
}
#[test]
fn tail_if_else() {
let p = parser_for(": t if 1 2 + else 1 2 - then ; t\n");
eprintwordlist(&p.wordlist);
assert_eq!(p.wordlist.0.len(), 4, "wordlist length");
let main = &p.wordlist.0[0];
assert_eq!(main.len(), 1);
assert_eq!(main[0], OpCode::Call(1));
let t = &p.wordlist.0[1];
assert_eq!(t.len(), 2);
assert_eq!(t[0], OpCode::TIf(2, Some(3)));
assert_eq!(t[1], OpCode::Ret);
let tr = &p.wordlist.0[2];
assert_eq!(tr.len(), 4);
assert_eq!(tr[0], OpCode::Num(1));
assert_eq!(tr[1], OpCode::Num(2));
assert_eq!(tr[2], OpCode::Add);
assert_eq!(tr[3], OpCode::Ret);
let fl = &p.wordlist.0[3];
assert_eq!(fl.len(), 4);
assert_eq!(fl[0], OpCode::Num(1));
assert_eq!(fl[1], OpCode::Num(2));
assert_eq!(fl[2], OpCode::Sub);
assert_eq!(fl[3], OpCode::Ret);
}
#[test]
fn recursion() {
let p = parser_for(": foo foo ; foo\n");
eprintwordlist(&p.wordlist);
assert_eq!(p.wordlist.0.len(), 2);
let main = &p.wordlist.0[0];
assert_eq!(main.len(), 1);
assert_eq!(main[0], OpCode::Call(1));
let foo = &p.wordlist.0[1];
assert_eq!(foo.len(), 1);
assert_eq!(foo[0], OpCode::TCall(1));
}
}
|