@grain/stdlib 0.4.6 → 0.5.2

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 (85) hide show
  1. package/CHANGELOG.md +93 -0
  2. package/array.gr +18 -18
  3. package/array.md +18 -18
  4. package/bigint.gr +497 -0
  5. package/bigint.md +811 -0
  6. package/buffer.gr +59 -223
  7. package/buffer.md +24 -17
  8. package/bytes.gr +100 -202
  9. package/bytes.md +19 -0
  10. package/char.gr +63 -133
  11. package/exception.gr +28 -2
  12. package/exception.md +43 -0
  13. package/float32.gr +76 -95
  14. package/float32.md +69 -30
  15. package/float64.gr +81 -95
  16. package/float64.md +69 -30
  17. package/hash.gr +37 -37
  18. package/int32.gr +152 -198
  19. package/int32.md +104 -0
  20. package/int64.gr +151 -197
  21. package/int64.md +104 -0
  22. package/list.gr +467 -70
  23. package/list.md +1141 -0
  24. package/map.gr +192 -7
  25. package/map.md +525 -0
  26. package/number.gr +111 -54
  27. package/number.md +100 -3
  28. package/option.md +1 -1
  29. package/package.json +3 -3
  30. package/pervasives.gr +499 -59
  31. package/pervasives.md +1116 -0
  32. package/queue.gr +4 -0
  33. package/queue.md +10 -0
  34. package/random.gr +196 -0
  35. package/random.md +179 -0
  36. package/regex.gr +1833 -842
  37. package/regex.md +11 -11
  38. package/result.md +1 -1
  39. package/runtime/bigint.gr +2045 -0
  40. package/runtime/bigint.md +326 -0
  41. package/runtime/dataStructures.gr +99 -278
  42. package/runtime/dataStructures.md +391 -0
  43. package/runtime/debug.md +6 -0
  44. package/runtime/equal.gr +5 -23
  45. package/runtime/equal.md +6 -0
  46. package/runtime/exception.md +30 -0
  47. package/runtime/gc.gr +20 -3
  48. package/runtime/gc.md +36 -0
  49. package/runtime/malloc.gr +13 -11
  50. package/runtime/malloc.md +55 -0
  51. package/runtime/numberUtils.gr +91 -41
  52. package/runtime/numberUtils.md +54 -0
  53. package/runtime/numbers.gr +1049 -391
  54. package/runtime/numbers.md +300 -0
  55. package/runtime/string.gr +136 -230
  56. package/runtime/string.md +24 -0
  57. package/runtime/stringUtils.gr +58 -38
  58. package/runtime/stringUtils.md +6 -0
  59. package/runtime/unsafe/constants.gr +17 -0
  60. package/runtime/unsafe/constants.md +72 -0
  61. package/runtime/unsafe/conv.md +71 -0
  62. package/runtime/unsafe/errors.md +204 -0
  63. package/runtime/unsafe/memory.md +54 -0
  64. package/runtime/unsafe/printWasm.md +24 -0
  65. package/runtime/unsafe/tags.gr +9 -8
  66. package/runtime/unsafe/tags.md +120 -0
  67. package/runtime/unsafe/wasmf32.md +168 -0
  68. package/runtime/unsafe/wasmf64.md +168 -0
  69. package/runtime/unsafe/wasmi32.md +282 -0
  70. package/runtime/unsafe/wasmi64.md +300 -0
  71. package/runtime/utils/printing.gr +62 -0
  72. package/runtime/utils/printing.md +18 -0
  73. package/runtime/wasi.gr +1 -1
  74. package/runtime/wasi.md +839 -0
  75. package/set.gr +17 -8
  76. package/set.md +24 -21
  77. package/stack.gr +3 -3
  78. package/stack.md +4 -6
  79. package/string.gr +194 -329
  80. package/string.md +3 -3
  81. package/sys/file.gr +245 -429
  82. package/sys/process.gr +27 -45
  83. package/sys/random.gr +47 -16
  84. package/sys/random.md +38 -0
  85. package/sys/time.gr +11 -27
package/pervasives.gr CHANGED
@@ -1,4 +1,10 @@
1
- export *
1
+ /**
2
+ * @module Pervasives: This module is automatically imported into every Grain program. You can think of it as the global environment. Although it is automatically imported, it can still be imported manually.
3
+ *
4
+ * @example import Pervasives from "pervasives"
5
+ *
6
+ * @since v0.1.0
7
+ */
2
8
 
3
9
  import Exception from "runtime/exception"
4
10
  import Memory from "runtime/unsafe/memory"
@@ -26,31 +32,380 @@ import {
26
32
  (>>>),
27
33
  (>>),
28
34
  } from "runtime/numbers"
35
+ import { toString, print, concat as (++) } from "runtime/string"
29
36
 
30
- // Math operations
31
- export incr
32
- export decr
37
+ /**
38
+ * @section Types: Type declarations included in the Pervasives module.
39
+ */
40
+
41
+ /**
42
+ * The type of Grain lists.
43
+ *
44
+ * @example [1, 2, 3]
45
+ * @example []
46
+ *
47
+ * @since v0.1.0
48
+ */
49
+ export enum List<a> {
50
+ [],
51
+ [...](a, List<a>),
52
+ }
53
+
54
+ /**
55
+ * Grain's type representing something that may or may not contain data.
56
+ * Think of this like a better, type-safe "null".
57
+ *
58
+ * @since v0.2.0
59
+ */
60
+ export enum Option<a> {
61
+ Some(a),
62
+ None,
63
+ }
64
+
65
+ /**
66
+ * Grain's type representing the result of something that might error.
67
+ *
68
+ * @since v0.2.0
69
+ */
70
+ export enum Result<t, e> {
71
+ Ok(t),
72
+ Err(e),
73
+ }
74
+
75
+ /**
76
+ * @section Boolean operations: Infix functions for working with Boolean values.
77
+ */
78
+
79
+ /**
80
+ * Computes the logical NOT (`!`) of the given operand.
81
+ * Inverts the given Boolean value.
82
+ *
83
+ * @param value: The operand
84
+ * @returns The inverted value
85
+ *
86
+ * @example !true // false
87
+ * @example !false // true
88
+ *
89
+ * @since v0.1.0
90
+ */
91
+ export primitive (!): Bool -> Bool = "@not"
92
+
93
+ /**
94
+ * Computes the logical AND (`&&`) of the given operands.
95
+ *
96
+ * If the first operand is `false`, returns `false` without evaluating the second operand.
97
+ * If the first operand is `true`, returns the value of the second operand.
98
+ *
99
+ * @param value1: The first operand
100
+ * @param value2: The second operand
101
+ * @returns The first operand if it is `false` or the value of the second operand otherwise
102
+ *
103
+ * @since v0.1.0
104
+ */
105
+ export primitive (&&): (Bool, Bool) -> Bool = "@and"
106
+
107
+ /**
108
+ * Computes the logical OR `||` of the given operands.
109
+ *
110
+ * If the first operand is `true`, returns `true` without evaluating the second operand.
111
+ * If the first operand is `false`, returns the value of the second operand.
112
+ *
113
+ * @param value1: The first operand
114
+ * @param value2: The second operand
115
+ * @returns The first operand if it is `true` or the value of the second operand otherwise
116
+ *
117
+ * @since v0.1.0
118
+ */
119
+ export primitive (||): (Bool, Bool) -> Bool = "@or"
120
+
121
+ /**
122
+ * @section Comparison operations: Infix functions for comparing values.
123
+ */
124
+
125
+ /**
126
+ * Check that two values are equal. This checks for structural equality,
127
+ * so it also works for comparing things like tuples and lists.
128
+ *
129
+ * @param value1: The first operand
130
+ * @param value2: The second operand
131
+ * @returns `true` if the values are structurally equal or `false` otherwise
132
+ *
133
+ * @since v0.1.0
134
+ */
135
+ export (==)
136
+
137
+ /**
138
+ * Check that two values are **not** equal. This checks for structural equality,
139
+ * so it also works for comparing things like tuples and lists.
140
+ *
141
+ * @param value1: The first operand
142
+ * @param value2: The second operand
143
+ * @returns `false` if the values are structurally equal or `true` otherwise
144
+ *
145
+ * @since v0.2.0
146
+ */
147
+ export let (!=): (a, a) -> Bool = (x, y) => !(x == y)
148
+
149
+ /**
150
+ * Checks that two values are physically equal.
151
+ * Use this operator if you don’t need or want structural equality.
152
+ *
153
+ * @param value1: The first operand
154
+ * @param value2: The second operand
155
+ * @returns `true` if the values are physically equal or `false` otherwise
156
+ *
157
+ * @since v0.1.0
158
+ */
159
+ export primitive (is): (a, a) -> Bool = "@is"
160
+
161
+ /**
162
+ * Checks that two values are **not** physically equal.
163
+ * Use this operator if you don’t need or want structural equality.
164
+ *
165
+ * @param value1: The first operand
166
+ * @param value2: The second operand
167
+ * @returns `false` if the values are physically equal or `true` otherwise
168
+ *
169
+ * @since v0.2.0
170
+ */
171
+ export let (isnt): (a, a) -> Bool = (x, y) => !(x is y)
172
+
173
+ /**
174
+ * @section Number comparisons: Infix functions for comparing Number values.
175
+ */
176
+
177
+ /**
178
+ * Checks if the first operand is less than the second operand.
179
+ *
180
+ * @param num1: The first operand
181
+ * @param num2: The second operand
182
+ * @returns `true` if the first operand is less than the second operand or `false` otherwise
183
+ *
184
+ * @since v0.1.0
185
+ */
186
+ export (<)
187
+
188
+ /**
189
+ * Checks if the first operand is greater than the second operand.
190
+ *
191
+ * @param num1: The first operand
192
+ * @param num2: The second operand
193
+ * @returns `true` if the first operand is greater than the second operand or `false` otherwise
194
+ *
195
+ * @since v0.1.0
196
+ */
197
+ export (>)
198
+
199
+ /**
200
+ * Checks if the first operand is less than or equal to the second operand.
201
+ *
202
+ * @param num1: The first operand
203
+ * @param num2: The second operand
204
+ * @returns `true` if the first operand is less than or equal to the second operand or `false` otherwise
205
+ *
206
+ * @since v0.1.0
207
+ */
208
+ export (<=)
209
+
210
+ /**
211
+ * Checks if the first operand is greater than or equal to the second operand.
212
+ *
213
+ * @param num1: The first operand
214
+ * @param num2: The second operand
215
+ * @returns `true` if the first operand is greater than or equal to the second operand or `false` otherwise
216
+ *
217
+ * @since v0.1.0
218
+ */
219
+ export (>=)
220
+
221
+ /**
222
+ * @section Math operations: Infix functions for working with Number values.
223
+ */
224
+
225
+ /**
226
+ * Computes the sum of its operands.
227
+ *
228
+ * @param num1: The first operand
229
+ * @param num2: The second operand
230
+ * @returns The sum of the two operands
231
+ *
232
+ * @since v0.1.0
233
+ */
33
234
  export (+)
235
+
236
+ /**
237
+ * Computes the difference of its operands.
238
+ *
239
+ * @param num1: The first operand
240
+ * @param num2: The second operand
241
+ * @returns The difference of the two operands
242
+ *
243
+ * @since v0.1.0
244
+ */
34
245
  export (-)
246
+
247
+ /**
248
+ * Computes the product of its operands.
249
+ *
250
+ * @param num1: The first operand
251
+ * @param num2: The second operand
252
+ * @returns The product of the two operands
253
+ *
254
+ * @since v0.1.0
255
+ */
35
256
  export (*)
257
+
258
+ /**
259
+ * Computes the quotient of its operands.
260
+ *
261
+ * @param num1: The first operand
262
+ * @param num2: The second operand
263
+ * @returns The quotient of the two operands
264
+ *
265
+ * @since v0.1.0
266
+ */
36
267
  export (/)
268
+
269
+ /**
270
+ * Computes the remainder of the division of the first operand by the second.
271
+ * The result will have the sign of the second operand.
272
+ *
273
+ * @param num1: The first operand
274
+ * @param num2: The second operand
275
+ * @returns The modulus of its operands
276
+ *
277
+ * @since v0.1.0
278
+ */
37
279
  export (%)
38
280
 
39
- // Number comparisons
40
- export (<)
41
- export (>)
42
- export (<=)
43
- export (>=)
281
+ /**
282
+ * Increments the value by one.
283
+ *
284
+ * @param value: The value to increment
285
+ * @returns The incremented value
286
+ *
287
+ * @since v0.1.0
288
+ */
289
+ export incr
290
+
291
+ /**
292
+ * Decrements the value by one.
293
+ *
294
+ * @param value: The value to decrement
295
+ * @returns The decremented value
296
+ *
297
+ * @since v0.1.0
298
+ */
299
+ export decr
300
+
301
+ /**
302
+ * @section String operations: Infix functions for operating on String values.
303
+ */
304
+
305
+ /**
306
+ * Concatenate two strings.
307
+ *
308
+ * @param str1: The beginning string
309
+ * @param str2: The ending string
310
+ * @returns The combined string
311
+ *
312
+ * @example "Foo" ++ "Bar" == "FooBar"
313
+ *
314
+ * @since v0.2.0
315
+ */
316
+ export (++)
44
317
 
45
- // Number bit/logical operations
318
+ /**
319
+ * @section Bitwise operations: Infix functions for operating on bits of Number values.
320
+ */
321
+
322
+ /**
323
+ * Computes the bitwise NOT of the operand.
324
+ *
325
+ * @param value: The operand
326
+ * @returns Containing the inverted bits of the operand
327
+ *
328
+ * @since v0.2.0
329
+ */
46
330
  export lnot
47
331
 
332
+ /**
333
+ * Computes the bitwise AND (`&`) on the given operands.
334
+ *
335
+ * @param value1: The first operand
336
+ * @param value2: The second operand
337
+ * @returns Containing a `1` in each bit position for which the corresponding bits of both operands are `1`
338
+ *
339
+ * @since v0.3.0
340
+ * @history v0.2.0: Originally named `land`
341
+ * @history v0.3.0: Renamed to `&`
342
+ */
48
343
  export (&)
344
+
345
+ /**
346
+ * Computes the bitwise OR (`|`) on the given operands.
347
+ *
348
+ * @param value1: The first operand
349
+ * @param value2: The second operand
350
+ * @returns Containing a `1` in each bit position for which the corresponding bits of either or both operands are `1`
351
+ *
352
+ * @since v0.3.0
353
+ * @history v0.2.0: Originally named `lor`
354
+ * @history v0.3.0: Renamed to `|`
355
+ */
49
356
  export (|)
357
+
358
+ /**
359
+ * Computes the bitwise XOR (`^`) on the given operands.
360
+ *
361
+ * @param value1: The first operand
362
+ * @param value2: The second operand
363
+ * @returns Containing a `1` in each bit position for which the corresponding bits of either but not both operands are `1`
364
+ *
365
+ * @since v0.3.0
366
+ * @history v0.1.0: The `^` operator was originally an alias of `unbox`
367
+ * @history v0.2.0: Originally named `lxor`
368
+ * @history v0.3.0: Renamed to `^`
369
+ */
50
370
  export (^)
51
371
 
372
+ /**
373
+ * Shifts the bits of the value left by the given number of bits.
374
+ *
375
+ * @param value: The value to shift
376
+ * @param amount: The number of bits to shift by
377
+ * @returns The shifted value
378
+ *
379
+ * @since v0.3.0
380
+ * @history v0.2.0: Originally named `lsl`
381
+ * @history v0.3.0: Renamed to `<<`
382
+ */
52
383
  export (<<)
384
+
385
+ /**
386
+ * Shifts the bits of the value right by the given number of bits, preserving the sign bit.
387
+ *
388
+ * @param value: The value to shift
389
+ * @param amount: The amount to shift by
390
+ * @returns The shifted value
391
+ *
392
+ * @since v0.3.0
393
+ * @history v0.2.0: Originally named `lsr`
394
+ * @history v0.3.0: Renamed to `>>>`
395
+ */
53
396
  export (>>>)
397
+
398
+ /**
399
+ * Shifts the bits of the value right by the given number of bits.
400
+ *
401
+ * @param value: The value to shift
402
+ * @param amount: The amount to shift by
403
+ * @returns The shifted value
404
+ *
405
+ * @since v0.3.0
406
+ * @history v0.2.0: Originally named `asr`
407
+ * @history v0.3.0: Renamed to `>>`
408
+ */
54
409
  export (>>)
55
410
 
56
411
  // Number coercions & conversions
@@ -59,72 +414,157 @@ export (>>)
59
414
  // import foreign wasm convertExactToInexact : Number -> Number as inexact from "stdlib-external/runtime"
60
415
  // import foreign wasm convertInexactToExact : Number -> Number as exact from "stdlib-external/runtime"
61
416
 
62
- // Boolean operations
63
- primitive (!): Bool -> Bool = "@not"
64
- primitive (&&): (Bool, Bool) -> Bool = "@and"
65
- primitive (||): (Bool, Bool) -> Bool = "@or"
417
+ /**
418
+ * @section Printing: Functions that deal with printing.
419
+ */
420
+
421
+ /**
422
+ * Converts the given operand to a string.
423
+ * Provides a better representation of data types if those types are exported from the module.
424
+ *
425
+ * @param value: The operand
426
+ * @returns The operand, as a string
427
+ *
428
+ * @since v0.1.0
429
+ */
430
+ export toString
431
+
432
+ /**
433
+ * Prints the given operand to the console. Works for any type. Internally, calls `toString`
434
+ * on the operand, so a better representation of data type will be printed if those types
435
+ * are exported from the module.
436
+ *
437
+ * @param value: The operand
438
+ *
439
+ * @since v0.1.0
440
+ */
441
+ export print
442
+
443
+ /**
444
+ * @section Type helpers: Functions that help with typechecking.
445
+ */
446
+
447
+ /**
448
+ * Accepts any value and always returns `void`.
449
+ *
450
+ * @param value: The value to ignore
451
+ *
452
+ * @since v0.1.0
453
+ */
454
+ export primitive ignore: a -> Void = "@ignore"
455
+
456
+ /**
457
+ * @section Assertions: Functions that raise if conditions are not met.
458
+ */
459
+
460
+ /**
461
+ * Assert that the given Boolean condition is `true`.
462
+ * Throws an `AssertionError` if the condition is `false`.
463
+ *
464
+ * @param condition: The condition to assert
465
+ *
466
+ * @example assert 3 > 2
467
+ * @example assert true
468
+ *
469
+ * @since v0.1.0
470
+ */
471
+ export primitive assert: Bool -> Void = "@assert"
66
472
 
67
- // Box operations
68
- primitive box: a -> Box<a> = "@box"
69
- primitive unbox: Box<a> -> a = "@unbox"
473
+ /**
474
+ * @section Failures: Functions that throw an Exception unconditionally.
475
+ */
70
476
 
71
477
  // Exceptions
72
- exception Failure(String)
73
- exception InvalidArgument(String)
478
+ export exception Failure(String)
479
+ export exception InvalidArgument(String)
74
480
 
75
- // Other operations
76
- primitive ignore: a -> Void = "@ignore"
77
- primitive assert: Bool -> Void = "@assert"
78
- primitive throw: Exception -> a = "@throw"
79
- let fail: String -> a = msg => throw Failure(msg)
481
+ /**
482
+ * Throw an exception. Currently, exceptions cannot be caught and will crash your program.
483
+ *
484
+ * @param exception: The exception to be thrown
485
+ * @returns Anything and nothing—your program won't continue past a throw
486
+ *
487
+ * @since v0.3.0
488
+ */
489
+ export primitive throw: Exception -> a = "@throw"
80
490
 
81
- import { toString, print, concat as (++) } from "runtime/string"
491
+ /**
492
+ * Unconditionally throw a `Failure` exception with a message.
493
+ * Currently, Exceptions cannot be caught and will crash your program.
494
+ *
495
+ * @param message: The reason for the failure
496
+ * @returns Anything and nothing—your program won't continue past a fail expression
497
+ */
498
+ export let fail: String -> a = msg => throw Failure(msg)
82
499
 
83
- // Converts the given value to a string
84
- export toString
85
- // Prints the given value to the console.
86
- export print
500
+ /**
501
+ * @section Other: Other functions on values.
502
+ */
87
503
 
88
- // String operations
89
- export (++)
504
+ /**
505
+ * Provides the operand untouched.
506
+ *
507
+ * @param value: The value to return
508
+ * @returns The value untouched
509
+ *
510
+ * @since v0.2.0
511
+ */
512
+ export let identity = x => x
90
513
 
91
- // Checks the given items for structural equality.
92
- export (==)
93
- let (!=): (a, a) -> Bool = (x, y) => !(x == y)
94
- // Checks the given items for physical equality.
95
- primitive (is): (a, a) -> Bool = "@is"
96
- // The opposite of is operator
97
- let (isnt): (a, a) -> Bool = (x, y) => !(x is y)
514
+ /**
515
+ * @section Box operations: Functions for working with Box values.
516
+ */
98
517
 
99
- export enum List<a> {
100
- [],
101
- [...](a, List<a>),
102
- }
518
+ /**
519
+ * Creates a box containing the given initial value.
520
+ * Values inside a box can be swapped out with the `:=` operator.
521
+ * Generally, `let mut` expressions are preferable to using a Box.
522
+ *
523
+ * @param initial: The initial value inside the box
524
+ * @returns The box containing the initial value
525
+ *
526
+ * @since v0.1.0
527
+ */
528
+ export primitive box: a -> Box<a> = "@box"
529
+
530
+ /**
531
+ * Retrieves the current value from a box.
532
+ *
533
+ * @param box: The box to unwrap
534
+ * @returns The value inside the box
535
+ *
536
+ * @since v0.1.0
537
+ */
538
+ export primitive unbox: Box<a> -> a = "@unbox"
539
+
540
+ /**
541
+ * @section List operations: Functions for working with List values.
542
+ */
103
543
 
104
544
  /**
545
+ * The list spread syntax (`[a, ...b]`) provided as a function.
546
+ *
547
+ * @param a: The head of the list
548
+ * @param b: The tail of the list
549
+ * @returns The new list
550
+ *
105
551
  * @deprecated This will be removed in a future release of Grain.
552
+ *
553
+ * @since v0.4.0
106
554
  */
107
- let cons = (a, b) =>
555
+ export let cons = (a, b) =>
108
556
  [
109
557
  a,
110
558
  ...b
111
559
  ] // <- workaround for (grain-lang/grain#802) [TODO] fix #802 and delete
112
- let empty = [] // <- for parity with `cons`, but should be deleted as well
113
-
114
- // Maybe some data, maybe not!
115
- enum Option<a> {
116
- Some(a),
117
- None,
118
- }
119
-
120
- // Maybe some data, maybe an error!
121
- enum Result<t, e> {
122
- Ok(t),
123
- Err(e),
124
- }
125
-
126
- // Identity function
127
- let identity = x => x
560
+ /**
561
+ * The empty list syntax (`[]`) provided as a value.
562
+ *
563
+ * @deprecated This will be removed in a future release of Grain.
564
+ *
565
+ * @since v0.4.0
566
+ */
567
+ export let empty = [] // <- for parity with `cons`, but should be deleted as well
128
568
 
129
569
  // Setup exception printing
130
570
  @disableGC