Compare commits
No commits in common. "4a7e2840473e6db39a05d12793b5f92b81dd6ec3" and "c575e0bf4dff184939f4eba1ff73d5260d069707" have entirely different histories.
4a7e284047
...
c575e0bf4d
8 changed files with 82 additions and 233 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1 @@
|
|||
/target
|
||||
Cargo.lock
|
||||
|
|
65
Cargo.lock
generated
Normal file
65
Cargo.lock
generated
Normal file
|
@ -0,0 +1,65 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||
|
||||
[[package]]
|
||||
name = "evalexpr"
|
||||
version = "7.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d4fd7bd9e32c1205549decf6f36772d7b606a579b26afaffa335ae148151a5d"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.126"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
|
||||
|
||||
[[package]]
|
||||
name = "numtoa"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef"
|
||||
|
||||
[[package]]
|
||||
name = "quickmaths"
|
||||
version = "0.1.1"
|
||||
dependencies = [
|
||||
"evalexpr",
|
||||
"termion",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.2.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "62f25bc4c7e55e0b0b7a1d43fb893f4fa1361d0abe38b9ce4f323c2adfe6ef42"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_termios"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8440d8acb4fd3d277125b4bd01a6f38aee8d814b3b5fc09b3f2b825d37d3fe8f"
|
||||
dependencies = [
|
||||
"redox_syscall",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "termion"
|
||||
version = "1.5.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "077185e2eac69c3f8379a4298e1e07cd36beb962290d4a51199acf0fdc10607e"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"numtoa",
|
||||
"redox_syscall",
|
||||
"redox_termios",
|
||||
]
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "quickmaths"
|
||||
version = "0.2.0"
|
||||
version = "0.1.1"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
|
10
README.md
10
README.md
|
@ -1,10 +0,0 @@
|
|||
|
||||
# quickmaths
|
||||
|
||||
A small Rust program that can do common math from the command line.
|
||||
|
||||
I got VERY tired of opening a calculator or a full Python interpreter to do simple math, so I initially created `quickmaths` in Python to do math from the command line.
|
||||
|
||||
As the features I wanted changed, such as variable assignment, interactive mode, and output status and formatting, I decided to rebuild the utility in Rust.
|
||||
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
|
||||
pub const EULER: f64 = 2.718281828459045;
|
||||
pub const GOLDEN_RATIO: f64 = 1.618033988749895;
|
||||
pub const PI: f64 = 3.141592653589793;
|
||||
pub const ROOT_TWO: f64 = 1.414213562373095;
|
||||
|
127
src/helper.rs
127
src/helper.rs
|
@ -1,127 +0,0 @@
|
|||
use evalexpr::{
|
||||
Value,
|
||||
|
||||
EvalexprError
|
||||
};
|
||||
|
||||
use crate::util;
|
||||
|
||||
// Mathematics
|
||||
pub fn logarithm(arg: &Value) -> Result<Value, EvalexprError> {
|
||||
let arguments: Vec<Value>;
|
||||
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 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, actual: count });
|
||||
}
|
||||
}
|
||||
return Ok(output);
|
||||
}
|
||||
|
||||
pub fn square_root(arg: &Value) -> Result<Value, EvalexprError> {
|
||||
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());
|
||||
}
|
||||
|
||||
// Data Science
|
||||
pub fn average(arg: &Value) -> Result<Value, EvalexprError> {
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
// Radix conversion
|
||||
pub fn binary(arg: &Value) -> Result<Value, EvalexprError> {
|
||||
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)
|
||||
}
|
||||
|
||||
pub fn hexadecimal(arg: &Value) -> Result<Value, EvalexprError> {
|
||||
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)
|
||||
}
|
||||
|
||||
pub fn octal(arg: &Value) -> Result<Value, EvalexprError> {
|
||||
if !arg.is_string() {
|
||||
let num = arg.as_int()?;
|
||||
let fmt = format!("{:#o}", num);
|
||||
return Ok(fmt.into());
|
||||
}
|
||||
util::parse_radix("0o", 8, arg)
|
||||
}
|
86
src/main.rs
86
src/main.rs
|
@ -1,116 +1,62 @@
|
|||
use std::{
|
||||
env,
|
||||
io::{
|
||||
stdin,
|
||||
stdout,
|
||||
|
||||
Write
|
||||
}
|
||||
io::stdin
|
||||
};
|
||||
|
||||
use evalexpr::{
|
||||
context_map,
|
||||
|
||||
eval_with_context_mut,
|
||||
|
||||
HashMapContext,
|
||||
Value
|
||||
Value,
|
||||
ValueType
|
||||
};
|
||||
use termion::{
|
||||
color,
|
||||
style
|
||||
};
|
||||
|
||||
mod global;
|
||||
mod helper;
|
||||
mod util;
|
||||
|
||||
pub const VERSION: &str = "0.2.0";
|
||||
|
||||
fn main() {
|
||||
let mut context = context_map! {
|
||||
// globals
|
||||
"e" => global::EULER,
|
||||
"phi" => global::GOLDEN_RATIO,
|
||||
"pi" => global::PI,
|
||||
"√2" => global::ROOT_TWO,
|
||||
|
||||
// math functions
|
||||
"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();
|
||||
let mut context = HashMapContext::new();
|
||||
let expressions: Vec<String> = env::args().skip(1).collect();
|
||||
if expressions.len() == 0 {
|
||||
println!("{}quickmaths v{}{}\n{}Interactive Mode{}", style::Bold, VERSION, style::Reset, style::Faint, style::Reset);
|
||||
println!("{}quickmaths v0.1.1{}\n{}Interactive Mode{}", style::Bold, style::Reset, 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;
|
||||
}
|
||||
let line = i_line.trim().to_string();
|
||||
match line.as_str() {
|
||||
"" |
|
||||
"exit" => break,
|
||||
_ => do_eval(line, &mut context)
|
||||
if line.is_empty() {
|
||||
break;
|
||||
}
|
||||
reset();
|
||||
let result = do_eval(line, &mut context);
|
||||
println!("{}{}{}", result.0, color::Fg(color::Reset), style::Reset);
|
||||
}
|
||||
} else {
|
||||
for expression in expressions {
|
||||
match expression.as_str() {
|
||||
"help" => help_text(),
|
||||
_ => do_eval(expression, &mut context)
|
||||
}
|
||||
let result = do_eval(expression, &mut context);
|
||||
println!("{}{}{}", result.0, color::Fg(color::Reset), style::Reset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn do_eval(i_expression: String, context: &mut HashMapContext) {
|
||||
fn do_eval(i_expression: String, context: &mut HashMapContext) -> (String, Option<Value>) {
|
||||
let expression = i_expression.as_str();
|
||||
let i_result = eval_with_context_mut(expression, context);
|
||||
if i_result.is_err() {
|
||||
println!("{}✕ {}{}", color::Fg(color::Red), style::Bold, expression);
|
||||
return;
|
||||
return (format!("{}🞪 {}{}", color::Fg(color::Red), style::Bold, expression), None);
|
||||
}
|
||||
let result = i_result.ok().unwrap();
|
||||
if result.is_empty() {
|
||||
println!("{}✓ {}{}", color::Fg(color::Green), style::Bold, expression);
|
||||
return;
|
||||
return (format!("{}✓ {}{}", color::Fg(color::Green), style::Bold, expression), None);
|
||||
}
|
||||
let delimiter;
|
||||
match result {
|
||||
Value::Boolean(_bool) => delimiter = "is",
|
||||
Value::String(ref _str) => delimiter = "=>",
|
||||
Value::String(ref _str) => delimiter = "=>",
|
||||
_ => delimiter = "="
|
||||
}
|
||||
println!("{}{}{}{} {} {}{}", style::Faint, style::Italic, expression, style::Reset, delimiter, style::Bold, result);
|
||||
return (format!("{}{}{}{} {} {}{}", style::Faint, style::Italic, expression, style::Reset, delimiter, style::Bold, result), Some(result));
|
||||
}
|
||||
|
||||
fn reset() {
|
||||
print!("{}{}", style::Reset, color::Fg(color::Reset));
|
||||
stdout().flush().unwrap();
|
||||
}
|
||||
|
||||
fn help_text() {
|
||||
println!("{}quickmaths v{}{}", style::Bold, crate::VERSION, style::Reset);
|
||||
println!("Valerie Wolfe <sleeplessval@gmail.com>");
|
||||
println!("A mathematical expression evaluator written in Rust.\n");
|
||||
println!("USAGE:");
|
||||
println!("\tqm [EXPRESSION]...\n");
|
||||
}
|
||||
|
|
18
src/util.rs
18
src/util.rs
|
@ -1,18 +0,0 @@
|
|||
use evalexpr::{
|
||||
Value,
|
||||
|
||||
EvalexprError
|
||||
};
|
||||
|
||||
pub(crate) fn parse_radix(prefix: &str, base: u32, arg: &Value) -> Result<Value, EvalexprError> {
|
||||
let i_parse = arg.as_string()?;
|
||||
let parse = i_parse.strip_prefix(prefix).unwrap_or(i_parse.as_str());
|
||||
|
||||
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());
|
||||
}
|
Loading…
Reference in a new issue