-
Notifications
You must be signed in to change notification settings - Fork 471
Introduce F#-style binary operators #7894
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
base: master
Are you sure you want to change the base?
Conversation
rescript
@rescript/darwin-arm64
@rescript/darwin-x64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
let rbt = make((~compare)) | ||
let rbt = make((~-compare) ^ ~x) | ||
let rbt = make(~~~compare) | ||
let rbt = make(~~~-compare ^^^ ~~~x) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps enforcing parens for nested unary application would look better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or any case when it's a right-hand operand?
-make(~~~-compare ^^^ ~~~x)
+make(~~~(-compare) ^^^ (~~~x))
@@ -151,8 +151,9 @@ let is_atomic_typ_expr_start = function | |||
let is_expr_start = function | |||
| Token.Assert | At | Await | Backtick | Bang | Codepoint _ | False | Float _ | |||
| For | Hash | If | Int _ | Lbrace | Lbracket | LessThan | Lident _ | List | |||
| Lparen | Minus | MinusDot | Module | Percent | Plus | PlusDot | Tilde | |||
| String _ | Switch | True | Try | Uident _ | Underscore (* _ => doThings() *) | |||
| Lparen | Minus | MinusDot | Module | Percent | Plus | PlusDot | Bnot | Bor |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this was not all part of this PR, but
| BitwiseNot
| BitwiseOr
| BitwiseXor
| BitwiseAnd
would read better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or actually, should it not be
| TildeTildeTilde
| BarBarBar
| CaretCaretCaret
| AndAndAnd
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only if there is a possibility to reuse that tokens across the grammar, I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice for consistency, as we already have DotDotDot
and EqualEqualEqual
, too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And in this spirit, I would also keep Tilde
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You know. we already have tokens like Land
, Lor
. Do you want to change those token names too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or leave them as is, as Christoph said it would just be nice to have.
This seems like a decent compromise. At least, it's clean. |
compiler/syntax/src/res_scanner.ml
Outdated
| '&', '&' -> | ||
next3 scanner; | ||
Token.Band | ||
| '&', _ -> | ||
next2 scanner; | ||
Token.Land | ||
| _ -> | ||
next scanner; | ||
Token.Band) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be another token.
I'm in favor of this. It might seem ugly at first glance, but if you look at the f# docs with them all on a nice table it does make sense and seems less ugly. |
You mean to support bitwise operators or not? |
Let's take a problematic(?) example:
The impression is that |
What's |
We need to settle on #7172 before v12 release.
But the previous implementation added some bloat to parsers due to the syntax ambiguity, e.g.,
apply(~param)
.And for bitwise-OR (
|
), I'm not sure if it's even possible to add without breaking changes on the critical syntax like pattern matching.Keeping our syntax as simple as possible is really important for parser maintainability and reproducibility. Being overly reliant on context can lead to broken syntax highlighting.
So we discussed alternative syntax for binary operators, imported from F# again.
https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/symbol-and-operator-reference/#bitwise-operators
~~~
for bitwise NOT|||
for bitwise OR&&&
for bitwise AND^^^
for bitwise XOR(
&
and^
operators could be handled without any ambiguity, but for consistency)Basically using dedicated tokens for each operator eliminates almost all complexity.
More characters, more noises. But they at least make it easier to work with than using stdlib functions without any operator support.