From 00706416e7872c6e4925770af900d9a24466d5fb Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Sat, 23 Mar 2019 10:44:11 +0530 Subject: basic readline is ready --- src/main.rs | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 0cd192d..50e2af8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ -use std::io::{ stdin }; use std::f64; mod lex; @@ -7,17 +6,37 @@ use crate::lex::*; mod parse; use crate::parse::*; +use rustyline::error::ReadlineError; +use rustyline::Editor; + fn main() { +let mut rl = Editor::<()>::new(); + if rl.load_history("history.txt").is_err() { + println!("No previous history."); + } loop { - let mut input = String::new(); - stdin().read_line(&mut input).unwrap(); - let input = input.trim(); - let input = input.replace(" ", ""); - if input == "exit" { - return + let readline = rl.readline("> "); + match readline { + Ok(line) => { + rl.add_history_entry(line.as_ref()); + let evaled = eval_math_expression(&line[..]).unwrap(); + println!("{}", evaled); + }, + Err(ReadlineError::Interrupted) => { + println!("CTRL-C"); + break + }, + Err(ReadlineError::Eof) => { + println!("CTRL-D"); + break + }, + Err(err) => { + println!("Error: {:?}", err); + break + } } - println!("ans: {}\n", eval_math_expression(&input[..]).unwrap()); } + rl.save_history("history.txt").unwrap(); } fn autobalance_parens(input: &str) -> Result { -- cgit v1.2.3