mirror of
https://github.com/nushell/nushell.git
synced 2024-11-23 00:43:33 +01:00
Fix operator precedence parser (#4947)
This commit is contained in:
parent
b007290a4e
commit
d122827a30
@ -67,6 +67,30 @@ fn precedence_of_operators2() {
|
|||||||
assert_eq!(actual.out, "6");
|
assert_eq!(actual.out, "6");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn precedence_of_operators3() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: "tests/fixtures/formats", pipeline(
|
||||||
|
r#"
|
||||||
|
5 - 5 * 10 + 5
|
||||||
|
"#
|
||||||
|
));
|
||||||
|
|
||||||
|
assert_eq!(actual.out, "-40");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn precedence_of_operators4() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: "tests/fixtures/formats", pipeline(
|
||||||
|
r#"
|
||||||
|
5 - (5 * 10) + 5
|
||||||
|
"#
|
||||||
|
));
|
||||||
|
|
||||||
|
assert_eq!(actual.out, "-40");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn division_of_ints() {
|
fn division_of_ints() {
|
||||||
let actual = nu!(
|
let actual = nu!(
|
||||||
|
@ -4019,7 +4019,7 @@ pub fn parse_math_expression(
|
|||||||
);
|
);
|
||||||
error = error.or(err);
|
error = error.or(err);
|
||||||
|
|
||||||
if op_prec <= last_prec && expr_stack.len() > 1 {
|
while op_prec <= last_prec && expr_stack.len() > 1 {
|
||||||
// Collapse the right associated operations first
|
// Collapse the right associated operations first
|
||||||
// so that we can get back to a stack with a lower precedence
|
// so that we can get back to a stack with a lower precedence
|
||||||
let mut rhs = expr_stack
|
let mut rhs = expr_stack
|
||||||
@ -4029,6 +4029,14 @@ pub fn parse_math_expression(
|
|||||||
.pop()
|
.pop()
|
||||||
.expect("internal error: expression stack empty");
|
.expect("internal error: expression stack empty");
|
||||||
|
|
||||||
|
last_prec = op.precedence();
|
||||||
|
|
||||||
|
if last_prec < op_prec {
|
||||||
|
expr_stack.push(op);
|
||||||
|
expr_stack.push(rhs);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
let mut lhs = expr_stack
|
let mut lhs = expr_stack
|
||||||
.pop()
|
.pop()
|
||||||
.expect("internal error: expression stack empty");
|
.expect("internal error: expression stack empty");
|
||||||
|
Loading…
Reference in New Issue
Block a user