Skip to content

Commit

Permalink
forth: Finish closing #961 - update example (#1033)
Browse files Browse the repository at this point in the history
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 <paulmabileau@hotmail.fr>
  • Loading branch information
PaulDance authored Nov 20, 2020
1 parent 29f67a3 commit b5f426f
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions exercises/forth/example.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::collections::HashMap;
use std::collections::LinkedList;
use std::collections::VecDeque;
use std::str::FromStr;

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

type Stack = LinkedList<Value>;
type Code = LinkedList<Term>;
type Stack = Vec<Value>;
type Code = VecDeque<Term>;
type Definitions = HashMap<String, Code>;

pub struct Forth {
Expand Down Expand Up @@ -49,14 +49,14 @@ impl FromStr for Term {
impl Forth {
pub fn new() -> Forth {
Forth {
code: LinkedList::new(),
code: Code::new(),
defs: HashMap::new(),
stack: LinkedList::new(),
stack: Stack::new(),
}
}

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

pub fn eval(&mut self, input: &str) -> ForthResult {
Expand Down Expand Up @@ -125,7 +125,7 @@ impl Forth {
}

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

loop {
match self.code.pop_front() {
Expand All @@ -143,12 +143,12 @@ impl Forth {
}

fn push(&mut self, value: Value) -> ForthResult {
self.stack.push_back(value);
self.stack.push(value);
Forth::ok()
}

fn pop(&mut self) -> StackResult<Value> {
self.stack.pop_back().ok_or_else(|| {
self.stack.pop().ok_or_else(|| {
eprintln!("Stack underflow");
Error::StackUnderflow
})
Expand Down Expand Up @@ -189,7 +189,7 @@ impl Forth {
Forth::ok()
}

fn into_code(input: &str) -> LinkedList<Term> {
fn into_code(input: &str) -> Code {
input
.split(|c: char| c.is_whitespace() || c.is_control())
.map(Term::from_str)
Expand Down

0 comments on commit b5f426f

Please sign in to comment.