Compare commits
No commits in common. "2bae1475b6e182224b53bd5bedd01dba181e2d04" and "d144bd92454cf5ec4d0d04eab7ddcd3fb218be9c" have entirely different histories.
2bae1475b6
...
d144bd9245
7 changed files with 43 additions and 97 deletions
|
@ -14,7 +14,6 @@ path = "src/main.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
evalexpr = "11.0.0"
|
evalexpr = "11.0.0"
|
||||||
pico-args = "0.5.0"
|
|
||||||
termion = "1.5.6"
|
termion = "1.5.6"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
|
|
||||||
pub mod global;
|
|
||||||
pub mod helper;
|
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
|
|
||||||
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" ];
|
|
||||||
|
|
67
src/main.rs
67
src/main.rs
|
@ -9,50 +9,64 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use evalexpr::{
|
use evalexpr::{
|
||||||
|
context_map,
|
||||||
eval_with_context_mut,
|
eval_with_context_mut,
|
||||||
ContextWithMutableVariables,
|
ContextWithMutableVariables,
|
||||||
HashMapContext,
|
HashMapContext,
|
||||||
Value
|
Value
|
||||||
};
|
};
|
||||||
use pico_args::Arguments;
|
|
||||||
use termion::{
|
use termion::{
|
||||||
color,
|
color,
|
||||||
style
|
style
|
||||||
};
|
};
|
||||||
|
|
||||||
mod context;
|
mod global;
|
||||||
mod eval;
|
mod helper;
|
||||||
mod flag;
|
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
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
|
// math functions
|
||||||
if args.contains(flag::HELP) {
|
"fix" => Function::new(|arg| helper::fix(arg)),
|
||||||
help_text();
|
"log" => Function::new(|arg| helper::logarithm(arg)),
|
||||||
return;
|
"sqrt" => Function::new(|arg| helper::square_root(arg)),
|
||||||
}
|
|
||||||
if args.contains(flag::VERSION) {
|
|
||||||
version_text();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut context = util::build_context(&mut args);
|
// 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();
|
||||||
|
|
||||||
// collect args and evaluate if present
|
// collect args and evaluate if present
|
||||||
let expressions = args.finish();
|
let expressions: Vec<String> = env::args().skip(1).collect();
|
||||||
if expressions.len() > 0 {
|
if expressions.len() > 0 {
|
||||||
for expression in expressions {
|
for expression in expressions {
|
||||||
let expression: String = expression.to_string_lossy().into();
|
match expression.as_str() {
|
||||||
do_eval(expression, &mut context)
|
"help" => help_text(),
|
||||||
|
_ => do_eval(expression, &mut context)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// enter interactive mode if no args are given
|
// enter interactive mode if no args are given
|
||||||
version_text();
|
println!("{}quickmaths v{}{}\n{}Interactive Mode{}", style::Bold, VERSION, style::Reset, style::Faint, style::Reset);
|
||||||
println!("{}Interactive Mode{}", style::Faint, style::Reset);
|
|
||||||
loop {
|
loop {
|
||||||
print!("> ");
|
print!("> ");
|
||||||
stdout().flush().unwrap();
|
stdout().flush().unwrap();
|
||||||
|
@ -117,17 +131,16 @@ fn reset() {
|
||||||
stdout().flush().unwrap();
|
stdout().flush().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn version_text() {
|
|
||||||
println!("qm v{VERSION}");
|
|
||||||
}
|
|
||||||
|
|
||||||
fn help_text() {
|
fn help_text() {
|
||||||
version_text();
|
|
||||||
println!(
|
println!(
|
||||||
"Valerie Wolfe <sleeplessval@gmail.com>
|
"{bold}quickmaths v{version}{reset}
|
||||||
|
Valerie Wolfe <sleeplessval@gmail.com>
|
||||||
A mathematical expression evaluator written in Rust.
|
A mathematical expression evaluator written in Rust.
|
||||||
|
|
||||||
usage:
|
usage:
|
||||||
qm [EXPRESSION]..."
|
qm [EXPRESSION]...",
|
||||||
|
bold = style::Bold,
|
||||||
|
reset = style::Reset,
|
||||||
|
version = crate::VERSION
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
61
src/util.rs
61
src/util.rs
|
@ -1,12 +1,7 @@
|
||||||
|
|
||||||
use evalexpr::{
|
use evalexpr::{
|
||||||
context_map, ContextWithMutableVariables, EvalexprError, HashMapContext, Value
|
Value,
|
||||||
};
|
|
||||||
use pico_args::Arguments;
|
EvalexprError
|
||||||
|
|
||||||
use crate::{
|
|
||||||
context::{ global, helper },
|
|
||||||
flag
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(crate) fn parse_radix(prefix: &str, base: u32, arg: &Value) -> Result<Value, EvalexprError> {
|
pub(crate) fn parse_radix(prefix: &str, base: u32, arg: &Value) -> Result<Value, EvalexprError> {
|
||||||
|
@ -21,53 +16,3 @@ pub(crate) fn parse_radix(prefix: &str, base: u32, arg: &Value) -> Result<Value,
|
||||||
|
|
||||||
return Ok(result.unwrap().into());
|
return Ok(result.unwrap().into());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn build_context(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
|
|
||||||
"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::<i64>() { Value::Int(integer) }
|
|
||||||
else if let Ok(float) = value_str.parse::<f64>() { Value::Float(float) }
|
|
||||||
else { Value::from(value_str) };
|
|
||||||
|
|
||||||
output.set_value(key, value).ok(); }
|
|
||||||
else { std::process::exit(1); }
|
|
||||||
}
|
|
||||||
|
|
||||||
output
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue