Skip to content

Commit 2b76e80

Browse files
committed
Refactor parsing
1 parent 46529af commit 2b76e80

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

forth/src/lib.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl Forth {
7474
}
7575

7676
fn eval_digits(&mut self, mut input: &str) -> String {
77-
while let (Some(head), tail) = Self::parse_digit(&input.clone()) {
77+
while let (Some(head), tail) = Self::parse_digit(&input) {
7878
self.stack.push(head);
7979
input = tail;
8080
}
@@ -204,24 +204,16 @@ impl Forth {
204204
}
205205

206206
fn parse_word_delcaration(input: &str) -> Result<(Option<Command>, String), Error> {
207-
if input.is_empty() || input.chars().nth(0).unwrap() != ':' {
207+
if !input.starts_with(':') {
208208
return Ok((None, "".to_string()));
209209
}
210210
let body = input
211211
.chars()
212212
.skip(1)
213213
.take_while(|&chr| chr != ';')
214214
.collect::<String>()
215-
.trim()
215+
.trim_left()
216216
.to_string();
217-
let rest = input
218-
.chars()
219-
.skip_while(|&chr| chr != ';')
220-
.skip(1)
221-
.collect::<String>()
222-
.trim()
223-
.to_string();
224-
225217
let key: String = body.chars().take_while(|&chr| chr != ' ').collect();
226218
let value: String = body.chars().skip_while(|&chr| chr != ' ').skip(1).collect();
227219

@@ -237,7 +229,16 @@ impl Forth {
237229
None => return Err(Error::InvalidWord),
238230
}
239231

240-
Ok((Some(Word((key.to_lowercase(), value))), rest))
232+
let rest: String = input
233+
.chars()
234+
.skip_while(|&chr| chr != ';')
235+
.skip(1)
236+
.collect();
237+
238+
Ok((
239+
Some(Word((key.to_lowercase(), value))),
240+
rest.trim_left().to_string(),
241+
))
241242
}
242243

243244
fn parse_word<'a>(&self, input: &'a str) -> (Option<String>, &'a str) {

0 commit comments

Comments
 (0)