made average helper function and made an initial implementation of a hex parser/formatter

This commit is contained in:
Valerie Wolfe 2022-06-13 00:18:32 -04:00
parent fbd05b9775
commit e241b824cc
2 changed files with 76 additions and 2 deletions

65
src/helper.rs Normal file
View file

@ -0,0 +1,65 @@
use evalexpr::{
Value,
EvalexprError
};
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());
}
}
pub fn hex(arg: &Value) -> Option<Value> {
if !arg.is_string() {
if arg.is_int() {
let num = arg.as_int().ok().unwrap();
let fmt = format!("0x{:X}", num);
return Some(fmt.into());
}
return None;
}
let i_parse = arg.as_string().ok().unwrap();
let parse = i_parse.strip_prefix("0x").unwrap_or(i_parse.as_str());
let i_result = i64::from_str_radix(parse, 16);
if i_result.is_err() { return None; }
let result: Option<i64> = i_result.ok();
if result.is_none() { return None; }
let output: Value = result.unwrap().into();
return Some(output);
}

View file

@ -9,6 +9,8 @@ use std::{
}; };
use evalexpr::{ use evalexpr::{
context_map,
eval_with_context_mut, eval_with_context_mut,
HashMapContext, HashMapContext,
@ -19,11 +21,18 @@ use termion::{
style style
}; };
mod helper;
pub const VERSION: &str = "0.1.4";
fn main() { fn main() {
let mut context = HashMapContext::new(); let mut context = context_map! {
"avg" => Function::new(|arg| helper::average(arg)),
"hex" => Function::new(|arg| Ok(helper::hex(arg).unwrap_or(0.into())))
}.unwrap();
let expressions: Vec<String> = env::args().skip(1).collect(); let expressions: Vec<String> = env::args().skip(1).collect();
if expressions.len() == 0 { if expressions.len() == 0 {
println!("{}quickmaths v0.1.3{}\n{}Interactive Mode{}", style::Bold, style::Reset, style::Faint, style::Reset); println!("{}quickmaths v{}{}\n{}Interactive Mode{}", style::Bold, VERSION, style::Reset, style::Faint, style::Reset);
loop { loop {
print!("> "); print!("> ");
stdout().flush().unwrap(); stdout().flush().unwrap();