From 82aa17e7398f6d831bf502a5548f655dfd1c205e Mon Sep 17 00:00:00 2001 From: Ryuichi Ueda Date: Sun, 15 Sep 2024 11:02:24 +0900 Subject: [PATCH] Simplify --- src/elements/arithmetic_expr.rs | 5 +++-- src/elements/arithmetic_expr/calculator.rs | 8 +------- src/elements/arithmetic_expr/parser.rs | 21 --------------------- 3 files changed, 4 insertions(+), 30 deletions(-) diff --git a/src/elements/arithmetic_expr.rs b/src/elements/arithmetic_expr.rs index a9a3271e..ca34327a 100644 --- a/src/elements/arithmetic_expr.rs +++ b/src/elements/arithmetic_expr.rs @@ -19,7 +19,6 @@ use super::word::Word; pub struct ArithmeticExpr { pub text: String, elements: Vec, - paren_stack: Vec, output_base: String, hide_base: bool, } @@ -48,6 +47,9 @@ impl ArithmeticExpr { } pub fn eval_elems(&mut self, core: &mut ShellCore) -> Result { + if self.elements.len() == 0 { + return Err("operand expexted (error token: \")\")".to_string()); + } let es = match self.decompose_increments() { Ok(data) => data, Err(err_msg) => return Err(err_msg), @@ -177,7 +179,6 @@ impl ArithmeticExpr { ArithmeticExpr { text: String::new(), elements: vec![], - paren_stack: vec![], output_base: "10".to_string(), hide_base: false, } diff --git a/src/elements/arithmetic_expr/calculator.rs b/src/elements/arithmetic_expr/calculator.rs index d8d415b8..70f13db6 100644 --- a/src/elements/arithmetic_expr/calculator.rs +++ b/src/elements/arithmetic_expr/calculator.rs @@ -8,13 +8,7 @@ use super::{elem, float, int, rev_polish, trenary, word}; pub fn pop_operand(stack: &mut Vec, core: &mut ShellCore) -> Result { match stack.pop() { Some(Elem::Word(w, inc)) => word::to_operand(&w, 0, inc, core), - Some(Elem::InParen(mut a)) => { - if a.elements.len() == 0 { - return Err("operand expected".to_string()); - } - - a.eval_elems(core) - }, + Some(Elem::InParen(mut a)) => a.eval_elems(core), Some(elem) => Ok(elem), None => Err("no operand".to_string()), } diff --git a/src/elements/arithmetic_expr/parser.rs b/src/elements/arithmetic_expr/parser.rs index 90373424..c3af1b54 100644 --- a/src/elements/arithmetic_expr/parser.rs +++ b/src/elements/arithmetic_expr/parser.rs @@ -129,7 +129,6 @@ impl ArithmeticExpr { return false; } - ans.paren_stack.push( '(' ); ans.text += &feeder.consume(1); let arith = Self::parse(feeder, core); @@ -138,30 +137,10 @@ impl ArithmeticExpr { } ans.text += &arith.as_ref().unwrap().text; - ans.paren_stack.pop(); ans.elements.push( Elem::InParen(arith.unwrap()) ); ans.text += &feeder.consume(1); return true; - - /* - if feeder.starts_with("(") { - ans.paren_stack.push( '(' ); - ans.elements.push( Elem::LeftParen ); - ans.text += &feeder.consume(1); - return true; - } - - if feeder.starts_with(")") { - if let Some('(') = ans.paren_stack.last() { - ans.paren_stack.pop(); - ans.elements.push( Elem::RightParen ); - ans.text += &feeder.consume(1); - return true; - } - } - -*/ } fn eat_binary_operator(feeder: &mut Feeder, ans: &mut Self, core: &mut ShellCore) -> bool {