@@ -51,39 +51,37 @@ impl Forth {
51
51
self . stack . clone ( )
52
52
}
53
53
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| {
56
56
if chr. is_whitespace ( ) || chr. is_control ( ) {
57
- acc = acc + & ' ' . to_string ( ) ;
58
- acc
57
+ acc + & ' ' . to_string ( )
59
58
} else {
60
- acc = acc + & chr. to_string ( ) ;
61
- acc
59
+ acc + & chr. to_string ( )
62
60
}
63
61
} )
64
62
}
65
63
66
64
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) ;
68
66
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 ( ) ;
71
69
input = self . eval_word_declarations ( input) ?;
72
70
input = self . eval_word ( & input) ?;
73
71
input = self . eval_commands ( input) ?;
74
72
}
75
73
Ok ( ( ) )
76
74
}
77
75
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 ( ) ) {
80
78
self . stack . push ( head) ;
81
- input = tail. to_string ( ) ;
79
+ input = tail;
82
80
}
83
- input
81
+ input. to_string ( )
84
82
}
85
83
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 > {
87
85
while let ( Some ( operator) , tail) = Self :: parse_operator ( & input) {
88
86
let value2 = self . stack . pop ( ) ?;
89
87
let value1 = self . stack . pop ( ) ?;
@@ -98,7 +96,7 @@ impl Forth {
98
96
}
99
97
Multiply => self . stack . push ( value1 * value2) ,
100
98
}
101
- input = tail. to_string ( ) ;
99
+ input = tail;
102
100
}
103
101
Ok ( input)
104
102
}
@@ -150,36 +148,36 @@ impl Forth {
150
148
Ok ( input)
151
149
}
152
150
153
- fn parse_digit ( input : String ) -> ( Option < Value > , String ) {
151
+ fn parse_digit ( input : & str ) -> ( Option < Value > , & str ) {
154
152
match input. chars ( ) . position ( |chr| chr. is_whitespace ( ) ) {
155
153
Some ( position) => {
156
154
let head = & input[ ..position] ;
157
155
let tail = & input[ position..] ;
158
156
if let Ok ( value) = head. parse :: < Value > ( ) {
159
- ( Some ( value) , tail. trim_left ( ) . to_string ( ) )
157
+ ( Some ( value) , tail. trim_left ( ) )
160
158
} else {
161
- ( None , input. trim ( ) . to_string ( ) )
159
+ ( None , input. trim ( ) )
162
160
}
163
161
}
164
162
_ => match input. parse :: < Value > ( ) {
165
- Ok ( value) => ( Some ( value) , "" . to_string ( ) ) ,
163
+ Ok ( value) => ( Some ( value) , "" ) ,
166
164
_ => ( None , input) ,
167
165
} ,
168
166
}
169
167
}
170
168
171
- fn parse_operator ( input : & str ) -> ( Option < Operator > , String ) {
169
+ fn parse_operator ( input : & str ) -> ( Option < Operator > , & str ) {
172
170
if input. is_empty ( ) {
173
- return ( None , "" . to_string ( ) ) ;
171
+ return ( None , "" ) ;
174
172
}
175
173
let head = & input[ ..1 ] ;
176
174
let tail = & input[ 1 ..] . trim_left ( ) ;
177
175
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) ,
183
181
}
184
182
}
185
183
0 commit comments