From ec3c5da79e4a5331283f274382656d128081527b Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Fri, 22 Mar 2019 19:42:22 +0530 Subject: flatten lexer --- src/lex/mod.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) (limited to 'src/lex') 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 @@ -use std::f64; #[derive(Debug, Copy, Clone, PartialEq)] pub struct Operator { token: char, @@ -90,11 +89,7 @@ pub fn lexer(input: &str) -> Result, String> { } }, '/' | '*' | '^' => { - let parse_num = num_vec.parse::().ok(); - if let Some(x) = parse_num { - result.push(Token::Num(x)); - num_vec.clear(); - } + drain_num_stack(&mut num_vec, &mut result); let operator_token: Token = match letter { '/' => Operator::token_from_op('/', |x, y| x / y, 3, true), '*' => Operator::token_from_op('*', |x, y| x * y, 3, true), @@ -141,11 +136,7 @@ pub fn lexer(input: &str) -> Result, String> { result.push(Token::LParen); }, ')' => { - let parse_num = num_vec.parse::().ok(); - if let Some(x) = parse_num { - result.push(Token::Num(x)); - num_vec.clear(); - } + drain_num_stack(&mut num_vec, &mut result); result.push(Token::RParen); } ' ' => {} @@ -154,10 +145,14 @@ pub fn lexer(input: &str) -> Result, String> { } } } + drain_num_stack(&mut num_vec, &mut result); + Ok(result) +} + +fn drain_num_stack(num_vec: &mut String, result: &mut Vec) { let parse_num = num_vec.parse::().ok(); if let Some(x) = parse_num { result.push(Token::Num(x)); num_vec.clear(); } - Ok(result) } -- cgit v1.2.3