Skip to content

Commit 971f935

Browse files
authored
Add remainder (% aka modulus) operator (#7152)
Resolved #6455
1 parent 7a58f3b commit 971f935

File tree

15 files changed

+40
-5
lines changed

15 files changed

+40
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
#### :rocket: New Feature
1616

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

1920
# 12.0.0-alpha.4
2021

compiler/ml/unified_ops.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,19 @@ let entries =
135135
string = None;
136136
};
137137
};
138+
{
139+
path = builtin "%";
140+
name = "%mod";
141+
form = Binary;
142+
specialization =
143+
{
144+
int = Pmodint Safe;
145+
bool = None;
146+
float = Some Pmodfloat;
147+
bigint = Some Pmodbigint;
148+
string = None;
149+
};
150+
};
138151
{
139152
path = builtin "mod";
140153
name = "%mod";

compiler/syntax/src/res_core.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2221,7 +2221,7 @@ and parse_binary_expr ?(context = OrdinaryExpr) ?a p prec =
22212221
*
22222222
* First case is unary, second is a binary operator.
22232223
* See Scanner.isBinaryOp *)
2224-
| (Minus | MinusDot | LessThan)
2224+
| (Minus | MinusDot | LessThan | Percent)
22252225
when (not
22262226
(Scanner.is_binary_op p.scanner.src p.start_pos.pos_cnum
22272227
p.end_pos.pos_cnum))

compiler/syntax/src/res_parsetree_viewer.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ let operator_precedence operator =
302302
| "&&" -> 3
303303
| "=" | "==" | "<" | ">" | "!=" | "<>" | "!==" | "<=" | ">=" | "|>" -> 4
304304
| "+" | "+." | "-" | "-." | "^" -> 5
305-
| "*" | "*." | "/" | "/." -> 6
305+
| "*" | "*." | "/" | "/." | "%" -> 6
306306
| "**" -> 7
307307
| "#" | "##" | "|." | "|.u" -> 8
308308
| _ -> 0
@@ -326,7 +326,7 @@ let is_binary_operator operator =
326326
match operator with
327327
| ":=" | "||" | "&&" | "=" | "==" | "<" | ">" | "!=" | "!==" | "<=" | ">="
328328
| "|>" | "+" | "+." | "-" | "-." | "^" | "*" | "*." | "/" | "/." | "**" | "|."
329-
| "|.u" | "<>" ->
329+
| "|.u" | "<>" | "%" ->
330330
true
331331
| _ -> false
332332

compiler/syntax/src/res_token.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ let precedence = function
106106
| BangEqualEqual | LessEqual | GreaterEqual | BarGreater ->
107107
4
108108
| Plus | PlusDot | Minus | MinusDot | PlusPlus -> 5
109-
| Asterisk | AsteriskDot | Forwardslash | ForwardslashDot -> 6
109+
| Asterisk | AsteriskDot | Forwardslash | ForwardslashDot | Percent -> 6
110110
| Exponentiation -> 7
111111
| MinusGreater -> 8
112112
| Dot -> 9

runtime/Pervasives.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ external \"+": ('a, 'a) => 'a = "%add"
4949
external \"-": ('a, 'a) => 'a = "%sub"
5050
external \"*": ('a, 'a) => 'a = "%mul"
5151
external \"/": ('a, 'a) => 'a = "%div"
52+
external \"%": ('a, 'a) => 'a = "%mod"
5253
external mod: ('a, 'a) => 'a = "%mod"
5354

5455
/* Comparisons */

runtime/Pervasives_mini.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ external \"+": (int, int) => int = "%addint"
2828
external \"-": (int, int) => int = "%subint"
2929
external \"*": (int, int) => int = "%mulint"
3030
external \"/": (int, int) => int = "%divint"
31+
external \"%": (int, int) => int = "%modint"
3132
external mod: (int, int) => int = "%modint"
3233

3334
/* Comparisons */

tests/syntax_tests/data/parsing/grammar/expressions/binary.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ let x = z |> @attr while condition { () }
2727

2828
let x = a + -1 + -2
2929
let x = a + @attr -1 + @attr -2
30+
let x = a % a == 0
3031

3132
// should be interpreted as binary expression not prefix op
3233
let x = a -b

tests/syntax_tests/data/parsing/grammar/expressions/expected/binary.res.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ let x = while condition do () done z
1313
let x = ((while condition do () done)[@attr ]) z
1414
let x = (a + (-1)) + (-2)
1515
let x = (a + (((-1))[@attr ])) + (((-2))[@attr ])
16+
let x = (a % a) = 0
1617
let x = a - b
1718
let x = a -. b
1819
;;Constructor (a, b)

tests/syntax_tests/data/printer/expr/binary.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ let () = (x: int) |> (print_int: int => unit)
5353
// math
5454
x + y / z
5555
x / y + z
56+
x % y * z
5657
100 * x / total
5758
2 / 3 * 10 / 2 + 2
5859
let rotateX = ((range / rect.height) * refY - range / 2) * getXMultiplication(rect.width)

0 commit comments

Comments
 (0)