This commit is contained in:
fengcen 2018-08-24 09:09:55 +08:00
parent 446bf1efec
commit 0c4413836d
3 changed files with 18 additions and 4 deletions

View File

@ -594,6 +594,13 @@ mod tests {
assert_eq!(tree.parse_node(), Err(Error::CommaNotWithFunction));
}
#[test]
fn test_eval_issue_2() {
assert_eq!(eval("2 * (4 + 0) + 4"), Ok(to_value(12)));
assert_eq!(eval("2 * (2 + 2) + (1 + 3)"), Ok(to_value(12)));
assert_eq!(eval("2 * (4) + (4)"), Ok(to_value(12)));
}
}
#[cfg(all(feature = "unstable", test))]

View File

@ -14,7 +14,7 @@ pub struct Node {
impl Node {
pub fn new(operator: Operator) -> Node {
Node {
operator: operator,
operator,
children: Vec::new(),
closed: false,
}
@ -47,7 +47,7 @@ impl Node {
}
}
pub fn is_value_or_enough(&self) -> bool {
pub fn is_value_or_full_children(&self) -> bool {
if self.operator.is_value_or_ident() {
true
} else if self.operator.can_have_child() {
@ -57,6 +57,10 @@ impl Node {
}
}
pub fn is_unclosed_arithmetic(&self) -> bool {
return !self.closed && self.operator.can_have_child() && self.operator.can_have_child()
}
pub fn is_unclosed_function(&self) -> bool {
match self.operator {
Operator::Function(_) => !self.closed,

View File

@ -205,7 +205,7 @@ impl Tree {
Operator::Rem(priority) => {
if !parsing_nodes.is_empty() {
let prev = parsing_nodes.pop().unwrap();
if prev.is_value_or_enough() {
if prev.is_value_or_full_children() {
if prev.operator.get_priority() < priority && !prev.closed {
parsing_nodes.extend_from_slice(&rob_to(prev, operator.to_node()));
} else {
@ -468,7 +468,7 @@ fn append_value_to_last_node(parsing_nodes: &mut Vec<Node>,
} else if prev.is_left_square_bracket() {
parsing_nodes.push(prev);
parsing_nodes.push(node);
} else if prev.is_value_or_enough() {
} else if prev.is_value_or_full_children() {
return Err(Error::DuplicateValueNode);
} else if prev.is_enough() {
parsing_nodes.push(prev);
@ -535,6 +535,9 @@ fn close_bracket(parsing_nodes: &mut Vec<Node>, bracket: Operator) -> Result<(),
penult.closed = true;
penult.add_child(current);
parsing_nodes.push(penult);
} else if penult.is_unclosed_arithmetic() {
penult.add_child(current);
parsing_nodes.push(penult);
} else {
parsing_nodes.push(penult);
parsing_nodes.push(current);