diff options
Diffstat (limited to 'src/parse')
-rw-r--r-- | src/parse/mod.rs | 44 |
1 files changed, 19 insertions, 25 deletions
diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 5c85172..aa8bd4b 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs | |||
@@ -2,8 +2,8 @@ | |||
2 | * Refer to LICENCE for more information. | 2 | * Refer to LICENCE for more information. |
3 | * */ | 3 | * */ |
4 | 4 | ||
5 | use crate::lex::Token; | ||
6 | use crate::error::CalcError; | 5 | use crate::error::CalcError; |
6 | use crate::lex::Token; | ||
7 | 7 | ||
8 | pub fn to_postfix(tokens: Vec<Token>) -> Result<Vec<Token>, CalcError> { | 8 | pub fn to_postfix(tokens: Vec<Token>) -> Result<Vec<Token>, CalcError> { |
9 | let mut postfixed: Vec<Token> = vec![]; | 9 | let mut postfixed: Vec<Token> = vec![]; |
@@ -12,7 +12,7 @@ pub fn to_postfix(tokens: Vec<Token>) -> Result<Vec<Token>, CalcError> { | |||
12 | match token { | 12 | match token { |
13 | Token::Num(_) => { | 13 | Token::Num(_) => { |
14 | postfixed.push(token); | 14 | postfixed.push(token); |
15 | }, | 15 | } |
16 | Token::Function(_) => { | 16 | Token::Function(_) => { |
17 | op_stack.push(token); | 17 | op_stack.push(token); |
18 | } | 18 | } |
@@ -34,14 +34,16 @@ pub fn to_postfix(tokens: Vec<Token>) -> Result<Vec<Token>, CalcError> { | |||
34 | Token::Function(_) => { | 34 | Token::Function(_) => { |
35 | postfixed.push(op_stack.pop().unwrap()); | 35 | postfixed.push(op_stack.pop().unwrap()); |
36 | } | 36 | } |
37 | _ => { unreachable!(); } | 37 | _ => { |
38 | unreachable!(); | ||
39 | } | ||
38 | } | 40 | } |
39 | } | 41 | } |
40 | op_stack.push(token); | 42 | op_stack.push(token); |
41 | }, | 43 | } |
42 | Token::LParen => { | 44 | Token::LParen => { |
43 | op_stack.push(token); | 45 | op_stack.push(token); |
44 | }, | 46 | } |
45 | Token::RParen => { | 47 | Token::RParen => { |
46 | let mut push_until_paren: bool = false; | 48 | let mut push_until_paren: bool = false; |
47 | while let Some(token) = op_stack.pop() { | 49 | while let Some(token) = op_stack.pop() { |
@@ -69,43 +71,35 @@ pub fn eval_postfix(postfixed: Vec<Token>) -> Result<f64, CalcError> { | |||
69 | match token { | 71 | match token { |
70 | Token::Num(n) => { | 72 | Token::Num(n) => { |
71 | num_stack.push(n); | 73 | num_stack.push(n); |
72 | }, | 74 | } |
73 | Token::Operator(op) => { | 75 | Token::Operator(op) => { |
74 | if let Some(n2) = num_stack.pop() { | 76 | if let Some(n2) = num_stack.pop() { |
75 | if let Some(n1) = num_stack.pop() { | 77 | if let Some(n1) = num_stack.pop() { |
76 | num_stack.push(op.operate(n1, n2)?); | 78 | num_stack.push(op.operate(n1, n2)?); |
77 | } else { | 79 | } else { |
78 | return Err( | 80 | return Err(CalcError::Parser( |
79 | CalcError::Parser( | 81 | "Too many operators, Too little operands".to_string(), |
80 | format!("Too many operators, Too little operands") | 82 | )); |
81 | ) | ||
82 | ) | ||
83 | } | 83 | } |
84 | } else { | 84 | } else { |
85 | return Err( | 85 | return Err(CalcError::Parser( |
86 | CalcError::Parser( | 86 | "Too many operators, Too little operands".to_string(), |
87 | format!("Too many operators, Too little operands") | 87 | )); |
88 | ) | ||
89 | ) | ||
90 | } | 88 | } |
91 | }, | 89 | } |
92 | Token::Function(funct) => { | 90 | Token::Function(funct) => { |
93 | if let Some(arg) = num_stack.pop() { | 91 | if let Some(arg) = num_stack.pop() { |
94 | num_stack.push(funct.apply(arg)?) | 92 | num_stack.push(funct.apply(arg)?) |
95 | } | 93 | } |
96 | } | 94 | } |
97 | _ => { | 95 | _ => unreachable!("wut"), |
98 | unreachable!("wut") | ||
99 | } | ||
100 | } | 96 | } |
101 | } | 97 | } |
102 | if num_stack.len() == 1 { | 98 | if num_stack.len() == 1 { |
103 | Ok(num_stack.pop().unwrap()) | 99 | Ok(num_stack.pop().unwrap()) |
104 | } else { | 100 | } else { |
105 | return Err( | 101 | Err(CalcError::Parser( |
106 | CalcError::Parser( | 102 | "Too many operators, Too little operands".to_string(), |
107 | format!("Too many operators, Too little operands") | 103 | )) |
108 | ) | ||
109 | ) | ||
110 | } | 104 | } |
111 | } | 105 | } |