improved code readability
This commit is contained in:
parent
8abb3c778e
commit
351d845992
1 changed files with 40 additions and 16 deletions
56
src/main.rs
56
src/main.rs
|
@ -28,6 +28,7 @@ mod util;
|
||||||
pub const VERSION: &str = "0.2.0";
|
pub const VERSION: &str = "0.2.0";
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
// build eval context
|
||||||
let mut context = context_map! {
|
let mut context = context_map! {
|
||||||
// globals
|
// globals
|
||||||
"e" => global::EULER,
|
"e" => global::EULER,
|
||||||
|
@ -53,8 +54,18 @@ fn main() {
|
||||||
"π" => global::PI,
|
"π" => global::PI,
|
||||||
"√" => Function::new(|arg| helper::square_root(arg))
|
"√" => Function::new(|arg| helper::square_root(arg))
|
||||||
}.unwrap();
|
}.unwrap();
|
||||||
|
|
||||||
|
// collect args and evaluate if present
|
||||||
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 {
|
||||||
|
for expression in expressions {
|
||||||
|
match expression.as_str() {
|
||||||
|
"help" => help_text(),
|
||||||
|
_ => 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);
|
println!("{}quickmaths v{}{}\n{}Interactive Mode{}", style::Bold, VERSION, style::Reset, style::Faint, style::Reset);
|
||||||
loop {
|
loop {
|
||||||
print!("> ");
|
print!("> ");
|
||||||
|
@ -72,13 +83,6 @@ fn main() {
|
||||||
}
|
}
|
||||||
reset();
|
reset();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
for expression in expressions {
|
|
||||||
match expression.as_str() {
|
|
||||||
"help" => help_text(),
|
|
||||||
_ => do_eval(expression, &mut context)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,12 +90,20 @@ fn do_eval(i_expression: String, context: &mut HashMapContext) {
|
||||||
let expression = i_expression.as_str();
|
let expression = i_expression.as_str();
|
||||||
let i_result = eval_with_context_mut(expression, context);
|
let i_result = eval_with_context_mut(expression, context);
|
||||||
if i_result.is_err() {
|
if i_result.is_err() {
|
||||||
println!("{}✕ {}{}", color::Fg(color::Red), style::Bold, expression);
|
println!(
|
||||||
|
"{red}✕ {bold}{expression}",
|
||||||
|
bold = style::Bold,
|
||||||
|
red = color::Fg(color::Red)
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let result = i_result.ok().unwrap();
|
let result = i_result.ok().unwrap();
|
||||||
if result.is_empty() {
|
if result.is_empty() {
|
||||||
println!("{}✓ {}{}", color::Fg(color::Green), style::Bold, expression);
|
println!(
|
||||||
|
"{green}✓ {bold}{expression}",
|
||||||
|
bold = style::Bold,
|
||||||
|
green = color::Fg(color::Green)
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let delimiter;
|
let delimiter;
|
||||||
|
@ -100,7 +112,13 @@ fn do_eval(i_expression: String, context: &mut HashMapContext) {
|
||||||
Value::String(ref _str) => delimiter = "=>",
|
Value::String(ref _str) => delimiter = "=>",
|
||||||
_ => delimiter = "="
|
_ => delimiter = "="
|
||||||
}
|
}
|
||||||
println!("{}{}{}{} {} {}{}", style::Faint, style::Italic, expression, style::Reset, delimiter, style::Bold, result);
|
println!(
|
||||||
|
"{faint}{italic}{expression}{reset} {delimiter} {bold}{result}",
|
||||||
|
bold = style::Bold,
|
||||||
|
faint = style::Faint,
|
||||||
|
italic = style::Italic,
|
||||||
|
reset = style::Reset
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reset() {
|
fn reset() {
|
||||||
|
@ -109,9 +127,15 @@ fn reset() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn help_text() {
|
fn help_text() {
|
||||||
println!("{}quickmaths v{}{}", style::Bold, crate::VERSION, style::Reset);
|
println!(
|
||||||
println!("Valerie Wolfe <sleeplessval@gmail.com>");
|
"{bold}quickmaths v{version}{reset}
|
||||||
println!("A mathematical expression evaluator written in Rust.\n");
|
Valerie Wolfe <sleeplessval@gmail.com>
|
||||||
println!("USAGE:");
|
A mathematical expression evaluator written in Rust.
|
||||||
println!("\tqm [EXPRESSION]...\n");
|
|
||||||
|
usage:
|
||||||
|
qm [EXPRESSION]...",
|
||||||
|
bold = style::Bold,
|
||||||
|
reset = style::Reset,
|
||||||
|
version = crate::VERSION
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue