Skip to content

forth: Finish closing #961 - update example #1033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 20, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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