Skip to content

add bitor (|) unified operator #7382

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions compiler/ml/unified_ops.ml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,19 @@ let entries =
string = None;
};
};
{
path = builtin "|";
name = "%bitor";
form = Binary;
specialization =
{
int = Porint;
bool = None;
float = None;
bigint = Some Porbigint;
string = None;
};
};
|]

let index_by_path =
Expand Down
15 changes: 8 additions & 7 deletions compiler/syntax/src/res_parsetree_viewer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,13 @@ let operator_precedence operator =
| ":=" -> 1
| "||" -> 2
| "&&" -> 3
| "^" -> 4
| "==" | "===" | "<" | ">" | "!=" | "<>" | "!==" | "<=" | ">=" | "|>" -> 5
| "+" | "+." | "-" | "-." | "++" -> 6
| "*" | "*." | "/" | "/." | "%" -> 7
| "**" -> 8
| "#" | "##" | "->" -> 9
| "|" -> 4
| "^" -> 5
| "==" | "===" | "<" | ">" | "!=" | "<>" | "!==" | "<=" | ">=" | "|>" -> 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
16 changes: 9 additions & 7 deletions compiler/syntax/src/res_token.ml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type t =
| Lor
| Band (* Bitwise and: & *)
| Caret

| BangEqual
| BangEqualEqual
| LessEqual
Expand All @@ -103,15 +104,16 @@ let precedence = function
| HashEqual | ColonEqual -> 1
| Lor -> 2
| Land -> 3
| Caret -> 4
| Bar -> 4
| Caret -> 5
| 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
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
1 change: 1 addition & 0 deletions runtime/Pervasives.res
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ external \"%": ('a, 'a) => 'a = "%mod"
external mod: ('a, 'a) => 'a = "%mod"
external \"**": ('a, 'a) => 'a = "%pow"
external \"^": ('a, 'a) => 'a = "%bitxor"
external \"|": ('a, 'a) => 'a = "%bitor"

/* 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 @@ -29,6 +29,7 @@ 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 == a

// should be interpreted as binary expression not prefix op
let x = a -b
Expand Down
1 change: 1 addition & 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,7 @@ 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
1 change: 1 addition & 0 deletions tests/tests/src/belt_int_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ describe(__MODULE__, () => {
eq(__LOC__, 2 / 3, 0)
eq(__LOC__, 2 % 2, 0)
eq(__LOC__, 2 ^ 3, 1)
eq(__LOC__, 2 | 3, 3)
})
})
3 changes: 3 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,6 @@ let pow_overflow = 2147483647 ** 2

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

let bitor_int = (a, b) => a | b
let bitor_bigint = (a: bigint, b) => a | b
Loading