Skip to content

Commit

Permalink
add parens based on rational being on rhs
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Roberts committed Jul 30, 2022
1 parent 3b222fe commit b35bc09
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 45 deletions.
16 changes: 3 additions & 13 deletions compiler/src/formatting/format.re
Original file line number Diff line number Diff line change
Expand Up @@ -1707,14 +1707,6 @@ and print_infix_application =
| _ => false
};

// wrap rational numbers in params when in an divide infix operation
// to ensure correct precedence

let left_is_rational =
function_name == "/" && is_rational_number(first.pexp_desc);
let right_is_rational =
function_name == "/" && is_rational_number(second.pexp_desc);

let (left_grouping_required, right_grouping_required) =
switch (first.pexp_desc, second.pexp_desc) {
| (PExpApp(fn1, _), PExpApp(fn2, _)) =>
Expand Down Expand Up @@ -1747,13 +1739,11 @@ and print_infix_application =
(false, false);
};

| _ => (false, false)
| _ => (false, is_rational_number(second.pexp_desc))
};

let left_needs_parens =
left_is_if || left_is_rational || left_grouping_required;
let right_needs_parens =
right_is_if || right_is_rational || right_grouping_required;
let left_needs_parens = left_is_if || left_grouping_required;
let right_needs_parens = right_is_if || right_grouping_required;

let wrapped_left =
if (left_needs_parens) {
Expand Down
59 changes: 27 additions & 32 deletions compiler/test/stdlib/number.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ assert Number.add(25, 5) == 30
assert 2/3 + 4 == 14/3
assert 2/3 + Int32.toNumber(4l) == 14/3
assert 2/3 + Int64.toNumber(4L) == 14/3
assert 4 + 2/3 == 14/3
assert 4 + (2/3) == 14/3
assert Int32.toNumber(4l) + 2/3 == 14/3
assert Int64.toNumber(4L) + 2/3 == 14/3
assert 2/3 + 4.0 == 4.666666666666667
Expand All @@ -34,25 +34,25 @@ assert Number.sub(25, 5) == 20
assert 2/3 - 4 == -10/3
assert 2/3 - Int32.toNumber(4l) == -10/3
assert 2/3 - Int64.toNumber(4L) == -10/3
assert 4 - 2/3 == 10/3
assert 4 - (2/3) == 10/3
assert Int32.toNumber(4l) - 2/3 == 10/3
assert Int64.toNumber(4L) - 2/3 == 10/3
assert 2/3 - 4.0 == -3.3333333333333335
assert 4.0 - 2/3 == 3.3333333333333335
assert (2/3) - 4.0 == -3.3333333333333335
assert 4.0 - (2/3) == 3.3333333333333335
}
// mul
assert Number.mul(5, 5) == 25
// Rational mult tests
{
let (*) = Number.mul
assert 2/3 * 4 == 8/3
assert 2/3 * Int32.toNumber(4l) == 8/3
assert 2/3 * Int64.toNumber(4L) == 8/3
assert 4 * 2 / 3 == 8/3
assert Int32.toNumber(4l) * 2 / 3 == 8/3
assert Int64.toNumber(4L) * 2 / 3 == 8/3
assert 2/3 * 4.0 < 2.666666666666667
assert 2.6666666666666 < 2/3 * 4.0
assert (2/3) * 4 == 8/3
assert (2/3) * Int32.toNumber(4l) == 8/3
assert (2/3) * Int64.toNumber(4L) == 8/3
assert 4 * (2/3) == 8/3
assert Int32.toNumber(4l) * (2/3) == 8/3
assert Int64.toNumber(4L) * (2/3) == 8/3
assert (2/3) * 4.0 < 2.666666666666667
assert 2.6666666666666 < (2/3) * 4.0
}
// div
assert Number.div(25, 5) == 5
Expand All @@ -65,7 +65,7 @@ assert Number.div(25, 5) == 5
assert 4 / (2/3) == 6
assert Int32.toNumber(4l) / (2/3) == 6
assert Int64.toNumber(4L) / (2/3) == 6
assert (2/3) / 4.0 == 0.16666666666666666
assert (2/3) / 4.0 == 0.16666666666666666
assert 4.0 / (2/3) == 6.0
}
// sqrt
Expand All @@ -86,8 +86,7 @@ assert Number.max(5, 6) == 6
assert Number.max(1/2, 1/4) == 1/2
assert Number.max(0.5, 0.25) == 0.5
assert Number.max(BI.toNumber(1234t), BI.toNumber(12t)) == BI.toNumber(1234t)
assert Number.max(355894508425808343204914141312, 6) ==
355894508425808343204914141312
assert Number.max(355894508425808343204914141312, 6) == 355894508425808343204914141312
// ceil
assert Number.ceil(-25.5) == -25
assert Number.ceil(25.5) == 26
Expand Down Expand Up @@ -125,9 +124,9 @@ assert Number.neg(BI.toNumber(1234t)) == BI.toNumber(-1234t)
assert Number.neg(BI.toNumber(-1234t)) == BI.toNumber(1234t)

// isFinite
assert Number.isFinite(0.0 / 0.0) == false // NaN
assert Number.isFinite(1.0 / 0.0) == false // infinity
assert Number.isFinite(-1.0 / 0.0) == false // -infinity
assert Number.isFinite(0.0/0.0) == false // NaN
assert Number.isFinite(1.0/0.0) == false // infinity
assert Number.isFinite(-1.0/0.0) == false // -infinity
assert Number.isFinite(1)
assert Number.isFinite(1.0)
assert Number.isFinite(0)
Expand All @@ -141,7 +140,7 @@ assert Number.isFinite(-1/2)
assert Number.isFinite(BI.toNumber(-141435902485091384901384t))

// isNaN
assert Number.isNaN(0.0 / 0.0)
assert Number.isNaN(0.0/0.0)
assert Number.isNaN(1) == false
assert Number.isNaN(1.0) == false
assert Number.isNaN(0) == false
Expand All @@ -152,14 +151,14 @@ assert Number.isNaN(25.76) == false
assert Number.isNaN(-25.00) == false
assert Number.isNaN(1/2) == false
assert Number.isNaN(-1/2) == false
assert Number.isNaN(1.0 / 0.0) == false // infinity
assert Number.isNaN(-1.0 / 0.0) == false // -infinity
assert Number.isNaN(1.0/0.0) == false // infinity
assert Number.isNaN(-1.0/0.0) == false // -infinity
assert Number.isNaN(BI.toNumber(1t)) == false

// isInfinite
assert Number.isInfinite(1.0 / 0.0) // infinity
assert Number.isInfinite(-1.0 / 0.0) // -infinity
assert Number.isInfinite(0.0 / 0.0) == false // NaN
assert Number.isInfinite(1.0/0.0) // infinity
assert Number.isInfinite(-1.0/0.0) // -infinity
assert Number.isInfinite(0.0/0.0) == false // NaN
assert Number.isInfinite(1) == false
assert Number.isInfinite(1.0) == false
assert Number.isInfinite(0) == false
Expand All @@ -179,16 +178,12 @@ assert Number.parseInt("_0___42___", 10) == Ok(42)
assert Number.parseInt("-42", 10) == Ok(-42)
assert Number.parseInt("-042", 10) == Ok(-42)
assert Number.parseInt("-_0___42___", 10) == Ok(-42)
assert Number.parseInt("1073741823", 10) ==
Ok(1073741823) // grain simple number max
assert Number.parseInt("-1073741824", 10) ==
Ok(-1073741824) // grain simple number min
assert Number.parseInt("1073741823", 10) == Ok(1073741823) // grain simple number max
assert Number.parseInt("-1073741824", 10) == Ok(-1073741824) // grain simple number min
assert Number.parseInt("2147483647", 10) == Ok(2147483647) // i32 max
assert Number.parseInt("-2147483648", 10) == Ok(-2147483648) // i32 min
assert Number.parseInt("9223372036854775807", 10) ==
Ok(9223372036854775807) // i64 max
assert Number.parseInt("-9223372036854775808", 10) ==
Ok(-9223372036854775808) // i64 min
assert Number.parseInt("9223372036854775807", 10) == Ok(9223372036854775807) // i64 max
assert Number.parseInt("-9223372036854775808", 10) == Ok(-9223372036854775808) // i64 min
assert Number.parseInt("0xabcdef", 10) == Ok(0xabcdef)
assert Number.parseInt("0Xabcdef", 10) == Ok(0xabcdef)
assert Number.parseInt("abcdef", 16) == Ok(0xabcdef)
Expand Down

0 comments on commit b35bc09

Please sign in to comment.