From e5d358477f75b682c78b7d0d06709c9c5f847049 Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 22 Aug 2024 13:00:20 -0400 Subject: [PATCH 1/9] changed arg parse to use pico-args --- Cargo.toml | 1 + src/main.rs | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 9f38498..e4d84fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ path = "src/main.rs" [dependencies] evalexpr = "11.0.0" +pico-args = "0.5.0" termion = "1.5.6" [profile.release] diff --git a/src/main.rs b/src/main.rs index 334f03c..dba1c20 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,7 @@ use evalexpr::{ HashMapContext, Value }; +use pico_args::Arguments; use termion::{ color, style @@ -27,6 +28,8 @@ mod util; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); fn main() { + let args = Arguments::from_env(); + // build eval context let mut context = context_map! { // globals @@ -56,9 +59,10 @@ fn main() { }.unwrap(); // collect args and evaluate if present - let expressions: Vec = env::args().skip(1).collect(); + let expressions = args.finish(); if expressions.len() > 0 { for expression in expressions { + let expression: String = expression.to_string_lossy().into(); match expression.as_str() { "help" => help_text(), _ => do_eval(expression, &mut context) From 6a526ba8a530695d43f6475d8eb2fce4e61dad9a Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 22 Aug 2024 13:19:33 -0400 Subject: [PATCH 2/9] refactored help flag and initial implementations of 'empty' and 'version' flags --- src/flag.rs | 6 ++++ src/main.rs | 85 +++++++++++++++++++++++++++++++---------------------- 2 files changed, 56 insertions(+), 35 deletions(-) create mode 100644 src/flag.rs diff --git a/src/flag.rs b/src/flag.rs new file mode 100644 index 0000000..b2e1be3 --- /dev/null +++ b/src/flag.rs @@ -0,0 +1,6 @@ + +pub const EMPTY_CONTEXT: [&str;2] = [ "-E", "--empty"]; +pub const HELP: [&str;2] = [ "-h", "--help" ]; +pub const QUIET: [&str;2] = [ "-q", "--quiet" ]; +pub const VERSION: [&str;2] = [ "-V", "--version" ]; + diff --git a/src/main.rs b/src/main.rs index dba1c20..8c9da05 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,7 @@ use termion::{ style }; +mod flag; mod global; mod helper; mod util; @@ -28,49 +29,62 @@ mod util; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); fn main() { - let args = Arguments::from_env(); + let mut args = Arguments::from_env(); - // build eval context - let mut context = context_map! { - // globals - "c" => global::LIGHT_SPEED, - "e" => global::EULER, - "phi" => global::GOLDEN_RATIO, - "pi" => global::PI, - "√2" => global::ROOT_TWO, + // handle breaking flags + if args.contains(flag::HELP) { + help_text(); + return; + } + if args.contains(flag::VERSION) { + version_text(); + return; + } - // math functions - "fix" => Function::new(|arg| helper::fix(arg)), - "log" => Function::new(|arg| helper::logarithm(arg)), - "sqrt" => Function::new(|arg| helper::square_root(arg)), + // build context and handle empty flag + let mut context = + if !args.contains(flag::EMPTY_CONTEXT) { + context_map! { + // globals + "c" => global::LIGHT_SPEED, + "e" => global::EULER, + "phi" => global::GOLDEN_RATIO, + "pi" => global::PI, + "√2" => global::ROOT_TWO, - // data science functions - "avg" => Function::new(|arg| helper::average(arg)), + // math functions + "fix" => Function::new(|arg| helper::fix(arg)), + "log" => Function::new(|arg| helper::logarithm(arg)), + "sqrt" => Function::new(|arg| helper::square_root(arg)), - // radix functions - "bin" => Function::new(|arg| helper::binary(arg)), - "hex" => Function::new(|arg| helper::hexadecimal(arg)), - "oct" => Function::new(|arg| helper::octal(arg)), + // data science functions + "avg" => Function::new(|arg| helper::average(arg)), - // character aliases - "ϕ" => global::GOLDEN_RATIO, - "π" => global::PI, - "√" => Function::new(|arg| helper::square_root(arg)) - }.unwrap(); + // radix functions + "bin" => Function::new(|arg| helper::binary(arg)), + "hex" => Function::new(|arg| helper::hexadecimal(arg)), + "oct" => Function::new(|arg| helper::octal(arg)), + + // character aliases + "ϕ" => global::GOLDEN_RATIO, + "π" => global::PI, + "√" => Function::new(|arg| helper::square_root(arg)) + }.unwrap() + } else { + HashMapContext::new() + }; // collect args and evaluate if present let expressions = args.finish(); if expressions.len() > 0 { for expression in expressions { let expression: String = expression.to_string_lossy().into(); - match expression.as_str() { - "help" => help_text(), - _ => do_eval(expression, &mut context) - } + do_eval(expression, &mut context) } } else { // enter interactive mode if no args are given - println!("{}quickmaths v{}{}\n{}Interactive Mode{}", style::Bold, VERSION, style::Reset, style::Faint, style::Reset); + version_text(); + println!("{}Interactive Mode{}", style::Faint, style::Reset); loop { print!("> "); stdout().flush().unwrap(); @@ -135,16 +149,17 @@ fn reset() { stdout().flush().unwrap(); } +fn version_text() { + println!("qm v{VERSION}"); +} + fn help_text() { + version_text(); println!( - "{bold}quickmaths v{version}{reset} -Valerie Wolfe +"Valerie Wolfe A mathematical expression evaluator written in Rust. usage: - qm [EXPRESSION]...", - bold = style::Bold, - reset = style::Reset, - version = crate::VERSION + qm [EXPRESSION]..." ); } From 2bae1475b6e182224b53bd5bedd01dba181e2d04 Mon Sep 17 00:00:00 2001 From: Valerie Date: Wed, 28 Aug 2024 10:40:03 -0400 Subject: [PATCH 3/9] gave context items module structure, moved context build to a new method, and implemented empty and set flags --- src/{ => context}/global.rs | 0 src/{ => context}/helper.rs | 0 src/context/mod.rs | 4 +++ src/flag.rs | 1 + src/main.rs | 38 ++--------------------- src/util.rs | 61 +++++++++++++++++++++++++++++++++++-- 6 files changed, 66 insertions(+), 38 deletions(-) rename src/{ => context}/global.rs (100%) rename src/{ => context}/helper.rs (100%) create mode 100644 src/context/mod.rs diff --git a/src/global.rs b/src/context/global.rs similarity index 100% rename from src/global.rs rename to src/context/global.rs diff --git a/src/helper.rs b/src/context/helper.rs similarity index 100% rename from src/helper.rs rename to src/context/helper.rs diff --git a/src/context/mod.rs b/src/context/mod.rs new file mode 100644 index 0000000..ee794e1 --- /dev/null +++ b/src/context/mod.rs @@ -0,0 +1,4 @@ + +pub mod global; +pub mod helper; + diff --git a/src/flag.rs b/src/flag.rs index b2e1be3..c3fa1d7 100644 --- a/src/flag.rs +++ b/src/flag.rs @@ -2,5 +2,6 @@ pub const EMPTY_CONTEXT: [&str;2] = [ "-E", "--empty"]; pub const HELP: [&str;2] = [ "-h", "--help" ]; pub const QUIET: [&str;2] = [ "-q", "--quiet" ]; +pub const SET: &str = "--set"; pub const VERSION: [&str;2] = [ "-V", "--version" ]; diff --git a/src/main.rs b/src/main.rs index 8c9da05..5a6f591 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,6 @@ use std::{ }; use evalexpr::{ - context_map, eval_with_context_mut, ContextWithMutableVariables, HashMapContext, @@ -21,9 +20,9 @@ use termion::{ style }; +mod context; +mod eval; mod flag; -mod global; -mod helper; mod util; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -41,38 +40,7 @@ fn main() { return; } - // build context and handle empty flag - let mut context = - if !args.contains(flag::EMPTY_CONTEXT) { - context_map! { - // globals - "c" => global::LIGHT_SPEED, - "e" => global::EULER, - "phi" => global::GOLDEN_RATIO, - "pi" => global::PI, - "√2" => global::ROOT_TWO, - - // math functions - "fix" => Function::new(|arg| helper::fix(arg)), - "log" => Function::new(|arg| helper::logarithm(arg)), - "sqrt" => Function::new(|arg| helper::square_root(arg)), - - // data science functions - "avg" => Function::new(|arg| helper::average(arg)), - - // radix functions - "bin" => Function::new(|arg| helper::binary(arg)), - "hex" => Function::new(|arg| helper::hexadecimal(arg)), - "oct" => Function::new(|arg| helper::octal(arg)), - - // character aliases - "ϕ" => global::GOLDEN_RATIO, - "π" => global::PI, - "√" => Function::new(|arg| helper::square_root(arg)) - }.unwrap() - } else { - HashMapContext::new() - }; + let mut context = util::build_context(&mut args); // collect args and evaluate if present let expressions = args.finish(); diff --git a/src/util.rs b/src/util.rs index 1cc557e..0eda23c 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,7 +1,12 @@ + use evalexpr::{ - Value, - - EvalexprError + context_map, ContextWithMutableVariables, EvalexprError, HashMapContext, Value +}; +use pico_args::Arguments; + +use crate::{ + context::{ global, helper }, + flag }; pub(crate) fn parse_radix(prefix: &str, base: u32, arg: &Value) -> Result { @@ -16,3 +21,53 @@ pub(crate) fn parse_radix(prefix: &str, base: u32, arg: &Value) -> Result HashMapContext { + let mut output = + if !args.contains(flag::EMPTY_CONTEXT) { + context_map! { + // globals + "c" => global::LIGHT_SPEED, + "e" => global::EULER, + "phi" => global::GOLDEN_RATIO, + "pi" => global::PI, + "√2" => global::ROOT_TWO, + + // math functions + "fix" => Function::new(|arg| helper::fix(arg)), + "log" => Function::new(|arg| helper::logarithm(arg)), + "sqrt" => Function::new(|arg| helper::square_root(arg)), + + // data science functions + "avg" => Function::new(|arg| helper::average(arg)), + + // radix functions + "bin" => Function::new(|arg| helper::binary(arg)), + "hex" => Function::new(|arg| helper::hexadecimal(arg)), + "oct" => Function::new(|arg| helper::octal(arg)), + + // character aliases + "ϕ" => global::GOLDEN_RATIO, + "π" => global::PI, + "√" => Function::new(|arg| helper::square_root(arg)) + }.unwrap() + } else { HashMapContext::new() }; + + while let Ok(value) = args.value_from_str::<&str, String>(flag::SET) { + let split: Vec<&str> = value.split('=').collect(); + if split.len() == 2 { + let key = split[0].to_owned(); + + let value_str = split[1]; + let value = + if let Ok(integer) = value_str.parse::() { Value::Int(integer) } + else if let Ok(float) = value_str.parse::() { Value::Float(float) } + else { Value::from(value_str) }; + + output.set_value(key, value).ok(); } + else { std::process::exit(1); } + } + + output +} + From ed0e572f14c037f4e0271e48f09b99e3dd3afd50 Mon Sep 17 00:00:00 2001 From: Valerie Date: Wed, 28 Aug 2024 13:46:11 -0400 Subject: [PATCH 4/9] major main method cleanup and implemented quiet flag --- src/main.rs | 58 +++++++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5a6f591..5506278 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,12 +5,14 @@ use std::{ stdout, Write - } + }, + process::exit }; use evalexpr::{ eval_with_context_mut, - ContextWithMutableVariables, + + EvalexprError, HashMapContext, Value }; @@ -21,7 +23,6 @@ use termion::{ }; mod context; -mod eval; mod flag; mod util; @@ -41,13 +42,14 @@ fn main() { } let mut context = util::build_context(&mut args); + let quiet = args.contains(flag::QUIET); // collect args and evaluate if present let expressions = args.finish(); if expressions.len() > 0 { for expression in expressions { let expression: String = expression.to_string_lossy().into(); - do_eval(expression, &mut context) + eval(&expression, &mut context, quiet); } } else { // enter interactive mode if no args are given @@ -65,44 +67,44 @@ fn main() { match line.as_str() { "" | "exit" => break, - _ => do_eval(line, &mut context) + _ => eval(&line, &mut context, quiet) } reset(); } } } -fn do_eval(i_expression: String, context: &mut HashMapContext) { - let expression = i_expression.as_str(); +fn eval(expression: &str, context: &mut HashMapContext, quiet: bool) { let result = eval_with_context_mut(expression, context); + if quiet { + if let Ok(result) = result { println!("{result}") } + else { exit(1) } + } else { format(expression, result) } +} + +fn format(expression: &str, result: Result) { if let Ok(result) = result { - // display empty result - if result.is_empty() { + if !result.is_empty() { + let delimiter = + match result { + Value::Boolean(_) => "is", + Value::String(_) => "=>", + _ => "=" + }; + println!( + "{faint}{italic}{expression}{reset} {delimiter} {bold}{result}", + bold = style::Bold, + faint = style::Faint, + italic = style::Italic, + reset = style::Reset + ); + } else { println!( "{green}✓ {bold}{expression}", bold = style::Bold, green = color::Fg(color::Green) ); - return; } - - // get appropriate symbol for result by type - let delimiter; - match result { - Value::Boolean(_bool) => delimiter = "is", - Value::String(ref _str) => delimiter = "=>", - _ => delimiter = "=" - } - println!( - "{faint}{italic}{expression}{reset} {delimiter} {bold}{result}", - bold = style::Bold, - faint = style::Faint, - italic = style::Italic, - reset = style::Reset - ); - - // set "last" variable - context.set_value("$".to_string(), result).ok(); } else { println!( "{red}✕ {bold}{expression}", From ca75ff126095b83d2bdb8757a89e91c616d0a24e Mon Sep 17 00:00:00 2001 From: Valerie Date: Wed, 28 Aug 2024 13:46:32 -0400 Subject: [PATCH 5/9] version bump --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index e4d84fa..1113088 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "quickmath" -version = "0.2.4" +version = "0.3.0" edition = "2021" authors = [ "Valerie Wolfe " ] description = "A quick command-line math evaluator." From 27abd81bf895aa875f7be34b7fce5a3cd2fb8948 Mon Sep 17 00:00:00 2001 From: Valerie Date: Wed, 28 Aug 2024 19:19:58 -0400 Subject: [PATCH 6/9] made non-terminal default to quiet --- src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 5506278..4e94690 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use std::{ stdin, stdout, + IsTerminal, Write }, process::exit @@ -42,7 +43,7 @@ fn main() { } let mut context = util::build_context(&mut args); - let quiet = args.contains(flag::QUIET); + let quiet = args.contains(flag::QUIET) || !stdout().is_terminal(); // collect args and evaluate if present let expressions = args.finish(); From 6a7475683507d5804c41b6103c4045a3b0096bee Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 29 Aug 2024 13:38:27 -0400 Subject: [PATCH 7/9] implemented trig functions and refactored math helpers --- Cargo.toml | 2 +- src/context/helper.rs | 132 +++++++++++++++++++++++++----------------- src/context/mod.rs | 64 ++++++++++++++++++++ src/main.rs | 2 +- src/util.rs | 59 +------------------ 5 files changed, 145 insertions(+), 114 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1113088..5c36965 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "quickmath" -version = "0.3.0" +version = "0.3.1" edition = "2021" authors = [ "Valerie Wolfe " ] description = "A quick command-line math evaluator." diff --git a/src/context/helper.rs b/src/context/helper.rs index 221147a..6f4ad2b 100644 --- a/src/context/helper.rs +++ b/src/context/helper.rs @@ -10,71 +10,95 @@ pub type EvalResult = Result; // Mathematics +pub fn cosine(arg: &Value) -> EvalResult { + Ok( + if let Value::Float(float) = arg { float.clone() } + else if let Value::Int(int) = arg { int.clone() as f64 } + else { return Err(EvalexprError::expected_number(arg.clone())) } + .cos().into() + ) +} + pub fn fix(arg: &Value) -> EvalResult { - let args = arg.as_tuple()?; + if let Value::Tuple(args) = arg { + let len = args.len(); + if len == 2 { + let value = + if let Value::Float(float) = args[0] { float.clone() } + else { return Err(EvalexprError::expected_float(args[0].clone())); }; - let count = args.len(); - if count != 2 { - return Err(EvalexprError::WrongFunctionArgumentAmount { expected: 2..=2, actual: count }); - } + let operand = 10u64.pow( + if let Value::Int(int) = args[1] { int.clone() } + else { return Err(EvalexprError::expected_int(args[1].clone())); } + as u32 + ) as f64; - let float = args[0].as_float()?; - let figures = args[1].as_int()?; - - let operand: f64 = i64::pow(10, figures as u32) as f64; - let output = f64::round(float * operand) / operand; - return Ok(output.into()); + Ok( ((value * operand).round() / operand).into() ) + } else { Err(EvalexprError::wrong_function_argument_amount(len, 2)) } + } else { Err(EvalexprError::wrong_function_argument_amount(1, 2)) } } pub fn logarithm(arg: &Value) -> EvalResult { - let arguments: Vec; - let count: usize; - if arg.is_tuple() { - arguments = arg.as_tuple()?; - count = arguments.len(); - } else if arg.is_float() { - arguments = vec!(arg.as_float()?.into()); - count = 1; - } else if arg.is_int() { - arguments = vec!((arg.as_int()? as f64).into()); - count = 1; - } else { - return Err(EvalexprError::CustomMessage("Expected numbers".to_string())); + let value: f64; + let base: Option; + match arg { + Value::Tuple(tuple) + => { + let len = tuple.len(); + if len != 2 { return Err(EvalexprError::WrongOperatorArgumentAmount { expected: 2, actual: len }) } + + let i_value = tuple.get(0).unwrap(); + if let Value::Float(float) = i_value { value = float.clone(); } + else if let Value::Int(int) = i_value { value = int.clone() as f64; } + else { return Err(EvalexprError::expected_number(i_value.clone())); } + + let i_base = tuple.get(1).unwrap(); + if let Value::Float(float) = i_value { base = Some(float.clone()); } + else if let Value::Int(int) = i_value { base = Some(int.clone() as f64); } + else { return Err(EvalexprError::expected_number(i_base.clone())); } + }, + Value::Float(float) + => { + value = float.clone(); + base = None; + }, + Value::Int(int) + => { + value = int.clone() as f64; + base = None; + } + _ => return Err(EvalexprError::CustomMessage("Expected numbers.".to_owned())) } - let output: Value; - match count { - 1 => { - let argument = &arguments[0]; - if !argument.is_number() { - return Err(EvalexprError::CustomMessage("Expected number".to_string())); - } - let number = if argument.is_float() { argument.as_float()? } else { argument.as_int()? as f64 }; - output = number.ln().into(); - }, - 2 => { - let arg_value = &arguments[0]; - let arg_base = &arguments[1]; - if !(arg_value.is_number() && arg_base.is_number()) { - return Err(EvalexprError::CustomMessage("Expected two numbers".to_string())); - } - let value: f64 = if arg_value.is_float() { arg_value.as_float()? } else { arg_value.as_int()? as f64 }; - let base: f64 = if arg_base.is_float() { arg_base.as_float()? } else { arg_base.as_int()? as f64 }; - output = value.log(base).into(); - }, - _ => { - return Err(EvalexprError::WrongFunctionArgumentAmount { expected: 2..=2, actual: count }); - } - } - return Ok(output); + if let Some(base) = base { Ok(value.log(base).into()) } + else { Ok(value.ln().into()) } +} + +pub fn sine(arg: &Value) -> EvalResult { + Ok( + if let Value::Float(float) = arg { float.clone() } + else if let Value::Int(int) = arg { int.clone() as f64 } + else { return Err(EvalexprError::expected_number(arg.clone())) } + .sin().into() + ) } pub fn square_root(arg: &Value) -> EvalResult { - if !arg.is_number() { - return Err(EvalexprError::CustomMessage("Expected a number.".to_string())); - } - let value: f64 = if arg.is_float() { arg.as_float()? } else { arg.as_int()? as f64 }; - return Ok(value.sqrt().into()); + Ok( + if let Value::Float(float) = arg { float.clone() } + else if let Value::Int(int) = arg { int.clone() as f64 } + else { return Err(EvalexprError::expected_number(arg.clone())) } + .sqrt().into() + ) +} + +pub fn tangent(arg: &Value) -> EvalResult { + Ok( + if let Value::Float(float) = arg { float.clone() } + else if let Value::Int(int) = arg { int.clone() as f64 } + else { return Err(EvalexprError::expected_number(arg.clone())) } + .tan().into() + ) } diff --git a/src/context/mod.rs b/src/context/mod.rs index ee794e1..3023be6 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -2,3 +2,67 @@ pub mod global; pub mod helper; + +use pico_args::Arguments; +use evalexpr::{ + context_map, + + ContextWithMutableVariables, + HashMapContext, + Value +}; + +use crate::flag; + +pub fn build(args: &mut Arguments) -> HashMapContext { + let mut output = + if !args.contains(flag::EMPTY_CONTEXT) { + context_map! { + // globals + "c" => global::LIGHT_SPEED, + "e" => global::EULER, + "phi" => global::GOLDEN_RATIO, + "pi" => global::PI, + "√2" => global::ROOT_TWO, + + // math functions + "cos" => Function::new(|arg| helper::cosine(arg)), + "fix" => Function::new(|arg| helper::fix(arg)), + "log" => Function::new(|arg| helper::logarithm(arg)), + "sin" => Function::new(|arg| helper::sine(arg)), + "sqrt" => Function::new(|arg| helper::square_root(arg)), + "tan" => Function::new(|arg| helper::tangent(arg)), + + // data science functions + "avg" => Function::new(|arg| helper::average(arg)), + + // radix functions + "bin" => Function::new(|arg| helper::binary(arg)), + "hex" => Function::new(|arg| helper::hexadecimal(arg)), + "oct" => Function::new(|arg| helper::octal(arg)), + + // character aliases + "ϕ" => global::GOLDEN_RATIO, + "π" => global::PI, + "√" => Function::new(|arg| helper::square_root(arg)) + }.unwrap() + } else { HashMapContext::new() }; + + while let Ok(value) = args.value_from_str::<&str, String>(flag::SET) { + let split: Vec<&str> = value.split('=').collect(); + if split.len() == 2 { + let key = split[0].to_owned(); + + let value_str = split[1]; + let value = + if let Ok(integer) = value_str.parse::() { Value::Int(integer) } + else if let Ok(float) = value_str.parse::() { Value::Float(float) } + else { Value::from(value_str) }; + + output.set_value(key, value).ok(); } + else { std::process::exit(1); } + } + + output +} + diff --git a/src/main.rs b/src/main.rs index 4e94690..241157f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,7 +42,7 @@ fn main() { return; } - let mut context = util::build_context(&mut args); + let mut context = context::build(&mut args); let quiet = args.contains(flag::QUIET) || !stdout().is_terminal(); // collect args and evaluate if present diff --git a/src/util.rs b/src/util.rs index 0eda23c..be4b2ec 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,13 +1,5 @@ -use evalexpr::{ - context_map, ContextWithMutableVariables, EvalexprError, HashMapContext, Value -}; -use pico_args::Arguments; - -use crate::{ - context::{ global, helper }, - flag -}; +use evalexpr::{ EvalexprError, Value }; pub(crate) fn parse_radix(prefix: &str, base: u32, arg: &Value) -> Result { let i_parse = arg.as_string()?; @@ -22,52 +14,3 @@ pub(crate) fn parse_radix(prefix: &str, base: u32, arg: &Value) -> Result HashMapContext { - let mut output = - if !args.contains(flag::EMPTY_CONTEXT) { - context_map! { - // globals - "c" => global::LIGHT_SPEED, - "e" => global::EULER, - "phi" => global::GOLDEN_RATIO, - "pi" => global::PI, - "√2" => global::ROOT_TWO, - - // math functions - "fix" => Function::new(|arg| helper::fix(arg)), - "log" => Function::new(|arg| helper::logarithm(arg)), - "sqrt" => Function::new(|arg| helper::square_root(arg)), - - // data science functions - "avg" => Function::new(|arg| helper::average(arg)), - - // radix functions - "bin" => Function::new(|arg| helper::binary(arg)), - "hex" => Function::new(|arg| helper::hexadecimal(arg)), - "oct" => Function::new(|arg| helper::octal(arg)), - - // character aliases - "ϕ" => global::GOLDEN_RATIO, - "π" => global::PI, - "√" => Function::new(|arg| helper::square_root(arg)) - }.unwrap() - } else { HashMapContext::new() }; - - while let Ok(value) = args.value_from_str::<&str, String>(flag::SET) { - let split: Vec<&str> = value.split('=').collect(); - if split.len() == 2 { - let key = split[0].to_owned(); - - let value_str = split[1]; - let value = - if let Ok(integer) = value_str.parse::() { Value::Int(integer) } - else if let Ok(float) = value_str.parse::() { Value::Float(float) } - else { Value::from(value_str) }; - - output.set_value(key, value).ok(); } - else { std::process::exit(1); } - } - - output -} - From 8c1610bcf9b1a91cdec48ece9ec3ef5a59cfb9d9 Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 29 Aug 2024 13:52:19 -0400 Subject: [PATCH 8/9] refactored other helper methods --- src/context/helper.rs | 71 ++++++++++++------------------------------- src/util.rs | 14 +++------ 2 files changed, 24 insertions(+), 61 deletions(-) diff --git a/src/context/helper.rs b/src/context/helper.rs index 6f4ad2b..4bfaa85 100644 --- a/src/context/helper.rs +++ b/src/context/helper.rs @@ -104,68 +104,37 @@ pub fn tangent(arg: &Value) -> EvalResult { // Data Science pub fn average(arg: &Value) -> EvalResult { - let arguments = arg.as_tuple()?; - let count = arguments.len() as i64; - let mut is_float = false; - let mut total_i = 0i64; - let mut total_f = 0f64; + if let Value::Tuple(args) = arg { + let len = args.len() as f64; + let mut total = 0f64; - for argument in arguments { - if let Value::Float(float) = argument { - if !is_float { - total_f = total_i as f64; - is_float = true; - } - total_f += float; - } else if let Value::Int(int) = argument { - if is_float { - total_f += int as f64; - } else { - total_i += int; - } + for arg in args { + total += + if let Value::Float(float) = arg { float.clone() } + else if let Value::Int(int) = arg { int.clone() as f64 } + else { todo!() }; } - } - - let result_i: i64; - let result_f: f64; - if !is_float { - is_float = total_i % count == 0; - total_f = total_i as f64; - } - if is_float { - result_f = total_f / (count as f64); - return Ok(result_f.into()); - } else { - result_i = total_i / count; - return Ok(result_i.into()); - } + Ok( (total / len).into() ) + } else { todo!() } } // Radix conversion pub fn binary(arg: &Value) -> EvalResult { - if !arg.is_string() { - let num = arg.as_int()?; - let fmt = format!("0b{:b}", num); - return Ok(fmt.into()); - } - util::parse_radix("0b", 2, arg) + if let Value::Int(int) = arg { Ok( format!("0b{:b}", int).into() ) } + else if let Value::String(string) = arg { util::parse_radix("0b", 2, string) } + else { Err(EvalexprError::expected_number_or_string(arg.clone())) } } pub fn hexadecimal(arg: &Value) -> EvalResult { - if !arg.is_string() { - let num = arg.as_int()?; - let fmt = format!("0x{:X}", num); - return Ok(fmt.into()); - } - util::parse_radix("0x", 16, arg) + if let Value::Int(int) = arg { Ok( format!("0x{:X}", int).into() ) } + else if let Value::String(string) = arg { util::parse_radix("0x", 16, string) } + else { Err(EvalexprError::expected_number_or_string(arg.clone())) } } pub fn octal(arg: &Value) -> EvalResult { - if !arg.is_string() { - let num = arg.as_int()?; - let fmt = format!("{:#o}", num); - return Ok(fmt.into()); - } - util::parse_radix("0o", 8, arg) + if let Value::Int(int) = arg { Ok( format!("0o{:#o}", int).into() ) } + else if let Value::String(string) = arg { util::parse_radix("0o", 8, string) } + else { Err(EvalexprError::expected_number_or_string(arg.clone())) } } + diff --git a/src/util.rs b/src/util.rs index be4b2ec..6daadbf 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,16 +1,10 @@ use evalexpr::{ EvalexprError, Value }; -pub(crate) fn parse_radix(prefix: &str, base: u32, arg: &Value) -> Result { - let i_parse = arg.as_string()?; - let parse = i_parse.strip_prefix(prefix).unwrap_or(i_parse.as_str()); +pub fn parse_radix(prefix: &str, base: u32, arg: &str) -> Result { + let parse = arg.strip_prefix(prefix).unwrap_or(arg); - let i_result = i64::from_str_radix(parse, base); - if i_result.is_err() { - return Err(EvalexprError::CustomMessage("failed to parse integer from string".to_string())); - } - let result = i_result.ok(); - - return Ok(result.unwrap().into()); + if let Ok(int) = i64::from_str_radix(parse, base) { Ok(int.into()) } + else { Err(EvalexprError::CustomMessage("failed to parse integer from string".to_string())) } } From 17165ff634a8809101616af8a98609042c1d12a8 Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 29 Aug 2024 09:17:30 -0400 Subject: [PATCH 9/9] added rustyline --- Cargo.toml | 1 + src/main.rs | 40 +++++++++++++++++++++++----------------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5c36965..7969df8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ path = "src/main.rs" [dependencies] evalexpr = "11.0.0" pico-args = "0.5.0" +rustyline = "14.0.0" termion = "1.5.6" [profile.release] diff --git a/src/main.rs b/src/main.rs index 241157f..289bd08 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use std::{ stdout, IsTerminal, + Read, Write }, process::exit @@ -18,6 +19,7 @@ use evalexpr::{ Value }; use pico_args::Arguments; +use rustyline::DefaultEditor; use termion::{ color, style @@ -43,7 +45,8 @@ fn main() { } let mut context = context::build(&mut args); - let quiet = args.contains(flag::QUIET) || !stdout().is_terminal(); + let is_terminal = stdin().is_terminal() && stdout().is_terminal(); + let quiet = args.contains(flag::QUIET) || !is_terminal; // collect args and evaluate if present let expressions = args.finish(); @@ -53,24 +56,27 @@ fn main() { eval(&expression, &mut context, quiet); } } else { - // enter interactive mode if no args are given - version_text(); - println!("{}Interactive Mode{}", style::Faint, style::Reset); - loop { - print!("> "); - stdout().flush().unwrap(); - let mut i_line = String::new(); - let line_result = stdin().read_line(&mut i_line); - if line_result.is_err() { - break; + if !is_terminal { + let mut buffer = String::with_capacity(0); + if let Ok(_) = stdin().read_to_string(&mut buffer) { + for expression in buffer.lines() { + eval(&expression.to_string(), &mut context, quiet); + } } - let line = i_line.trim().to_string(); - match line.as_str() { - "" | - "exit" => break, - _ => eval(&line, &mut context, quiet) + } else if let Ok(mut rl) = DefaultEditor::new() { + // enter interactive mode if no args are given + version_text(); + println!("{}Interactive Mode{}", style::Faint, style::Reset); + loop { + if let Ok(line) = rl.readline("> ") { + match line.as_str() { + "" | + "exit" => break, + _ => eval(&line, &mut context, quiet) + } + } + reset(); } - reset(); } } }