diff --git a/members/builtins/src/calc.rs b/members/builtins/src/calc.rs index 62edabd8df9dc3492400b0af8bceb89b83b201c8..074e23bc281d4ef86b0fda91043c65f75d680727 100644 --- a/members/builtins/src/calc.rs +++ b/members/builtins/src/calc.rs @@ -1,6 +1,47 @@ use calculate::{eval, eval_polish, CalcError, Value}; use std::io::{self, Write}; +const REPL_GUIDE: &'static str = r#"ion-calc +Type in expressions to have them evaluated. +Type "help" for help."#; + +pub const MAN_CALC: &str = r#"NAME + calc - Floating point calculator + +SYNOPSIS + calc [EXPRESSION] + +DESCRIPTION + Evaluates arithmetic expressions + +SPECIAL EXPRESSIONS + help (only in interactive mode) + prints this help text + + --help (only in non-interactive mode) + prints this help text + + exit (only in interactive mode) + exits the program + +NOTATIONS + infix notation + e.g. 3 * 4 + 5 + + polish notation + e.g. + * 3 4 5 + +EXAMPLES + Add two plus two in infix notation + calc 2+2 + + Add two plus two in polish notation + calc + 2 2 + +AUTHOR + Written by Hunter Goldstein. +"#; + fn calc_or_polish_calc(args: &str) -> Result<Value, CalcError> { match eval(&args) { Ok(t) => Ok(t), @@ -11,35 +52,47 @@ fn calc_or_polish_calc(args: &str) -> Result<Value, CalcError> { pub fn calc(args: &[String]) -> Result<(), String> { let stdout = io::stdout(); let mut stdout = stdout.lock(); - if !args.is_empty() { - let result = calc_or_polish_calc(&args.join(" ")); - let _ = match result { - Ok(v) => writeln!(stdout, "{}", v), - Err(e) => writeln!(stdout, "{}", e), - }; - } else { - let prompt = b"ion-calc: "; - loop { - let _ = stdout.write(prompt); - let _ = stdout.flush(); - let mut input = String::new(); - let _ = io::stdin().read_line(&mut input); - if input.is_empty() { - break; - } else { - match input.trim() { - "" => (), - "exit" => break, - s => { - let result = calc_or_polish_calc(s); - let _ = match result { - Ok(v) => writeln!(stdout, "{}", v), - Err(e) => writeln!(stdout, "{}", e), - }; + match args.first() { + Some(ref s) if (*s == "--help") => { + // "--help" only makes sense if it is the first option. Only look for it + // in the first position. + println!("{}", MAN_CALC); + Ok(()) + } + Some(_) => { + let result = calc_or_polish_calc(&args.join(" ")); + let _ = match result { + Ok(v) => writeln!(stdout, "{}", v), + Err(e) => writeln!(stdout, "{}", e), + }; + Ok(()) + } + None => { + let prompt = b"ion-calc: "; + println!("{}", REPL_GUIDE); + loop { + let _ = stdout.write(prompt); + let _ = stdout.flush(); + let mut input = String::new(); + let _ = io::stdin().read_line(&mut input); + if input.is_empty() { + break; + } else { + match input.trim() { + "" => (), + "exit" => break, + "help" => println!("{}", MAN_CALC), + s => { + let result = calc_or_polish_calc(s); + let _ = match result { + Ok(v) => writeln!(stdout, "{}", v), + Err(e) => writeln!(stdout, "{}", e), + }; + } } } } + Ok(()) } } - Ok(()) }