Skip to content

Commit 46529af

Browse files
committed
More refactoring
1 parent 96d595f commit 46529af

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

forth/src/lib.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,39 +51,37 @@ impl Forth {
5151
self.stack.clone()
5252
}
5353

54-
fn filter_non_words(input: &str) -> String {
55-
input.chars().fold(String::new(), |mut acc, chr| {
54+
fn filter_words(input: &str) -> String {
55+
input.chars().fold(String::new(), |acc, chr| {
5656
if chr.is_whitespace() || chr.is_control() {
57-
acc = acc + &' '.to_string();
58-
acc
57+
acc + &' '.to_string()
5958
} else {
60-
acc = acc + &chr.to_string();
61-
acc
59+
acc + &chr.to_string()
6260
}
6361
})
6462
}
6563

6664
pub fn eval<'a>(&'a mut self, input: &'a str) -> ForthResult {
67-
let mut input = Self::filter_non_words(input);
65+
let mut input = Self::filter_words(input);
6866
while !input.is_empty() {
69-
input = self.eval_digits(input);
70-
input = self.eval_operators(input)?;
67+
input = self.eval_digits(&input);
68+
input = self.eval_operators(&input)?.to_string();
7169
input = self.eval_word_declarations(input)?;
7270
input = self.eval_word(&input)?;
7371
input = self.eval_commands(input)?;
7472
}
7573
Ok(())
7674
}
7775

78-
fn eval_digits(&mut self, mut input: String) -> String {
79-
while let (Some(head), tail) = Self::parse_digit(input.clone()) {
76+
fn eval_digits(&mut self, mut input: &str) -> String {
77+
while let (Some(head), tail) = Self::parse_digit(&input.clone()) {
8078
self.stack.push(head);
81-
input = tail.to_string();
79+
input = tail;
8280
}
83-
input
81+
input.to_string()
8482
}
8583

86-
fn eval_operators(&mut self, mut input: String) -> Result<String, Error> {
84+
fn eval_operators<'a>(&'a mut self, mut input: &'a str) -> Result<&'a str, Error> {
8785
while let (Some(operator), tail) = Self::parse_operator(&input) {
8886
let value2 = self.stack.pop()?;
8987
let value1 = self.stack.pop()?;
@@ -98,7 +96,7 @@ impl Forth {
9896
}
9997
Multiply => self.stack.push(value1 * value2),
10098
}
101-
input = tail.to_string();
99+
input = tail;
102100
}
103101
Ok(input)
104102
}
@@ -150,36 +148,36 @@ impl Forth {
150148
Ok(input)
151149
}
152150

153-
fn parse_digit(input: String) -> (Option<Value>, String) {
151+
fn parse_digit(input: &str) -> (Option<Value>, &str) {
154152
match input.chars().position(|chr| chr.is_whitespace()) {
155153
Some(position) => {
156154
let head = &input[..position];
157155
let tail = &input[position..];
158156
if let Ok(value) = head.parse::<Value>() {
159-
(Some(value), tail.trim_left().to_string())
157+
(Some(value), tail.trim_left())
160158
} else {
161-
(None, input.trim().to_string())
159+
(None, input.trim())
162160
}
163161
}
164162
_ => match input.parse::<Value>() {
165-
Ok(value) => (Some(value), "".to_string()),
163+
Ok(value) => (Some(value), ""),
166164
_ => (None, input),
167165
},
168166
}
169167
}
170168

171-
fn parse_operator(input: &str) -> (Option<Operator>, String) {
169+
fn parse_operator(input: &str) -> (Option<Operator>, &str) {
172170
if input.is_empty() {
173-
return (None, "".to_string());
171+
return (None, "");
174172
}
175173
let head = &input[..1];
176174
let tail = &input[1..].trim_left();
177175
match head {
178-
"+" => (Some(Plus), tail.to_string()),
179-
"-" => (Some(Minus), tail.to_string()),
180-
"/" => (Some(Divide), tail.to_string()),
181-
"*" => (Some(Multiply), tail.to_string()),
182-
_ => (None, input.to_string()),
176+
"+" => (Some(Plus), tail),
177+
"-" => (Some(Minus), tail),
178+
"/" => (Some(Divide), tail),
179+
"*" => (Some(Multiply), tail),
180+
_ => (None, input),
183181
}
184182
}
185183

0 commit comments

Comments
 (0)