diff --git a/src/lexer.rs b/src/lexer.rs index 9e60666..eff3350 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -12,7 +12,6 @@ use crate::FunctionIdentifier::{Cbrt, Ceil, Cos, Exp, Abs, Floor, Ln, Log, Round use crate::units::Unit; use crate::units::Unit::*; use unicode_segmentation::{Graphemes, UnicodeSegmentation}; -use std::cmp::Ordering; fn is_word_char_str(input: &str) -> bool { matches!(input, "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" @@ -625,19 +624,15 @@ pub fn lex(input: &str, remove_trailing_operator: bool, default_degree: Unit) -> } let tokens = &mut lexer.tokens; // auto insert missing parentheses in first and last position - match lexer.left_paren_count.cmp(&lexer.right_paren_count) { - Ordering::Less => { - let missing_left_parens = lexer.right_paren_count - lexer.left_paren_count; - for _ in 0..missing_left_parens { - tokens.insert(0, Token::Operator(LeftParen)); - } + if lexer.left_paren_count > lexer.right_paren_count { + let missing_right_parens = lexer.left_paren_count - lexer.right_paren_count; + for _ in 0..missing_right_parens { + tokens.push(Token::Operator(RightParen)); } - Ordering::Equal => {} - Ordering::Greater => { - let missing_right_parens = lexer.left_paren_count - lexer.right_paren_count; - for _ in 0..missing_right_parens { - tokens.push(Token::Operator(RightParen)); - } + } else if lexer.left_paren_count < lexer.right_paren_count { + let missing_left_parens = lexer.right_paren_count - lexer.left_paren_count; + for _ in 0..missing_left_parens { + tokens.insert(0, Token::Operator(LeftParen)); } }