diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 70 |
1 files changed, 66 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index c91b662..d9f6b01 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -6,7 +6,7 @@ pub struct Operator { | |||
6 | is_left_associative: bool, | 6 | is_left_associative: bool, |
7 | } | 7 | } |
8 | 8 | ||
9 | #[derive(Debug)] | 9 | #[derive(Debug, Copy, Clone)] |
10 | pub enum Token { | 10 | pub enum Token { |
11 | Operator(Operator), | 11 | Operator(Operator), |
12 | Num(f64), | 12 | Num(f64), |
@@ -31,11 +31,11 @@ impl Operator { | |||
31 | } | 31 | } |
32 | 32 | ||
33 | fn main() { | 33 | fn main() { |
34 | let input = "1(2)"; | 34 | let input = "1 + 2 * 3"; |
35 | let input = input.replace(" ", ""); | 35 | let input = input.replace(" ", ""); |
36 | let lexed = lexer(&input); | 36 | let lexed = lexer(&input); |
37 | println!("{:?}", lexed); | 37 | let postfixed = to_postfix(lexed.unwrap()); |
38 | println!("{}", lexed.unwrap().len()); | 38 | println!("{:?}", postfixed); |
39 | } | 39 | } |
40 | 40 | ||
41 | fn lexer(input: &str) -> Result<Vec<Token>, String> { | 41 | fn lexer(input: &str) -> Result<Vec<Token>, String> { |
@@ -93,3 +93,65 @@ fn lexer(input: &str) -> Result<Vec<Token>, String> { | |||
93 | } | 93 | } |
94 | Ok(result) | 94 | Ok(result) |
95 | } | 95 | } |
96 | |||
97 | fn to_postfix(tokens: Vec<Token>) -> Result<Vec<Token>, String> { | ||
98 | let mut postfixed: Vec<Token> = vec![]; | ||
99 | let mut op_stack: Vec<Token> = vec![]; | ||
100 | for token in tokens { | ||
101 | match token { | ||
102 | Token::Num(_) => { | ||
103 | postfixed.push(token); | ||
104 | println!("pushed a number {:?}", token); | ||
105 | }, | ||
106 | Token::Operator(current_op) => { | ||
107 | while let Some(top_op) = op_stack.last() { | ||
108 | match top_op { | ||
109 | Token::LParen => { | ||
110 | return Err(format!("Mismatched Parentheses!")) | ||
111 | } | ||
112 | Token::Operator(top_op) => { | ||
113 | let tp = top_op.precedence; | ||
114 | let cp = current_op.precedence; | ||
115 | if tp > cp || (tp == cp && top_op.is_left_associative) { | ||
116 | postfixed.push(op_stack.pop().unwrap()); | ||
117 | println!("pushed an operator special {:?}", token); | ||
118 | } else { | ||
119 | break; | ||
120 | } | ||
121 | } | ||
122 | _ => { | ||
123 | return Err(format!("Unexpected match branch part 2")) | ||
124 | } | ||
125 | } | ||
126 | } | ||
127 | op_stack.push(token); | ||
128 | println!("pushed an operator {:?}", token); | ||
129 | }, | ||
130 | Token::LParen => { | ||
131 | op_stack.push(token); | ||
132 | }, | ||
133 | Token::RParen => { | ||
134 | let mut found: bool = false; | ||
135 | while let Some(top_op) = op_stack.last() { | ||
136 | match top_op { | ||
137 | Token::LParen => { | ||
138 | let _ = op_stack.pop().unwrap(); | ||
139 | found = true; | ||
140 | }, | ||
141 | _ => { | ||
142 | postfixed.push(op_stack.pop().unwrap()); | ||
143 | } | ||
144 | } | ||
145 | } | ||
146 | if found == false { | ||
147 | return Err(format!("Mismatched parentheses part 2")) | ||
148 | } | ||
149 | } | ||
150 | |||
151 | } | ||
152 | } | ||
153 | while let Some(op) = op_stack.pop() { | ||
154 | postfixed.push(op); | ||
155 | } | ||
156 | Ok(postfixed) | ||
157 | } | ||