Skip to content

Commit b5f426f

Browse files
authored
forth: Finish closing #961 - update example (#1033)
As discussed in #1032, the PR was merged too quickly, so this is to complete the contribution by updating the exercise's example in order to be compatible with the modified unit tests. Signed-off-by: Paul Mabileau <[email protected]>
1 parent 29f67a3 commit b5f426f

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

exercises/forth/example.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::collections::HashMap;
2-
use std::collections::LinkedList;
2+
use std::collections::VecDeque;
33
use std::str::FromStr;
44

55
pub type Value = i32;
66
pub type ForthResult = Result<(), Error>;
77
type StackResult<T> = Result<T, Error>;
88

9-
type Stack = LinkedList<Value>;
10-
type Code = LinkedList<Term>;
9+
type Stack = Vec<Value>;
10+
type Code = VecDeque<Term>;
1111
type Definitions = HashMap<String, Code>;
1212

1313
pub struct Forth {
@@ -49,14 +49,14 @@ impl FromStr for Term {
4949
impl Forth {
5050
pub fn new() -> Forth {
5151
Forth {
52-
code: LinkedList::new(),
52+
code: Code::new(),
5353
defs: HashMap::new(),
54-
stack: LinkedList::new(),
54+
stack: Stack::new(),
5555
}
5656
}
5757

58-
pub fn stack(&self) -> Vec<Value> {
59-
self.stack.iter().cloned().collect()
58+
pub fn stack(&self) -> &[Value] {
59+
self.stack.as_slice()
6060
}
6161

6262
pub fn eval(&mut self, input: &str) -> ForthResult {
@@ -125,7 +125,7 @@ impl Forth {
125125
}
126126

127127
fn store_definition(&mut self) -> ForthResult {
128-
let mut def = LinkedList::new();
128+
let mut def = Code::new();
129129

130130
loop {
131131
match self.code.pop_front() {
@@ -143,12 +143,12 @@ impl Forth {
143143
}
144144

145145
fn push(&mut self, value: Value) -> ForthResult {
146-
self.stack.push_back(value);
146+
self.stack.push(value);
147147
Forth::ok()
148148
}
149149

150150
fn pop(&mut self) -> StackResult<Value> {
151-
self.stack.pop_back().ok_or_else(|| {
151+
self.stack.pop().ok_or_else(|| {
152152
eprintln!("Stack underflow");
153153
Error::StackUnderflow
154154
})
@@ -189,7 +189,7 @@ impl Forth {
189189
Forth::ok()
190190
}
191191

192-
fn into_code(input: &str) -> LinkedList<Term> {
192+
fn into_code(input: &str) -> Code {
193193
input
194194
.split(|c: char| c.is_whitespace() || c.is_control())
195195
.map(Term::from_str)

0 commit comments

Comments
 (0)