aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs112
1 files changed, 57 insertions, 55 deletions
diff --git a/src/main.rs b/src/main.rs
index c3f3342..140150c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -6,26 +6,26 @@
6/* imports */ 6/* imports */
7// std 7// std
8use std::f64; 8use std::f64;
9use std::path::PathBuf;
10use std::fs::create_dir_all; 9use std::fs::create_dir_all;
10use std::path::PathBuf;
11 11
12// modules 12// modules
13mod lex;
14mod parse;
15mod error; 13mod error;
16mod format; 14mod format;
15mod lex;
16mod parse;
17mod readline; 17mod readline;
18use crate::error::{handler, CalcError};
19use crate::format::*;
18use crate::lex::*; 20use crate::lex::*;
19use crate::parse::*; 21use crate::parse::*;
20use crate::error::{ CalcError, handler };
21use crate::format::*;
22use crate::readline::*; 22use crate::readline::*;
23 23
24// extern crates 24// extern crates
25use rustyline::error::ReadlineError; 25use clap::{App, Arg};
26use clap::{Arg, App}; 26use directories::{ProjectDirs, UserDirs};
27use lazy_static::lazy_static; 27use lazy_static::lazy_static;
28use directories::{ ProjectDirs, UserDirs }; 28use rustyline::error::ReadlineError;
29 29
30/* end of imports */ 30/* end of imports */
31 31
@@ -33,7 +33,7 @@ struct Configuration {
33 radian_mode: bool, 33 radian_mode: bool,
34 fix: usize, 34 fix: usize,
35 base: usize, 35 base: usize,
36 input: String 36 input: String,
37} 37}
38 38
39lazy_static! { 39lazy_static! {
@@ -41,7 +41,7 @@ lazy_static! {
41} 41}
42 42
43fn main() { 43fn main() {
44 if CONFIGURATION.input.len() > 0 { 44 if !CONFIGURATION.input.is_empty() {
45 // command mode // 45 // command mode //
46 let evaled = eval_math_expression(&CONFIGURATION.input[..], 0f64); 46 let evaled = eval_math_expression(&CONFIGURATION.input[..], 0f64);
47 match evaled { 47 match evaled {
@@ -49,11 +49,11 @@ fn main() {
49 Err(e) => { 49 Err(e) => {
50 eprintln!("{}", handler(e)); 50 eprintln!("{}", handler(e));
51 std::process::exit(1); 51 std::process::exit(1);
52 }, 52 }
53 }; 53 };
54 } else { 54 } else {
55 // REPL mode // 55 // REPL mode //
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 59 // previous answer
@@ -65,7 +65,7 @@ fn main() {
65 let mut history_path = PathBuf::from(eva_data_dir); 65 let mut history_path = PathBuf::from(eva_data_dir);
66 match create_dir_all(eva_data_dir) { 66 match create_dir_all(eva_data_dir) {
67 Ok(_) => history_path.push("history.txt"), 67 Ok(_) => history_path.push("history.txt"),
68 Err(_) => history_path = PathBuf::from(UserDirs::new().unwrap().home_dir()), 68 Err(_) => history_path = PathBuf::from(UserDirs::new().unwrap().home_dir()),
69 }; 69 };
70 70
71 if rl.load_history(history_path.as_path()).is_err() { 71 if rl.load_history(history_path.as_path()).is_err() {
@@ -86,17 +86,15 @@ fn main() {
86 } 86 }
87 Err(e) => println!("{}", handler(e)), 87 Err(e) => println!("{}", handler(e)),
88 }; 88 };
89 }, 89 }
90 Err(ReadlineError::Interrupted) => { 90 Err(ReadlineError::Interrupted) => {
91 println!("CTRL-C"); 91 println!("CTRL-C");
92 break 92 break;
93 }, 93 }
94 Err(ReadlineError::Eof) => { 94 Err(ReadlineError::Eof) => break,
95 break
96 },
97 Err(err) => { 95 Err(err) => {
98 println!("Error: {:?}", err); 96 println!("Error: {:?}", err);
99 break 97 break;
100 } 98 }
101 } 99 }
102 } 100 }
@@ -109,25 +107,33 @@ fn parse_arguments() -> Configuration {
109 .version(env!("CARGO_PKG_VERSION")) 107 .version(env!("CARGO_PKG_VERSION"))
110 .author(env!("CARGO_PKG_AUTHORS")) 108 .author(env!("CARGO_PKG_AUTHORS"))
111 .about(env!("CARGO_PKG_DESCRIPTION")) 109 .about(env!("CARGO_PKG_DESCRIPTION"))
112 .arg(Arg::with_name("fix") 110 .arg(
113 .short("f") 111 Arg::with_name("fix")
114 .long("fix") 112 .short("f")
115 .takes_value(true) 113 .long("fix")
116 .value_name("FIX") 114 .takes_value(true)
117 .help("set number of decimal places in the output")) 115 .value_name("FIX")
118 .arg(Arg::with_name("base") 116 .help("set number of decimal places in the output"),
119 .short("b") 117 )
120 .long("base") 118 .arg(
121 .takes_value(true) 119 Arg::with_name("base")
122 .value_name("RADIX") 120 .short("b")
123 .help("set the radix of calculation output (1 - 36)")) 121 .long("base")
124 .arg(Arg::with_name("INPUT") 122 .takes_value(true)
125 .help("optional expression string to run eva in command mode") 123 .value_name("RADIX")
126 .index(1)) 124 .help("set the radix of calculation output (1 - 36)"),
127 .arg(Arg::with_name("radian") 125 )
128 .short("r") 126 .arg(
129 .long("radian") 127 Arg::with_name("INPUT")
130 .help("set eva to radian mode")) 128 .help("optional expression string to run eva in command mode")
129 .index(1),
130 )
131 .arg(
132 Arg::with_name("radian")
133 .short("r")
134 .long("radian")
135 .help("set eva to radian mode"),
136 )
131 .get_matches(); 137 .get_matches();
132 138
133 let mut input = String::new(); 139 let mut input = String::new();
@@ -136,29 +142,25 @@ fn parse_arguments() -> Configuration {
136 }; 142 };
137 Configuration { 143 Configuration {
138 radian_mode: config.is_present("radian"), 144 radian_mode: config.is_present("radian"),
139 fix: config.value_of("fix") 145 fix: config.value_of("fix").unwrap_or("10").parse().unwrap(),
140 .unwrap_or("10") 146 base: config.value_of("base").unwrap_or("10").parse().unwrap(),
141 .parse() 147 input,
142 .unwrap(),
143 base: config.value_of("base")
144 .unwrap_or("10")
145 .parse()
146 .unwrap(),
147 input,
148 } 148 }
149} 149}
150 150
151pub fn eval_math_expression(input: &str, prev_ans: f64) -> Result<f64, CalcError> { 151pub fn eval_math_expression(input: &str, prev_ans: f64) -> Result<f64, CalcError> {
152 let input = input.trim(); 152 let input = input.trim();
153 let input = input.replace(" ", ""); 153 let input = input.replace(" ", "");
154 if input.len() == 0 { 154 if input.is_empty() {
155 return Ok(0.) 155 return Ok(0.);
156 } 156 }
157 let input = format::autobalance_parens(&input[..])?; 157 let input = format::autobalance_parens(&input[..])?;
158 let lexed = lexer(&input[..], prev_ans)?; 158 let lexed = lexer(&input[..], prev_ans)?;
159 let postfixed = to_postfix(lexed)?; 159 let postfixed = to_postfix(lexed)?;
160 let evaled = eval_postfix(postfixed)?; 160 let evaled = eval_postfix(postfixed)?;
161 let evaled_fixed = format!("{:.*}", CONFIGURATION.fix, evaled).parse::<f64>().unwrap(); 161 let evaled_fixed = format!("{:.*}", CONFIGURATION.fix, evaled)
162 .parse::<f64>()
163 .unwrap();
162 Ok(evaled_fixed) 164 Ok(evaled_fixed)
163} 165}
164 166