@grain/stdlib 0.5.13 → 0.6.1

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 (155) hide show
  1. package/CHANGELOG.md +201 -0
  2. package/LICENSE +1 -1
  3. package/README.md +25 -2
  4. package/array.gr +1512 -199
  5. package/array.md +2032 -94
  6. package/bigint.gr +239 -140
  7. package/bigint.md +450 -106
  8. package/buffer.gr +595 -102
  9. package/buffer.md +903 -145
  10. package/bytes.gr +401 -110
  11. package/bytes.md +551 -63
  12. package/char.gr +228 -49
  13. package/char.md +373 -7
  14. package/exception.gr +26 -12
  15. package/exception.md +29 -5
  16. package/float32.gr +130 -109
  17. package/float32.md +185 -57
  18. package/float64.gr +112 -99
  19. package/float64.md +185 -57
  20. package/hash.gr +62 -40
  21. package/hash.md +27 -3
  22. package/int16.gr +430 -0
  23. package/int16.md +618 -0
  24. package/int32.gr +200 -269
  25. package/int32.md +254 -289
  26. package/int64.gr +142 -225
  27. package/int64.md +254 -289
  28. package/int8.gr +511 -0
  29. package/int8.md +786 -0
  30. package/json.gr +2071 -0
  31. package/json.md +646 -0
  32. package/list.gr +120 -68
  33. package/list.md +125 -80
  34. package/map.gr +560 -57
  35. package/map.md +672 -56
  36. package/marshal.gr +239 -227
  37. package/marshal.md +36 -4
  38. package/number.gr +626 -676
  39. package/number.md +738 -153
  40. package/option.gr +33 -35
  41. package/option.md +58 -42
  42. package/package.json +2 -2
  43. package/path.gr +148 -187
  44. package/path.md +47 -96
  45. package/pervasives.gr +75 -416
  46. package/pervasives.md +85 -180
  47. package/priorityqueue.gr +433 -74
  48. package/priorityqueue.md +422 -54
  49. package/queue.gr +362 -80
  50. package/queue.md +433 -38
  51. package/random.gr +67 -75
  52. package/random.md +68 -40
  53. package/range.gr +135 -63
  54. package/range.md +198 -43
  55. package/rational.gr +284 -0
  56. package/rational.md +545 -0
  57. package/regex.gr +933 -1066
  58. package/regex.md +59 -60
  59. package/result.gr +23 -25
  60. package/result.md +54 -39
  61. package/runtime/atof/common.gr +78 -82
  62. package/runtime/atof/common.md +22 -10
  63. package/runtime/atof/decimal.gr +102 -127
  64. package/runtime/atof/decimal.md +28 -7
  65. package/runtime/atof/lemire.gr +56 -71
  66. package/runtime/atof/lemire.md +9 -1
  67. package/runtime/atof/parse.gr +83 -110
  68. package/runtime/atof/parse.md +12 -2
  69. package/runtime/atof/slow.gr +28 -35
  70. package/runtime/atof/slow.md +9 -1
  71. package/runtime/atof/table.gr +19 -18
  72. package/runtime/atof/table.md +10 -2
  73. package/runtime/atoi/parse.gr +153 -136
  74. package/runtime/atoi/parse.md +50 -1
  75. package/runtime/bigint.gr +410 -517
  76. package/runtime/bigint.md +71 -57
  77. package/runtime/compare.gr +176 -85
  78. package/runtime/compare.md +31 -1
  79. package/runtime/dataStructures.gr +144 -32
  80. package/runtime/dataStructures.md +267 -31
  81. package/runtime/debugPrint.gr +34 -15
  82. package/runtime/debugPrint.md +37 -5
  83. package/runtime/equal.gr +53 -52
  84. package/runtime/equal.md +30 -1
  85. package/runtime/exception.gr +38 -47
  86. package/runtime/exception.md +10 -8
  87. package/runtime/gc.gr +23 -152
  88. package/runtime/gc.md +13 -17
  89. package/runtime/malloc.gr +31 -31
  90. package/runtime/malloc.md +11 -3
  91. package/runtime/numberUtils.gr +193 -174
  92. package/runtime/numberUtils.md +29 -9
  93. package/runtime/numbers.gr +1695 -1021
  94. package/runtime/numbers.md +1098 -134
  95. package/runtime/string.gr +543 -245
  96. package/runtime/string.md +76 -6
  97. package/runtime/unsafe/constants.gr +30 -13
  98. package/runtime/unsafe/constants.md +80 -0
  99. package/runtime/unsafe/conv.gr +55 -28
  100. package/runtime/unsafe/conv.md +41 -9
  101. package/runtime/unsafe/memory.gr +10 -30
  102. package/runtime/unsafe/memory.md +15 -19
  103. package/runtime/unsafe/tags.gr +37 -21
  104. package/runtime/unsafe/tags.md +88 -8
  105. package/runtime/unsafe/wasmf32.gr +30 -36
  106. package/runtime/unsafe/wasmf32.md +64 -56
  107. package/runtime/unsafe/wasmf64.gr +30 -36
  108. package/runtime/unsafe/wasmf64.md +64 -56
  109. package/runtime/unsafe/wasmi32.gr +49 -66
  110. package/runtime/unsafe/wasmi32.md +102 -94
  111. package/runtime/unsafe/wasmi64.gr +52 -79
  112. package/runtime/unsafe/wasmi64.md +108 -100
  113. package/runtime/utils/printing.gr +13 -15
  114. package/runtime/utils/printing.md +11 -3
  115. package/runtime/wasi.gr +294 -295
  116. package/runtime/wasi.md +62 -42
  117. package/set.gr +574 -64
  118. package/set.md +634 -54
  119. package/stack.gr +181 -64
  120. package/stack.md +271 -42
  121. package/string.gr +453 -533
  122. package/string.md +241 -151
  123. package/uint16.gr +369 -0
  124. package/uint16.md +585 -0
  125. package/uint32.gr +470 -0
  126. package/uint32.md +737 -0
  127. package/uint64.gr +471 -0
  128. package/uint64.md +737 -0
  129. package/uint8.gr +369 -0
  130. package/uint8.md +585 -0
  131. package/uri.gr +1093 -0
  132. package/uri.md +477 -0
  133. package/{sys → wasi}/file.gr +914 -500
  134. package/{sys → wasi}/file.md +454 -50
  135. package/wasi/process.gr +292 -0
  136. package/{sys → wasi}/process.md +164 -6
  137. package/wasi/random.gr +77 -0
  138. package/wasi/random.md +80 -0
  139. package/{sys → wasi}/time.gr +15 -22
  140. package/{sys → wasi}/time.md +5 -5
  141. package/immutablearray.gr +0 -929
  142. package/immutablearray.md +0 -1038
  143. package/immutablemap.gr +0 -493
  144. package/immutablemap.md +0 -479
  145. package/immutablepriorityqueue.gr +0 -360
  146. package/immutablepriorityqueue.md +0 -291
  147. package/immutableset.gr +0 -498
  148. package/immutableset.md +0 -449
  149. package/runtime/debug.gr +0 -2
  150. package/runtime/debug.md +0 -6
  151. package/runtime/unsafe/errors.gr +0 -36
  152. package/runtime/unsafe/errors.md +0 -204
  153. package/sys/process.gr +0 -254
  154. package/sys/random.gr +0 -79
  155. package/sys/random.md +0 -66
package/float32.gr CHANGED
@@ -1,95 +1,72 @@
1
1
  /**
2
- * @module Float32: Utilities for working with the Float32 type.
3
- * @example import Float32 from "float32"
4
- *
2
+ * Utilities for working with the Float32 type.
3
+ *
4
+ * @example from "float32" include Float32
5
+ *
6
+ * @example 4.0f
7
+ * @example -4.0f
8
+ * @example Infinityf
9
+ * @example NaNf
10
+ *
5
11
  * @since v0.2.0
6
12
  */
7
- import WasmI32 from "runtime/unsafe/wasmi32"
8
- import WasmF32 from "runtime/unsafe/wasmf32"
9
- import { newFloat32 } from "runtime/dataStructures"
13
+ module Float32
14
+
15
+ from "runtime/unsafe/wasmi32" include WasmI32
16
+ from "runtime/unsafe/wasmf32" include WasmF32
17
+ use WasmF32.{ (+), (-), (*), (/), (<), (<=), (>), (>=) }
18
+ from "runtime/dataStructures" include DataStructures
19
+ use DataStructures.{ newFloat32 }
10
20
 
11
- import {
21
+ from "runtime/numbers" include Numbers
22
+ use Numbers.{
12
23
  coerceNumberToFloat32 as fromNumber,
13
24
  coerceFloat32ToNumber as toNumber,
14
- } from "runtime/numbers"
25
+ }
15
26
 
16
- /**
17
- * @section Constants: Float32 constant values.
18
- */
27
+ @unsafe
28
+ let _VALUE_OFFSET = 4n
19
29
 
20
30
  /**
21
31
  * Infinity represented as a Float32 value.
22
- *
32
+ * This is an alternative to the `Infinityf` literal.
33
+ *
23
34
  * @since v0.4.0
24
35
  */
25
36
  @unsafe
26
- export let infinity = {
27
- let ptr = newFloat32(
28
- WasmF32.reinterpretI32(0b01111111100000000000000000000000n)
29
- )
30
- WasmI32.toGrain(ptr): Float32
31
- }
37
+ provide let infinity = Infinityf
32
38
 
33
39
  /**
34
40
  * NaN (Not a Number) represented as a Float32 value.
35
- *
41
+ * This is an alternative to the `NaNf` literal.
42
+ *
36
43
  * @since v0.4.0
37
44
  */
38
45
  @unsafe
39
- export let nan = {
40
- let ptr = newFloat32(
41
- WasmF32.reinterpretI32(0b01111111100000000000000000000001n)
42
- )
43
- WasmI32.toGrain(ptr): Float32
44
- }
46
+ provide let nan = NaNf
45
47
 
46
48
  /**
47
49
  * Pi represented as a Float32 value.
48
- *
50
+ *
49
51
  * @since v0.5.2
50
52
  */
51
- export let pi = 3.1415927f
53
+ provide let pi = 3.1415927f
52
54
 
53
55
  /**
54
56
  * Tau represented as a Float32 value.
55
- *
57
+ *
56
58
  * @since v0.5.2
57
59
  */
58
- export let tau = 6.2831853f
60
+ provide let tau = 6.2831853f
59
61
 
60
62
  /**
61
63
  * Euler's number represented as a Float32 value.
62
- *
63
- * @since v0.5.2
64
- */
65
- export let e = 2.7182817f
66
- /**
67
- * @section Conversions: Functions for converting between Numbers and the Float32 type.
68
- */
69
-
70
- /**
71
- * Converts a Number to a Float32.
72
64
  *
73
- * @param number: The value to convert
74
- * @returns The Number represented as a Float32
75
- *
76
- * @since v0.2.0
77
- */
78
- export fromNumber
79
-
80
- /**
81
- * Converts a Float32 to a Number.
82
- *
83
- * @param float: The value to convert
84
- * @returns The Float32 represented as a Number
85
- *
86
- * @since v0.2.0
65
+ * @since v0.5.2
87
66
  */
88
- export toNumber
67
+ provide let e = 2.7182817f
89
68
 
90
- /**
91
- * @section Operations: Mathematical operations for Float32 values.
92
- */
69
+ provide { fromNumber, toNumber }
93
70
 
94
71
  /**
95
72
  * Computes the sum of its operands.
@@ -97,14 +74,19 @@ export toNumber
97
74
  * @param x: The first operand
98
75
  * @param y: The second operand
99
76
  * @returns The sum of the two operands
100
- *
101
- * @since v0.2.0
77
+ *
78
+ * @example
79
+ * use Float32.{ (+) }
80
+ * assert 1.0f + 1.0f == 2.0f
81
+ *
82
+ * @since v0.6.0
83
+ * @history v0.2.0: Originally named `add`
102
84
  */
103
85
  @unsafe
104
- export let add = (x: Float32, y: Float32) => {
105
- let xv = WasmF32.load(WasmI32.fromGrain(x), 8n)
106
- let yv = WasmF32.load(WasmI32.fromGrain(y), 8n)
107
- let ptr = newFloat32(WasmF32.add(xv, yv))
86
+ provide let (+) = (x: Float32, y: Float32) => {
87
+ let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
88
+ let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
89
+ let ptr = newFloat32(xv + yv)
108
90
  WasmI32.toGrain(ptr): Float32
109
91
  }
110
92
 
@@ -114,14 +96,19 @@ export let add = (x: Float32, y: Float32) => {
114
96
  * @param x: The first operand
115
97
  * @param y: The second operand
116
98
  * @returns The difference of the two operands
117
- *
118
- * @since v0.2.0
99
+ *
100
+ * @example
101
+ * use Float32.{ (-) }
102
+ * assert 1.0f - 1.0f == 0.0f
103
+ *
104
+ * @since v0.6.0
105
+ * @history v0.2.0: Originally named `sub`
119
106
  */
120
107
  @unsafe
121
- export let sub = (x: Float32, y: Float32) => {
122
- let xv = WasmF32.load(WasmI32.fromGrain(x), 8n)
123
- let yv = WasmF32.load(WasmI32.fromGrain(y), 8n)
124
- let ptr = newFloat32(WasmF32.sub(xv, yv))
108
+ provide let (-) = (x: Float32, y: Float32) => {
109
+ let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
110
+ let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
111
+ let ptr = newFloat32(xv - yv)
125
112
  WasmI32.toGrain(ptr): Float32
126
113
  }
127
114
 
@@ -131,14 +118,19 @@ export let sub = (x: Float32, y: Float32) => {
131
118
  * @param x: The first operand
132
119
  * @param y: The second operand
133
120
  * @returns The product of the two operands
134
- *
135
- * @since v0.2.0
121
+ *
122
+ * @example
123
+ * use Float32.{ (*) }
124
+ * assert 2.0f * 2.0f == 4.0f
125
+ *
126
+ * @since v0.6.0
127
+ * @history v0.2.0: Originally named `mul`
136
128
  */
137
129
  @unsafe
138
- export let mul = (x: Float32, y: Float32) => {
139
- let xv = WasmF32.load(WasmI32.fromGrain(x), 8n)
140
- let yv = WasmF32.load(WasmI32.fromGrain(y), 8n)
141
- let ptr = newFloat32(WasmF32.mul(xv, yv))
130
+ provide let (*) = (x: Float32, y: Float32) => {
131
+ let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
132
+ let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
133
+ let ptr = newFloat32(xv * yv)
142
134
  WasmI32.toGrain(ptr): Float32
143
135
  }
144
136
 
@@ -148,35 +140,41 @@ export let mul = (x: Float32, y: Float32) => {
148
140
  * @param x: The first operand
149
141
  * @param y: The second operand
150
142
  * @returns The quotient of the two operands
151
- *
152
- * @since v0.2.0
143
+ *
144
+ * @example
145
+ * use Float32.{ (/) }
146
+ * assert 10.0f / 4.0f == 2.5f
147
+ *
148
+ * @since v0.6.0
149
+ * @history v0.2.0: Originally named `div`
153
150
  */
154
151
  @unsafe
155
- export let div = (x: Float32, y: Float32) => {
156
- let xv = WasmF32.load(WasmI32.fromGrain(x), 8n)
157
- let yv = WasmF32.load(WasmI32.fromGrain(y), 8n)
158
- let ptr = newFloat32(WasmF32.div(xv, yv))
152
+ provide let (/) = (x: Float32, y: Float32) => {
153
+ let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
154
+ let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
155
+ let ptr = newFloat32(xv / yv)
159
156
  WasmI32.toGrain(ptr): Float32
160
157
  }
161
158
 
162
- /**
163
- * @section Comparisons: Functions for comparing Float32 values.
164
- */
165
-
166
159
  /**
167
160
  * Checks if the first value is less than the second value.
168
161
  *
169
162
  * @param x: The first value
170
163
  * @param y: The second value
171
164
  * @returns `true` if the first value is less than the second value or `false` otherwise
172
- *
173
- * @since v0.2.0
165
+ *
166
+ * @example
167
+ * use Float32.{ (<) }
168
+ * assert 1.0f < 2.0f
169
+ *
170
+ * @since v0.6.0
171
+ * @history v0.2.0: Originally named `lt`
174
172
  */
175
173
  @unsafe
176
- export let lt = (x: Float32, y: Float32) => {
177
- let xv = WasmF32.load(WasmI32.fromGrain(x), 8n)
178
- let yv = WasmF32.load(WasmI32.fromGrain(y), 8n)
179
- WasmF32.lt(xv, yv)
174
+ provide let (<) = (x: Float32, y: Float32) => {
175
+ let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
176
+ let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
177
+ xv < yv
180
178
  }
181
179
 
182
180
  /**
@@ -185,14 +183,19 @@ export let lt = (x: Float32, y: Float32) => {
185
183
  * @param x: The first value
186
184
  * @param y: The second value
187
185
  * @returns `true` if the first value is greater than the second value or `false` otherwise
188
- *
189
- * @since v0.2.0
186
+ *
187
+ * @example
188
+ * use Float32.{ (>) }
189
+ * assert 2.0f > 1.0f
190
+ *
191
+ * @since v0.6.0
192
+ * @history v0.2.0: Originally named `gt`
190
193
  */
191
194
  @unsafe
192
- export let gt = (x: Float32, y: Float32) => {
193
- let xv = WasmF32.load(WasmI32.fromGrain(x), 8n)
194
- let yv = WasmF32.load(WasmI32.fromGrain(y), 8n)
195
- WasmF32.gt(xv, yv)
195
+ provide let (>) = (x: Float32, y: Float32) => {
196
+ let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
197
+ let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
198
+ xv > yv
196
199
  }
197
200
 
198
201
  /**
@@ -201,14 +204,23 @@ export let gt = (x: Float32, y: Float32) => {
201
204
  * @param x: The first value
202
205
  * @param y: The second value
203
206
  * @returns `true` if the first value is less than or equal to the second value or `false` otherwise
204
- *
205
- * @since v0.2.0
207
+ *
208
+ * @example
209
+ * use Float32.{ (<=) }
210
+ * assert -1.0f <= 1.0f
211
+ *
212
+ * @example
213
+ * use Float32.{ (<=) }
214
+ * assert -2.0f <= -2.0f
215
+ *
216
+ * @since v0.6.0
217
+ * @history v0.2.0: Originally named `lte`
206
218
  */
207
219
  @unsafe
208
- export let lte = (x: Float32, y: Float32) => {
209
- let xv = WasmF32.load(WasmI32.fromGrain(x), 8n)
210
- let yv = WasmF32.load(WasmI32.fromGrain(y), 8n)
211
- WasmF32.le(xv, yv)
220
+ provide let (<=) = (x: Float32, y: Float32) => {
221
+ let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
222
+ let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
223
+ xv <= yv
212
224
  }
213
225
 
214
226
  /**
@@ -217,12 +229,21 @@ export let lte = (x: Float32, y: Float32) => {
217
229
  * @param x: The first value
218
230
  * @param y: The second value
219
231
  * @returns `true` if the first value is greater than or equal to the second value or `false` otherwise
220
- *
221
- * @since v0.2.0
232
+ *
233
+ * @example
234
+ * use Float32.{ (>=) }
235
+ * assert 4.0f >= 1.0f
236
+ *
237
+ * @example
238
+ * use Float32.{ (>=) }
239
+ * assert 3.0f >= 3.0f
240
+ *
241
+ * @since v0.6.0
242
+ * @history v0.2.0: Originally named `gte`
222
243
  */
223
244
  @unsafe
224
- export let gte = (x: Float32, y: Float32) => {
225
- let xv = WasmF32.load(WasmI32.fromGrain(x), 8n)
226
- let yv = WasmF32.load(WasmI32.fromGrain(y), 8n)
227
- WasmF32.ge(xv, yv)
245
+ provide let (>=) = (x: Float32, y: Float32) => {
246
+ let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
247
+ let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
248
+ xv >= yv
228
249
  }