From 6c38d9b13b8e1cc976f9b22d15d100fd03ee54d4 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Sat, 3 Aug 2019 13:49:17 +0800 Subject: Reuse ans output and fix tests --- src/lex/mod.rs | 4 ++-- src/main.rs | 24 +++++++++++++----------- src/readline/mod.rs | 2 +- 3 files changed, 16 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/lex/mod.rs b/src/lex/mod.rs index cac16ca..caafd4b 100644 --- a/src/lex/mod.rs +++ b/src/lex/mod.rs @@ -122,7 +122,7 @@ fn factorial (n: f64) -> f64 { n.signum() * (1.. n.abs() as u64 +1).fold(1, |p, n| p*n) as f64 } -pub fn lexer(input: &str, prev_ans: &mut f64) -> Result, CalcError> { +pub fn lexer(input: &str, prev_ans: f64) -> Result, CalcError> { let functions: HashMap<&str, Token> = get_functions(); let operators: HashMap = get_operators(); @@ -159,7 +159,7 @@ pub fn lexer(input: &str, prev_ans: &mut f64) -> Result, CalcError> { num_vec.clear(); } last_char_is_op = false; - result.push(Token::Num(*prev_ans)); + result.push(Token::Num(prev_ans)); } 'a'...'z' | 'A'...'Z' => { let parse_num = num_vec.parse::().ok(); 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! { fn main() { if CONFIGURATION.input.len() > 0 { // command mode // - let evaled = eval_math_expression(&CONFIGURATION.input[..], &mut 0f64); + let evaled = eval_math_expression(&CONFIGURATION.input[..], 0f64); match evaled { Ok(ans) => pprint(ans), Err(e) => { @@ -78,9 +78,12 @@ fn main() { match readline { Ok(line) => { rl.add_history_entry(line.as_str()); - let evaled = eval_math_expression(&line[..], &mut prev_ans); + let evaled = eval_math_expression(&line[..], prev_ans); match evaled { - Ok(ans) => pprint(ans), + Ok(ans) => { + prev_ans = ans; + pprint(ans); + } Err(e) => println!("{}", handler(e)), }; }, @@ -145,7 +148,7 @@ fn parse_arguments() -> Configuration { } } -pub fn eval_math_expression(input: &str, prev_ans: &mut f64) -> Result { +pub fn eval_math_expression(input: &str, prev_ans: f64) -> Result { let input = input.trim(); let input = input.replace(" ", ""); if input.len() == 0 { @@ -155,7 +158,6 @@ pub fn eval_math_expression(input: &str, prev_ans: &mut f64) -> Result().unwrap(); Ok(evaled_fixed) } @@ -166,32 +168,32 @@ mod tests { #[test] fn basic_ops() { - let evaled = eval_math_expression("6*2 + 3 + 12 -3", &0f64).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", &0f64).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)", &0f64).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", &0f64).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))", &0f64).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(); + let evaled = eval_math_expression("_ + 9", 9f64).unwrap(); assert_eq!(18.0, evaled); } } diff --git a/src/readline/mod.rs b/src/readline/mod.rs index 9ae14ba..97f03bd 100644 --- a/src/readline/mod.rs +++ b/src/readline/mod.rs @@ -23,7 +23,7 @@ impl Highlighter for LineHighlighter { Owned(format!("\x1b[90m{}\x1b[0m", hint)) } fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> { - let op = eval_math_expression(line, &mut 0f64); + let op = eval_math_expression(line, 0f64); match op { Ok(_) => { let functions = [ -- cgit v1.2.3