@fraczak/k 8.7.0 → 8.8.0

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 (73) hide show
  1. package/CHANGELOG.md +40 -8
  2. package/DOCS/CODECS.md +42 -40
  3. package/DOCS/FILE_FORMATS.md +41 -64
  4. package/DOCS/GETTING_STARTED.md +10 -17
  5. package/DOCS/PATTERNS.md +3 -10
  6. package/DOCS/REPL.md +5 -9
  7. package/Examples/ieee-arithmetics.k +14 -29
  8. package/Examples/poly.k +18 -29
  9. package/README.md +14 -18
  10. package/backend-api.mjs +1 -3
  11. package/backends/llvm/src/cli.mjs +7 -11
  12. package/backends/llvm/tests/integration.sh +2 -4
  13. package/backends/wasm/src/wasm.mjs +2 -6
  14. package/backends/wasm/tests/integration.sh +17 -7
  15. package/browser/shims/fs.mjs +2 -6
  16. package/canonical-pattern.mjs +9 -32
  17. package/compiler.mjs +36 -0
  18. package/harness/suites/poly.mjs +9 -11
  19. package/index.mjs +140 -6
  20. package/k.mjs +4 -12
  21. package/kir.mjs +3 -1
  22. package/kvm.mjs +4 -8
  23. package/lib/djson.k +981 -294
  24. package/lib/generic.k +701 -0
  25. package/lib/ieee.k +662 -638
  26. package/lib/integer.k +542 -0
  27. package/named-patterns.mjs +0 -12
  28. package/object.mjs +22 -32
  29. package/objects/README.md +6 -7
  30. package/objects/compile.mjs +5 -10
  31. package/package.json +1 -1
  32. package/repl.mjs +4 -7
  33. package/scripts/benchmark-idempotency.mjs +16 -17
  34. package/scripts/benchmark.mjs +39 -29
  35. package/scripts/build-repl-html.mjs +45 -35
  36. package/scripts/perf-ieee.mjs +1 -5
  37. package/scripts/perf-int.mjs +12 -27
  38. package/scripts/perf-multiplication.mjs +8 -9
  39. package/scripts/perf-poly.mjs +7 -10
  40. package/scripts/render-benchmark.mjs +1 -1
  41. package/scripts/run-tests.mjs +5 -2
  42. package/scripts/test-package.mjs +6 -7
  43. package/tests/README.md +4 -5
  44. package/tests/helpers/cli.mjs +31 -0
  45. package/tests/integration.sh +17 -7
  46. package/tests/test-arithmetics.mjs +23 -16
  47. package/tests/test-djson-methods.mjs +19 -22
  48. package/tests/test-djson.mjs +273 -0
  49. package/tests/test-generic.mjs +239 -0
  50. package/tests/test-ieee-arithmetic.mjs +1 -5
  51. package/tests/test-ieee.mjs +153 -0
  52. package/tests/test-integer.mjs +264 -0
  53. package/tests/test-k-object.mjs +11 -14
  54. package/tests/test-kvm-polymorphic.mjs +7 -4
  55. package/tests/test-lib.mjs +62 -50
  56. package/tests/test-library-isolation.mjs +704 -0
  57. package/tests/test-named-patterns.mjs +13 -4
  58. package/tests/test-parse-json.mjs +20 -19
  59. package/tests/test-poly.mjs +21 -16
  60. package/tests/test-repl-html.mjs +39 -44
  61. package/tests/test-repl.mjs +46 -59
  62. package/DOCS/book-combined.md +0 -3786
  63. package/DOCS/dependency-graph.md +0 -144
  64. package/Examples/arithmetics.k +0 -108
  65. package/Examples/esc.k +0 -9
  66. package/lib/arithmetics.k +0 -287
  67. package/lib/bit.k +0 -68
  68. package/lib/bits.k +0 -221
  69. package/lib/btrie.k +0 -187
  70. package/lib/list.k +0 -49
  71. package/lib/string.k +0 -588
  72. package/tests/test-ieee-text.mjs +0 -70
  73. package/tests/test-karatsuba.mjs +0 -25
package/lib/string.k DELETED
@@ -1,588 +0,0 @@
1
- # --lib lib/bit.k --use eq_bit?
2
- # --lib lib/bits.k --use byte[] push_bits1 push_bits2 push_bits3 push_bits4 push_bits7 push_bits8 push_bits11 read_bit field7 trim inv unpack16 small1 small2 small3 small4 small7 inc lt? plus minus times16
3
- # --lib lib/list.k --use reverse list_length
4
- # --lib lib/btrie.k --use btrie[V] step_bits1 step_bits2 step_bits3 step_bits4 step_bits7 step_bits8 step_bits11 put_nth remove_nth
5
-
6
- -- lib/string.k — ASCII, Unicode code points, and character strings (string[])
7
-
8
- ---------------------------------------------------------------
9
- -- §1. Base Patterns & Primitives
10
- ---------------------------------------------------------------
11
-
12
- bit[] = < {} 0, {} 1 >;
13
- bits[] = < {} _, B 0, B 1 > = B;
14
- list[X] = < {} nil, { X car, L cdr } cons > = L;
15
- opt[V] = < {} nil, V val >;
16
-
17
- 0 = {} |0 ?bit[];
18
- 1 = {} |1 ?bit[];
19
- is0 = ?bit[] /0;
20
-
21
- _ = {} |_ ?bits[];
22
- 0b = |0 ?bits[];
23
- 1b = |1 ?bits[];
24
- un_ = ?bits[] /_;
25
- un0 = ?bits[] /0;
26
- un1 = ?bits[] /1;
27
-
28
- nil = {} |nil ?list[X];
29
- cons = |cons ?list[X];
30
- un_nil = ?list[X] /nil;
31
- car = ?list[X] /cons .car;
32
- cdr = ?list[X] /cons .cdr;
33
-
34
- un_nil_opt = ?opt[V] /nil;
35
- un_val = ?opt[V] /val;
36
- un_nil_trie = ?btrie[V] /nil;
37
- trie_val = ?btrie[V] /trie .val;
38
- step_0 = ?btrie[V] /trie .0;
39
- step_1 = ?btrie[V] /trie .1;
40
-
41
- ---------------------------------------------------------------
42
- -- §2. Patterns & Primitive Constructors/Destructors
43
- ---------------------------------------------------------------
44
-
45
- ascii[] = {
46
- bit[] 0, bit[] 1, bit[] 2, bit[] 3,
47
- bit[] 4, bit[] 5, bit[] 6
48
- };
49
-
50
- bmp_hi_common[] = <
51
- { bit[] 0, bit[] 1, bit[] 2 } h08_0F,
52
- ascii[] h10_7F,
53
- ascii[] h80_CF,
54
- { bit[] 0, bit[] 1, bit[] 2 } hD0_D7,
55
- {} hF9,
56
- { bit[] 0 } hFA_FB,
57
- { bit[] 0 } hFC_FD,
58
- { bit[] 0 } hFE_FF
59
- >;
60
-
61
- bmp_hi_private_use[] = <
62
- { bit[] 0, bit[] 1, bit[] 2, bit[] 3 } hE0_EF,
63
- { bit[] 0, bit[] 1, bit[] 2 } hF0_F7,
64
- {} hF8
65
- >;
66
-
67
- bmp_hi[] = <
68
- bmp_hi_common[] common,
69
- bmp_hi_private_use[] private
70
- >;
71
-
72
- plane0[] = {
73
- bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4,
74
- bit[] 5, bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10
75
- };
76
-
77
- plane_02_10[] = <
78
- { bit[] 0 } p02_03,
79
- { bit[] 0, bit[] 1 } p04_07,
80
- { bit[] 0, bit[] 1, bit[] 2 } p08_0F,
81
- {} p10
82
- >;
83
-
84
- unicode[] = <
85
- ascii[] ascii,
86
- { bmp_hi_common[] hi, byte[] lo } bmp_common,
87
- { bmp_hi_private_use[] hi, byte[] lo } bmp_private_use,
88
- plane0[] plane0,
89
- { byte[] mid, byte[] lo } supplementary_plane1,
90
- { plane_02_10[] plane, byte[] mid, byte[] lo } supplementary_planes2_16
91
- >;
92
-
93
- string[] = list[unicode[]];
94
- digit_result[] = { string[] rest, bits[] value };
95
-
96
- -- Primitive constructors and destructors
97
- h08_0F = |h08_0F ?bmp_hi_common[];
98
- h10_7F = |h10_7F ?bmp_hi_common[];
99
- h80_CF = |h80_CF ?bmp_hi_common[];
100
- hD0_D7 = |hD0_D7 ?bmp_hi_common[];
101
- hF9 = {} |hF9 ?bmp_hi_common[];
102
- hFA_FB = |hFA_FB ?bmp_hi_common[];
103
- hFC_FD = |hFC_FD ?bmp_hi_common[];
104
- hFE_FF = |hFE_FF ?bmp_hi_common[];
105
- un_h08_0F = ?bmp_hi_common[] /h08_0F;
106
- un_h10_7F = ?bmp_hi_common[] /h10_7F;
107
- un_h80_CF = ?bmp_hi_common[] /h80_CF;
108
- un_hD0_D7 = ?bmp_hi_common[] /hD0_D7;
109
- un_hF9 = ?bmp_hi_common[] /hF9;
110
- un_hFA_FB = ?bmp_hi_common[] /hFA_FB;
111
- un_hFC_FD = ?bmp_hi_common[] /hFC_FD;
112
- un_hFE_FF = ?bmp_hi_common[] /hFE_FF;
113
-
114
- hE0_EF = |hE0_EF ?bmp_hi_private_use[];
115
- hF0_F7 = |hF0_F7 ?bmp_hi_private_use[];
116
- hF8 = {} |hF8 ?bmp_hi_private_use[];
117
- un_hE0_EF = ?bmp_hi_private_use[] /hE0_EF;
118
- un_hF0_F7 = ?bmp_hi_private_use[] /hF0_F7;
119
- un_hF8 = ?bmp_hi_private_use[] /hF8;
120
-
121
- common = |common ?bmp_hi[];
122
- private = |private ?bmp_hi[];
123
- un_common = ?bmp_hi[] /common;
124
- un_private = ?bmp_hi[] /private;
125
-
126
- p02_03 = |p02_03 ?plane_02_10[];
127
- p04_07 = |p04_07 ?plane_02_10[];
128
- p08_0F = |p08_0F ?plane_02_10[];
129
- p10 = {} |p10 ?plane_02_10[];
130
- un_p02_03 = ?plane_02_10[] /p02_03;
131
- un_p04_07 = ?plane_02_10[] /p04_07;
132
- un_p08_0F = ?plane_02_10[] /p08_0F;
133
- un_p10 = ?plane_02_10[] /p10;
134
-
135
- ascii = |ascii ?unicode[];
136
- bmp_common = |bmp_common ?unicode[];
137
- bmp_private_use = |bmp_private_use ?unicode[];
138
- plane0 = |plane0 ?unicode[];
139
- supplementary_plane1 = |supplementary_plane1 ?unicode[];
140
- supplementary_planes2_16 = |supplementary_planes2_16 ?unicode[];
141
-
142
- un_ascii = ?unicode[] /ascii;
143
- un_bmp_common = ?unicode[] /bmp_common;
144
- un_bmp_private_use = ?unicode[] /bmp_private_use;
145
- un_plane0 = ?unicode[] /plane0;
146
- un_supplementary_plane1 = ?unicode[] /supplementary_plane1;
147
- un_supplementary_planes2_16 = ?unicode[] /supplementary_planes2_16;
148
-
149
- ---------------------------------------------------------------
150
- -- §3. Character Equality, ASCII Constants & Matchers
151
- ---------------------------------------------------------------
152
-
153
- eq_ascii? = {
154
- { .x.0 x, .y.0 y } eq_bit? 0,
155
- { .x.1 x, .y.1 y } eq_bit? 1,
156
- { .x.2 x, .y.2 y } eq_bit? 2,
157
- { .x.3 x, .y.3 y } eq_bit? 3,
158
- { .x.4 x, .y.4 y } eq_bit? 4,
159
- { .x.5 x, .y.5 y } eq_bit? 5,
160
- { .x.6 x, .y.6 y } eq_bit? 6,
161
- () then
162
- } .then;
163
-
164
- eq_char? = {
165
- { .x un_ascii x, .y un_ascii y } eq_ascii? if,
166
- () then
167
- } .then;
168
-
169
- _0_ = { 0 0, 0 1, 0 2, 0 3, 1 4, 1 5, 0 6 } ascii;
170
- _1_ = { 1 0, 0 1, 0 2, 0 3, 1 4, 1 5, 0 6 } ascii;
171
- _2_ = { 0 0, 1 1, 0 2, 0 3, 1 4, 1 5, 0 6 } ascii;
172
- _3_ = { 1 0, 1 1, 0 2, 0 3, 1 4, 1 5, 0 6 } ascii;
173
- _4_ = { 0 0, 0 1, 1 2, 0 3, 1 4, 1 5, 0 6 } ascii;
174
- _5_ = { 1 0, 0 1, 1 2, 0 3, 1 4, 1 5, 0 6 } ascii;
175
- _6_ = { 0 0, 1 1, 1 2, 0 3, 1 4, 1 5, 0 6 } ascii;
176
- _7_ = { 1 0, 1 1, 1 2, 0 3, 1 4, 1 5, 0 6 } ascii;
177
- _8_ = { 0 0, 0 1, 0 2, 1 3, 1 4, 1 5, 0 6 } ascii;
178
- _9_ = { 1 0, 0 1, 0 2, 1 3, 1 4, 1 5, 0 6 } ascii;
179
-
180
- _plus_ = { 1 0, 1 1, 0 2, 1 3, 0 4, 1 5, 0 6 } ascii;
181
- _minus_ = { 1 0, 0 1, 1 2, 1 3, 0 4, 1 5, 0 6 } ascii;
182
- _dot_ = { 0 0, 1 1, 1 2, 1 3, 0 4, 1 5, 0 6 } ascii;
183
- _space_ = { 0 0, 0 1, 0 2, 0 3, 0 4, 1 5, 0 6 } ascii;
184
- _tab_ = { 1 0, 0 1, 0 2, 1 3, 0 4, 0 5, 0 6 } ascii;
185
- _lf_ = { 0 0, 1 1, 0 2, 1 3, 0 4, 0 5, 0 6 } ascii;
186
- _cr_ = { 1 0, 0 1, 1 2, 1 3, 0 4, 0 5, 0 6 } ascii;
187
- _bs_ = { 0 0, 0 1, 0 2, 1 3, 0 4, 0 5, 0 6 } ascii;
188
- _ff_ = { 0 0, 0 1, 1 2, 1 3, 0 4, 0 5, 0 6 } ascii;
189
-
190
- _quote_ = { 0 0, 1 1, 0 2, 0 3, 0 4, 1 5, 0 6 } ascii;
191
- _backslash_ = { 0 0, 0 1, 1 2, 1 3, 1 4, 0 5, 1 6 } ascii;
192
- _slash_ = { 1 0, 1 1, 1 2, 1 3, 0 4, 1 5, 0 6 } ascii;
193
- _comma_ = { 0 0, 0 1, 1 2, 1 3, 0 4, 1 5, 0 6 } ascii;
194
- _colon_ = { 0 0, 1 1, 0 2, 1 3, 1 4, 1 5, 0 6 } ascii;
195
- _open_array_ = { 1 0, 1 1, 0 2, 1 3, 1 4, 0 5, 1 6 } ascii;
196
- _close_array_ = { 1 0, 0 1, 1 2, 1 3, 1 4, 0 5, 1 6 } ascii;
197
- _open_object_ = { 1 0, 1 1, 0 2, 1 3, 1 4, 1 5, 1 6 } ascii;
198
- _close_object_ = { 1 0, 0 1, 1 2, 1 3, 1 4, 1 5, 1 6 } ascii;
199
-
200
- _A_ = { 1 0, 0 1, 0 2, 0 3, 0 4, 0 5, 1 6 } ascii;
201
- _B_ = { 0 0, 1 1, 0 2, 0 3, 0 4, 0 5, 1 6 } ascii;
202
- _C_ = { 1 0, 1 1, 0 2, 0 3, 0 4, 0 5, 1 6 } ascii;
203
- _D_ = { 0 0, 0 1, 1 2, 0 3, 0 4, 0 5, 1 6 } ascii;
204
- _E_ = { 1 0, 0 1, 1 2, 0 3, 0 4, 0 5, 1 6 } ascii;
205
- _F_ = { 0 0, 1 1, 1 2, 0 3, 0 4, 0 5, 1 6 } ascii;
206
- _I_ = { 1 0, 0 1, 0 2, 1 3, 0 4, 0 5, 1 6 } ascii;
207
- _N_ = { 0 0, 1 1, 1 2, 1 3, 0 4, 0 5, 1 6 } ascii;
208
-
209
- _a_ = { 1 0, 0 1, 0 2, 0 3, 0 4, 1 5, 1 6 } ascii;
210
- _b_ = { 0 0, 1 1, 0 2, 0 3, 0 4, 1 5, 1 6 } ascii;
211
- _c_ = { 1 0, 1 1, 0 2, 0 3, 0 4, 1 5, 1 6 } ascii;
212
- _d_ = { 0 0, 0 1, 1 2, 0 3, 0 4, 1 5, 1 6 } ascii;
213
- _e_ = { 1 0, 0 1, 1 2, 0 3, 0 4, 1 5, 1 6 } ascii;
214
- _f_ = { 0 0, 1 1, 1 2, 0 3, 0 4, 1 5, 1 6 } ascii;
215
- _i_ = { 1 0, 0 1, 0 2, 1 3, 0 4, 1 5, 1 6 } ascii;
216
- _l_ = { 0 0, 0 1, 1 2, 1 3, 0 4, 1 5, 1 6 } ascii;
217
- _n_ = { 0 0, 1 1, 1 2, 1 3, 0 4, 1 5, 1 6 } ascii;
218
- _r_ = { 0 0, 1 1, 0 2, 0 3, 1 4, 1 5, 1 6 } ascii;
219
- _s_ = { 1 0, 1 1, 0 2, 0 3, 1 4, 1 5, 1 6 } ascii;
220
- _t_ = { 0 0, 0 1, 1 2, 0 3, 1 4, 1 5, 1 6 } ascii;
221
- _u_ = { 1 0, 0 1, 1 2, 0 3, 1 4, 1 5, 1 6 } ascii;
222
- _y_ = { 1 0, 0 1, 0 2, 1 3, 1 4, 1 5, 1 6 } ascii;
223
-
224
- space = { { car x, _space_ y } eq_char? if, cdr then } .then;
225
- tab = { { car x, _tab_ y } eq_char? if, cdr then } .then;
226
- lf = { { car x, _lf_ y } eq_char? if, cdr then } .then;
227
- cr = { { car x, _cr_ y } eq_char? if, cdr then } .then;
228
- e_char = { { car x, _e_ y } eq_char? if, cdr then } .then;
229
- E_char = { { car x, _E_ y } eq_char? if, cdr then } .then;
230
- minus_char = { { car x, _minus_ y } eq_char? if, cdr then } .then;
231
- plus_char = { { car x, _plus_ y } eq_char? if, cdr then } .then;
232
- dot_char = { { car x, _dot_ y } eq_char? if, cdr then } .then;
233
- I_char = { { car x, _I_ y } eq_char? if, cdr then } .then;
234
- n_char = { { car x, _n_ y } eq_char? if, cdr then } .then;
235
- f_char = { { car x, _f_ y } eq_char? if, cdr then } .then;
236
- i_char = { { car x, _i_ y } eq_char? if, cdr then } .then;
237
- t_char = { { car x, _t_ y } eq_char? if, cdr then } .then;
238
- y_char = { { car x, _y_ y } eq_char? if, cdr then } .then;
239
- N_char = { { car x, _N_ y } eq_char? if, cdr then } .then;
240
- a_char = { { car x, _a_ y } eq_char? if, cdr then } .then;
241
- quote_char = { { car x, _quote_ y } eq_char? if, cdr then } .then;
242
- backslash_char = { { car x, _backslash_ y } eq_char? if, cdr then } .then;
243
- slash_char = { { car x, _slash_ y } eq_char? if, cdr then } .then;
244
- b_char = { { car x, _b_ y } eq_char? if, cdr then } .then;
245
- l_char = { { car x, _l_ y } eq_char? if, cdr then } .then;
246
- r_char = { { car x, _r_ y } eq_char? if, cdr then } .then;
247
- s_char = { { car x, _s_ y } eq_char? if, cdr then } .then;
248
- u_char = { { car x, _u_ y } eq_char? if, cdr then } .then;
249
- comma_char = { { car x, _comma_ y } eq_char? if, cdr then } .then;
250
- colon_char = { { car x, _colon_ y } eq_char? if, cdr then } .then;
251
- open_array = { { car x, _open_array_ y } eq_char? if, cdr then } .then;
252
- close_array = { { car x, _close_array_ y } eq_char? if, cdr then } .then;
253
- open_object = { { car x, _open_object_ y } eq_char? if, cdr then } .then;
254
- close_object = { { car x, _close_object_ y } eq_char? if, cdr then } .then;
255
-
256
- ws = <
257
- < space, tab, lf, cr > ws,
258
- ?string[]
259
- >;
260
-
261
- scan_digit = { cdr rest, car <
262
- { () x, _0_ y } eq_char? _,
263
- { () x, _1_ y } eq_char? _ 1b,
264
- { () x, _2_ y } eq_char? _ 0b 1b,
265
- { () x, _3_ y } eq_char? _ 1b 1b,
266
- { () x, _4_ y } eq_char? _ 0b 0b 1b,
267
- { () x, _5_ y } eq_char? _ 1b 0b 1b,
268
- { () x, _6_ y } eq_char? _ 0b 1b 1b,
269
- { () x, _7_ y } eq_char? _ 1b 1b 1b,
270
- { () x, _8_ y } eq_char? _ 0b 0b 0b 1b,
271
- { () x, _9_ y } eq_char? _ 1b 0b 0b 1b
272
- > value };
273
- parse_digit = scan_digit;
274
- digit = scan_digit;
275
-
276
- hex_letter = <
277
- { () x, _a_ y } eq_char? _ 0b 1b 0b 1b,
278
- { () x, _A_ y } eq_char? _ 0b 1b 0b 1b,
279
- { () x, _b_ y } eq_char? _ 1b 1b 0b 1b,
280
- { () x, _B_ y } eq_char? _ 1b 1b 0b 1b,
281
- { () x, _c_ y } eq_char? _ 0b 0b 1b 1b,
282
- { () x, _C_ y } eq_char? _ 0b 0b 1b 1b,
283
- { () x, _d_ y } eq_char? _ 1b 0b 1b 1b,
284
- { () x, _D_ y } eq_char? _ 1b 0b 1b 1b,
285
- { () x, _e_ y } eq_char? _ 0b 1b 1b 1b,
286
- { () x, _E_ y } eq_char? _ 0b 1b 1b 1b,
287
- { () x, _f_ y } eq_char? _ 1b 1b 1b 1b,
288
- { () x, _F_ y } eq_char? _ 1b 1b 1b 1b
289
- >;
290
-
291
- hex = < digit, { cdr rest, car hex_letter value } >;
292
-
293
- hex4 = hex
294
- { .rest hex next, .value times16 high }
295
- { .next.rest rest, { .high x, .next.value y } plus value }
296
- { .rest hex next, .value times16 high }
297
- { .next.rest rest, { .high x, .next.value y } plus value }
298
- { .rest hex next, .value times16 high }
299
- { .next.rest rest, { .high x, .next.value y } plus value };
300
-
301
- ---------------------------------------------------------------
302
- -- §4. Unicode Bit-Trie Traversal, Key Encoding & Decoding
303
- ---------------------------------------------------------------
304
-
305
- step_bmp_common_hi = <
306
- { .hi un_h08_0F x, .t step_0 step_0 step_0 t } step_bits3,
307
- { .hi un_h10_7F x, .t step_0 step_0 step_1 t } step_bits7,
308
- { .hi un_h80_CF x, .t step_0 step_1 step_0 t } step_bits7,
309
- { .hi un_hD0_D7 x, .t step_0 step_1 step_1 t } step_bits3,
310
- { .hi un_hF9 if, .t step_1 step_0 step_0 then } .then,
311
- { .hi un_hFA_FB x, .t step_1 step_0 step_1 t } step_bits1,
312
- { .hi un_hFC_FD x, .t step_1 step_1 step_0 t } step_bits1,
313
- { .hi un_hFE_FF x, .t step_1 step_1 step_1 t } step_bits1
314
- >;
315
-
316
- step_bmp_priv_hi = <
317
- { .hi un_hE0_EF x, .t step_0 t } step_bits4,
318
- { .hi un_hF0_F7 x, .t step_1 step_0 t } step_bits3,
319
- { .hi un_hF8 if, .t step_1 step_1 then } .then
320
- >;
321
-
322
- step_plane2_16 = <
323
- { .p un_p02_03 x, .t step_0 step_0 t } step_bits1,
324
- { .p un_p04_07 x, .t step_0 step_1 t } step_bits2,
325
- { .p un_p08_0F x, .t step_1 step_0 t } step_bits3,
326
- { .p un_p10 if, .t step_1 step_1 then } .then
327
- >;
328
-
329
- step_char = <
330
- { .char un_ascii x, .t step_0 t } step_bits7,
331
- { .char un_plane0 x, .t step_1 step_0 t } step_bits11,
332
- {
333
- .char un_bmp_common .lo x,
334
- { .char un_bmp_common .hi hi, .t step_1 step_1 step_0 t }
335
- step_bmp_common_hi t
336
- } step_bits8,
337
- {
338
- .char un_bmp_private_use .lo x,
339
- { .char un_bmp_private_use .hi hi, .t step_1 step_1 step_1 step_0 t }
340
- step_bmp_priv_hi t
341
- } step_bits8,
342
- {
343
- .char un_supplementary_plane1 .lo x,
344
- {
345
- .char un_supplementary_plane1 .mid x,
346
- .t step_1 step_1 step_1 step_1 step_0 t
347
- } step_bits8 t
348
- } step_bits8,
349
- {
350
- .char un_supplementary_planes2_16 .lo x,
351
- {
352
- .char un_supplementary_planes2_16 .mid x,
353
- {
354
- .char un_supplementary_planes2_16 .plane p,
355
- .t step_1 step_1 step_1 step_1 step_1 t
356
- } step_plane2_16 t
357
- } step_bits8 t
358
- } step_bits8
359
- >;
360
-
361
- dot = <
362
- { .key un_nil if, .object trie_val un_val then } .then,
363
- {
364
- .key cdr key,
365
- { .key car char, .object t } step_char object
366
- } dot
367
- >;
368
-
369
- push_bmp_common_hi = <
370
- { .hi un_h08_0F x, .acc acc } push_bits3 0b 0b 0b,
371
- { .hi un_h10_7F x, .acc acc } push_bits7 1b 0b 0b,
372
- { .hi un_h80_CF x, .acc acc } push_bits7 0b 1b 0b,
373
- { .hi un_hD0_D7 x, .acc acc } push_bits3 1b 1b 0b,
374
- { .hi un_hF9 if, .acc 0b 0b 1b then } .then,
375
- { .hi un_hFA_FB x, .acc acc } push_bits1 1b 0b 1b,
376
- { .hi un_hFC_FD x, .acc acc } push_bits1 0b 1b 1b,
377
- { .hi un_hFE_FF x, .acc acc } push_bits1 1b 1b 1b
378
- >;
379
-
380
- push_bmp_priv_hi = <
381
- { .hi un_hE0_EF x, .acc acc } push_bits4 0b,
382
- { .hi un_hF0_F7 x, .acc acc } push_bits3 0b 1b,
383
- { .hi un_hF8 if, .acc 1b 1b then } .then
384
- >;
385
-
386
- push_plane2_16 = <
387
- { .p un_p02_03 x, .acc acc } push_bits1 0b 0b,
388
- { .p un_p04_07 x, .acc acc } push_bits2 1b 0b,
389
- { .p un_p08_0F x, .acc acc } push_bits3 0b 1b,
390
- { .p un_p10 if, .acc 1b 1b then } .then
391
- >;
392
-
393
- prepend_char_bits = <
394
- { .char un_ascii x, .acc acc } push_bits7 0b,
395
- { .char un_plane0 x, .acc acc } push_bits11 0b 1b,
396
- {
397
- .char un_bmp_common .hi hi,
398
- { .char un_bmp_common .lo x, .acc acc } push_bits8 acc
399
- } push_bmp_common_hi 0b 1b 1b,
400
- {
401
- .char un_bmp_private_use .hi hi,
402
- { .char un_bmp_private_use .lo x, .acc acc } push_bits8 acc
403
- } push_bmp_priv_hi 0b 1b 1b 1b,
404
- {
405
- .char un_supplementary_plane1 .mid x,
406
- { .char un_supplementary_plane1 .lo x, .acc acc } push_bits8 acc
407
- } push_bits8 0b 1b 1b 1b 1b,
408
- {
409
- .char un_supplementary_planes2_16 .plane p,
410
- {
411
- .char un_supplementary_planes2_16 .mid x,
412
- { .char un_supplementary_planes2_16 .lo x, .acc acc } push_bits8 acc
413
- } push_bits8 acc
414
- } push_plane2_16 1b 1b 1b 1b 1b
415
- >;
416
-
417
- str_to_bits_loop = <
418
- { .str un_nil if, .acc then } .then,
419
- {
420
- .str cdr str,
421
- { .str car char, .acc acc } prepend_char_bits acc
422
- } str_to_bits_loop
423
- >;
424
-
425
- str_to_bits = { reverse str, _ acc } str_to_bits_loop;
426
-
427
- put = {
428
- .key str_to_bits index,
429
- .val val,
430
- .object list
431
- } put_nth;
432
-
433
- remove = {
434
- .key str_to_bits index,
435
- .object list
436
- } remove_nth;
437
-
438
- read_bits7 =
439
- read_bit
440
- { .bit b6, (.rest read_bit) r5 }
441
- { .b6 b6, .r5 .bit b5, (.r5 .rest read_bit) r4 }
442
- { .b6 b6, .b5 b5, .r4 .bit b4, (.r4 .rest read_bit) r3 }
443
- { .b6 b6, .b5 b5, .b4 b4, .r3 .bit b3, (.r3 .rest read_bit) r2 }
444
- { .b6 b6, .b5 b5, .b4 b4, .b3 b3, .r2 .bit b2, (.r2 .rest read_bit) r1 }
445
- {
446
- .b6 b6, .b5 b5, .b4 b4, .b3 b3, .b2 b2,
447
- .r1 .bit b1, (.r1 .rest read_bit) r0
448
- }
449
- {
450
- { .r0 .bit 0, .b1 1, .b2 2, .b3 3, .b4 4, .b5 5, .b6 6 } ascii val,
451
- .r0 .rest rest
452
- };
453
-
454
- read_char = un0 read_bits7;
455
-
456
- bits_to_str_loop = <
457
- { .bits un_ if, .acc reverse then } .then,
458
- { .bits read_char next, .acc acc }
459
- { .next.rest bits, { .next.val car, .acc cdr } cons acc }
460
- bits_to_str_loop
461
- >;
462
-
463
- bits_to_str = { () bits, nil acc } bits_to_str_loop;
464
-
465
- collect_keys = <
466
- { .tree un_nil_trie if, .acc then } .then,
467
- {
468
- .tree step_0 tree,
469
- .rev_path 0b rev_path,
470
- {
471
- .tree step_1 tree,
472
- .rev_path 1b rev_path,
473
- <
474
- { .tree trie_val un_nil_opt if, .acc then } .then,
475
- {
476
- .tree trie_val un_val if,
477
- { .rev_path inv bits_to_str car, .acc cdr } cons then
478
- } .then
479
- > acc
480
- } collect_keys acc
481
- } collect_keys
482
- >;
483
-
484
- obj_keys_raw = { () tree, _ rev_path, nil acc } collect_keys;
485
-
486
- c2 = _ 0b 1b;
487
- c4 = _ 0b 0b 1b;
488
- c8 = _ 0b 0b 0b 1b;
489
- c16 = _ 0b 0b 0b 0b 1b;
490
- c128 = _ 0b 0b 0b 0b 0b 0b 0b 1b;
491
- c208 = _ 0b 0b 0b 0b 1b 0b 1b 1b;
492
- c216 = _ 0b 0b 0b 1b 1b 0b 1b 1b;
493
- c224 = _ 0b 0b 0b 0b 0b 1b 1b 1b;
494
- c240 = _ 0b 0b 0b 0b 1b 1b 1b 1b;
495
- c248 = _ 0b 0b 0b 1b 1b 1b 1b 1b;
496
- c249 = _ 1b 0b 0b 1b 1b 1b 1b 1b;
497
- c250 = _ 0b 1b 0b 1b 1b 1b 1b 1b;
498
- c252 = _ 0b 0b 1b 1b 1b 1b 1b 1b;
499
- c254 = _ 0b 1b 1b 1b 1b 1b 1b 1b;
500
- c1023 = _ 1b 1b 1b 1b 1b 1b 1b 1b 1b 1b;
501
- c1024 = c1023 inc;
502
- c55296 = _ 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 1b 1b 0b 1b 1b;
503
- c56320 = _ 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 1b 1b 1b 0b 1b 1b;
504
- c57344 = _ 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 1b 1b 1b;
505
- c65536 = _ 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 0b 1b;
506
-
507
- bmp_hi = <
508
- {
509
- { () x, c16 y } lt? if,
510
- { () x, c8 y } minus small3 h08_0F common then
511
- } .then,
512
- {
513
- { () x, c128 y } lt? if,
514
- { () x, c16 y } minus small7 h10_7F common then
515
- } .then,
516
- {
517
- { () x, c208 y } lt? if,
518
- { () x, c128 y } minus small7 h80_CF common then
519
- } .then,
520
- {
521
- { () x, c216 y } lt? if,
522
- { () x, c208 y } minus small3 hD0_D7 common then
523
- } .then,
524
- {
525
- { () x, c240 y } lt? if,
526
- { () x, c224 y } minus small4 hE0_EF private then
527
- } .then,
528
- {
529
- { () x, c248 y } lt? if,
530
- { () x, c240 y } minus small3 hF0_F7 private then
531
- } .then,
532
- { () x, c248 y } minus un_ hF8 private,
533
- { () x, c249 y } minus un_ hF9 common,
534
- {
535
- { () x, c252 y } lt? if,
536
- { () x, c250 y } minus small1 hFA_FB common then
537
- } .then,
538
- {
539
- { () x, c254 y } lt? if,
540
- { () x, c252 y } minus small1 hFC_FD common then
541
- } .then,
542
- { () x, c254 y } minus small1 hFE_FF common
543
- >;
544
-
545
- plane = <
546
- {
547
- { () x, c4 y } lt? if,
548
- { () x, c2 y } minus small1 p02_03 then
549
- } .then,
550
- {
551
- { () x, c8 y } lt? if,
552
- { () x, c4 y } minus small2 p04_07 then
553
- } .then,
554
- {
555
- { () x, c16 y } lt? if,
556
- { () x, c8 y } minus small3 p08_0F then
557
- } .then,
558
- un1 un0 un0 un0 un0 un_ p10
559
- >;
560
-
561
- bmp = {
562
- () parts, .hi { () x, _ acc } push_bits8 trim high
563
- } <
564
- { .high un_ a, .parts.lo.7 is0 b, .parts.lo field7 ascii then } .then,
565
- {
566
- { .high x, c8 y } lt? if,
567
- {
568
- .parts.lo.0 0, .parts.lo.1 1, .parts.lo.2 2, .parts.lo.3 3,
569
- .parts.lo.4 4, .parts.lo.5 5, .parts.lo.6 6, .parts.lo.7 7,
570
- .parts.hi.0 8, .parts.hi.1 9, .parts.hi.2 10
571
- } plane0 then
572
- } .then,
573
- { .high bmp_hi high, .parts.lo lo } <
574
- { .high un_common hi, .lo lo } bmp_common,
575
- { .high un_private hi, .lo lo } bmp_private_use
576
- >
577
- >;
578
-
579
- to_unicode = unpack16 <
580
- { .plane un_ if, () bmp then } .then,
581
- {
582
- .plane un1 un_ if,
583
- { .hi mid, .lo lo } supplementary_plane1 then
584
- } .then,
585
- { .plane plane plane, .hi mid, .lo lo } supplementary_planes2_16
586
- >;
587
-
588
- str_len = ?string[] list_length;
@@ -1,70 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { readFileSync } from "node:fs";
3
- import k from "../index.mjs";
4
- import { compileLibrary, loadLibrary } from "../object.mjs";
5
- import { compileWasmArtifact, instantiateWasmArtifact } from "../backends/wasm/src/wasm.mjs";
6
- import { encodeNumberToValue, decodeValueToNumber } from "../codecs/ieee.mjs";
7
- import { textToStringValue, stringValueToText } from "../codecs/runtime/unicode-string.mjs";
8
-
9
- const ieeeSources = [
10
- "lib/bit.k",
11
- "lib/bits.k",
12
- "lib/list.k",
13
- "lib/btrie.k",
14
- "lib/string.k",
15
- "lib/ieee.k"
16
- ];
17
- let ieee = null;
18
- for (const file of ieeeSources) {
19
- ieee = loadLibrary(compileLibrary(readFileSync(new URL(`../${file}`, import.meta.url), "utf8"), {
20
- source: file,
21
- libraries: ieee ? [ieee] : []
22
- }));
23
- }
24
- assert.equal(ieee.relAlias.add, undefined, "Text conversion must not load IEEE arithmetic");
25
- const parser = await instantiateWasmArtifact(await compileWasmArtifact(ieee.relAlias.parseFloat64, { libraries: [ieee] }));
26
- const printer = await instantiateWasmArtifact(await compileWasmArtifact(ieee.relAlias.printFloat64, { libraries: [ieee] }));
27
- const parse = text => parser.executeValue(textToStringValue(text));
28
- const print = number => stringValueToText(printer.executeValue(encodeNumberToValue(number)));
29
- const js = (name, value) => k.compile(ieee.relAlias[name], { libraries: [ieee] })(value);
30
-
31
- for (const [text, number] of [
32
- [" +12.5\n", 12.5], ["-0", -0], ["0.1", 0.1], ["1e309", Infinity],
33
- ["-1e999999999", -Infinity], ["+Infinity", Infinity], ["-Infinity", -Infinity]
34
- ]) {
35
- assert.equal(decodeValueToNumber(parse(text)), number, text);
36
- }
37
- assert.ok(Number.isNaN(decodeValueToNumber(parse("NaN"))));
38
- for (const text of ["", " ", ".1", "1.", "01", "1e", "1e+", "Infinityx", "NaNx", "1 2", "--1", "+-1", "-+1"]) {
39
- assert.equal(parse(text), undefined, text);
40
- }
41
- for (const [number, text] of [
42
- [0, "0"], [-0, "-0"], [1.5, "1.5"], [-12.5, "-12.5"],
43
- [Infinity, "Infinity"], [-Infinity, "-Infinity"], [NaN, "NaN"],
44
- [0.1, "0.1000000000000000055511151231257827021181583404541015625"]
45
- ]) assert.equal(print(number), text);
46
-
47
- // Round-trip representative extremes and deterministic binary64 bit patterns.
48
- const numbers = [
49
- Number.MIN_VALUE, -Number.MIN_VALUE, 2.2250738585072014e-308,
50
- Number.MAX_VALUE, -Number.MAX_VALUE, 1 / 3, 9007199254740992,
51
- 1e-100, 1e100, 1e20, -0
52
- ];
53
- const view = new DataView(new ArrayBuffer(8));
54
- let bits = 0x123456789abcdefn;
55
- for (let i = 0; i < 16; i++) {
56
- bits = BigInt.asUintN(64, bits * 6364136223846793005n + 1442695040888963407n);
57
- view.setBigUint64(0, bits);
58
- const value = view.getFloat64(0);
59
- if (Number.isFinite(value)) numbers.push(value);
60
- }
61
- for (const number of numbers) {
62
- const text = print(number);
63
- assert.equal(Number(text), number, `Native round-trip: ${number}`);
64
- assert.equal(decodeValueToNumber(parse(text)), number, `k round-trip: ${number}`);
65
- }
66
- for (const [text, number] of [["1.5", 1.5], ["-0", -0], ["Infinity", Infinity]]) {
67
- assert.equal(decodeValueToNumber(js("parseFloat64", textToStringValue(text))), number);
68
- assert.equal(stringValueToText(js("printFloat64", encodeNumberToValue(number))), text);
69
- }
70
- console.log("IEEE text: parsing, exact printing, special values and binary64 round-trips passed");
@@ -1,25 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { withCompiledLibrary, runLibrary, integer } from "./helpers/cli.mjs";
3
-
4
- const large = "11111112222223333334444444555555666666777777788888899999900000011111122222233333344444455555";
5
-
6
- withCompiledLibrary(["lib/bit.k", "lib/bits.k", "lib/arithmetics.k"], (library) => {
7
- const multiply = (source, input) =>
8
- integer(runLibrary(library, ["karatsuba"], source, input), source);
9
-
10
- for (const input of [
11
- "0", "1", "-1", "7", "-7", "42", "-42", "123456789", "-123456789",
12
- "987654321987654321", "-987654321987654321", large, `-${large}`
13
- ]) {
14
- const n = BigInt(input);
15
- assert.equal(multiply("{() x, () y} karatsuba", input), String(n * n),
16
- `karatsuba square(${input})`);
17
- }
18
-
19
- assert.equal(multiply("/cons { .car x, .cdr /cons .car y } karatsuba",
20
- "[12345678901234567890, -98765432109876543210]"),
21
- String(12345678901234567890n * -98765432109876543210n),
22
- "asymmetric mixed-sign multiplication");
23
- });
24
-
25
- console.log("Karatsuba CLI and BigInt oracles: ok");