aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorNerdyPepper <[email protected]>2019-03-19 17:42:59 +0000
committerNerdyPepper <[email protected]>2019-03-19 17:42:59 +0000
commit5385cffe564eb3ddec78eecf6ffff30bdfc49bf4 (patch)
treefb8ced81314d05a293014ae7a5ce192de4e32df5 /src/main.rs
parente4a077e175eee58516c7335c97f4985d26138e61 (diff)
basic REPL functionality
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs24
1 files changed, 17 insertions, 7 deletions
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 @@
1use std::io::{ stdin, stdout };
2
1#[derive(Debug, Copy, Clone, PartialEq)] 3#[derive(Debug, Copy, Clone, PartialEq)]
2pub struct Operator { 4pub struct Operator {
3 token: char, 5 token: char,
@@ -31,14 +33,22 @@ impl Operator {
31} 33}
32 34
33fn main() { 35fn main() {
34 let input = "1 + 5 * 6 + 2"; 36 loop {
35 println!("{}", input); 37 let mut input = String::new();
36 let input = input.replace(" ", ""); 38 stdin().read_line(&mut input).unwrap();
37 let lexed = lexer(&input); 39
38 let postfixed = to_postfix(lexed.unwrap()); 40 let input = input.trim();
39 let evaled = eval_postfix(postfixed.unwrap()); 41 let input = input.replace(" ", "");
40 42
41 println!("{:?}", evaled); 43 if input == "exit" {
44 return
45 }
46
47 let lexed = lexer(&input[..]);
48 let postfixed = to_postfix(lexed.unwrap());
49 let evaled = eval_postfix(postfixed.unwrap());
50 println!("{}", evaled.unwrap());
51 }
42} 52}
43 53
44fn lexer(input: &str) -> Result<Vec<Token>, String> { 54fn lexer(input: &str) -> Result<Vec<Token>, String> {