diff options
author | NerdyPepper <[email protected]> | 2019-03-22 14:12:22 +0000 |
---|---|---|
committer | NerdyPepper <[email protected]> | 2019-03-22 14:12:22 +0000 |
commit | ec3c5da79e4a5331283f274382656d128081527b (patch) | |
tree | a960e9d1009b049a3a2121eb978ac064fd016113 /src | |
parent | a5238794efce5767139d9c4dbdf056bb859921d1 (diff) |
flatten lexer
Diffstat (limited to 'src')
-rw-r--r-- | src/lex/mod.rs | 19 |
1 files changed, 7 insertions, 12 deletions
diff --git a/src/lex/mod.rs b/src/lex/mod.rs index 47e35b0..e39fd23 100644 --- a/src/lex/mod.rs +++ b/src/lex/mod.rs | |||
@@ -1,4 +1,3 @@ | |||
1 | use std::f64; | ||
2 | #[derive(Debug, Copy, Clone, PartialEq)] | 1 | #[derive(Debug, Copy, Clone, PartialEq)] |
3 | pub struct Operator { | 2 | pub struct Operator { |
4 | token: char, | 3 | token: char, |
@@ -90,11 +89,7 @@ pub fn lexer(input: &str) -> Result<Vec<Token>, String> { | |||
90 | } | 89 | } |
91 | }, | 90 | }, |
92 | '/' | '*' | '^' => { | 91 | '/' | '*' | '^' => { |
93 | let parse_num = num_vec.parse::<f64>().ok(); | 92 | drain_num_stack(&mut num_vec, &mut result); |
94 | if let Some(x) = parse_num { | ||
95 | result.push(Token::Num(x)); | ||
96 | num_vec.clear(); | ||
97 | } | ||
98 | let operator_token: Token = match letter { | 93 | let operator_token: Token = match letter { |
99 | '/' => Operator::token_from_op('/', |x, y| x / y, 3, true), | 94 | '/' => Operator::token_from_op('/', |x, y| x / y, 3, true), |
100 | '*' => Operator::token_from_op('*', |x, y| x * y, 3, true), | 95 | '*' => Operator::token_from_op('*', |x, y| x * y, 3, true), |
@@ -141,11 +136,7 @@ pub fn lexer(input: &str) -> Result<Vec<Token>, String> { | |||
141 | result.push(Token::LParen); | 136 | result.push(Token::LParen); |
142 | }, | 137 | }, |
143 | ')' => { | 138 | ')' => { |
144 | let parse_num = num_vec.parse::<f64>().ok(); | 139 | drain_num_stack(&mut num_vec, &mut result); |
145 | if let Some(x) = parse_num { | ||
146 | result.push(Token::Num(x)); | ||
147 | num_vec.clear(); | ||
148 | } | ||
149 | result.push(Token::RParen); | 140 | result.push(Token::RParen); |
150 | } | 141 | } |
151 | ' ' => {} | 142 | ' ' => {} |
@@ -154,10 +145,14 @@ pub fn lexer(input: &str) -> Result<Vec<Token>, String> { | |||
154 | } | 145 | } |
155 | } | 146 | } |
156 | } | 147 | } |
148 | drain_num_stack(&mut num_vec, &mut result); | ||
149 | Ok(result) | ||
150 | } | ||
151 | |||
152 | fn drain_num_stack(num_vec: &mut String, result: &mut Vec<Token>) { | ||
157 | let parse_num = num_vec.parse::<f64>().ok(); | 153 | let parse_num = num_vec.parse::<f64>().ok(); |
158 | if let Some(x) = parse_num { | 154 | if let Some(x) = parse_num { |
159 | result.push(Token::Num(x)); | 155 | result.push(Token::Num(x)); |
160 | num_vec.clear(); | 156 | num_vec.clear(); |
161 | } | 157 | } |
162 | Ok(result) | ||
163 | } | 158 | } |