Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9616716

Browse files
authored
Introduce F#-style binary operators (#7894)
* switch to F#-style binary operators* fix dangling token selection* add changelog* fix migrations
1 parent72750a9 commit9616716

File tree

34 files changed

+227
-180
lines changed

34 files changed

+227
-180
lines changed

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
####:boom: Breaking Change
1616

17+
- Replace binary operators with F#-style`~~~`,`^^^`,`&&&`,`|||`.https://github.com/rescript-lang/rescript/pull/7894
18+
1719
####:eyeglasses: Spec Compliance
1820

1921
####:rocket: New Feature

‎compiler/ml/unified_ops.ml‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ let entries =
208208
};
209209
};
210210
{
211-
path= builtin"~~";
211+
path= builtin"~~~";
212212
name="%bitnot";
213213
form=Unary;
214214
specialization=
@@ -221,20 +221,20 @@ let entries =
221221
};
222222
};
223223
{
224-
path= builtin"&";
225-
name="%bitand";
224+
path= builtin"|||";
225+
name="%bitor";
226226
form=Binary;
227227
specialization=
228228
{
229-
int=Pandint;
229+
int=Porint;
230230
bool=None;
231231
float=None;
232-
bigint=SomePandbigint;
232+
bigint=SomePorbigint;
233233
string=None;
234234
};
235235
};
236236
{
237-
path= builtin"^";
237+
path= builtin"^^^";
238238
name="%bitxor";
239239
form=Binary;
240240
specialization=
@@ -246,6 +246,19 @@ let entries =
246246
string=None;
247247
};
248248
};
249+
{
250+
path= builtin"&&&";
251+
name="%bitand";
252+
form=Binary;
253+
specialization=
254+
{
255+
int=Pandint;
256+
bool=None;
257+
float=None;
258+
bigint=SomePandbigint;
259+
string=None;
260+
};
261+
};
249262
|]
250263

251264
let index_by_path=

‎compiler/syntax/src/res_comments_table.ml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ and walk_expression expr t comments =
14961496
{
14971497
txt=
14981498
Longident.Lident
1499-
("~+"|"~+."|"~-"|"~-."|"~~"|"not"|"!");
1499+
("~+"|"~+."|"~-"|"~-."|"~~~"|"not"|"!");
15001500
};
15011501
};
15021502
args= [(Nolabel, arg_expr)];
@@ -1517,7 +1517,8 @@ and walk_expression expr t comments =
15171517
Longident.Lident
15181518
(":="|"||"|"&&"|"=="|"==="|"<"|">"|"!="
15191519
|"!=="|"<="|">="|"+"|"+."|"-"|"-."|"++"
1520-
|"^"|"*"|"*."|"/"|"/."|"**"|"->"|"<>" );
1520+
|"|||"|"^^^"|"&&&"|"*"|"*."|"/"|"/."|"**"
1521+
|"->"|"<>" );
15211522
};
15221523
};
15231524
args= [(Nolabel, operand1); (Nolabel, operand2)];

‎compiler/syntax/src/res_core.ml‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,12 @@ let make_unary_expr start_pos token_end token operand =
508508
}
509509
| (Minus |MinusDot),Pexp_constant (Pconst_float (n,m)) ->
510510
{operandwith pexp_desc=Pexp_constant (Pconst_float (negate_string n, m))}
511-
| (Token.Plus |PlusDot |Minus |MinusDot |Tilde),_ ->
511+
| (Token.Plus |PlusDot |Minus |MinusDot |Bnot),_ ->
512512
let token_loc= mk_loc start_pos token_endin
513-
let operator="~"^Token.to_string tokenin
513+
let token_string=Token.to_string tokenin
514+
let operator=
515+
if token_string.[0]='~'then token_stringelse"~"^ token_string
516+
in
514517
Ast_helper.Exp.apply
515518
~loc:(mk_loc start_pos operand.Parsetree.pexp_loc.loc_end)
516519
(Ast_helper.Exp.ident~loc:token_loc
@@ -2292,7 +2295,7 @@ and parse_primary_expr ~operand ?(no_call = false) p =
22922295
andparse_unary_exprp=
22932296
let start_pos= p.Parser.start_posin
22942297
match p.Parser.tokenwith
2295-
| (Minus |MinusDot |Plus |PlusDot |Bang |Tilde)astoken ->
2298+
| (Minus |MinusDot |Plus |PlusDot |Bang |Bnot)astoken ->
22962299
Parser.leave_breadcrumb pGrammar.ExprUnary;
22972300
let token_end= p.end_posin
22982301
Parser.next p;
@@ -5940,15 +5943,15 @@ and parse_polymorphic_variant_type_spec_hash ~attrs ~full p :
59405943
let ident, loc= parse_hash_ident~start_pos pin
59415944
letrecloopp=
59425945
match p.Parser.tokenwith
5943-
|Bandwhen full ->
5946+
|Ampersandwhen full ->
59445947
Parser.next p;
59455948
let row_field= parse_polymorphic_variant_type_args pin
59465949
row_field :: loop p
59475950
|_ ->[]
59485951
in
59495952
let first_tuple, tag_contains_a_constant_empty_constructor=
59505953
match p.Parser.tokenwith
5951-
|Bandwhen full ->
5954+
|Ampersandwhen full ->
59525955
Parser.next p;
59535956
([parse_polymorphic_variant_type_args p],true)
59545957
|Lparen -> ([parse_polymorphic_variant_type_args p],false)

‎compiler/syntax/src/res_grammar.ml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,9 @@ let is_atomic_typ_expr_start = function
151151
letis_expr_start=function
152152
|Token.Assert|At|Await|Backtick|Bang|Codepoint _|False|Float _
153153
|For|Hash|If|Int _|Lbrace|Lbracket|LessThan|Lident _|List
154-
|Lparen|Minus|MinusDot|Module|Percent|Plus|PlusDot|Tilde
155-
|String _|Switch|True|Try|Uident _|Underscore(* _ => doThings()*)
154+
|Lparen|Minus|MinusDot|Module|Percent|Plus|PlusDot|Bnot|Bor
155+
|Bxor|Band|String _|Switch|True|Try|Uident _
156+
|Underscore(* _ => doThings()*)
156157
|While|Forwardslash|ForwardslashDot|Dict ->
157158
true
158159
|_ ->false

‎compiler/syntax/src/res_parens.ml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ let binary_expr_operand ~is_lhs expr =
129129
|_whenAst_uncurried.expr_is_uncurried_fun expr ->Parenthesized
130130
|exprwhenParsetreeViewer.is_binary_expression expr ->Parenthesized
131131
|exprwhenParsetreeViewer.is_ternary_expr expr ->Parenthesized
132-
|exprwhenParsetreeViewer.is_unary_bitnot_expression expr ->Parenthesized
133132
|{pexp_desc =Pexp_assert_}when is_lhs ->Parenthesized
134133
|_whenParsetreeViewer.expr_is_await expr ->Parenthesized
135134
|{Parsetree.pexp_attributes =attrs} ->

‎compiler/syntax/src/res_parsetree_viewer.ml‎

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -267,19 +267,20 @@ let operator_precedence operator =
267267
|":=" ->1
268268
|"||" ->2
269269
|"&&" ->3
270-
|"^" ->4
271-
|"&" ->5
272-
|"=="|"==="|"<"|">"|"!="|"<>"|"!=="|"<="|">=" ->6
273-
|"<<"|">>"|">>>" ->7
274-
|"+"|"+."|"-"|"-."|"++" ->8
275-
|"*"|"*."|"/"|"/."|"%" ->9
276-
|"**" ->10
277-
|"#"|"##"|"->" ->11
270+
|"|||" ->4
271+
|"^^^" ->5
272+
|"&&&" ->6
273+
|"=="|"==="|"<"|">"|"!="|"<>"|"!=="|"<="|">=" ->7
274+
|"<<"|">>"|">>>" ->8
275+
|"+"|"+."|"-"|"-."|"++" ->9
276+
|"*"|"*."|"/"|"/."|"%" ->10
277+
|"**" ->11
278+
|"#"|"##"|"->" ->12
278279
|_ ->0
279280

280281
letis_unary_operatoroperator=
281282
match operatorwith
282-
|"~+"|"~+."|"~-"|"~-."|"~~"|"not" ->true
283+
|"~+"|"~+."|"~-"|"~-."|"~~~"|"not" ->true
283284
|_ ->false
284285

285286
letis_unary_expressionexpr=
@@ -293,21 +294,11 @@ let is_unary_expression expr =
293294
true
294295
|_ ->false
295296

296-
letis_unary_bitnot_expressionexpr=
297-
match expr.pexp_descwith
298-
|Pexp_apply
299-
{
300-
funct= {pexp_desc=Pexp_ident {txt=Longident.Lident"~~"}};
301-
args= [(Nolabel, _arg)];
302-
} ->
303-
true
304-
|_ ->false
305-
306297
letis_binary_operatoroperator=
307298
match operatorwith
308299
|":="|"||"|"&&"|"=="|"==="|"<"|">"|"!="|"!=="|"<="|">="
309300
|"+"|"+."|"-"|"-."|"++"|"*"|"*."|"/"|"/."|"**"|"->"
310-
|"<>"|"%"|"&"|"^"|"<<"|">>"|">>>" ->
301+
|"<>"|"%"|"|||"|"^^^"|"&&&"|"<<"|">>"|">>>" ->
311302
true
312303
|_ ->false
313304

‎compiler/syntax/src/res_parsetree_viewer.mli‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ val operator_precedence : string -> int
6666

6767
valnot_ghost_operator :string ->Location.t ->bool
6868
valis_unary_expression :Parsetree.expression ->bool
69-
valis_unary_bitnot_expression :Parsetree.expression ->bool
7069
valis_binary_operator :string ->bool
7170
valis_binary_expression :Parsetree.expression ->bool
7271
valis_rhs_binary_operator :string ->bool

‎compiler/syntax/src/res_printer.ml‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3777,7 +3777,7 @@ and print_unary_expression ~state expr cmt_tbl =
37773777
|"~+." ->"+."
37783778
|"~-" ->"-"
37793779
|"~-." ->"-."
3780-
|"~~" ->"~"
3780+
|"~~~" ->"~~~"
37813781
|"not" ->"!"
37823782
|_ ->assertfalse)
37833783
in
@@ -5101,8 +5101,6 @@ and print_argument ~state (arg_lbl, arg) cmt_tbl =
51015101
matchParens.expr exprwith
51025102
|Parenthesized -> add_parens doc
51035103
|Bracedbraces -> print_braces doc expr braces
5104-
|NothingwhenParsetreeViewer.is_unary_bitnot_expression expr ->
5105-
add_parens doc
51065104
|Nothing -> doc
51075105
in
51085106
let loc= {arg_locwith loc_end= expr.pexp_loc.loc_end}in

‎compiler/syntax/src/res_scanner.ml‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -756,9 +756,14 @@ let rec scan scanner =
756756
|'`' ->
757757
next scanner;
758758
Token.Backtick
759-
|'~' ->
760-
next scanner;
761-
Token.Tilde
759+
|'~' -> (
760+
match (peek scanner, peek2 scanner)with
761+
|'~','~' ->
762+
next3 scanner;
763+
Token.Bnot
764+
|_ ->
765+
next scanner;
766+
Token.Tilde)
762767
|'?' ->
763768
next scanner;
764769
Token.Question
@@ -830,24 +835,35 @@ let rec scan scanner =
830835
next scanner;
831836
Token.Percent)
832837
|'|' -> (
833-
match peek scannerwith
834-
|'|' ->
838+
match (peek scanner, peek2 scanner)with
839+
|'|','|' ->
840+
next3 scanner;
841+
Token.Bor
842+
|'|',_ ->
835843
next2 scanner;
836844
Token.Lor
837845
|_ ->
838846
next scanner;
839847
Token.Bar)
840848
|'&' -> (
841-
match peek scannerwith
842-
|'&' ->
849+
match (peek scanner, peek2 scanner)with
850+
|'&','&' ->
851+
next3 scanner;
852+
Token.Band
853+
|'&',_ ->
843854
next2 scanner;
844855
Token.Land
845856
|_ ->
846857
next scanner;
847-
Token.Band)
848-
|'^' ->
849-
next scanner;
850-
Token.Caret
858+
Token.Ampersand)
859+
|'^' -> (
860+
match (peek scanner, peek2 scanner)with
861+
|'^','^' ->
862+
next3 scanner;
863+
Token.Bxor
864+
|_ ->
865+
next scanner;
866+
Token.Caret)
851867
|':' -> (
852868
match peek scannerwith
853869
|'=' ->

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp