From: Harishankar Date: Fri, 29 May 2020 09:26:25 +0000 (+0530) Subject: Fixed the regular expression - also made the command line robust X-Git-Tag: 0.1-a~8 X-Git-Url: https://harishankar.org/repos/?p=evpf.git;a=commitdiff_plain;h=8c424fbdf4baf3e27f63b2526def89ddf4ac1b83 Fixed the regular expression - also made the command line robust Made the command line more robust and fixed the regular expression to make literal dot using \. instead of . (inadvertent error) --- diff --git a/src/main.rs b/src/main.rs index e25413b..c911ca2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use std::io; +use std::io::Write; use regex::Regex; enum Operator { @@ -117,26 +118,39 @@ fn main() { // get a line from input and evaluate it let mut expr = String::new (); // regular expression to match a number - let match_num = Regex::new (r"^\d+?.*?\d*?$").unwrap (); + let match_num = Regex::new (r"^\d+?\.*?\d*?$").unwrap (); // loop until a blank line is received loop { expr.clear (); - println!("Enter an expression (postfix) (blank to quit): "); + print!("evpf>"); + io::stdout().flush ().expect ("Error flushing!"); // read a line of text io::stdin().read_line (&mut expr).expect ("Error reading line!"); // trim the text let expr = expr.trim (); - // quit if the expression is blank - if expr == "" { + + if expr == "q" || expr == "Q" { + // quit if the expression is q or Qs break; + } else if expr == "?" || expr == "h" || expr == "H" { + // display help text + println! ("Type an expression in postfix style to evaluate."); + println! ("Example: 4 5 + 12 -"); + println! ("Supported operators: +, -, *, /"); + println! ("Type q, Q to quit"); + continue; + } else if expr == "" { + // continue without proceeding + continue; } let res = evaluate (&expr, &match_num); if res.is_ok () { - println! ("Result is : {}", res.unwrap()); + println! ("Result: {}", res.unwrap()); } else { eprintln! ("Error: {}", res.unwrap_err()); + eprintln! ("Type ? or h or H for help"); } } }