basic/
basic.rs

1//! This example shows a simple command line calculator written with this library, with some basic
2//! error handling.
3
4extern crate mexprp;
5
6use std::io::{self, Write};
7
8use mexprp::Expression;
9
10fn main() {
11	println!("MEXPRP Test Calculator\n---------------------");
12	loop {
13		let mut buf = String::new();
14		print!("> ");
15		io::stdout().flush().unwrap();
16		io::stdin().read_line(&mut buf).unwrap();
17
18		// Parse the expression (with the default context)
19		let expr: Expression<f64> = match Expression::parse(&buf) {
20			Ok(expr) => expr,
21			Err(e) => {
22				println!("Failed to parse expression: {}", e);
23				continue;
24			}
25		};
26
27		// Evaluate the expression or print the error if there was one
28		match expr.eval() {
29			Ok(val) => println!("\t= {}", val),
30			Err(e) => println!("Failed to evaluate the expression: {}", e),
31		}
32	}
33}