@grain/stdlib 0.5.3 → 0.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/CHANGELOG.md +61 -0
  2. package/array.gr +65 -57
  3. package/array.md +54 -6
  4. package/buffer.gr +71 -1
  5. package/buffer.md +142 -0
  6. package/bytes.gr +52 -3
  7. package/bytes.md +117 -0
  8. package/char.gr +23 -20
  9. package/char.md +18 -3
  10. package/immutablemap.gr +493 -0
  11. package/immutablemap.md +479 -0
  12. package/immutablepriorityqueue.gr +44 -16
  13. package/immutablepriorityqueue.md +44 -1
  14. package/immutableset.gr +498 -0
  15. package/immutableset.md +449 -0
  16. package/int32.gr +39 -37
  17. package/int32.md +6 -0
  18. package/int64.gr +39 -37
  19. package/int64.md +6 -0
  20. package/list.gr +33 -24
  21. package/list.md +39 -10
  22. package/map.gr +19 -28
  23. package/marshal.gr +4 -4
  24. package/number.gr +727 -26
  25. package/number.md +345 -23
  26. package/option.gr +30 -26
  27. package/option.md +12 -0
  28. package/package.json +1 -1
  29. package/path.gr +787 -0
  30. package/path.md +727 -0
  31. package/pervasives.gr +3 -4
  32. package/pervasives.md +6 -1
  33. package/priorityqueue.gr +25 -5
  34. package/priorityqueue.md +30 -0
  35. package/queue.gr +22 -7
  36. package/queue.md +18 -1
  37. package/regex.gr +161 -65
  38. package/regex.md +70 -0
  39. package/result.gr +24 -20
  40. package/result.md +12 -0
  41. package/runtime/atof/common.gr +198 -0
  42. package/runtime/atof/common.md +243 -0
  43. package/runtime/atof/decimal.gr +663 -0
  44. package/runtime/atof/decimal.md +59 -0
  45. package/runtime/atof/lemire.gr +264 -0
  46. package/runtime/atof/lemire.md +6 -0
  47. package/runtime/atof/parse.gr +615 -0
  48. package/runtime/atof/parse.md +12 -0
  49. package/runtime/atof/slow.gr +238 -0
  50. package/runtime/atof/slow.md +6 -0
  51. package/runtime/atof/table.gr +2016 -0
  52. package/runtime/atof/table.md +12 -0
  53. package/runtime/{stringUtils.gr → atoi/parse.gr} +1 -1
  54. package/runtime/{stringUtils.md → atoi/parse.md} +1 -1
  55. package/runtime/bigint.gr +7 -7
  56. package/runtime/compare.gr +2 -1
  57. package/runtime/equal.gr +3 -2
  58. package/runtime/exception.gr +9 -5
  59. package/runtime/exception.md +8 -2
  60. package/runtime/gc.gr +2 -1
  61. package/runtime/malloc.gr +1 -3
  62. package/runtime/numberUtils.gr +13 -13
  63. package/runtime/numberUtils.md +6 -0
  64. package/runtime/numbers.gr +123 -39
  65. package/runtime/numbers.md +26 -0
  66. package/runtime/string.gr +4 -2
  67. package/runtime/unsafe/conv.gr +21 -41
  68. package/runtime/unsafe/conv.md +0 -3
  69. package/runtime/unsafe/printWasm.gr +4 -40
  70. package/runtime/utils/printing.gr +3 -3
  71. package/set.gr +25 -25
  72. package/stack.gr +14 -0
  73. package/stack.md +17 -0
  74. package/string.gr +313 -39
  75. package/string.md +99 -0
  76. package/sys/file.gr +1 -1
  77. package/sys/time.gr +4 -4
@@ -0,0 +1,12 @@
1
+ ### Table.**get_F64_POWERS10_FAST_PATH**
2
+
3
+ ```grain
4
+ get_F64_POWERS10_FAST_PATH : () -> WasmI32
5
+ ```
6
+
7
+ ### Table.**get_POWERS5**
8
+
9
+ ```grain
10
+ get_POWERS5 : () -> WasmI32
11
+ ```
12
+
@@ -18,7 +18,7 @@ import BI from "runtime/bigint"
18
18
  import { reducedInteger } from "runtime/numbers"
19
19
 
20
20
  @unsafe
21
- export let rec parseInt = (string: String, radix: Number) => {
21
+ export let parseInt = (string: String, radix: Number) => {
22
22
  let _CHAR_0 = 0x30n
23
23
  let _CHAR_B = 0x42n
24
24
  let _CHAR_b = 0x62n
@@ -1,4 +1,4 @@
1
- ### StringUtils.**parseInt**
1
+ ### Parse.**parseInt**
2
2
 
3
3
  ```grain
4
4
  parseInt : (String, Number) -> Result<Number, String>
package/runtime/bigint.gr CHANGED
@@ -786,7 +786,7 @@ let _SIZES = [>
786
786
  ]
787
787
 
788
788
  @unsafe
789
- export let rec bigIntToString = (num: WasmI32, base: WasmI32) => {
789
+ export let bigIntToString = (num: WasmI32, base: WasmI32) => {
790
790
  let getDigit = n =>
791
791
  WasmI32.load8U(WasmI32.add(WasmI32.fromGrain(_DIGITS), n), 8n)
792
792
  if (WasmI32.ltS(base, 2n) || WasmI32.gtS(base, 32n)) {
@@ -1339,7 +1339,7 @@ export let shl = (num: WasmI32, places: WasmI32) => {
1339
1339
  let (&) = WasmI64.and
1340
1340
  let a = places / 32n
1341
1341
  let b = places % 32n
1342
- let mask = (1N << WasmI64.extendI32U(b)) - 1N << 64N - WasmI64.extendI32U(b)
1342
+ let mask = ((1N << WasmI64.extendI32U(b)) - 1N) << 64N - WasmI64.extendI32U(b)
1343
1343
  let result = init(numLimbs + a)
1344
1344
  setFlag(result, _IS_NEGATIVE, getFlag(num, _IS_NEGATIVE))
1345
1345
  let numHalfLimbs = WasmI32.shl(numLimbs, 1n)
@@ -1350,7 +1350,7 @@ export let shl = (num: WasmI32, places: WasmI32) => {
1350
1350
  setHalfLimb(
1351
1351
  result,
1352
1352
  i + a,
1353
- WasmI32.wrapI64(acc << WasmI64.extendI32U(b) >> 32N)
1353
+ WasmI32.wrapI64((acc << WasmI64.extendI32U(b)) >> 32N)
1354
1354
  )
1355
1355
  }
1356
1356
  let (>) = WasmI64.gtU
@@ -1827,10 +1827,10 @@ let baseCaseDivRem = (a: WasmI32, b: WasmI32, result: WasmI32) => {
1827
1827
  let n = getHalfSize(b)
1828
1828
  let m = getHalfSize(a) - n
1829
1829
  let qsize = (if (WasmI32.eqz(WasmI32.and(m + 1n, 1n))) {
1830
- m + 1n
1831
- } else {
1832
- m + 2n
1833
- }) >>
1830
+ m + 1n
1831
+ } else {
1832
+ m + 2n
1833
+ }) >>
1834
1834
  1n
1835
1835
  let mut q = init(qsize)
1836
1836
  let mut a = 0n
@@ -146,7 +146,8 @@ let rec heapCompareHelp = (heapTag, xptr, yptr) => {
146
146
  tagSimpleNumber(xptr - yptr)
147
147
  },
148
148
  }
149
- }, compareHelp = (x, y) => {
149
+ },
150
+ compareHelp = (x, y) => {
150
151
  let xtag = x & Tags._GRAIN_GENERIC_TAG_MASK
151
152
  let ytag = y & Tags._GRAIN_GENERIC_TAG_MASK
152
153
  if ((xtag & ytag) != Tags._GRAIN_GENERIC_HEAP_TAG_TYPE) {
package/runtime/equal.gr CHANGED
@@ -189,7 +189,8 @@ let rec heapEqualHelp = (heapTag, xptr, yptr) => {
189
189
  xptr == yptr
190
190
  },
191
191
  }
192
- }, equalHelp = (x, y) => {
192
+ },
193
+ equalHelp = (x, y) => {
193
194
  if (
194
195
  (x & Tags._GRAIN_GENERIC_TAG_MASK) != 0n &&
195
196
  (y & Tags._GRAIN_GENERIC_TAG_MASK) != 0n
@@ -207,6 +208,6 @@ let rec heapEqualHelp = (heapTag, xptr, yptr) => {
207
208
  }
208
209
 
209
210
  @unsafe
210
- export let rec equal = (x: a, y: a) => {
211
+ export let equal = (x: a, y: a) => {
211
212
  equalHelp(WasmI32.fromGrain(x), WasmI32.fromGrain(y))
212
213
  }
@@ -9,6 +9,8 @@ import foreign wasm fd_write: (
9
9
  WasmI32,
10
10
  ) -> WasmI32 from "wasi_snapshot_preview1"
11
11
 
12
+ primitive unreachable: () -> a = "@unreachable"
13
+
12
14
  enum Option<a> {
13
15
  Some(a),
14
16
  None,
@@ -79,8 +81,8 @@ let exceptionToString = (e: Exception) => {
79
81
  // the runtime heap, but this is the only module that needs to do it
80
82
  let iov = WasmI32.fromGrain([> 0n, 0n, 0n, 0n, 0n, 0n, 0n, 0n, 0n, 0n])
81
83
 
82
- export let printException = (e: Exception) => {
83
- let ptr = WasmI32.fromGrain(exceptionToString(e))
84
+ export let panic = (msg: String) => {
85
+ let ptr = WasmI32.fromGrain(msg)
84
86
  let written = iov + 32n
85
87
  let lf = iov + 36n
86
88
  WasmI32.store(iov, ptr + 8n, 0n)
@@ -89,7 +91,11 @@ export let printException = (e: Exception) => {
89
91
  WasmI32.store(iov, lf, 8n)
90
92
  WasmI32.store(iov, 1n, 12n)
91
93
  fd_write(2n, iov, 2n, written)
92
- void
94
+ unreachable()
95
+ }
96
+
97
+ export let panicWithException = (e: Exception) => {
98
+ panic(exceptionToString(e))
93
99
  }
94
100
 
95
101
  export exception IndexOutOfBounds
@@ -109,7 +115,6 @@ export exception MatchFailure
109
115
  */
110
116
  export exception AssertionError(String)
111
117
  export exception InvalidArgument(String)
112
- export exception OutOfMemory
113
118
 
114
119
  let runtimeErrorPrinter = e => {
115
120
  match (e) {
@@ -123,7 +128,6 @@ let runtimeErrorPrinter = e => {
123
128
  Some("NumberNotRational: Can't coerce number to rational"),
124
129
  MatchFailure => Some("MatchFailure: No matching pattern"),
125
130
  AssertionError(s) => Some(s),
126
- OutOfMemory => Some("OutOfMemory: Maximum memory size exceeded"),
127
131
  InvalidArgument(msg) => Some(msg),
128
132
  _ => None,
129
133
  }
@@ -22,9 +22,15 @@ dangerouslyRegisterBasePrinter : a -> Void
22
22
  dangerouslyRegisterPrinter : a -> Void
23
23
  ```
24
24
 
25
- ### Exception.**printException**
25
+ ### Exception.**panic**
26
26
 
27
27
  ```grain
28
- printException : Exception -> Void
28
+ panic : String -> a
29
+ ```
30
+
31
+ ### Exception.**panicWithException**
32
+
33
+ ```grain
34
+ panicWithException : Exception -> a
29
35
  ```
30
36
 
package/runtime/gc.gr CHANGED
@@ -213,7 +213,8 @@ let rec decRef = (userPtr: WasmI32, ignoreZeros: Bool) => {
213
213
  } else {
214
214
  userPtr
215
215
  }
216
- }, decRefChildren = (userPtr: WasmI32) => {
216
+ },
217
+ decRefChildren = (userPtr: WasmI32) => {
217
218
  match (WasmI32.load(userPtr, 0n)) {
218
219
  t when t == Tags._GRAIN_BOXED_NUM_HEAP_TAG => {
219
220
  let tag = WasmI32.load(userPtr, 4n)
package/runtime/malloc.gr CHANGED
@@ -28,8 +28,6 @@ primitive (!): Bool -> Bool = "@not"
28
28
  primitive (&&): (Bool, Bool) -> Bool = "@and"
29
29
  primitive (||): (Bool, Bool) -> Bool = "@or"
30
30
 
31
- primitive throw: Exception -> a = "@throw"
32
-
33
31
  primitive heapBase: WasmI32 = "@heap.base"
34
32
 
35
33
  /* UNDERSTANDING THE STRUCTURE OF THE FREE LIST
@@ -221,7 +219,7 @@ let morecore = (nbytes: WasmI32) => {
221
219
 
222
220
  // If there was an error, fail
223
221
  if (cp == -1n) {
224
- throw Exception.OutOfMemory
222
+ Exception.panic("OutOfMemory: Maximum memory size exceeded")
225
223
  } else {
226
224
  // Set the size of the new block to the amount the
227
225
  // heap was grown.
@@ -46,7 +46,7 @@ let _I32_MAX = 0xffffffffN
46
46
  let mut _POWERS10 = -1n
47
47
 
48
48
  @unsafe
49
- let get_POWERS10 = () => {
49
+ export let get_POWERS10 = () => {
50
50
  if (_POWERS10 == -1n) {
51
51
  _POWERS10 = Memory.malloc(40n)
52
52
  WasmI32.store(_POWERS10, 1n, 0n)
@@ -728,16 +728,16 @@ export let decimalCount32 = value => {
728
728
  1n + (if (WasmI32.geU(value, 10n)) 1n else 0n)
729
729
  } else {
730
730
  3n +
731
- (if (WasmI32.geU(value, 10000n)) 1n else 0n) +
732
- (if (WasmI32.geU(value, 1000n)) 1n else 0n)
731
+ (if (WasmI32.geU(value, 10000n)) 1n else 0n) +
732
+ (if (WasmI32.geU(value, 1000n)) 1n else 0n)
733
733
  }
734
734
  } else {
735
735
  if (WasmI32.ltU(value, 10000000n)) {
736
736
  6n + (if (WasmI32.geU(value, 1000000n)) 1n else 0n)
737
737
  } else {
738
738
  8n +
739
- (if (WasmI32.geU(value, 1000000000n)) 1n else 0n) +
740
- (if (WasmI32.geU(value, 100000000n)) 1n else 0n)
739
+ (if (WasmI32.geU(value, 1000000000n)) 1n else 0n) +
740
+ (if (WasmI32.geU(value, 100000000n)) 1n else 0n)
741
741
  }
742
742
  }
743
743
  }
@@ -749,20 +749,20 @@ let decimalCount64High = value => {
749
749
  if (WasmI64.ltU(value, 1000000000000000N)) {
750
750
  if (WasmI64.ltU(value, 1000000000000N)) {
751
751
  10n +
752
- (if (WasmI64.geU(value, 100000000000N)) 1n else 0n) +
753
- (if (WasmI64.geU(value, 10000000000N)) 1n else 0n)
752
+ (if (WasmI64.geU(value, 100000000000N)) 1n else 0n) +
753
+ (if (WasmI64.geU(value, 10000000000N)) 1n else 0n)
754
754
  } else {
755
755
  13n +
756
- (if (WasmI64.geU(value, 100000000000000N)) 1n else 0n) +
757
- (if (WasmI64.geU(value, 10000000000000N)) 1n else 0n)
756
+ (if (WasmI64.geU(value, 100000000000000N)) 1n else 0n) +
757
+ (if (WasmI64.geU(value, 10000000000000N)) 1n else 0n)
758
758
  }
759
759
  } else {
760
760
  if (WasmI64.ltU(value, 100000000000000000N)) {
761
761
  16n + (if (WasmI64.geU(value, 10000000000000000N)) 1n else 0n)
762
762
  } else {
763
763
  18n +
764
- (if (WasmI64.geU(value, 0x8AC7230489E80000N)) 1n else 0n) +
765
- (if (WasmI64.geU(value, 1000000000000000000N)) 1n else 0n)
764
+ (if (WasmI64.geU(value, 0x8AC7230489E80000N)) 1n else 0n) +
765
+ (if (WasmI64.geU(value, 1000000000000000000N)) 1n else 0n)
766
766
  }
767
767
  }
768
768
  }
@@ -774,7 +774,7 @@ let ulog_base = (num, base) => {
774
774
  63n - WasmI32.wrapI64(WasmI64.clz(num)),
775
775
  31n - WasmI32.clz(base)
776
776
  ) +
777
- 1n
777
+ 1n
778
778
  } else {
779
779
  let b64 = WasmI64.extendI32U(base)
780
780
  let mut b = b64
@@ -1277,7 +1277,7 @@ let genDigits = (buffer, w_frc, mp_frc, mp_exp, delta, sign) => {
1277
1277
  wp_w_frc = WasmI64.mul(
1278
1278
  wp_w_frc,
1279
1279
  WasmI64.extendI32U(
1280
- WasmI32.load(get_POWERS10() + (0n - kappa << 2n), 0n)
1280
+ WasmI32.load(get_POWERS10() + ((0n - kappa) << 2n), 0n)
1281
1281
  )
1282
1282
  )
1283
1283
  grisuRound(buffer, len, delta, p2, one_frc, wp_w_frc)
@@ -4,6 +4,12 @@
4
4
  _MAX_DOUBLE_LENGTH : WasmI32
5
5
  ```
6
6
 
7
+ ### NumberUtils.**get_POWERS10**
8
+
9
+ ```grain
10
+ get_POWERS10 : () -> WasmI32
11
+ ```
12
+
7
13
  ### NumberUtils.**get_HEX_DIGITS**
8
14
 
9
15
  ```grain