aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lex/mod.rs4
-rw-r--r--src/main.rs24
-rw-r--r--src/readline/mod.rs2
3 files changed, 16 insertions, 14 deletions
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 {
122 n.signum() * (1.. n.abs() as u64 +1).fold(1, |p, n| p*n) as f64 122 n.signum() * (1.. n.abs() as u64 +1).fold(1, |p, n| p*n) as f64
123} 123}
124 124
125pub fn lexer(input: &str, prev_ans: &mut f64) -> Result<Vec<Token>, CalcError> { 125pub fn lexer(input: &str, prev_ans: f64) -> Result<Vec<Token>, CalcError> {
126 let functions: HashMap<&str, Token> = get_functions(); 126 let functions: HashMap<&str, Token> = get_functions();
127 let operators: HashMap<char, Token> = get_operators(); 127 let operators: HashMap<char, Token> = get_operators();
128 128
@@ -159,7 +159,7 @@ pub fn lexer(input: &str, prev_ans: &mut f64) -> Result<Vec<Token>, CalcError> {
159 num_vec.clear(); 159 num_vec.clear();
160 } 160 }
161 last_char_is_op = false; 161 last_char_is_op = false;
162 result.push(Token::Num(*prev_ans)); 162 result.push(Token::Num(prev_ans));
163 } 163 }
164 'a'...'z' | 'A'...'Z' => { 164 'a'...'z' | 'A'...'Z' => {
165 let parse_num = num_vec.parse::<f64>().ok(); 165 let parse_num = num_vec.parse::<f64>().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! {
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}
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 {
23 Owned(format!("\x1b[90m{}\x1b[0m", hint)) 23 Owned(format!("\x1b[90m{}\x1b[0m", hint))
24 } 24 }
25 fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> { 25 fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> {
26 let op = eval_math_expression(line, &mut 0f64); 26 let op = eval_math_expression(line, 0f64);
27 match op { 27 match op {
28 Ok(_) => { 28 Ok(_) => {
29 let functions = [ 29 let functions = [