diff options
author | NerdyPepper <[email protected]> | 2019-03-19 17:42:59 +0000 |
---|---|---|
committer | NerdyPepper <[email protected]> | 2019-03-19 17:42:59 +0000 |
commit | 5385cffe564eb3ddec78eecf6ffff30bdfc49bf4 (patch) | |
tree | fb8ced81314d05a293014ae7a5ce192de4e32df5 | |
parent | e4a077e175eee58516c7335c97f4985d26138e61 (diff) |
basic REPL functionality
-rw-r--r-- | src/main.rs | 24 |
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 @@ | |||
1 | use std::io::{ stdin, stdout }; | ||
2 | |||
1 | #[derive(Debug, Copy, Clone, PartialEq)] | 3 | #[derive(Debug, Copy, Clone, PartialEq)] |
2 | pub struct Operator { | 4 | pub struct Operator { |
3 | token: char, | 5 | token: char, |
@@ -31,14 +33,22 @@ impl Operator { | |||
31 | } | 33 | } |
32 | 34 | ||
33 | fn main() { | 35 | fn 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 | ||
44 | fn lexer(input: &str) -> Result<Vec<Token>, String> { | 54 | fn lexer(input: &str) -> Result<Vec<Token>, String> { |