From 5385cffe564eb3ddec78eecf6ffff30bdfc49bf4 Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Tue, 19 Mar 2019 23:12:59 +0530 Subject: basic REPL functionality --- src/main.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 78a05b0..aaf285d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use std::io::{ stdin, stdout }; + #[derive(Debug, Copy, Clone, PartialEq)] pub struct Operator { token: char, @@ -31,14 +33,22 @@ impl Operator { } fn main() { - let input = "1 + 5 * 6 + 2"; - println!("{}", input); - let input = input.replace(" ", ""); - let lexed = lexer(&input); - let postfixed = to_postfix(lexed.unwrap()); - let evaled = eval_postfix(postfixed.unwrap()); + loop { + let mut input = String::new(); + stdin().read_line(&mut input).unwrap(); + + let input = input.trim(); + let input = input.replace(" ", ""); - println!("{:?}", evaled); + if input == "exit" { + return + } + + let lexed = lexer(&input[..]); + let postfixed = to_postfix(lexed.unwrap()); + let evaled = eval_postfix(postfixed.unwrap()); + println!("{}", evaled.unwrap()); + } } fn lexer(input: &str) -> Result, String> { -- cgit v1.2.3