diff --git a/CHANGELOG.md b/CHANGELOG.md index 0938d313..51a6ac63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,19 @@ # Changelog +## v1.0.8-alpha - unreleased + +### Added + +N/A + +### Fixed + +- Fixed operator precedences, in particular the pipe operator (`|>`) which is now of the lowest precedence. + +### Removed + +N/A + ## v1.0.7-alpha - 2023-06-02 ### Added diff --git a/crates/aiken-lang/src/ast.rs b/crates/aiken-lang/src/ast.rs index 6c9f0d49..5f01af29 100644 --- a/crates/aiken-lang/src/ast.rs +++ b/crates/aiken-lang/src/ast.rs @@ -771,15 +771,15 @@ impl BinOp { pub fn precedence(&self) -> u8 { // Ensure that this matches the other precedence function for guards match self { - Self::Or => 1, + // Pipe is 0 + // Unary operators are 1 + Self::Or => 2, - Self::And => 2, + Self::And => 3, - Self::Eq | Self::NotEq => 3, + Self::Eq | Self::NotEq | Self::LtInt | Self::LtEqInt | Self::GtEqInt | Self::GtInt => 4, - Self::LtInt | Self::LtEqInt | Self::GtEqInt | Self::GtInt => 4, - - // Pipe is 5 + // Concatenation operators are typically 5, so we skip it. Self::AddInt | Self::SubInt => 6, Self::MultInt | Self::DivInt | Self::ModInt => 7, diff --git a/crates/aiken-lang/src/expr.rs b/crates/aiken-lang/src/expr.rs index c88a86f6..a82a67f9 100644 --- a/crates/aiken-lang/src/expr.rs +++ b/crates/aiken-lang/src/expr.rs @@ -651,7 +651,7 @@ impl UntypedExpr { pub fn binop_precedence(&self) -> u8 { match self { Self::BinOp { name, .. } => name.precedence(), - Self::PipeLine { .. } => 5, + Self::PipeLine { .. } => 0, _ => std::u8::MAX, } } diff --git a/crates/aiken-lang/src/tests/format.rs b/crates/aiken-lang/src/tests/format.rs index 7c488d1d..a698593a 100644 --- a/crates/aiken-lang/src/tests/format.rs +++ b/crates/aiken-lang/src/tests/format.rs @@ -799,3 +799,20 @@ fn test_fail() { assert_fmt(src, src); } + +#[test] +fn pipes_and_expressions() { + let src = indoc! {r#" + test fmt() { + (x == y) && ((z |> length()) == x) + } + "#}; + + let expected = indoc! {r#" + test fmt() { + x == y && ( z |> length() ) == x + } + "#}; + + assert_fmt(src, expected); +}