From 3729348632c409065ac7bfce53ca970f50c7b081 Mon Sep 17 00:00:00 2001 From: fengcen Date: Thu, 17 Nov 2016 08:35:02 +0800 Subject: [PATCH] remove redundant code. --- Cargo.toml | 2 +- src/expression/mod.rs | 97 +++++++++++++++++++++---------------------- 2 files changed, 49 insertions(+), 50 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 721204f..d9eab50 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "eval" version = "0.1.1" description = "Expression evaluator" -keywords = ["expression", "evaluate", "evaluator", "expr"] +keywords = ["expression", "evaluate", "evaluator", "expr", "template"] authors = ["fengcen "] repository = "https://github.com/fengcen/eval.git" homepage = "https://github.com/fengcen/eval" diff --git a/src/expression/mod.rs b/src/expression/mod.rs index 922b8ef..188a70d 100644 --- a/src/expression/mod.rs +++ b/src/expression/mod.rs @@ -180,6 +180,50 @@ impl Expression { } } + pub fn parse_node(&mut self) -> Result<(), Error> { + let mut parsing_nodes = Vec::::new(); + + for operator in &self.operators { + match *operator { + Operator::Add(priority) | Operator::Sub(priority) | + Operator::Mul(priority) | Operator::Div(priority) | + Operator::Not(priority) | Operator::Eq(priority) | + Operator::Ne(priority) | Operator::Gt(priority) | + Operator::Lt(priority) | Operator::Ge(priority) | + Operator::And(priority) | Operator::Or(priority) | + Operator::Le(priority) | Operator::Rem(priority) => { + if ! parsing_nodes.is_empty() { + let prev = parsing_nodes.pop().unwrap(); + if prev.is_value_or_enough() { + if prev.operator.get_priority() < priority && ! prev.closed { + parsing_nodes.extend_from_slice(&rob_to(prev, operator.to_node())); + } else { + parsing_nodes.push(operator.children_to_node(vec![prev])); + } + } else if prev.operator.can_at_beginning() { + parsing_nodes.push(prev); + parsing_nodes.push(operator.to_node()); + } else { + return Err(Error::DuplicateOperatorNode); + } + } else if operator.can_at_beginning() { + parsing_nodes.push(operator.to_node()); + } else { + return Err(Error::StartWithNonValueOperator); + } + }, + Operator::Function(_) | Operator::LeftParenthesis | Operator::LeftSquareBracket => parsing_nodes.push(operator.to_node()), + Operator::Comma => close_comma(&mut parsing_nodes)?, + Operator::RightParenthesis | Operator::RightSquareBracket => close_bracket(&mut parsing_nodes, operator.get_left())?, + Operator::Value(_) | Operator::Identifier(_) => append_child_to_last_node(&mut parsing_nodes, operator)?, + _ => () + } + } + + self.node = Some(get_final_node(parsing_nodes)?); + Ok(()) + } + pub fn compile(&self) -> Box Result> { let node = self.node.clone().unwrap(); @@ -247,50 +291,6 @@ impl Expression { } }) } - - pub fn parse_node(&mut self) -> Result<(), Error> { - let mut parsing_nodes = Vec::::new(); - - for operator in &self.operators { - match *operator { - Operator::Add(priority) | Operator::Sub(priority) | - Operator::Mul(priority) | Operator::Div(priority) | - Operator::Not(priority) | Operator::Eq(priority) | - Operator::Ne(priority) | Operator::Gt(priority) | - Operator::Lt(priority) | Operator::Ge(priority) | - Operator::And(priority) | Operator::Or(priority) | - Operator::Le(priority) | Operator::Rem(priority) => { - if ! parsing_nodes.is_empty() { - let prev = parsing_nodes.pop().unwrap(); - if prev.is_value_or_enough() { - if prev.operator.get_priority() < priority && ! prev.closed { - parsing_nodes.extend_from_slice(&rob_to(prev, operator.to_node())); - } else { - parsing_nodes.push(operator.children_to_node(vec![prev])); - } - } else if prev.operator.can_at_beginning() { - parsing_nodes.push(prev); - parsing_nodes.push(operator.to_node()); - } else { - return Err(Error::DuplicateOperatorNode); - } - } else if operator.can_at_beginning() { - parsing_nodes.push(operator.to_node()); - } else { - return Err(Error::StartWithNonValueOperator); - } - }, - Operator::Function(_) | Operator::LeftParenthesis | Operator::LeftSquareBracket => parsing_nodes.push(operator.to_node()), - Operator::Comma => close_comma(&mut parsing_nodes)?, - Operator::RightParenthesis | Operator::RightSquareBracket => close_bracket(&mut parsing_nodes, operator.get_left())?, - Operator::Value(_) | Operator::Identifier(_) => append_child_to_last_node(&mut parsing_nodes, operator)?, - _ => () - } - } - - self.node = Some(get_final_node(parsing_nodes)?); - Ok(()) - } } fn append_child_to_last_node(parsing_nodes: &mut Vec, operator: &Operator) -> Result<(), Error> { @@ -320,11 +320,10 @@ fn get_final_node(mut parsing_nodes: Vec) -> Result { } while parsing_nodes.len() != 1 { - let mut last_node = parsing_nodes.pop().unwrap(); - let mut prev_node = parsing_nodes.pop().unwrap(); - prev_node.add_child(last_node); - last_node = prev_node; - parsing_nodes.push(last_node); + let last = parsing_nodes.pop().unwrap(); + let mut prev = parsing_nodes.pop().unwrap(); + prev.add_child(last); + parsing_nodes.push(prev); } Ok(parsing_nodes.pop().unwrap())