Skip to content

add bitwise AND (&) operator #7415

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
May 3, 2025
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#### :rocket: New Feature

- Add shift (`<<`, `>>`, `>>>`) operators for `int` and `bigint`. https://github.com/rescript-lang/rescript/pull/7183
- Add bitwise AND (`&`) operator for `int` and `bigint`. https://github.com/rescript-lang/rescript/pull/7415
- 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

#### :bug: Bug fix
Expand Down
13 changes: 13 additions & 0 deletions compiler/ml/unified_ops.ml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@ let entries =
string = None;
};
};
{
path = builtin "&";
name = "%bitand";
form = Binary;
specialization =
{
int = Pandint;
bool = None;
float = None;
bigint = Some Pandbigint;
string = None;
};
};
{
path = builtin "^";
name = "%bitxor";
Expand Down
15 changes: 8 additions & 7 deletions compiler/syntax/src/res_parsetree_viewer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,13 @@ let operator_precedence operator =
| "||" -> 2
| "&&" -> 3
| "^" -> 4
| "==" | "===" | "<" | ">" | "!=" | "<>" | "!==" | "<=" | ">=" | "|>" -> 5
| "<<" | ">>" | ">>>" -> 6
| "+" | "+." | "-" | "-." | "++" -> 7
| "*" | "*." | "/" | "/." | "%" -> 8
| "**" -> 9
| "#" | "##" | "->" -> 10
| "&" -> 5
| "==" | "===" | "<" | ">" | "!=" | "<>" | "!==" | "<=" | ">=" | "|>" -> 6
| "<<" | ">>" | ">>>" -> 7
| "+" | "+." | "-" | "-." | "++" -> 8
| "*" | "*." | "/" | "/." | "%" -> 9
| "**" -> 10
| "#" | "##" | "->" -> 11
| _ -> 0

let is_unary_operator operator =
Expand All @@ -301,7 +302,7 @@ let is_binary_operator operator =
match operator with
| ":=" | "||" | "&&" | "==" | "===" | "<" | ">" | "!=" | "!==" | "<=" | ">="
| "|>" | "+" | "+." | "-" | "-." | "++" | "*" | "*." | "/" | "/." | "**"
| "->" | "<>" | "%" | "^" | "<<" | ">>" | ">>>" ->
| "->" | "<>" | "%" | "&" | "^" | "<<" | ">>" | ">>>" ->
true
| _ -> false

Expand Down
15 changes: 8 additions & 7 deletions compiler/syntax/src/res_token.ml
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,16 @@ let precedence = function
| Lor -> 2
| Land -> 3
| Caret -> 4
| Band -> 5
| Equal | EqualEqual | EqualEqualEqual | LessThan | GreaterThan | BangEqual
| BangEqualEqual | LessEqual | GreaterEqual | BarGreater ->
5
| LeftShift | RightShift | RightShiftUnsigned -> 6
| Plus | PlusDot | Minus | MinusDot | PlusPlus -> 7
| Asterisk | AsteriskDot | Forwardslash | ForwardslashDot | Percent -> 8
| Exponentiation -> 9
| MinusGreater -> 10
| Dot -> 11
6
| LeftShift | RightShift | RightShiftUnsigned -> 7
| Plus | PlusDot | Minus | MinusDot | PlusPlus -> 8
| Asterisk | AsteriskDot | Forwardslash | ForwardslashDot | Percent -> 9
| Exponentiation -> 10
| MinusGreater -> 11
| Dot -> 12
| _ -> 0

let to_string = function
Expand Down
1 change: 1 addition & 0 deletions runtime/Pervasives.res
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ external \"%": ('a, 'a) => 'a = "%mod"
external \"<<": ('a, 'a) => 'a = "%lsl"
external mod: ('a, 'a) => 'a = "%mod"
external \"**": ('a, 'a) => 'a = "%pow"
external \"&": ('a, 'a) => 'a = "%bitand"
external \"^": ('a, 'a) => 'a = "%bitxor"
external \">>": ('a, 'a) => 'a = "%asr"
external \">>>": ('a, 'a) => 'a = "%lsr"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ let x = a + -1 + -2
let x = a + @attr -1 + @attr -2
let x = a % a == 0
let x = a ^ a == 0
let x = a & a == 0
let x = a << a == 0
let x = a >> a == 0
let x = a >>> a == 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ let x = (a + (-1)) + (-2)
let x = (a + (((-1))[@attr ])) + (((-2))[@attr ])
let x = (a % a) == 0
let x = a ^ (a == 0)
let x = a & (a == 0)
let x = (a << a) == 0
let x = (a >> a) == 0
let x = (a >>> a) == 0
Expand Down
2 changes: 1 addition & 1 deletion tests/syntax_tests/data/printer/expr/binary.res
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ let () = x.left = value |> logMutation
let () = x.left = (value |> process) |> x.right = value |> process
let () = (x: int) |> (print_int: int => unit)

// math
x + y / z
x / y + z
x % y * z
x ^ y + z
x & y + z
x << y + z
x >> y + z
x >>> y + z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ let () = x.left = logMutation(value)
let () = x.left = (x.right = process(value))(process(value))
let () = (print_int: int => unit)((x: int))

// math
x + y / z
x / y + z
x % y * z
x ^ y + z
x & y + z
x << y + z
x >> y + z
x >>> y + z
Expand Down
10 changes: 10 additions & 0 deletions tests/tests/src/unified_ops_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ function bxor_bigint(a, b) {
return a ^ b;
}

function band_int(a, b) {
return a & b;
}

function band_bigint(a, b) {
return a & b;
}

let bigintShiftLeft = (1n << 2n);

let bigintShiftRight = (8n >> 2n);
Expand Down Expand Up @@ -111,6 +119,8 @@ export {
pow_overflow,
bxor_int,
bxor_bigint,
band_int,
band_bigint,
intShiftLeft,
intShiftRight,
intShiftRightUnsigned,
Expand Down
3 changes: 3 additions & 0 deletions tests/tests/src/unified_ops_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ let pow_overflow = 2147483647 ** 2
let bxor_int = (a, b) => a ^ b
let bxor_bigint = (a: bigint, b) => a ^ b

let band_int = (a, b) => a & b
let band_bigint = (a: bigint, b) => a & b

let intShiftLeft = 1 << 2
let intShiftRight = 8 >> 2
let intShiftRightUnsigned = -2 >>> 1
Expand Down