diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/error.rs | 71 | ||||
-rw-r--r-- | src/error/mod.rs | 29 | ||||
-rw-r--r-- | src/format.rs (renamed from src/format/mod.rs) | 0 | ||||
-rw-r--r-- | src/lex.rs (renamed from src/lex/mod.rs) | 9 | ||||
-rw-r--r-- | src/main.rs | 6 | ||||
-rw-r--r-- | src/parse.rs (renamed from src/parse/mod.rs) | 0 | ||||
-rw-r--r-- | src/readline.rs (renamed from src/readline/mod.rs) | 2 |
7 files changed, 81 insertions, 36 deletions
diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..e05d4e3 --- /dev/null +++ b/src/error.rs | |||
@@ -0,0 +1,71 @@ | |||
1 | /* Copyright (C) 2019 Akshay Oppiliappan <[email protected]> | ||
2 | * Refer to LICENCE for more information. | ||
3 | * */ | ||
4 | |||
5 | use std::iter::ExactSizeIterator; | ||
6 | |||
7 | use crate::lex; | ||
8 | |||
9 | #[derive(Debug)] | ||
10 | pub enum CalcError { | ||
11 | Math(Math), | ||
12 | Syntax(String), | ||
13 | Parser(String), | ||
14 | Help, | ||
15 | } | ||
16 | |||
17 | #[derive(Debug)] | ||
18 | pub enum Math { | ||
19 | DivideByZero, | ||
20 | OutOfBounds, | ||
21 | UnknownBase, | ||
22 | } | ||
23 | |||
24 | pub fn handler(e: CalcError) -> String { | ||
25 | match e { | ||
26 | CalcError::Math(math_err) => match math_err { | ||
27 | Math::DivideByZero => "Math Error: Divide by zero error!".to_string(), | ||
28 | Math::OutOfBounds => "Domain Error: Out of bounds!".to_string(), | ||
29 | Math::UnknownBase => "Base too large! Accepted ranges: 0 - 36".to_string(), | ||
30 | }, | ||
31 | CalcError::Syntax(details) => format!("Syntax Error: {}", details), | ||
32 | CalcError::Parser(details) => format!("Parser Error: {}", details), | ||
33 | CalcError::Help => { | ||
34 | // calculate max width but ideally this should be calculated once | ||
35 | let mut max_width = 79; // capped at 79 | ||
36 | if let Some((w, _)) = term_size::dimensions() { | ||
37 | if w < max_width { | ||
38 | max_width = w; | ||
39 | } | ||
40 | } | ||
41 | let operators: Vec<_> = lex::OPERATORS.keys().map(|c| c.to_string()).collect(); | ||
42 | format!( | ||
43 | "Constants\n{}\nFunctions\n{}\nOperators\n{}\n", | ||
44 | blocks(max_width, lex::CONSTANTS.keys().cloned()), | ||
45 | blocks(max_width, lex::FUNCTIONS.keys().cloned()), | ||
46 | operators.join(" ") | ||
47 | ) | ||
48 | } | ||
49 | } | ||
50 | } | ||
51 | |||
52 | /// Convert iterator into strings of chunks of 8 right padded with space. | ||
53 | fn blocks( | ||
54 | max_width: usize, | ||
55 | mut iter: impl Iterator<Item = &'static str> + ExactSizeIterator, | ||
56 | ) -> String { | ||
57 | // multiply by eight since we are formatting it into chunks of 8 | ||
58 | let items_per_line = max_width / 8; | ||
59 | let full_bytes = (iter.len() - iter.len() % items_per_line) * 8; | ||
60 | let part_bytes = iter.len() % items_per_line * 8; // leftovers | ||
61 | let n_newlines = iter.len() / items_per_line + if part_bytes > 0 { 1 } else { 0 }; | ||
62 | let mut s = String::with_capacity(full_bytes + part_bytes + n_newlines); | ||
63 | for _ in 0..n_newlines { | ||
64 | for item in iter.by_ref().take(items_per_line) { | ||
65 | s.extend(format!("{:>8}", item).chars()); | ||
66 | } | ||
67 | s.push('\n'); | ||
68 | } | ||
69 | debug_assert_eq!(s.capacity(), s.len()); // check capacity calculation | ||
70 | s | ||
71 | } | ||
diff --git a/src/error/mod.rs b/src/error/mod.rs deleted file mode 100644 index ec2b555..0000000 --- a/src/error/mod.rs +++ /dev/null | |||
@@ -1,29 +0,0 @@ | |||
1 | /* Copyright (C) 2019 Akshay Oppiliappan <[email protected]> | ||
2 | * Refer to LICENCE for more information. | ||
3 | * */ | ||
4 | |||
5 | #[derive(Debug)] | ||
6 | pub enum CalcError { | ||
7 | Math(Math), | ||
8 | Syntax(String), | ||
9 | Parser(String), | ||
10 | } | ||
11 | |||
12 | #[derive(Debug)] | ||
13 | pub enum Math { | ||
14 | DivideByZero, | ||
15 | OutOfBounds, | ||
16 | UnknownBase, | ||
17 | } | ||
18 | |||
19 | pub fn handler(e: CalcError) -> String { | ||
20 | match e { | ||
21 | CalcError::Math(math_err) => match math_err { | ||
22 | Math::DivideByZero => "Math Error: Divide by zero error!".to_string(), | ||
23 | Math::OutOfBounds => "Domain Error: Out of bounds!".to_string(), | ||
24 | Math::UnknownBase => "Base too large! Accepted ranges: 0 - 36".to_string(), | ||
25 | }, | ||
26 | CalcError::Syntax(details) => format!("Syntax Error: {}", details), | ||
27 | CalcError::Parser(details) => format!("Parser Error: {}", details), | ||
28 | } | ||
29 | } | ||
diff --git a/src/format/mod.rs b/src/format.rs index 45673d7..45673d7 100644 --- a/src/format/mod.rs +++ b/src/format.rs | |||
diff --git a/src/lex/mod.rs b/src/lex.rs index 3222c12..76f61a0 100644 --- a/src/lex/mod.rs +++ b/src/lex.rs | |||
@@ -5,9 +5,8 @@ | |||
5 | use lazy_static::lazy_static; | 5 | use lazy_static::lazy_static; |
6 | use std::collections::HashMap; | 6 | use std::collections::HashMap; |
7 | 7 | ||
8 | use crate::CONFIGURATION; | ||
9 | |||
10 | use crate::error::{CalcError, Math}; | 8 | use crate::error::{CalcError, Math}; |
9 | use crate::CONFIGURATION; | ||
11 | 10 | ||
12 | #[derive(Debug, Copy, Clone, PartialEq)] | 11 | #[derive(Debug, Copy, Clone, PartialEq)] |
13 | pub struct Operator { | 12 | pub struct Operator { |
@@ -70,14 +69,14 @@ pub enum Token { | |||
70 | } | 69 | } |
71 | 70 | ||
72 | lazy_static! { | 71 | lazy_static! { |
73 | static ref CONSTANTS: HashMap<&'static str, Token> = { | 72 | pub static ref CONSTANTS: HashMap<&'static str, Token> = { |
74 | let mut m = HashMap::new(); | 73 | let mut m = HashMap::new(); |
75 | m.insert("e", Token::Num(std::f64::consts::E)); | 74 | m.insert("e", Token::Num(std::f64::consts::E)); |
76 | m.insert("pi", Token::Num(std::f64::consts::PI)); | 75 | m.insert("pi", Token::Num(std::f64::consts::PI)); |
77 | m | 76 | m |
78 | }; | 77 | }; |
79 | 78 | ||
80 | static ref FUNCTIONS: HashMap<&'static str, Token> = { | 79 | pub static ref FUNCTIONS: HashMap<&'static str, Token> = { |
81 | let mut m = HashMap::new(); | 80 | let mut m = HashMap::new(); |
82 | m.insert("sin", Function::token_from_fn("sin".into(), |x| is_radian_mode(x, CONFIGURATION.radian_mode).sin())); | 81 | m.insert("sin", Function::token_from_fn("sin".into(), |x| is_radian_mode(x, CONFIGURATION.radian_mode).sin())); |
83 | m.insert("cos", Function::token_from_fn("cos".into(), |x| is_radian_mode(x, CONFIGURATION.radian_mode).cos())); | 82 | m.insert("cos", Function::token_from_fn("cos".into(), |x| is_radian_mode(x, CONFIGURATION.radian_mode).cos())); |
@@ -106,7 +105,7 @@ lazy_static! { | |||
106 | m | 105 | m |
107 | }; | 106 | }; |
108 | 107 | ||
109 | static ref OPERATORS: HashMap<char, Token> = { | 108 | pub static ref OPERATORS: HashMap<char, Token> = { |
110 | let mut m = HashMap::new(); | 109 | let mut m = HashMap::new(); |
111 | m.insert('+', Operator::token_from_op('+', |x, y| x + y, 2, true)); | 110 | m.insert('+', Operator::token_from_op('+', |x, y| x + y, 2, true)); |
112 | m.insert('-', Operator::token_from_op('-', |x, y| x - y, 2, true)); | 111 | m.insert('-', Operator::token_from_op('-', |x, y| x - y, 2, true)); |
diff --git a/src/main.rs b/src/main.rs index 5b7fdb4..75c33bb 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -193,8 +193,10 @@ fn parse_arguments() -> Configuration { | |||
193 | } | 193 | } |
194 | 194 | ||
195 | pub fn eval_math_expression(input: &str, prev_ans: Option<f64>) -> Result<f64, CalcError> { | 195 | pub fn eval_math_expression(input: &str, prev_ans: Option<f64>) -> Result<f64, CalcError> { |
196 | let input = input.trim(); | 196 | let input = input.trim().replace(" ", ""); |
197 | let input = input.replace(" ", ""); | 197 | if input == "help" { |
198 | return Err(CalcError::Help); | ||
199 | } | ||
198 | if input.is_empty() { | 200 | if input.is_empty() { |
199 | return Ok(0.); | 201 | return Ok(0.); |
200 | } | 202 | } |
diff --git a/src/parse/mod.rs b/src/parse.rs index aa8bd4b..aa8bd4b 100644 --- a/src/parse/mod.rs +++ b/src/parse.rs | |||
diff --git a/src/readline/mod.rs b/src/readline.rs index ea195ee..d689f95 100644 --- a/src/readline/mod.rs +++ b/src/readline.rs | |||
@@ -12,6 +12,7 @@ use directories::ProjectDirs; | |||
12 | 12 | ||
13 | use regex::Regex; | 13 | use regex::Regex; |
14 | 14 | ||
15 | use crate::error::CalcError; | ||
15 | use crate::eval_math_expression; | 16 | use crate::eval_math_expression; |
16 | 17 | ||
17 | pub struct RLHelper { | 18 | pub struct RLHelper { |
@@ -67,6 +68,7 @@ impl Highlighter for LineHighlighter { | |||
67 | } | 68 | } |
68 | Owned(coloured) | 69 | Owned(coloured) |
69 | } | 70 | } |
71 | Err(CalcError::Help) => Owned(line.replace("help", "\x1b[36mhelp\x1b[0m")), | ||
70 | Err(_) => Owned(format!("\x1b[31m{}\x1b[0m", line)), | 72 | Err(_) => Owned(format!("\x1b[31m{}\x1b[0m", line)), |
71 | } | 73 | } |
72 | } | 74 | } |