From 8b49603f0c6d06bcb444bd0ee14aa5c1fbc4648c Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Tue, 9 Jul 2019 12:44:35 +0530 Subject: add previous answer access --- src/main.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'src/main.rs') 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! { fn main() { if CONFIGURATION.input.len() > 0 { // command mode // - let evaled = eval_math_expression(&CONFIGURATION.input[..]); + let evaled = eval_math_expression(&CONFIGURATION.input[..], &mut 0f64); match evaled { Ok(ans) => pprint(ans), Err(e) => { @@ -56,6 +56,9 @@ fn main() { // create fancy readline let mut rl = create_readline(); + // previous answer + let mut prev_ans = 0f64; + // handle history storage let eva_dirs = ProjectDirs::from("com", "NerdyPepper", "eva").unwrap(); let eva_data_dir = eva_dirs.data_dir(); @@ -75,7 +78,7 @@ fn main() { match readline { Ok(line) => { rl.add_history_entry(line.as_str()); - let evaled = eval_math_expression(&line[..]); + let evaled = eval_math_expression(&line[..], &mut prev_ans); match evaled { Ok(ans) => pprint(ans), Err(e) => println!("{}", handler(e)), @@ -142,16 +145,17 @@ fn parse_arguments() -> Configuration { } } -pub fn eval_math_expression(input: &str) -> Result { +pub fn eval_math_expression(input: &str, prev_ans: &mut f64) -> Result { let input = input.trim(); let input = input.replace(" ", ""); if input.len() == 0 { return Ok(0.) } let input = format::autobalance_parens(&input[..])?; - let lexed = lexer(&input[..])?; + let lexed = lexer(&input[..], prev_ans)?; let postfixed = to_postfix(lexed)?; let evaled = eval_postfix(postfixed)?; + *prev_ans = evaled; let evaled_fixed = format!("{:.*}", CONFIGURATION.fix, evaled).parse::().unwrap(); Ok(evaled_fixed) } @@ -162,27 +166,32 @@ mod tests { #[test] fn basic_ops() { - let evaled = eval_math_expression("6*2 + 3 + 12 -3").unwrap(); + let evaled = eval_math_expression("6*2 + 3 + 12 -3", &0f64).unwrap(); assert_eq!(24., evaled); } #[test] fn trignometric_fns() { - let evaled = eval_math_expression("sin(30) + tan(45").unwrap(); + let evaled = eval_math_expression("sin(30) + tan(45", &0f64).unwrap(); assert_eq!(1.5, evaled); } #[test] fn brackets() { - let evaled = eval_math_expression("(((1 + 2 + 3) ^ 2 ) - 4)").unwrap(); + let evaled = eval_math_expression("(((1 + 2 + 3) ^ 2 ) - 4)", &0f64).unwrap(); assert_eq!(32., evaled); } #[test] fn floating_ops() { - let evaled = eval_math_expression("1.2816 + 1 + 1.2816/1.2").unwrap(); + let evaled = eval_math_expression("1.2816 + 1 + 1.2816/1.2", &0f64).unwrap(); assert_eq!(3.3496, evaled); } #[test] fn inverse_trignometric_fns() { - let evaled = eval_math_expression("deg(asin(1) + acos(1))").unwrap(); + let evaled = eval_math_expression("deg(asin(1) + acos(1))", &0f64).unwrap(); assert_eq!(90., evaled); } + #[test] + fn prev_ans() { + let evaled = eval_math_expression("_ + 9", &9f64).unwrap(); + assert_eq!(18.0, evaled); + } } -- cgit v1.2.3