Skip to content

Commit ffb0149

Browse files
committed
Stdlib: rename binary operations to match JavaScript terms
1 parent 2470f75 commit ffb0149

File tree

8 files changed

+169
-87
lines changed

8 files changed

+169
-87
lines changed

lib/es6/Stdlib_BigInt.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ function toInt(t) {
55
return Number(t) | 0;
66
}
77

8-
function lnot(x) {
8+
function bitwiseNot(x) {
99
return x ^ -1n;
1010
}
1111

1212
export {
1313
toInt,
14-
lnot,
14+
bitwiseNot,
1515
}
1616
/* No side effect */

lib/es6/Stdlib_Int.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,10 @@ function clamp(min, max, value) {
6262
}
6363
}
6464

65-
function lnot(x) {
65+
function bitwiseNot(x) {
6666
return x ^ -1;
6767
}
6868

69-
let Bitwise = {
70-
lnot: lnot
71-
};
72-
7369
let Constants = {
7470
minValue: -2147483648,
7571
maxValue: 2147483647
@@ -81,6 +77,6 @@ export {
8177
range,
8278
rangeWithOptions,
8379
clamp,
84-
Bitwise,
80+
bitwiseNot,
8581
}
8682
/* No side effect */

lib/js/Stdlib_BigInt.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ function toInt(t) {
55
return Number(t) | 0;
66
}
77

8-
function lnot(x) {
8+
function bitwiseNot(x) {
99
return x ^ -1n;
1010
}
1111

1212
exports.toInt = toInt;
13-
exports.lnot = lnot;
13+
exports.bitwiseNot = bitwiseNot;
1414
/* No side effect */

lib/js/Stdlib_Int.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,10 @@ function clamp(min, max, value) {
6262
}
6363
}
6464

65-
function lnot(x) {
65+
function bitwiseNot(x) {
6666
return x ^ -1;
6767
}
6868

69-
let Bitwise = {
70-
lnot: lnot
71-
};
72-
7369
let Constants = {
7470
minValue: -2147483648,
7571
maxValue: 2147483647
@@ -80,5 +76,5 @@ exports.fromString = fromString;
8076
exports.range = range;
8177
exports.rangeWithOptions = rangeWithOptions;
8278
exports.clamp = clamp;
83-
exports.Bitwise = Bitwise;
79+
exports.bitwiseNot = bitwiseNot;
8480
/* No side effect */

runtime/Stdlib_BigInt.res

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,18 @@ external div: (bigint, bigint) => bigint = "%divbigint"
8484

8585
external mod: (bigint, bigint) => bigint = "%modbigint"
8686

87-
external land: (bigint, bigint) => bigint = "%andbigint"
88-
external lor: (bigint, bigint) => bigint = "%orbigint"
89-
external lxor: (bigint, bigint) => bigint = "%xorbigint"
87+
external bitwiseAnd: (bigint, bigint) => bigint = "%andbigint"
88+
external bitwiseOr: (bigint, bigint) => bigint = "%orbigint"
89+
external bitwiseXor: (bigint, bigint) => bigint = "%xorbigint"
90+
91+
// TODO: make it a primitive
92+
let bitwiseNot = x => bitwiseXor(x, -1n)
9093

9194
external lsl: (bigint, bigint) => bigint = "%lslbigint"
9295
external asr: (bigint, bigint) => bigint = "%asrbigint"
9396

94-
let lnot = x => lxor(x, -1n)
97+
external leftShift: (bigint, bigint) => bigint = "%lslbigint"
98+
external rightShift: (bigint, bigint) => bigint = "%asrbigint"
9599

96100
/**
97101
`ignore(bigint)` ignores the provided bigint and returns unit.

runtime/Stdlib_Int.res

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,15 @@ let clamp = (~min=?, ~max=?, value): int => {
9595
}
9696
}
9797

98-
module Bitwise = {
99-
external land: (int, int) => int = "%andint"
100-
external lor: (int, int) => int = "%orint"
101-
external lxor: (int, int) => int = "%xorint"
98+
external bitwiseAnd: (int, int) => int = "%andint"
99+
external bitwiseOr: (int, int) => int = "%orint"
100+
external bitwiseXor: (int, int) => int = "%xorint"
102101

103-
external lsl: (int, int) => int = "%lslint"
104-
external lsr: (int, int) => int = "%lsrint"
105-
external asr: (int, int) => int = "%asrint"
102+
// TODO: make it a primitive
103+
let bitwiseNot = x => bitwiseXor(x, -1)
106104

107-
let lnot = x => lxor(x, -1)
108-
}
105+
external leftShift: (int, int) => int = "%lslint"
106+
external rightShift: (int, int) => int = "%asrint"
107+
external unsignedRightShift: (int, int) => int = "%lsrint"
109108

110109
external ignore: int => unit = "%ignore"

runtime/Stdlib_Int.resi

Lines changed: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -393,84 +393,87 @@ Int.clamp(42, ~min=50, ~max=40) == 50
393393
*/
394394
let clamp: (~min: int=?, ~max: int=?, int) => int
395395

396-
module Bitwise: {
397-
/**
398-
`land(n1, n2)` calculates the bitwise logical AND of two integers.
396+
/**
397+
`bitwiseAnd(n1, n2)` calculates the bitwise AND of two integers.
399398

400-
## Examples
399+
## Examples
401400

402-
```rescript
403-
Int.Bitwise.land(7, 4) == 4
404-
```
405-
*/
406-
external land: (int, int) => int = "%andint"
401+
```rescript
402+
Int.bitwiseAnd(7, 4) == 4
403+
```
404+
*/
405+
external bitwiseAnd: (int, int) => int = "%andint"
407406

408-
/**
409-
`lor(n1, n2)` calculates the bitwise logical OR of two integers.
407+
/**
408+
`bitwiseOr(n1, n2)` calculates the bitwise OR of two integers.
410409

411-
## Examples
410+
## Examples
412411

413-
```rescript
414-
Int.Bitwise.lor(7, 4) == 7
415-
```
416-
*/
417-
external lor: (int, int) => int = "%orint"
412+
```rescript
413+
Int.bitwiseOr(7, 4) == 7
414+
```
415+
*/
416+
external bitwiseOr: (int, int) => int = "%orint"
418417

419-
/**
420-
`lxor(n1, n2)` calculates the bitwise logical XOR of two integers.
418+
/**
419+
`bigwiseXor(n1, n2)` calculates the bitwise XOR of two integers.
421420

422-
## Examples
421+
## Examples
423422

424-
```rescript
425-
Int.Bitwise.lxor(7, 4) == 3
426-
```
427-
*/
428-
external lxor: (int, int) => int = "%xorint"
423+
```rescript
424+
Int.bitwiseXor(7, 4) == 3
425+
```
426+
*/
427+
external bitwiseXor: (int, int) => int = "%xorint"
429428

430-
/**
431-
`lnot(n)` calculates the bitwise logical NOT of an integer.
429+
/**
430+
`bitwiseNot(n)` calculates the bitwise NOT of an integer.
432431

433-
## Examples
432+
## Examples
434433

435-
```rescript
436-
Int.Bitwise.lnot(2) == -3
437-
```
438-
*/
439-
let lnot: int => int
434+
```rescript
435+
Int.bitwiseNot(2) == -3
436+
```
437+
*/
438+
let bitwiseNot: int => int
440439

441-
/**
442-
`lsl(n, length)` calculates the bitwise logical left shift of an integer `n` by `length`.
440+
/**
441+
`leftShift(n, length)` calculates the shifted value of an integer `n` by `length` bits to the left.
443442

444-
## Examples
443+
## Examples
445444

446-
```rescript
447-
Int.Bitwise.lsl(4, 1) == 8
448-
```
449-
*/
450-
external lsl: (int, int) => int = "%lslint"
445+
```rescript
446+
Int.leftShift(4, 1) == 8
447+
```
448+
*/
449+
external leftShift: (int, int) => int = "%lslint"
451450

452-
/**
453-
`lsr(n, length)` calculates the bitwise logical right shift of an integer `n` by `length`.
451+
/**
452+
`rightShift(n, length)` calculates the shifted value of an integer `n` by `length` bits to the right.
454453

455-
## Examples
454+
Also known as "arithmetic right shift" operation.
456455

457-
```rescript
458-
Int.Bitwise.lsr(8, 1) == 4
459-
```
460-
*/
461-
external lsr: (int, int) => int = "%lsrint"
456+
## Examples
462457

463-
/**
464-
`asr(n, length)` calculates the bitwise arithmetic right shift of an integer `n` by `length`.
458+
```rescript
459+
Int.rightShift(8, 1) == 4
460+
```
461+
*/
462+
external rightShift: (int, int) => int = "%asrint"
465463

466-
## Examples
464+
/**
465+
`unsignedRightShift(n, length)` calculates the shifted value of an integer `n` by `length` bits to the right.
466+
Excess bits shifted off to the right are discarded, and zero bits are shifted in from the left.
467467

468-
```rescript
469-
Int.Bitwise.asr(4, 1) == 2
470-
```
471-
*/
472-
external asr: (int, int) => int = "%asrint"
473-
}
468+
Also known as "zero-filling right shift" operation.
469+
470+
## Examples
471+
472+
```rescript
473+
Int.unsignedRightShift(4, 1) == 2
474+
```
475+
*/
476+
external unsignedRightShift: (int, int) => int = "%lsrint"
474477

475478
/**
476479
`ignore(int)` ignores the provided int and returns unit.

0 commit comments

Comments
 (0)