Skip to content

Commit 8a8ffa0

Browse files
committed
switch to F#-style binary operators
1 parent d93f51b commit 8a8ffa0

28 files changed

+171
-128
lines changed

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 = Some Pandbigint;
232+
bigint = Some Porbigint;
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 = Some Pandbigint;
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: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,12 @@ let make_unary_expr start_pos token_end token operand =
488488
}
489489
| (Minus | MinusDot), Pexp_constant (Pconst_float (n, m)) ->
490490
{operand with pexp_desc = Pexp_constant (Pconst_float (negate_string n, m))}
491-
| (Token.Plus | PlusDot | Minus | MinusDot | Tilde), _ ->
491+
| (Token.Plus | PlusDot | Minus | MinusDot | Bnot), _ ->
492492
let token_loc = mk_loc start_pos token_end in
493-
let operator = "~" ^ Token.to_string token in
493+
let token_string = Token.to_string token in
494+
let operator =
495+
if token_string.[0] = '~' then token_string else "~" ^ token_string
496+
in
494497
Ast_helper.Exp.apply
495498
~loc:(mk_loc start_pos operand.Parsetree.pexp_loc.loc_end)
496499
(Ast_helper.Exp.ident ~loc:token_loc
@@ -2265,7 +2268,7 @@ and parse_primary_expr ~operand ?(no_call = false) p =
22652268
and parse_unary_expr p =
22662269
let start_pos = p.Parser.start_pos in
22672270
match p.Parser.token with
2268-
| (Minus | MinusDot | Plus | PlusDot | Bang | Tilde) as token ->
2271+
| (Minus | MinusDot | Plus | PlusDot | Bang | Bnot) as token ->
22692272
Parser.leave_breadcrumb p Grammar.ExprUnary;
22702273
let token_end = p.end_pos in
22712274
Parser.next p;

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
let is_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
| _ when Ast_uncurried.expr_is_uncurried_fun expr -> Parenthesized
130130
| expr when ParsetreeViewer.is_binary_expression expr -> Parenthesized
131131
| expr when ParsetreeViewer.is_ternary_expr expr -> Parenthesized
132-
| expr when ParsetreeViewer.is_unary_bitnot_expression expr -> Parenthesized
133132
| {pexp_desc = Pexp_assert _} when is_lhs -> Parenthesized
134133
| _ when ParsetreeViewer.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
let is_unary_operator operator =
281282
match operator with
282-
| "~+" | "~+." | "~-" | "~-." | "~~" | "not" -> true
283+
| "~+" | "~+." | "~-" | "~-." | "~~~" | "not" -> true
283284
| _ -> false
284285

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

296-
let is_unary_bitnot_expression expr =
297-
match expr.pexp_desc with
298-
| Pexp_apply
299-
{
300-
funct = {pexp_desc = Pexp_ident {txt = Longident.Lident "~~"}};
301-
args = [(Nolabel, _arg)];
302-
} ->
303-
true
304-
| _ -> false
305-
306297
let is_binary_operator operator =
307298
match operator with
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
val not_ghost_operator : string -> Location.t -> bool
6868
val is_unary_expression : Parsetree.expression -> bool
69-
val is_unary_bitnot_expression : Parsetree.expression -> bool
7069
val is_binary_operator : string -> bool
7170
val is_binary_expression : Parsetree.expression -> bool
7271
val is_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
| _ -> assert false)
37833783
in
@@ -5101,8 +5101,6 @@ and print_argument ~state (arg_lbl, arg) cmt_tbl =
51015101
match Parens.expr expr with
51025102
| Parenthesized -> add_parens doc
51035103
| Braced braces -> print_braces doc expr braces
5104-
| Nothing when ParsetreeViewer.is_unary_bitnot_expression expr ->
5105-
add_parens doc
51065104
| Nothing -> doc
51075105
in
51085106
let loc = {arg_loc with loc_end = expr.pexp_loc.loc_end} in

compiler/syntax/src/res_scanner.ml

Lines changed: 26 additions & 10 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 scanner with
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 scanner with
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;
847858
Token.Band)
848-
| '^' ->
849-
next scanner;
850-
Token.Caret
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 scanner with
853869
| '=' ->

compiler/syntax/src/res_token.ml

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type t =
2525
| Equal
2626
| EqualEqual
2727
| EqualEqualEqual
28+
| Ampersand
2829
| Bar
2930
| Lparen
3031
| Rparen
@@ -76,7 +77,10 @@ type t =
7677
| Of
7778
| Land
7879
| Lor
79-
| Band (* Bitwise and: & *)
80+
| Bnot (* Bitwise and: ~~~ *)
81+
| Bor (* Bitwise and: ||| *)
82+
| Bxor (* Bitwise and: ^^^ *)
83+
| Band (* Bitwise and: &&& *)
8084
| Caret
8185
| BangEqual
8286
| BangEqualEqual
@@ -104,17 +108,18 @@ let precedence = function
104108
| HashEqual | ColonEqual -> 1
105109
| Lor -> 2
106110
| Land -> 3
107-
| Caret -> 4
108-
| Band -> 5
111+
| Bor -> 4
112+
| Bxor -> 5
113+
| Band -> 6
109114
| Equal | EqualEqual | EqualEqualEqual | LessThan | GreaterThan | BangEqual
110115
| BangEqualEqual | LessEqual | GreaterEqual ->
111-
6
112-
| LeftShift | RightShift | RightShiftUnsigned -> 7
113-
| Plus | PlusDot | Minus | MinusDot | PlusPlus -> 8
114-
| Asterisk | AsteriskDot | Forwardslash | ForwardslashDot | Percent -> 9
115-
| Exponentiation -> 10
116-
| MinusGreater -> 11
117-
| Dot -> 12
116+
7
117+
| LeftShift | RightShift | RightShiftUnsigned -> 8
118+
| Plus | PlusDot | Minus | MinusDot | PlusPlus -> 9
119+
| Asterisk | AsteriskDot | Forwardslash | ForwardslashDot | Percent -> 10
120+
| Exponentiation -> 11
121+
| MinusGreater -> 12
122+
| Dot -> 13
118123
| _ -> 0
119124

120125
let to_string = function
@@ -143,6 +148,7 @@ let to_string = function
143148
| EqualEqual -> "=="
144149
| EqualEqualEqual -> "==="
145150
| Eof -> "eof"
151+
| Ampersand -> "&"
146152
| Bar -> "|"
147153
| As -> "as"
148154
| Lparen -> "("
@@ -193,7 +199,10 @@ let to_string = function
193199
| Module -> "module"
194200
| Of -> "of"
195201
| Lor -> "||"
196-
| Band -> "&"
202+
| Bnot -> "~~~"
203+
| Bor -> "|||"
204+
| Bxor -> "^^^"
205+
| Band -> "&&&"
197206
| Caret -> "^"
198207
| Land -> "&&"
199208
| BangEqual -> "!="

0 commit comments

Comments
 (0)