Skip to content

Commit 6303dc5

Browse files
authored
add bitwise AND (&) operator (#7415)
1 parent 9a8f133 commit 6303dc5

File tree

11 files changed

+48
-16
lines changed

11 files changed

+48
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#### :rocket: New Feature
2020

2121
- Add shift (`<<`, `>>`, `>>>`) operators for `int` and `bigint`. https://github.com/rescript-lang/rescript/pull/7183
22+
- Add bitwise AND (`&`) operator for `int` and `bigint`. https://github.com/rescript-lang/rescript/pull/7415
2223
- Significantly reduced the download size by splitting binaries into optional platform-specific dependencies (e.g, `@rescript/linux-x64`). https://github.com/rescript-lang/rescript/pull/7395
2324

2425
#### :bug: Bug fix

compiler/ml/unified_ops.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,19 @@ let entries =
207207
string = None;
208208
};
209209
};
210+
{
211+
path = builtin "&";
212+
name = "%bitand";
213+
form = Binary;
214+
specialization =
215+
{
216+
int = Pandint;
217+
bool = None;
218+
float = None;
219+
bigint = Some Pandbigint;
220+
string = None;
221+
};
222+
};
210223
{
211224
path = builtin "^";
212225
name = "%bitxor";

compiler/syntax/src/res_parsetree_viewer.ml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,13 @@ let operator_precedence operator =
273273
| "||" -> 2
274274
| "&&" -> 3
275275
| "^" -> 4
276-
| "==" | "===" | "<" | ">" | "!=" | "<>" | "!==" | "<=" | ">=" | "|>" -> 5
277-
| "<<" | ">>" | ">>>" -> 6
278-
| "+" | "+." | "-" | "-." | "++" -> 7
279-
| "*" | "*." | "/" | "/." | "%" -> 8
280-
| "**" -> 9
281-
| "#" | "##" | "->" -> 10
276+
| "&" -> 5
277+
| "==" | "===" | "<" | ">" | "!=" | "<>" | "!==" | "<=" | ">=" | "|>" -> 6
278+
| "<<" | ">>" | ">>>" -> 7
279+
| "+" | "+." | "-" | "-." | "++" -> 8
280+
| "*" | "*." | "/" | "/." | "%" -> 9
281+
| "**" -> 10
282+
| "#" | "##" | "->" -> 11
282283
| _ -> 0
283284

284285
let is_unary_operator operator =
@@ -301,7 +302,7 @@ let is_binary_operator operator =
301302
match operator with
302303
| ":=" | "||" | "&&" | "==" | "===" | "<" | ">" | "!=" | "!==" | "<=" | ">="
303304
| "|>" | "+" | "+." | "-" | "-." | "++" | "*" | "*." | "/" | "/." | "**"
304-
| "->" | "<>" | "%" | "^" | "<<" | ">>" | ">>>" ->
305+
| "->" | "<>" | "%" | "&" | "^" | "<<" | ">>" | ">>>" ->
305306
true
306307
| _ -> false
307308

compiler/syntax/src/res_token.ml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,16 @@ let precedence = function
107107
| Lor -> 2
108108
| Land -> 3
109109
| Caret -> 4
110+
| Band -> 5
110111
| Equal | EqualEqual | EqualEqualEqual | LessThan | GreaterThan | BangEqual
111112
| BangEqualEqual | LessEqual | GreaterEqual | BarGreater ->
112-
5
113-
| LeftShift | RightShift | RightShiftUnsigned -> 6
114-
| Plus | PlusDot | Minus | MinusDot | PlusPlus -> 7
115-
| Asterisk | AsteriskDot | Forwardslash | ForwardslashDot | Percent -> 8
116-
| Exponentiation -> 9
117-
| MinusGreater -> 10
118-
| Dot -> 11
113+
6
114+
| LeftShift | RightShift | RightShiftUnsigned -> 7
115+
| Plus | PlusDot | Minus | MinusDot | PlusPlus -> 8
116+
| Asterisk | AsteriskDot | Forwardslash | ForwardslashDot | Percent -> 9
117+
| Exponentiation -> 10
118+
| MinusGreater -> 11
119+
| Dot -> 12
119120
| _ -> 0
120121

121122
let to_string = function

runtime/Pervasives.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ external \"%": ('a, 'a) => 'a = "%mod"
6868
external \"<<": ('a, 'a) => 'a = "%lsl"
6969
external mod: ('a, 'a) => 'a = "%mod"
7070
external \"**": ('a, 'a) => 'a = "%pow"
71+
external \"&": ('a, 'a) => 'a = "%bitand"
7172
external \"^": ('a, 'a) => 'a = "%bitxor"
7273
external \">>": ('a, 'a) => 'a = "%asr"
7374
external \">>>": ('a, 'a) => 'a = "%lsr"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ let x = a + -1 + -2
2929
let x = a + @attr -1 + @attr -2
3030
let x = a % a == 0
3131
let x = a ^ a == 0
32+
let x = a & a == 0
3233
let x = a << a == 0
3334
let x = a >> a == 0
3435
let x = a >>> a == 0

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
@@ -15,6 +15,7 @@ let x = (a + (-1)) + (-2)
1515
let x = (a + (((-1))[@attr ])) + (((-2))[@attr ])
1616
let x = (a % a) == 0
1717
let x = a ^ (a == 0)
18+
let x = a & (a == 0)
1819
let x = (a << a) == 0
1920
let x = (a >> a) == 0
2021
let x = (a >>> a) == 0

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ let () = x.left = value |> logMutation
5050
let () = x.left = (value |> process) |> x.right = value |> process
5151
let () = (x: int) |> (print_int: int => unit)
5252

53-
// math
5453
x + y / z
5554
x / y + z
5655
x % y * z
5756
x ^ y + z
57+
x & y + z
5858
x << y + z
5959
x >> y + z
6060
x >>> y + z

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ let () = x.left = logMutation(value)
8181
let () = x.left = (x.right = process(value))(process(value))
8282
let () = (print_int: int => unit)((x: int))
8383

84-
// math
8584
x + y / z
8685
x / y + z
8786
x % y * z
8887
x ^ y + z
88+
x & y + z
8989
x << y + z
9090
x >> y + z
9191
x >>> y + z

tests/tests/src/unified_ops_test.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ function bxor_bigint(a, b) {
7575
return a ^ b;
7676
}
7777

78+
function band_int(a, b) {
79+
return a & b;
80+
}
81+
82+
function band_bigint(a, b) {
83+
return a & b;
84+
}
85+
7886
let bigintShiftLeft = (1n << 2n);
7987

8088
let bigintShiftRight = (8n >> 2n);
@@ -111,6 +119,8 @@ export {
111119
pow_overflow,
112120
bxor_int,
113121
bxor_bigint,
122+
band_int,
123+
band_bigint,
114124
intShiftLeft,
115125
intShiftRight,
116126
intShiftRightUnsigned,

tests/tests/src/unified_ops_test.res

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ let pow_overflow = 2147483647 ** 2
3030
let bxor_int = (a, b) => a ^ b
3131
let bxor_bigint = (a: bigint, b) => a ^ b
3232

33+
let band_int = (a, b) => a & b
34+
let band_bigint = (a: bigint, b) => a & b
35+
3336
let intShiftLeft = 1 << 2
3437
let intShiftRight = 8 >> 2
3538
let intShiftRightUnsigned = -2 >>> 1

0 commit comments

Comments
 (0)