aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs24
1 files changed, 13 insertions, 11 deletions
diff --git a/src/main.rs b/src/main.rs
index 0742ee8..99d2059 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -43,7 +43,7 @@ lazy_static! {
43fn main() { 43fn main() {
44 if CONFIGURATION.input.len() > 0 { 44 if CONFIGURATION.input.len() > 0 {
45 // command mode // 45 // command mode //
46 let evaled = eval_math_expression(&CONFIGURATION.input[..], &mut 0f64); 46 let evaled = eval_math_expression(&CONFIGURATION.input[..], 0f64);
47 match evaled { 47 match evaled {
48 Ok(ans) => pprint(ans), 48 Ok(ans) => pprint(ans),
49 Err(e) => { 49 Err(e) => {
@@ -78,9 +78,12 @@ fn main() {
78 match readline { 78 match readline {
79 Ok(line) => { 79 Ok(line) => {
80 rl.add_history_entry(line.as_str()); 80 rl.add_history_entry(line.as_str());
81 let evaled = eval_math_expression(&line[..], &mut prev_ans); 81 let evaled = eval_math_expression(&line[..], prev_ans);
82 match evaled { 82 match evaled {
83 Ok(ans) => pprint(ans), 83 Ok(ans) => {
84 prev_ans = ans;
85 pprint(ans);
86 }
84 Err(e) => println!("{}", handler(e)), 87 Err(e) => println!("{}", handler(e)),
85 }; 88 };
86 }, 89 },
@@ -145,7 +148,7 @@ fn parse_arguments() -> Configuration {
145 } 148 }
146} 149}
147 150
148pub fn eval_math_expression(input: &str, prev_ans: &mut f64) -> Result<f64, CalcError> { 151pub fn eval_math_expression(input: &str, prev_ans: f64) -> Result<f64, CalcError> {
149 let input = input.trim(); 152 let input = input.trim();
150 let input = input.replace(" ", ""); 153 let input = input.replace(" ", "");
151 if input.len() == 0 { 154 if input.len() == 0 {
@@ -155,7 +158,6 @@ pub fn eval_math_expression(input: &str, prev_ans: &mut f64) -> Result<f64, Calc
155 let lexed = lexer(&input[..], prev_ans)?; 158 let lexed = lexer(&input[..], prev_ans)?;
156 let postfixed = to_postfix(lexed)?; 159 let postfixed = to_postfix(lexed)?;
157 let evaled = eval_postfix(postfixed)?; 160 let evaled = eval_postfix(postfixed)?;
158 *prev_ans = evaled;
159 let evaled_fixed = format!("{:.*}", CONFIGURATION.fix, evaled).parse::<f64>().unwrap(); 161 let evaled_fixed = format!("{:.*}", CONFIGURATION.fix, evaled).parse::<f64>().unwrap();
160 Ok(evaled_fixed) 162 Ok(evaled_fixed)
161} 163}
@@ -166,32 +168,32 @@ mod tests {
166 168
167 #[test] 169 #[test]
168 fn basic_ops() { 170 fn basic_ops() {
169 let evaled = eval_math_expression("6*2 + 3 + 12 -3", &0f64).unwrap(); 171 let evaled = eval_math_expression("6*2 + 3 + 12 -3", 0f64).unwrap();
170 assert_eq!(24., evaled); 172 assert_eq!(24., evaled);
171 } 173 }
172 #[test] 174 #[test]
173 fn trignometric_fns() { 175 fn trignometric_fns() {
174 let evaled = eval_math_expression("sin(30) + tan(45", &0f64).unwrap(); 176 let evaled = eval_math_expression("sin(30) + tan(45", 0f64).unwrap();
175 assert_eq!(1.5, evaled); 177 assert_eq!(1.5, evaled);
176 } 178 }
177 #[test] 179 #[test]
178 fn brackets() { 180 fn brackets() {
179 let evaled = eval_math_expression("(((1 + 2 + 3) ^ 2 ) - 4)", &0f64).unwrap(); 181 let evaled = eval_math_expression("(((1 + 2 + 3) ^ 2 ) - 4)", 0f64).unwrap();
180 assert_eq!(32., evaled); 182 assert_eq!(32., evaled);
181 } 183 }
182 #[test] 184 #[test]
183 fn floating_ops() { 185 fn floating_ops() {
184 let evaled = eval_math_expression("1.2816 + 1 + 1.2816/1.2", &0f64).unwrap(); 186 let evaled = eval_math_expression("1.2816 + 1 + 1.2816/1.2", 0f64).unwrap();
185 assert_eq!(3.3496, evaled); 187 assert_eq!(3.3496, evaled);
186 } 188 }
187 #[test] 189 #[test]
188 fn inverse_trignometric_fns() { 190 fn inverse_trignometric_fns() {
189 let evaled = eval_math_expression("deg(asin(1) + acos(1))", &0f64).unwrap(); 191 let evaled = eval_math_expression("deg(asin(1) + acos(1))", 0f64).unwrap();
190 assert_eq!(90., evaled); 192 assert_eq!(90., evaled);
191 } 193 }
192 #[test] 194 #[test]
193 fn prev_ans() { 195 fn prev_ans() {
194 let evaled = eval_math_expression("_ + 9", &9f64).unwrap(); 196 let evaled = eval_math_expression("_ + 9", 9f64).unwrap();
195 assert_eq!(18.0, evaled); 197 assert_eq!(18.0, evaled);
196 } 198 }
197} 199}