Skip to content

Commit 1308af2

Browse files
cometkimgwansikk
andcommitted
Complete implementation of shift operators
I looked back at the last session with Gwansik and realized that, I had misguided him by simply misunderstanding a particular syntx error. Gwansik's initial approach was almost close. Co-authored-by: gwansikk <[email protected]>
1 parent ff9bfa1 commit 1308af2

File tree

8 files changed

+45
-71
lines changed

8 files changed

+45
-71
lines changed

compiler/syntax/src/res_core.ml

-31
Original file line numberDiff line numberDiff line change
@@ -2138,27 +2138,6 @@ and parse_operand_expr ~context p =
21382138
(* pexp_loc = mkLoc startPos endPos *)
21392139
}
21402140

2141-
and parse_shift_operator (p : Parser.t) =
2142-
let start_pos = p.start_pos in
2143-
let end_pos = p.end_pos in
2144-
if p.token = LessThan && Scanner.is_left_shift p.scanner start_pos.pos_cnum
2145-
then (
2146-
Parser.next p;
2147-
{p with token = LeftShift; start_pos; prev_end_pos = end_pos})
2148-
else if
2149-
p.token = GreaterThan
2150-
&& Scanner.is_right_shift_unsigned p.scanner start_pos.pos_cnum
2151-
then (
2152-
Parser.next p;
2153-
Parser.next p;
2154-
{p with token = RightShiftUnsigned; start_pos; prev_end_pos = end_pos})
2155-
else if
2156-
p.token = GreaterThan && Scanner.is_right_shift p.scanner start_pos.pos_cnum
2157-
then (
2158-
Parser.next p;
2159-
{p with token = RightShift; start_pos; prev_end_pos = end_pos})
2160-
else p
2161-
21622141
(* a binary expression is an expression that combines two expressions with an
21632142
* operator. Examples:
21642143
* a + b
@@ -2171,11 +2150,6 @@ and parse_binary_expr ?(context = OrdinaryExpr) ?a p prec =
21712150
| None -> parse_operand_expr ~context p
21722151
in
21732152
let rec loop a =
2174-
let p =
2175-
match p.token with
2176-
| LessThan | GreaterThan -> parse_shift_operator p
2177-
| _ -> p
2178-
in
21792153
let token = p.Parser.token in
21802154
let token_prec =
21812155
match token with
@@ -2205,11 +2179,6 @@ and parse_binary_expr ?(context = OrdinaryExpr) ?a p prec =
22052179
Parser.leave_breadcrumb p (Grammar.ExprBinaryAfterOp token);
22062180
let start_pos = p.start_pos in
22072181
Parser.next p;
2208-
(* let p =
2209-
match p.token with
2210-
| LessThan | GreaterThan -> parse_shift_operator p
2211-
| _ -> p
2212-
in *)
22132182
let end_pos = p.prev_end_pos in
22142183
let token_prec =
22152184
(* exponentiation operator is right-associative *)

compiler/syntax/src/res_scanner.ml

+16-33
Original file line numberDiff line numberDiff line change
@@ -886,24 +886,30 @@ let rec scan scanner =
886886
| _ ->
887887
next scanner;
888888
Token.Plus)
889-
| '>' -> (
889+
| '>' when not (in_diamond_mode scanner) -> (
890890
match peek scanner with
891-
| '=' when not (in_diamond_mode scanner) ->
891+
| '=' ->
892892
next2 scanner;
893893
Token.GreaterEqual
894+
| '>' -> (
895+
match peek2 scanner with
896+
| '>' ->
897+
next3 scanner;
898+
Token.RightShiftUnsigned
899+
| _ ->
900+
next2 scanner;
901+
Token.RightShift)
894902
| _ ->
895903
next scanner;
896904
Token.GreaterThan)
897-
| '<' when not (in_diamond_mode scanner) -> (
898-
match peek scanner with
899-
| '=' ->
900-
next2 scanner;
901-
Token.LessEqual
902-
| _ ->
903-
next scanner;
904-
Token.LessThan)
905+
| '>' ->
906+
next scanner;
907+
Token.GreaterThan
905908
| '<' when not (in_jsx_mode scanner) -> (
906909
match peek scanner with
910+
| '<' when not (in_diamond_mode scanner) ->
911+
next2 scanner;
912+
Token.LeftShift
907913
| '=' ->
908914
next2 scanner;
909915
Token.LessEqual
@@ -1054,26 +1060,3 @@ let is_binary_op src start_cnum end_cnum =
10541060
|| is_whitespace (String.unsafe_get src end_cnum)
10551061
in
10561062
left_ok && right_ok)
1057-
1058-
let is_left_shift scanner _start_cnum =
1059-
(* print_endline ("@@@@@ is_left_shift ch: " ^ (Char.escaped scanner.ch)); *)
1060-
(* print_endline ("@@@@@ is_left_shift cnum: " ^ (string_of_int _start_cnum)); *)
1061-
(* print_endline ("@@@@@ is_left_shift peek: " ^ (Char.escaped (peek scanner))); *)
1062-
match scanner.ch with
1063-
| '<' -> true
1064-
| _ -> false
1065-
1066-
let is_right_shift scanner _start_cnum =
1067-
(* print_endline ("@@@@@ is_right_shift ch: " ^ (Char.escaped scanner.ch)); *)
1068-
(* print_endline ("@@@@@ is_right_shift cnum: " ^ (string_of_int _start_cnum)); *)
1069-
(* print_endline ("@@@@@ is_right_shift peek: " ^ (Char.escaped (peek scanner))); *)
1070-
match scanner.ch with
1071-
| '>' -> true
1072-
| _ -> false
1073-
1074-
let is_right_shift_unsigned scanner _start_cnum =
1075-
(* print_endline ("@@@@@ is_right_shift_unsigned ch: " ^ (Char.escaped scanner.ch)); *)
1076-
(* print_endline ("@@@@@ is_right_shift_unsigned peek: " ^ (Char.escaped (peek scanner))); *)
1077-
match (scanner.ch, peek scanner) with
1078-
| '>', '>' -> true
1079-
| _ -> false

compiler/syntax/src/res_scanner.mli

-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ val scan : t -> Lexing.position * Lexing.position * Res_token.t
2626

2727
val is_binary_op : string -> int -> int -> bool
2828

29-
val is_left_shift : t -> int -> bool
30-
val is_right_shift : t -> int -> bool
31-
val is_right_shift_unsigned : t -> int -> bool
32-
3329
val set_jsx_mode : t -> unit
3430
val set_diamond_mode : t -> unit
3531
val pop_mode : t -> mode -> unit

tests/syntax_tests/data/parsing/errors/typexpr/expected/typeConstructorArgs.res.txt

+5-3
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,20 @@
3838

3939

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

43+
6 │ type t<'a> = private Belt.Map.t('a)
4344
7 │
4445
8 │ type t = option<<node<int>>
4546
9 │ type t = option(<node<int>>)
4647
10 │
4748

48-
I'm not sure what to parse here when looking at ")".
49+
I'm not sure what to parse here when looking at "<<".
4950

5051
type nonrec 'a node = {
5152
_value: 'a Js.Nullable.value }
5253
type nonrec 'a t = 'a Belt.Map.t
5354
type nonrec 'a t = private 'a Belt.Map.t
54-
type nonrec t = int node option
55+
type nonrec t = option
56+
;;node < (int >> ([%rescript.exprhole ]))
5557
type nonrec t = int node option

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

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ 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
19+
let x = (a >> a) == 0
20+
let x = (a >>> a) == 0
1821
let x = a - b
1922
let x = a -. b
2023
;;Constructor (a, b)

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

+3
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ x + y / z
8686
x / y + z
8787
x % y * z
8888
x ^ y + z
89+
x << y + z
90+
x >> y + z
91+
x >>> y + z
8992
100 * x / total
9093
2 / 3 * 10 / 2 + 2
9194
let rotateX = (range / rect.height * refY - range / 2) * getXMultiplication(rect.width)

tests/tests/src/belt_int_test.mjs

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ Mocha.describe("Belt_int_test", () => {
3737
Test_utils.eq("File \"belt_int_test.res\", line 42, characters 7-14", 0, 0);
3838
Test_utils.eq("File \"belt_int_test.res\", line 43, characters 7-14", 0, 0);
3939
Test_utils.eq("File \"belt_int_test.res\", line 44, characters 7-14", 1, 1);
40+
Test_utils.eq("File \"belt_int_test.res\", line 45, characters 7-14", 16, 16);
41+
Test_utils.eq("File \"belt_int_test.res\", line 46, characters 7-14", 2, 2);
42+
Test_utils.eq("File \"belt_int_test.res\", line 47, characters 7-14", 2, 2);
4043
});
4144
});
4245

tests/tests/src/unified_ops_test.mjs

+15
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,18 @@ function bxor_bigint(a, b) {
7575
return a ^ b;
7676
}
7777

78+
let shl_bigint = (1n << 2n);
79+
80+
let shr_bigint = (8n >> 2n);
81+
7882
let int = 3;
7983

84+
let shl_int = 4;
85+
86+
let shr_int = 2;
87+
88+
let lsr_int = 2147483647;
89+
8090
export {
8191
int,
8292
float,
@@ -101,5 +111,10 @@ export {
101111
pow_overflow,
102112
bxor_int,
103113
bxor_bigint,
114+
shl_int,
115+
shr_int,
116+
lsr_int,
117+
shl_bigint,
118+
shr_bigint,
104119
}
105120
/* No side effect */

0 commit comments

Comments
 (0)