From f1c5f96ed4ab72a5d2f9ef2abb9645ae24d9e60e Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Wed, 20 Mar 2019 22:01:58 +0530 Subject: add function parsing --- src/main.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/main.rs') 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) -> Result, String> { Token::Num(_) => { postfixed.push(token); }, + Token::Function(_) => { + op_stack.push(token); + } Token::Operator(current_op) => { while let Some(top_op) = op_stack.last() { match top_op { @@ -185,6 +188,9 @@ fn to_postfix(tokens: Vec) -> Result, String> { break; } } + Token::Function(_) => { + postfixed.push(op_stack.pop().unwrap()); + } _ => { return Err(format!("Unexpected match branch part 2")) } @@ -218,6 +224,7 @@ fn to_postfix(tokens: Vec) -> Result, String> { while let Some(op) = op_stack.pop() { postfixed.push(op); } + println!("{:?}", postfixed); Ok(postfixed) } @@ -238,6 +245,11 @@ fn eval_postfix(postfixed: Vec) -> Result { } else { return Err(format!("Too many operators, Too little operands")) } + }, + Token::Function(funct) => { + if let Some(arg) = num_stack.pop() { + num_stack.push(funct.apply(arg)) + } } _ => { return Err(format!("Yo nibba how did this get here")) -- cgit v1.2.3