From 982f067d0e433ef686bad48b85075d68a6da4bd2 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Thu, 11 Jun 2020 10:17:08 -0700 Subject: [PATCH] Proper precedence history in math (#1966) --- crates/nu-cli/tests/commands/math.rs | 12 ++++++++++++ crates/nu-parser/src/parse.rs | 7 +++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/crates/nu-cli/tests/commands/math.rs b/crates/nu-cli/tests/commands/math.rs index 9822b48f9e..680b1b641b 100644 --- a/crates/nu-cli/tests/commands/math.rs +++ b/crates/nu-cli/tests/commands/math.rs @@ -84,6 +84,18 @@ fn division_of_ints2() { assert_eq!(actual.out, "0.25"); } +#[test] +fn proper_precedence_history() { + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + = 2 / 2 / 2 + 1 + "# + )); + + assert_eq!(actual.out, "1.5"); +} + #[test] fn parens_precedence() { let actual = nu!( diff --git a/crates/nu-parser/src/parse.rs b/crates/nu-parser/src/parse.rs index 3b3ddcd79f..39024f983e 100644 --- a/crates/nu-parser/src/parse.rs +++ b/crates/nu-parser/src/parse.rs @@ -861,10 +861,8 @@ fn parse_math_expression( shorthand_mode: bool, ) -> (usize, SpannedExpression, Option) { // Precedence parsing is included - // Some notes: - // * short_hand mode means that the left-hand side of an expression can point to a column-path. To make this possible, - // we parse as normal, but then go back and when we detect a left-hand side, reparse that value if it's a string - // * parens are handled earlier, so they're not handled explicitly here + // Short_hand mode means that the left-hand side of an expression can point to a column-path. To make this possible, + // we parse as normal, but then go back and when we detect a left-hand side, reparse that value if it's a string let mut idx = 0; let mut error = None; @@ -948,6 +946,7 @@ fn parse_math_expression( } working_exprs.push((None, op)); working_exprs.push(rhs_working_expr); + prec.push(next_prec); } idx += 1;