aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNerdyPepper <[email protected]>2019-03-20 16:31:58 +0000
committerNerdyPepper <[email protected]>2019-03-20 16:31:58 +0000
commitf1c5f96ed4ab72a5d2f9ef2abb9645ae24d9e60e (patch)
treea55b0bc3aaeb54d84eaa550729783e01d1645632
parent1ddd824f0e2bfaa38b3dad56808bbce951bb1b5c (diff)
add function parsing
-rw-r--r--src/main.rs12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
index 8f0d4e6..06d650e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -170,6 +170,9 @@ fn to_postfix(tokens: Vec<Token>) -> Result<Vec<Token>, String> {
170 Token::Num(_) => { 170 Token::Num(_) => {
171 postfixed.push(token); 171 postfixed.push(token);
172 }, 172 },
173 Token::Function(_) => {
174 op_stack.push(token);
175 }
173 Token::Operator(current_op) => { 176 Token::Operator(current_op) => {
174 while let Some(top_op) = op_stack.last() { 177 while let Some(top_op) = op_stack.last() {
175 match top_op { 178 match top_op {
@@ -185,6 +188,9 @@ fn to_postfix(tokens: Vec<Token>) -> Result<Vec<Token>, String> {
185 break; 188 break;
186 } 189 }
187 } 190 }
191 Token::Function(_) => {
192 postfixed.push(op_stack.pop().unwrap());
193 }
188 _ => { 194 _ => {
189 return Err(format!("Unexpected match branch part 2")) 195 return Err(format!("Unexpected match branch part 2"))
190 } 196 }
@@ -218,6 +224,7 @@ fn to_postfix(tokens: Vec<Token>) -> Result<Vec<Token>, String> {
218 while let Some(op) = op_stack.pop() { 224 while let Some(op) = op_stack.pop() {
219 postfixed.push(op); 225 postfixed.push(op);
220 } 226 }
227 println!("{:?}", postfixed);
221 Ok(postfixed) 228 Ok(postfixed)
222} 229}
223 230
@@ -238,6 +245,11 @@ fn eval_postfix(postfixed: Vec<Token>) -> Result<f64, String> {
238 } else { 245 } else {
239 return Err(format!("Too many operators, Too little operands")) 246 return Err(format!("Too many operators, Too little operands"))
240 } 247 }
248 },
249 Token::Function(funct) => {
250 if let Some(arg) = num_stack.pop() {
251 num_stack.push(funct.apply(arg))
252 }
241 } 253 }
242 _ => { 254 _ => {
243 return Err(format!("Yo nibba how did this get here")) 255 return Err(format!("Yo nibba how did this get here"))