Skip to content

Commit

Permalink
Handle all rational cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ospencer committed Jul 31, 2022
1 parent b86ce0b commit bdda868
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 50 deletions.
64 changes: 17 additions & 47 deletions compiler/src/formatting/format.re
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,6 @@ let get_original_code = (location: Location.t, source: array(string)) => {
};
};

let is_rational_number = (expr: Parsetree.expression_desc) => {
switch (expr) {
| PExpConstant(const) =>
switch (const) {
| PConstNumber(nt) =>
switch (nt) {
| PConstNumberRational(_, _) => true
| _ => false
}
| _ => false
}

| _ => false
};
};

// Be AWARE! This is only to be called when you know the comments list is not empty.
// Moved here in case we want to change the implementation in future
let get_last_item_in_list = comments =>
Expand Down Expand Up @@ -1707,39 +1691,25 @@ and print_infix_application =
| _ => false
};

let (left_grouping_required, right_grouping_required) =
switch (first.pexp_desc, second.pexp_desc) {
| (PExpApp(fn1, _), PExpApp(fn2, _)) =>
let left_prec = op_precedence(get_function_name(fn1));
let right_prec = op_precedence(get_function_name(fn2));
let parent_prec = op_precedence(function_name);

// the equality check is needed for the function on the right
// as we process from the left by default when the same prededence

let needed_left = left_prec < parent_prec;
let needed_right = right_prec <= parent_prec;

(needed_left, needed_right);
let parent_prec = op_precedence(function_name);

| (PExpApp(fn1, _), _) =>
let left_prec = op_precedence(get_function_name(fn1));
let parent_prec = op_precedence(function_name);
if (left_prec < parent_prec) {
(true, false);
} else {
(false, false);
};
| (_, PExpApp(fn2, _)) =>
let parent_prec = op_precedence(function_name);
let right_prec = op_precedence(get_function_name(fn2));
if (right_prec <= parent_prec) {
(false, true);
} else {
(false, false);
};
let left_grouping_required =
switch (first.pexp_desc) {
| PExpApp(fn1, _) =>
op_precedence(get_function_name(fn1)) < parent_prec
| PExpConstant(PConstNumber(PConstNumberRational(_, _))) =>
op_precedence("/") < parent_prec
| _ => false
};

| _ => (false, is_rational_number(second.pexp_desc))
let right_grouping_required =
switch (second.pexp_desc) {
| PExpApp(fn1, _) =>
op_precedence(get_function_name(fn1)) < parent_prec
| PExpConstant(PConstNumber(PConstNumberRational(_, _))) =>
// Less than or equals is used here to distinguish rationals from division as they share precedence
op_precedence("/") <= parent_prec
| _ => false
};

let left_needs_parens = left_is_if || left_grouping_required;
Expand Down
10 changes: 10 additions & 0 deletions compiler/test/formatter_inputs/rationals.gr
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import Int32 from "int32"

let a = Int32.toNumber(3l) * (2/3)

let b = 2/3 + Int32.toNumber(4l)

let c = Int32.toNumber(3l) + (2/3)

let x = 3 * (2/3)

let y = 2/3 + 4

let z = 3 + (2/3)

assert 4 * (2/3) == 8/3
6 changes: 3 additions & 3 deletions compiler/test/formatter_outputs/parens.gr
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ if (a && (b || c)) {
(3 + 4) * 5

3 - 4 + 5
3 - (4 + 5)
3 - 4 + 5

"a" ++ "b" ++ "c"

let zz = 3 - 4 + (5 - 6)
let zz = 3 - 4 + 5 - 6
let zz2 = (3 - 4) * (5 - 6)
let zz3 = 3 * 4 * (5 - 6)

print(x(3) + x(4))
print(x(3) - x(4) + x(5))
print(x(3) - (x(4) + x(5)))
print(x(3) - x(4) + x(5))
print(x(3) - x(4) + x(5))
10 changes: 10 additions & 0 deletions compiler/test/formatter_outputs/rationals.gr
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import Int32 from "int32"

let a = Int32.toNumber(3l) * (2/3)

let b = 2/3 + Int32.toNumber(4l)

let c = Int32.toNumber(3l) + 2/3

let x = 3 * (2/3)

let y = 2/3 + 4

let z = 3 + 2/3

assert 4 * (2/3) == 8/3

0 comments on commit bdda868

Please sign in to comment.