aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs27
1 files changed, 18 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs
index 91d91e5..0742ee8 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[..]); 46 let evaled = eval_math_expression(&CONFIGURATION.input[..], &mut 0f64);
47 match evaled { 47 match evaled {
48 Ok(ans) => pprint(ans), 48 Ok(ans) => pprint(ans),
49 Err(e) => { 49 Err(e) => {
@@ -56,6 +56,9 @@ fn main() {
56 // create fancy readline 56 // create fancy readline
57 let mut rl = create_readline(); 57 let mut rl = create_readline();
58 58
59 // previous answer
60 let mut prev_ans = 0f64;
61
59 // handle history storage 62 // handle history storage
60 let eva_dirs = ProjectDirs::from("com", "NerdyPepper", "eva").unwrap(); 63 let eva_dirs = ProjectDirs::from("com", "NerdyPepper", "eva").unwrap();
61 let eva_data_dir = eva_dirs.data_dir(); 64 let eva_data_dir = eva_dirs.data_dir();
@@ -75,7 +78,7 @@ fn main() {
75 match readline { 78 match readline {
76 Ok(line) => { 79 Ok(line) => {
77 rl.add_history_entry(line.as_str()); 80 rl.add_history_entry(line.as_str());
78 let evaled = eval_math_expression(&line[..]); 81 let evaled = eval_math_expression(&line[..], &mut prev_ans);
79 match evaled { 82 match evaled {
80 Ok(ans) => pprint(ans), 83 Ok(ans) => pprint(ans),
81 Err(e) => println!("{}", handler(e)), 84 Err(e) => println!("{}", handler(e)),
@@ -142,16 +145,17 @@ fn parse_arguments() -> Configuration {
142 } 145 }
143} 146}
144 147
145pub fn eval_math_expression(input: &str) -> Result<f64, CalcError> { 148pub fn eval_math_expression(input: &str, prev_ans: &mut f64) -> Result<f64, CalcError> {
146 let input = input.trim(); 149 let input = input.trim();
147 let input = input.replace(" ", ""); 150 let input = input.replace(" ", "");
148 if input.len() == 0 { 151 if input.len() == 0 {
149 return Ok(0.) 152 return Ok(0.)
150 } 153 }
151 let input = format::autobalance_parens(&input[..])?; 154 let input = format::autobalance_parens(&input[..])?;
152 let lexed = lexer(&input[..])?; 155 let lexed = lexer(&input[..], prev_ans)?;
153 let postfixed = to_postfix(lexed)?; 156 let postfixed = to_postfix(lexed)?;
154 let evaled = eval_postfix(postfixed)?; 157 let evaled = eval_postfix(postfixed)?;
158 *prev_ans = evaled;
155 let evaled_fixed = format!("{:.*}", CONFIGURATION.fix, evaled).parse::<f64>().unwrap(); 159 let evaled_fixed = format!("{:.*}", CONFIGURATION.fix, evaled).parse::<f64>().unwrap();
156 Ok(evaled_fixed) 160 Ok(evaled_fixed)
157} 161}
@@ -162,27 +166,32 @@ mod tests {
162 166
163 #[test] 167 #[test]
164 fn basic_ops() { 168 fn basic_ops() {
165 let evaled = eval_math_expression("6*2 + 3 + 12 -3").unwrap(); 169 let evaled = eval_math_expression("6*2 + 3 + 12 -3", &0f64).unwrap();
166 assert_eq!(24., evaled); 170 assert_eq!(24., evaled);
167 } 171 }
168 #[test] 172 #[test]
169 fn trignometric_fns() { 173 fn trignometric_fns() {
170 let evaled = eval_math_expression("sin(30) + tan(45").unwrap(); 174 let evaled = eval_math_expression("sin(30) + tan(45", &0f64).unwrap();
171 assert_eq!(1.5, evaled); 175 assert_eq!(1.5, evaled);
172 } 176 }
173 #[test] 177 #[test]
174 fn brackets() { 178 fn brackets() {
175 let evaled = eval_math_expression("(((1 + 2 + 3) ^ 2 ) - 4)").unwrap(); 179 let evaled = eval_math_expression("(((1 + 2 + 3) ^ 2 ) - 4)", &0f64).unwrap();
176 assert_eq!(32., evaled); 180 assert_eq!(32., evaled);
177 } 181 }
178 #[test] 182 #[test]
179 fn floating_ops() { 183 fn floating_ops() {
180 let evaled = eval_math_expression("1.2816 + 1 + 1.2816/1.2").unwrap(); 184 let evaled = eval_math_expression("1.2816 + 1 + 1.2816/1.2", &0f64).unwrap();
181 assert_eq!(3.3496, evaled); 185 assert_eq!(3.3496, evaled);
182 } 186 }
183 #[test] 187 #[test]
184 fn inverse_trignometric_fns() { 188 fn inverse_trignometric_fns() {
185 let evaled = eval_math_expression("deg(asin(1) + acos(1))").unwrap(); 189 let evaled = eval_math_expression("deg(asin(1) + acos(1))", &0f64).unwrap();
186 assert_eq!(90., evaled); 190 assert_eq!(90., evaled);
187 } 191 }
192 #[test]
193 fn prev_ans() {
194 let evaled = eval_math_expression("_ + 9", &9f64).unwrap();
195 assert_eq!(18.0, evaled);
196 }
188} 197}