Fix operator precedences, in particular |>

Fixes #571.
This commit is contained in:
KtorZ 2023-06-06 17:12:31 +02:00
parent 5faa925aea
commit 0afc3aba13
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
4 changed files with 38 additions and 7 deletions

View File

@ -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

View File

@ -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,

View File

@ -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,
}
}

View File

@ -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);
}