From 27a825b2a23af273b697591ba207ef2594e8a801 Mon Sep 17 00:00:00 2001 From: Ivan Bushchik Date: Wed, 25 Jan 2023 22:35:37 +0300 Subject: [PATCH] 0.2.0 --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 43 +++++++++++++++++++++++++++++++------------ 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c8f9d8f..1a28beb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,7 +10,7 @@ checksum = "aacfb566035f8cd02f6ec9247c242f3f9904a0b288ea383abcf4e95df6436a34" [[package]] name = "feval" -version = "0.1.2" +version = "0.2.0" dependencies = [ "evalexpr", ] diff --git a/Cargo.toml b/Cargo.toml index 83b7579..8372274 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "feval" -version = "0.1.2" +version = "0.2.0" edition = "2021" description = "clt for fast evaluations" readme = "README.md" diff --git a/src/main.rs b/src/main.rs index b6ee903..9aefb5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,25 +1,44 @@ use evalexpr; -use std::env::args; +use std::{env::args, io::{stdin, stdout, Write}}; mod tasks; use crate::tasks::tasks::all; fn help() { println!(r#"feval: evaluate expressions -usage: provide exactly one argument (with expression) +usage: provide exactly one argument (with expression) or no arguments for shell mode. example: feval "math::sin(30 * math::pi / 180)""#); } +fn main_loop() { + print!(">>> "); + stdout().flush().unwrap(); + let mut input = String::new(); + while stdin().read_line(&mut input).unwrap() != 0 { + println!("{}", evalexpr::eval(&input.trim()).unwrap()); + input.clear(); + print!(">>> "); + stdout().flush().unwrap(); + } +} + fn main() { let args: Vec = args().collect(); - if args.len() > 2 { - println!("Too many args."); - help(); - return - } else if args.len() < 2 { - println!("Too few args."); - help(); - return + match args.len() { + 2 => { + let expr = all(args[1].clone()); + println!("{}", evalexpr::eval(&expr).unwrap()) + }, + 1 => {main_loop()}, + _ => { + if args.len() > 2 { + println!("Too many args."); + help(); + return + } else if args.len() < 1 { + println!("Too few args."); + help(); + return + } + } } - let expr = all(args[1].clone()); - println!("{}", evalexpr::eval(&expr).unwrap()) }