Skip to content

add shift(<<, >>, >>>) operator #7183

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 16 commits into from
Apr 16, 2025
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

# 12.0.0-alpha.13 (Unreleased)

#### :rocket: New Feature

- Add shift (`<<`, `>>`, `>>>`) operators for `int` and `bigint`. https://github.com/rescript-lang/rescript/pull/7183

# 12.0.0-alpha.12

#### :bug: Bug fix
Expand Down
33 changes: 33 additions & 0 deletions compiler/ml/unified_ops.ml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,39 @@ let entries =
string = None;
};
};
{
path = builtin "<<";
name = "%lsl";
form = Binary;
specialization =
{
int = Plslint;
bool = None;
float = None;
bigint = Some Plslbigint;
string = None;
};
};
{
path = builtin ">>";
name = "%asr";
form = Binary;
specialization =
{
int = Pasrint;
bool = None;
float = None;
bigint = Some Pasrbigint;
string = None;
};
};
{
path = builtin ">>>";
name = "%lsr";
form = Binary;
specialization =
{int = Plsrint; bool = None; float = None; bigint = None; string = None};
};
{
path = builtin "mod";
name = "%mod";
Expand Down
11 changes: 6 additions & 5 deletions compiler/syntax/src/res_parsetree_viewer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,11 @@ let operator_precedence operator =
| "&&" -> 3
| "^" -> 4
| "==" | "===" | "<" | ">" | "!=" | "<>" | "!==" | "<=" | ">=" | "|>" -> 5
| "+" | "+." | "-" | "-." | "++" -> 6
| "*" | "*." | "/" | "/." | "%" -> 7
| "**" -> 8
| "#" | "##" | "->" -> 9
| "<<" | ">>" | ">>>" -> 6
| "+" | "+." | "-" | "-." | "++" -> 7
| "*" | "*." | "/" | "/." | "%" -> 8
| "**" -> 9
| "#" | "##" | "->" -> 10
| _ -> 0

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

Expand Down
18 changes: 16 additions & 2 deletions compiler/syntax/src/res_scanner.ml
Original file line number Diff line number Diff line change
Expand Up @@ -886,16 +886,30 @@ let rec scan scanner =
| _ ->
next scanner;
Token.Plus)
| '>' -> (
| '>' when not (in_diamond_mode scanner) -> (
match peek scanner with
| '=' when not (in_diamond_mode scanner) ->
| '=' ->
next2 scanner;
Token.GreaterEqual
| '>' -> (
match peek2 scanner with
| '>' ->
next3 scanner;
Token.RightShiftUnsigned
| _ ->
next2 scanner;
Token.RightShift)
| _ ->
next scanner;
Token.GreaterThan)
| '>' ->
next scanner;
Token.GreaterThan
| '<' when not (in_jsx_mode scanner) -> (
match peek scanner with
| '<' when not (in_diamond_mode scanner) ->
next2 scanner;
Token.LeftShift
| '=' ->
next2 scanner;
Token.LessEqual
Expand Down
17 changes: 12 additions & 5 deletions compiler/syntax/src/res_token.ml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ type t =
| Try
| DocComment of Location.t * string
| ModuleComment of Location.t * string
| LeftShift
| RightShift
| RightShiftUnsigned

let precedence = function
| HashEqual | ColonEqual -> 1
Expand All @@ -107,11 +110,12 @@ let precedence = function
| Equal | EqualEqual | EqualEqualEqual | LessThan | GreaterThan | BangEqual
| BangEqualEqual | LessEqual | GreaterEqual | BarGreater ->
5
| Plus | PlusDot | Minus | MinusDot | PlusPlus -> 6
| Asterisk | AsteriskDot | Forwardslash | ForwardslashDot | Percent -> 7
| Exponentiation -> 8
| MinusGreater -> 9
| Dot -> 10
| LeftShift | RightShift | RightShiftUnsigned -> 6
| Plus | PlusDot | Minus | MinusDot | PlusPlus -> 7
| Asterisk | AsteriskDot | Forwardslash | ForwardslashDot | Percent -> 8
| Exponentiation -> 9
| MinusGreater -> 10
| Dot -> 11
| _ -> 0

let to_string = function
Expand Down Expand Up @@ -212,6 +216,9 @@ let to_string = function
| Try -> "try"
| DocComment (_loc, s) -> "DocComment " ^ s
| ModuleComment (_loc, s) -> "ModuleComment " ^ s
| LeftShift -> "<<"
| RightShift -> ">>"
| RightShiftUnsigned -> ">>>"

let keyword_table = function
| "and" -> And
Expand Down
3 changes: 3 additions & 0 deletions runtime/Pervasives.res
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ external \"-": ('a, 'a) => 'a = "%sub"
external \"*": ('a, 'a) => 'a = "%mul"
external \"/": ('a, 'a) => 'a = "%div"
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 = "%bitxor"
external \">>": ('a, 'a) => 'a = "%asr"
external \">>>": ('a, 'a) => 'a = "%lsr"

/* Comparisons */
/* Note: Later comparisons will be converted to unified operations too */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,20 @@


Syntax error!
syntax_tests/data/parsing/errors/typexpr/typeConstructorArgs.res:9:28
syntax_tests/data/parsing/errors/typexpr/typeConstructorArgs.res:8:16-17

6 │ type t<'a> = private Belt.Map.t('a)
7 │
8 │ type t = option<<node<int>>
9 │ type t = option(<node<int>>)
10 │

I'm not sure what to parse here when looking at ")".
I'm not sure what to parse here when looking at "<<".

type nonrec 'a node = {
_value: 'a Js.Nullable.value }
type nonrec 'a t = 'a Belt.Map.t
type nonrec 'a t = private 'a Belt.Map.t
type nonrec t = int node option
type nonrec t = option
;;node < (int >> ([%rescript.exprhole ]))
type nonrec t = int node option
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ 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

// should be interpreted as binary expression not prefix op
let x = a -b
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ 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 - b
let x = a -. b
;;Constructor (a, b)
Expand Down
3 changes: 3 additions & 0 deletions tests/syntax_tests/data/printer/expr/binary.res
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ x + y / z
x / y + z
x % y * z
x ^ y + z
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)
Expand Down
3 changes: 3 additions & 0 deletions tests/syntax_tests/data/printer/expr/expected/binary.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ x + y / z
x / y + z
x % y * z
x ^ y + z
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)
Expand Down
3 changes: 3 additions & 0 deletions tests/tests/src/belt_int_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ Mocha.describe("Belt_int_test", () => {
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);
Test_utils.eq("File \"belt_int_test.res\", line 44, characters 7-14", 1, 1);
Test_utils.eq("File \"belt_int_test.res\", line 45, characters 7-14", 16, 16);
Test_utils.eq("File \"belt_int_test.res\", line 46, characters 7-14", 2, 2);
Test_utils.eq("File \"belt_int_test.res\", line 47, characters 7-14", 2, 2);
});
});

Expand Down
3 changes: 3 additions & 0 deletions tests/tests/src/belt_int_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@ describe(__MODULE__, () => {
eq(__LOC__, 2 / 3, 0)
eq(__LOC__, 2 % 2, 0)
eq(__LOC__, 2 ^ 3, 1)
eq(__LOC__, 2 << 3, 16)
eq(__LOC__, 16 >> 3, 2)
eq(__LOC__, 16 >>> 3, 2)
})
})
15 changes: 15 additions & 0 deletions tests/tests/src/unified_ops_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,18 @@ function bxor_bigint(a, b) {
return a ^ b;
}

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

let bigintShiftRight = (8n >> 2n);

let int = 3;

let intShiftLeft = 4;

let intShiftRight = 2;

let intShiftRightUnsigned = 2147483647;

export {
int,
float,
Expand All @@ -101,5 +111,10 @@ export {
pow_overflow,
bxor_int,
bxor_bigint,
intShiftLeft,
intShiftRight,
intShiftRightUnsigned,
bigintShiftLeft,
bigintShiftRight,
}
/* No side effect */
7 changes: 7 additions & 0 deletions tests/tests/src/unified_ops_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ let pow_overflow = 2147483647 ** 2

let bxor_int = (a, b) => a ^ b
let bxor_bigint = (a: bigint, b) => a ^ b

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

let bigintShiftLeft = 1n << 2n
let bigintShiftRight = 8n >> 2n