Skip to content

add remainder (% aka modulus) operator #7152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -14,7 +14,8 @@

#### :rocket: New Feature

- Introduce "Unified operators" for arithmetic operators (`+`, `-`, `*`, `/`, `mod`). See https://github.com/rescript-lang/rescript-compiler/pull/7057
- Introduce "Unified operators" for arithmetic operators (`+`, `-`, `*`, `/`, `mod`). https://github.com/rescript-lang/rescript-compiler/pull/7057
- Add remainder (`%`, aka modulus) operator. https://github.com/rescript-lang/rescript-compiler/pull/7152

# 12.0.0-alpha.4

13 changes: 13 additions & 0 deletions compiler/ml/unified_ops.ml
Original file line number Diff line number Diff line change
@@ -135,6 +135,19 @@ let entries =
string = None;
};
};
{
path = builtin "%";
name = "%mod";
form = Binary;
specialization =
{
int = Pmodint Safe;
bool = None;
float = Some Pmodfloat;
bigint = Some Pmodbigint;
string = None;
};
};
{
path = builtin "mod";
name = "%mod";
2 changes: 1 addition & 1 deletion compiler/syntax/src/res_core.ml
Original file line number Diff line number Diff line change
@@ -2221,7 +2221,7 @@ and parse_binary_expr ?(context = OrdinaryExpr) ?a p prec =
*
* First case is unary, second is a binary operator.
* See Scanner.isBinaryOp *)
| (Minus | MinusDot | LessThan)
| (Minus | MinusDot | LessThan | Percent)
when (not
(Scanner.is_binary_op p.scanner.src p.start_pos.pos_cnum
p.end_pos.pos_cnum))
4 changes: 2 additions & 2 deletions compiler/syntax/src/res_parsetree_viewer.ml
Original file line number Diff line number Diff line change
@@ -302,7 +302,7 @@ let operator_precedence operator =
| "&&" -> 3
| "=" | "==" | "<" | ">" | "!=" | "<>" | "!==" | "<=" | ">=" | "|>" -> 4
| "+" | "+." | "-" | "-." | "^" -> 5
| "*" | "*." | "/" | "/." -> 6
| "*" | "*." | "/" | "/." | "%" -> 6
| "**" -> 7
| "#" | "##" | "|." | "|.u" -> 8
| _ -> 0
@@ -326,7 +326,7 @@ let is_binary_operator operator =
match operator with
| ":=" | "||" | "&&" | "=" | "==" | "<" | ">" | "!=" | "!==" | "<=" | ">="
| "|>" | "+" | "+." | "-" | "-." | "^" | "*" | "*." | "/" | "/." | "**" | "|."
| "|.u" | "<>" ->
| "|.u" | "<>" | "%" ->
true
| _ -> false

2 changes: 1 addition & 1 deletion compiler/syntax/src/res_token.ml
Original file line number Diff line number Diff line change
@@ -106,7 +106,7 @@ let precedence = function
| BangEqualEqual | LessEqual | GreaterEqual | BarGreater ->
4
| Plus | PlusDot | Minus | MinusDot | PlusPlus -> 5
| Asterisk | AsteriskDot | Forwardslash | ForwardslashDot -> 6
| Asterisk | AsteriskDot | Forwardslash | ForwardslashDot | Percent -> 6
| Exponentiation -> 7
| MinusGreater -> 8
| Dot -> 9
1 change: 1 addition & 0 deletions runtime/Pervasives.res
Original file line number Diff line number Diff line change
@@ -49,6 +49,7 @@ external \"+": ('a, 'a) => 'a = "%add"
external \"-": ('a, 'a) => 'a = "%sub"
external \"*": ('a, 'a) => 'a = "%mul"
external \"/": ('a, 'a) => 'a = "%div"
external \"%": ('a, 'a) => 'a = "%mod"
external mod: ('a, 'a) => 'a = "%mod"

/* Comparisons */
1 change: 1 addition & 0 deletions runtime/Pervasives_mini.res
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@ external \"+": (int, int) => int = "%addint"
external \"-": (int, int) => int = "%subint"
external \"*": (int, int) => int = "%mulint"
external \"/": (int, int) => int = "%divint"
external \"%": (int, int) => int = "%modint"
external mod: (int, int) => int = "%modint"

/* Comparisons */
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ let x = z |> @attr while condition { () }

let x = a + -1 + -2
let x = a + @attr -1 + @attr -2
let x = a % a == 0

// should be interpreted as binary expression not prefix op
let x = a -b
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ let x = while condition do () done z
let x = ((while condition do () done)[@attr ]) z
let x = (a + (-1)) + (-2)
let x = (a + (((-1))[@attr ])) + (((-2))[@attr ])
let x = (a % a) = 0
let x = a - b
let x = a -. b
;;Constructor (a, b)
1 change: 1 addition & 0 deletions tests/syntax_tests/data/printer/expr/binary.res
Original file line number Diff line number Diff line change
@@ -53,6 +53,7 @@ let () = (x: int) |> (print_int: int => unit)
// math
x + y / z
x / y + z
x % y * z
100 * x / total
2 / 3 * 10 / 2 + 2
let rotateX = ((range / rect.height) * refY - range / 2) * getXMultiplication(rect.width)
Original file line number Diff line number Diff line change
@@ -84,6 +84,7 @@ let () = (print_int: int => unit)((x: int))
// math
x + y / z
x / y + z
x % y * z
100 * x / total
2 / 3 * 10 / 2 + 2
let rotateX = (range / rect.height * refY - range / 2) * getXMultiplication(rect.width)
1 change: 1 addition & 0 deletions tests/tests/src/belt_int_test.mjs
Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@ Mocha.describe("Belt_int_test", () => {
Test_utils.eq("File \"belt_int_test.res\", line 40, characters 7-14", -1, -1);
Test_utils.eq("File \"belt_int_test.res\", line 41, characters 7-14", 6, 6);
Test_utils.eq("File \"belt_int_test.res\", line 42, characters 7-14", 0, 0);
Test_utils.eq("File \"belt_int_test.res\", line 43, characters 7-14", 0, 0);
});
});

1 change: 1 addition & 0 deletions tests/tests/src/belt_int_test.res
Original file line number Diff line number Diff line change
@@ -40,5 +40,6 @@ describe(__MODULE__, () => {
eq(__LOC__, 2 - 3, -1)
eq(__LOC__, 2 * 3, 6)
eq(__LOC__, 2 / 3, 0)
eq(__LOC__, 2 % 2, 0)
})
})
10 changes: 10 additions & 0 deletions tests/tests/src/unified_ops_test.mjs
Original file line number Diff line number Diff line change
@@ -51,6 +51,14 @@ function case2(a, b) {
return a + "test" + b;
}

function even(n) {
return n % 2 === 0;
}

function odd(n) {
return n % 2 === 1;
}

let int = 3;

export {
@@ -69,5 +77,7 @@ export {
rhsstring,
case1,
case2,
even,
odd,
}
/* No side effect */
3 changes: 3 additions & 0 deletions tests/tests/src/unified_ops_test.res
Original file line number Diff line number Diff line change
@@ -17,3 +17,6 @@ let rhsstring = (a, b: string) => a + b

let case1 = a => 1 + a
let case2 = (a, b) => a + "test" + b

let even = n => n % 2 == 0
let odd = n => n % 2 == 1