@fraczak/k 8.6.0 → 8.7.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 (71) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/DOCS/CODECS.md +20 -14
  3. package/DOCS/FILE_FORMATS.md +79 -12
  4. package/DOCS/GETTING_STARTED.md +17 -8
  5. package/DOCS/PATTERNS.md +17 -0
  6. package/DOCS/REPL.md +15 -8
  7. package/Examples/arithmetics.k +81 -468
  8. package/Examples/ieee-arithmetics.k +37 -33
  9. package/Examples/poly.k +107 -104
  10. package/README.md +19 -11
  11. package/backend-api.mjs +3 -1
  12. package/backends/llvm/README.md +1 -1
  13. package/backends/llvm/bin/k-llvm-build.mjs +5 -5
  14. package/backends/llvm/bin/k-llvm-compile.mjs +4 -4
  15. package/backends/llvm/bin/k-llvm.mjs +6 -6
  16. package/backends/llvm/src/cli.mjs +17 -12
  17. package/backends/llvm/tests/integration.sh +4 -1
  18. package/backends/wasm/README.md +3 -3
  19. package/backends/wasm/bin/k-wasm-compile.mjs +4 -4
  20. package/backends/wasm/bin/k-wasm.mjs +4 -4
  21. package/backends/wasm/src/cli.mjs +12 -10
  22. package/backends/wasm/src/wasm.mjs +6 -2
  23. package/backends/wasm/tests/integration.sh +8 -3
  24. package/browser/shims/fs.mjs +19 -3
  25. package/canonical-pattern.mjs +351 -82
  26. package/core.k +2 -1
  27. package/harness/suites/poly.mjs +14 -30
  28. package/k.mjs +17 -13
  29. package/kvm.mjs +13 -9
  30. package/lib/arithmetics.k +287 -0
  31. package/lib/bit.k +68 -0
  32. package/lib/bits.k +221 -0
  33. package/lib/btrie.k +187 -0
  34. package/lib/djson.k +326 -0
  35. package/lib/ieee.k +709 -0
  36. package/lib/list.k +49 -0
  37. package/lib/string.k +588 -0
  38. package/named-patterns.mjs +2 -2
  39. package/object.mjs +52 -10
  40. package/objects/README.md +10 -3
  41. package/objects/compile.mjs +10 -8
  42. package/package.json +1 -1
  43. package/repl.mjs +227 -22
  44. package/scripts/benchmark-idempotency.mjs +22 -15
  45. package/scripts/benchmark.mjs +29 -10
  46. package/scripts/build-repl-html.mjs +49 -14
  47. package/scripts/perf-ieee.mjs +9 -1
  48. package/scripts/perf-int.mjs +10 -17
  49. package/scripts/perf-multiplication.mjs +9 -15
  50. package/scripts/perf-poly.mjs +16 -32
  51. package/scripts/run-tests.mjs +2 -2
  52. package/scripts/test-package.mjs +8 -2
  53. package/tests/helpers/cli.mjs +11 -3
  54. package/tests/integration.sh +9 -4
  55. package/tests/test-arithmetics.mjs +4 -2
  56. package/tests/test-djson-methods.mjs +16 -12
  57. package/tests/test-ieee-arithmetic.mjs +11 -3
  58. package/tests/test-ieee-text.mjs +15 -4
  59. package/tests/test-k-object.mjs +11 -3
  60. package/tests/test-karatsuba.mjs +1 -1
  61. package/tests/test-kvm-polymorphic.mjs +54 -34
  62. package/tests/test-lib.mjs +115 -0
  63. package/tests/test-named-patterns.mjs +12 -3
  64. package/tests/test-parse-json.mjs +33 -13
  65. package/tests/test-poly.mjs +10 -4
  66. package/tests/test-repl-html.mjs +18 -7
  67. package/tests/test-repl.mjs +154 -54
  68. package/Examples/djson.k +0 -894
  69. package/Examples/ieee.k +0 -969
  70. package/Examples/karatsuba-mult.k +0 -227
  71. package/Examples/list.k +0 -10
package/Examples/ieee.k DELETED
@@ -1,969 +0,0 @@
1
- -- IEEE binary64 types and text conversion.
2
- -- Arithmetic operations live in ieee-arithmetics.k.
3
-
4
- -- Defined in 'core.k':
5
- bit[] = @MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg[];
6
- bits[] = @VtPHxGf5GNMzzyVFxtv7gegFfJRYapBGtCeyV56bs5Zb[];
7
- ascii[] = @VgLSCtg5j6bmqeMXzdk7rHrs9DPYcjxXntYmR6kMpuXc[];
8
- unicode[] = @AhRir67FipnHnJfDU7fSQ3Xhcg6BTJYwtq6HGsWwZtbi[];
9
- string[] = @Pt3MwQwzUPxQKu58zXR3qUBHWTzfycx5bSZGzcK6EZQK[];
10
-
11
- sign[] = < {} "+", {} "-" >;
12
- exp11[] = {
13
- bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4,
14
- bit[] 5, bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10
15
- };
16
- frac52[] = {
17
- bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4, bit[] 5,
18
- bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10, bit[] 11,
19
- bit[] 12, bit[] 13, bit[] 14, bit[] 15, bit[] 16, bit[] 17,
20
- bit[] 18, bit[] 19, bit[] 20, bit[] 21, bit[] 22, bit[] 23,
21
- bit[] 24, bit[] 25, bit[] 26, bit[] 27, bit[] 28, bit[] 29,
22
- bit[] 30, bit[] 31, bit[] 32, bit[] 33, bit[] 34, bit[] 35,
23
- bit[] 36, bit[] 37, bit[] 38, bit[] 39, bit[] 40, bit[] 41,
24
- bit[] 42, bit[] 43, bit[] 44, bit[] 45, bit[] 46, bit[] 47,
25
- bit[] 48, bit[] 49, bit[] 50, bit[] 51
26
- };
27
- float64[] = { sign[] sign, exp11[] exponent, frac52[] fraction };
28
-
29
- -- Parse one complete decimal spelling, optionally prefixed by + or -, or
30
- -- Infinity/NaN. Decimal syntax otherwise follows JSON. Overflow becomes
31
- -- infinity; NaN spellings use one canonical payload.
32
- parseFloat64 = ?string[] ws scanFloat64 {
33
- .rest ws /nil end, .value value
34
- } .value ?float64[];
35
-
36
- -- Prefix scanner shared with the JSON parser, which applies finite_float64?.
37
- scanFloat64 = ?string[] <
38
- plus_char { () rest, + sign } signed_decimal,
39
- number,
40
- special
41
- > ?result[];
42
- natToFloat64 = ?bits[] trim <
43
- /_ pos_zero,
44
- { () n, 1b d } ratio { + sign, .exponent exponent, .fraction fraction }
45
- > ?float64[];
46
-
47
- -- Print an exact decimal expansion (no shortest-format rounding). Signed zero
48
- -- and infinities retain their signs; every NaN prints the canonical word NaN.
49
- printFloat64 = ?float64[] <
50
- nan_float64? nan_text,
51
- { inf_float64? if, .sign /- and, negative_infinity_text then } .then,
52
- inf_float64? infinity_text,
53
- { zero_float64? if, .sign /- and, negative_zero_text then } .then,
54
- zero_float64? zero_text,
55
- { () value, print_ratio format_decimal text } <
56
- { .value.sign /- if, { _minus_ car, .text cdr } |cons then } .then,
57
- .text
58
- >
59
- > ?string[];
60
-
61
- nil = {} |nil;
62
- ws = ?string[] <
63
- < space, tab, lf, cr > ws,
64
- ()
65
- > ?string[];
66
-
67
- -- Small constructors and classification filters.
68
- 0 = {} |0;
69
- 1 = {} |1;
70
-
71
- + = {} |+;
72
- - = {} |-;
73
-
74
- 0? = /0 |0;
75
- 1? = /1 |1;
76
-
77
- +? = /+ |+;
78
- -? = /- |-;
79
-
80
- zero_exponent? = {
81
- .0 0? 0, .1 0? 1, .2 0? 2, .3 0? 3, .4 0? 4, .5 0? 5,
82
- .6 0? 6, .7 0? 7, .8 0? 8, .9 0? 9, .10 0? 10
83
- };
84
-
85
- all_ones_exponent? = {
86
- .0 1? 0, .1 1? 1, .2 1? 2, .3 1? 3, .4 1? 4, .5 1? 5,
87
- .6 1? 6, .7 1? 7, .8 1? 8, .9 1? 9, .10 1? 10
88
- };
89
-
90
- not_all_ones_exponent? = {
91
- <.10 /0, .9 /0, .8 /0, .7 /0, .6 /0, .5 /0, .4 /0, .3 /0, .2 /0, .1 /0, .0 /0> if,
92
- () then
93
- } .then;
94
-
95
- zero_fraction? = {
96
- .0 0? 0, .1 0? 1, .2 0? 2, .3 0? 3, .4 0? 4, .5 0? 5, .6 0? 6, .7 0? 7, .8 0? 8, .9 0? 9,
97
- .10 0? 10, .11 0? 11, .12 0? 12, .13 0? 13, .14 0? 14, .15 0? 15, .16 0? 16, .17 0? 17, .18 0? 18, .19 0? 19,
98
- .20 0? 20, .21 0? 21, .22 0? 22, .23 0? 23, .24 0? 24, .25 0? 25, .26 0? 26, .27 0? 27, .28 0? 28, .29 0? 29,
99
- .30 0? 30, .31 0? 31, .32 0? 32, .33 0? 33, .34 0? 34, .35 0? 35, .36 0? 36, .37 0? 37, .38 0? 38, .39 0? 39,
100
- .40 0? 40, .41 0? 41, .42 0? 42, .43 0? 43, .44 0? 44, .45 0? 45, .46 0? 46, .47 0? 47, .48 0? 48, .49 0? 49,
101
- .50 0? 50, .51 0? 51
102
- };
103
-
104
- nonzero_fraction? = {
105
- <.51 /1, .50 /1, .49 /1, .48 /1, .47 /1, .46 /1, .45 /1, .44 /1, .43 /1, .42 /1,
106
- .41 /1, .40 /1, .39 /1, .38 /1, .37 /1, .36 /1, .35 /1, .34 /1, .33 /1, .32 /1,
107
- .31 /1, .30 /1, .29 /1, .28 /1, .27 /1, .26 /1, .25 /1, .24 /1, .23 /1, .22 /1,
108
- .21 /1, .20 /1, .19 /1, .18 /1, .17 /1, .16 /1, .15 /1, .14 /1, .13 /1, .12 /1,
109
- .11 /1, .10 /1, .9 /1, .8 /1, .7 /1, .6 /1, .5 /1, .4 /1, .3 /1, .2 /1, .1 /1, .0 /1> if,
110
- () then
111
- } .then;
112
-
113
- nan_float64? = ?float64[] {
114
- .sign sign, .exponent all_ones_exponent? exponent, .fraction nonzero_fraction? fraction
115
- };
116
-
117
- inf_float64? = ?float64[] {
118
- .sign sign, .exponent all_ones_exponent? exponent, .fraction zero_fraction? fraction
119
- };
120
-
121
- finite_float64? = ?float64[] {
122
- .sign sign, .exponent not_all_ones_exponent? exponent, .fraction fraction
123
- };
124
-
125
- zero_float64? = ?float64[] {
126
- .sign sign, .exponent zero_exponent? exponent, .fraction zero_fraction? fraction
127
- };
128
-
129
- exp0 = {0 0, 0 1, 0 2, 0 3, 0 4, 0 5, 0 6, 0 7, 0 8, 0 9, 0 10};
130
-
131
- exp_all1 = {1 0, 1 1, 1 2, 1 3, 1 4, 1 5, 1 6, 1 7, 1 8, 1 9, 1 10};
132
-
133
- frac0 = {
134
- 0 0, 0 1, 0 2, 0 3, 0 4, 0 5, 0 6, 0 7, 0 8, 0 9, 0 10, 0 11, 0 12, 0 13, 0 14, 0 15,
135
- 0 16, 0 17, 0 18, 0 19, 0 20, 0 21, 0 22, 0 23, 0 24, 0 25, 0 26, 0 27, 0 28, 0 29, 0 30, 0 31,
136
- 0 32, 0 33, 0 34, 0 35, 0 36, 0 37, 0 38, 0 39, 0 40, 0 41, 0 42, 0 43, 0 44, 0 45, 0 46, 0 47,
137
- 0 48, 0 49, 0 50, 0 51
138
- };
139
-
140
- frac_lsb1 = {
141
- 1 0, 0 1, 0 2, 0 3, 0 4, 0 5, 0 6, 0 7, 0 8, 0 9, 0 10, 0 11, 0 12, 0 13, 0 14, 0 15,
142
- 0 16, 0 17, 0 18, 0 19, 0 20, 0 21, 0 22, 0 23, 0 24, 0 25, 0 26, 0 27, 0 28, 0 29, 0 30, 0 31,
143
- 0 32, 0 33, 0 34, 0 35, 0 36, 0 37, 0 38, 0 39, 0 40, 0 41, 0 42, 0 43, 0 44, 0 45, 0 46, 0 47,
144
- 0 48, 0 49, 0 50, 0 51
145
- };
146
-
147
- pos_zero = {+ sign, exp0 exponent, frac0 fraction};
148
- neg_zero = {- sign, exp0 exponent, frac0 fraction};
149
- pos_inf = {+ sign, exp_all1 exponent, frac0 fraction};
150
- neg_inf = {- sign, exp_all1 exponent, frac0 fraction};
151
- qnan = {+ sign, exp_all1 exponent, frac_lsb1 fraction};
152
-
153
- flip_sign = < /+ -, /- + >;
154
-
155
- negate_float64 =
156
- {.sign flip_sign sign, .exponent exponent, .fraction fraction};
157
-
158
- -- Exact decimal parsing and shared bit helpers.
159
- nat_pair[] = { bits[] x, bits[] y };
160
- order[] = < {} lt, {} eq, {} gt >;
161
- decimal[] = {
162
- string[] rest, bits[] n, bits[] d, bits[] count
163
- };
164
- result[] = { string[] rest, float64[] value };
165
- digit_result[] = { string[] rest, bits[] value };
166
-
167
- _ = {} |_;
168
- 1b = _ |1;
169
-
170
- lt = {} |lt;
171
- eq = {} |eq;
172
- gt = {} |gt;
173
-
174
- eq_bit? = ?{ bit[] x, bit[] y } <
175
- { .x /0 a, .y /0 b },
176
- { .x /1 a, .y /1 b }
177
- >;
178
-
179
- eq_ascii? = ?{ ascii[] x, ascii[] y } {
180
- { .x.0 x, .y.0 y } eq_bit? 0,
181
- { .x.1 x, .y.1 y } eq_bit? 1,
182
- { .x.2 x, .y.2 y } eq_bit? 2,
183
- { .x.3 x, .y.3 y } eq_bit? 3,
184
- { .x.4 x, .y.4 y } eq_bit? 4,
185
- { .x.5 x, .y.5 y } eq_bit? 5,
186
- { .x.6 x, .y.6 y } eq_bit? 6
187
- };
188
-
189
- eq_char? = ?{ unicode[] x, unicode[] y } {
190
- .x /ascii x, .y /ascii y
191
- } eq_ascii?;
192
-
193
- push_bit = ?{ bit[] b, bits[] acc } <
194
- { .b /0 if, .acc |0 then } .then,
195
- { .b /1 if, .acc |1 then } .then
196
- > ?bits[];
197
-
198
- push_bits1 = { .x .0 b, .acc acc } push_bit;
199
- push_bits2 = { .x .1 b, { .x x, .acc acc } push_bits1 acc } push_bit;
200
- push_bits3 = { .x .2 b, { .x x, .acc acc } push_bits2 acc } push_bit;
201
- push_bits4 = { .x .3 b, { .x x, .acc acc } push_bits3 acc } push_bit;
202
-
203
- trim = ?bits[] <
204
- /1 |1,
205
- /_ |_,
206
- /0 trim
207
- >;
208
-
209
- inv_loop = ?{ bits[] x, bits[] r } <
210
- { .x /_ if, .r then } .then,
211
- <
212
- { .x /0 x, .r |0 r },
213
- { .x /1 x, .r |1 r }
214
- > inv_loop
215
- > ?bits[];
216
-
217
- inv = ?bits[] { () x, _ r } inv_loop;
218
-
219
- inc_rev = ?{ bits[] x, bits[] r } <
220
- { .x /_ if, .r |1 then } .then,
221
- { .x /0 x, .r |1 r } inv_loop,
222
- { .x /1 x, .r |0 r } inc_rev
223
- > ?bits[];
224
-
225
- inc = ?bits[] { inv x, _ r } inc_rev;
226
-
227
- dec_rev = ?{ bits[] x, bits[] r } <
228
- { .x /1 x, .r |0 r } inv_loop,
229
- { .x /0 x, .r |1 r } dec_rev
230
- > ?bits[];
231
-
232
- dec = ?bits[] { inv x, _ r } dec_rev trim;
233
-
234
- cmp_loop = ?{ bits[] x, bits[] y, order[] order } <
235
- { .x /_ a, .y /_ b, .order then } .then,
236
- { .x /_ if, lt then } .then,
237
- { .y /_ if, gt then } .then,
238
- <
239
- { .x /0 x, .y /1 y, .order /eq lt order },
240
- { .x /1 x, .y /0 y, .order /eq gt order },
241
- { .x </0, /1> x, .y </0, /1> y, .order order }
242
- > cmp_loop
243
- > ?order[];
244
-
245
- cmp = ?nat_pair[] { .x x, .y y, eq order } cmp_loop;
246
-
247
- lt? = { cmp /lt if, () then } .then;
248
-
249
- plus_loop = ?{ bits[] x, bits[] y, bit[] c, bits[] r } <
250
- { .y /_ a, .c /0 b, { .x x, .r r } inv_loop trim then } .then,
251
- { .x /_ a, .c /0 b, { .y x, .r r } inv_loop trim then } .then,
252
- { .y /_ a, .c /1 b, { .x x, .r r } inc_rev trim then } .then,
253
- { .x /_ a, .c /1 b, { .y x, .r r } inc_rev trim then } .then,
254
- <
255
- { .x /0 x, .y /0 y, .c /0 |0 c, .r |0 r },
256
- { .x /0 x, .y /0 y, .c /1 |0 c, .r |1 r },
257
- { .x /0 x, .y /1 y, .c /0 |0 c, .r |1 r },
258
- { .x /0 x, .y /1 y, .c /1 |1 c, .r |0 r },
259
- { .x /1 x, .y /0 y, .c /0 |0 c, .r |1 r },
260
- { .x /1 x, .y /0 y, .c /1 |1 c, .r |0 r },
261
- { .x /1 x, .y /1 y, .c /0 |1 c, .r |0 r },
262
- { .x /1 x, .y /1 y, .c /1 |1 c, .r |1 r }
263
- > plus_loop
264
- > ?bits[];
265
-
266
- plus = ?nat_pair[] { .x inv x, .y inv y, 0 c, _ r } plus_loop;
267
-
268
- minus_loop = ?{ bits[] x, bits[] y, bit[] c, bits[] r } <
269
- { .y /_ a, .c /0 b, { .x x, .r r } inv_loop trim then } .then,
270
- { .y /_ a, .c /1 b, { .x x, .r r } dec_rev trim then } .then,
271
- <
272
- { .x /0 x, .y /0 y, .c /0 |0 c, .r |0 r },
273
- { .x /0 x, .y /0 y, .c /1 |1 c, .r |1 r },
274
- { .x /0 x, .y /1 y, .c /0 |1 c, .r |1 r },
275
- { .x /0 x, .y /1 y, .c /1 |1 c, .r |0 r },
276
- { .x /1 x, .y /0 y, .c /0 |0 c, .r |1 r },
277
- { .x /1 x, .y /0 y, .c /1 |0 c, .r |0 r },
278
- { .x /1 x, .y /1 y, .c /0 |0 c, .r |0 r },
279
- { .x /1 x, .y /1 y, .c /1 |1 c, .r |1 r }
280
- > minus_loop
281
- > ?bits[];
282
-
283
- minus = ?nat_pair[] { .x inv x, .y inv y, 0 c, _ r } minus_loop;
284
-
285
- twice = ?bits[] <
286
- /_ _,
287
- { inv |0 x, _ r } inv_loop
288
- > ?bits[];
289
-
290
- tenfold = ?bits[] <
291
- /_ _,
292
- inv { |0 x, |0 |0 |0 y, 0 c, _ r } plus_loop
293
- > ?bits[];
294
-
295
- lsb = ?bits[] <
296
- /0 { 0 bit, () rest },
297
- /1 { 1 bit, () rest },
298
- /_ { 0 bit, _ rest }
299
- >;
300
-
301
- _0_ = { 0 0, 0 1, 0 2, 0 3, 1 4, 1 5, 0 6 } |ascii;
302
- _1_ = { 1 0, 0 1, 0 2, 0 3, 1 4, 1 5, 0 6 } |ascii;
303
- _2_ = { 0 0, 1 1, 0 2, 0 3, 1 4, 1 5, 0 6 } |ascii;
304
- _3_ = { 1 0, 1 1, 0 2, 0 3, 1 4, 1 5, 0 6 } |ascii;
305
- _4_ = { 0 0, 0 1, 1 2, 0 3, 1 4, 1 5, 0 6 } |ascii;
306
- _5_ = { 1 0, 0 1, 1 2, 0 3, 1 4, 1 5, 0 6 } |ascii;
307
- _6_ = { 0 0, 1 1, 1 2, 0 3, 1 4, 1 5, 0 6 } |ascii;
308
- _7_ = { 1 0, 1 1, 1 2, 0 3, 1 4, 1 5, 0 6 } |ascii;
309
- _8_ = { 0 0, 0 1, 0 2, 1 3, 1 4, 1 5, 0 6 } |ascii;
310
- _9_ = { 1 0, 0 1, 0 2, 1 3, 1 4, 1 5, 0 6 } |ascii;
311
-
312
- _plus_ = { 1 0, 1 1, 0 2, 1 3, 0 4, 1 5, 0 6 } |ascii;
313
- _minus_ = { 1 0, 0 1, 1 2, 1 3, 0 4, 1 5, 0 6 } |ascii;
314
- _dot_ = { 0 0, 1 1, 1 2, 1 3, 0 4, 1 5, 0 6 } |ascii;
315
- _e_ = { 1 0, 0 1, 1 2, 0 3, 0 4, 1 5, 1 6 } |ascii;
316
- _E_ = { 1 0, 0 1, 1 2, 0 3, 0 4, 0 5, 1 6 } |ascii;
317
-
318
- -- Match leading 'e' from string[]. Input: ?string[], Output: ?string[] (tail of string)
319
- e_char = /cons { { .car x, _e_ y } eq_char? if, .cdr then } .then;
320
- -- Match leading 'E' from string[]. Input: ?string[], Output: ?string[] (tail of string)
321
- E_char = /cons { { .car x, _E_ y } eq_char? if, .cdr then } .then;
322
- -- Match leading '-' from string[]. Input: ?string[], Output: ?string[] (tail of string)
323
- minus_char = /cons { { .car x, _minus_ y } eq_char? if, .cdr then } .then;
324
- -- Match leading '+' from string[]. Input: ?string[], Output: ?string[] (tail of string)
325
- plus_char = /cons { { .car x, _plus_ y } eq_char? if, .cdr then } .then;
326
- -- Match leading '.' from string[]. Input: ?string[], Output: ?string[] (tail of string)
327
- dot_char = /cons { { .car x, _dot_ y } eq_char? if, .cdr then } .then;
328
-
329
- -- Parse one leading ASCII decimal digit ('0'..'9') from a character string.
330
- -- Input: ?string[] - character string (list of unicode code points)
331
- -- Output: ?digit_result[] - { rest: remaining string[], value: bits[] in 0..9 }
332
- -- Fails if the string is empty or does not start with a decimal digit ('0'..'9').
333
- scan_digit = ?string[] /cons { .cdr rest, .car <
334
- { () x, _0_ y } eq_char? _,
335
- { () x, _1_ y } eq_char? _ |1,
336
- { () x, _2_ y } eq_char? _ |0 |1,
337
- { () x, _3_ y } eq_char? _ |1 |1,
338
- { () x, _4_ y } eq_char? _ |0 |0 |1,
339
- { () x, _5_ y } eq_char? _ |1 |0 |1,
340
- { () x, _6_ y } eq_char? _ |0 |1 |1,
341
- { () x, _7_ y } eq_char? _ |1 |1 |1,
342
- { () x, _8_ y } eq_char? _ |0 |0 |0 |1,
343
- { () x, _9_ y } eq_char? _ |1 |0 |0 |1
344
- > value } ?digit_result[];
345
- parse_digit = scan_digit;
346
- digit = scan_digit;
347
-
348
- -- Unpack lowest 8 bits from bits[] into a byte record { 0..7 } and remaining bits.
349
- -- Input: bits[]
350
- -- Output: { byte: { 0..7 }, rest: bits[] }
351
- take_byte = lsb
352
- { .bit b0, .rest lsb tail }
353
- { .b0 b0, .tail.bit b1, .tail.rest lsb tail }
354
- { .b0 b0, .b1 b1, .tail.bit b2, .tail.rest lsb tail }
355
- { .b0 b0, .b1 b1, .b2 b2, .tail.bit b3, .tail.rest lsb tail }
356
- { .b0 b0, .b1 b1, .b2 b2, .b3 b3, .tail.bit b4, .tail.rest lsb tail }
357
- { .b0 b0, .b1 b1, .b2 b2, .b3 b3, .b4 b4, .tail.bit b5, .tail.rest lsb tail }
358
- { .b0 b0, .b1 b1, .b2 b2, .b3 b3, .b4 b4, .b5 b5, .tail.bit b6, .tail.rest lsb tail }
359
- { { .b0 0, .b1 1, .b2 2, .b3 3, .b4 4, .b5 5, .b6 6, .tail.bit 7 } byte, .tail.rest rest };
360
-
361
- -- Unpack lowest 4 bits from bits[] into a nibble record { 0..3 } and remaining bits.
362
- -- Input: bits[]
363
- -- Output: { byte: { 0..3 }, rest: bits[] }
364
- take_nibble = lsb
365
- { .bit b0, .rest lsb tail }
366
- { .b0 b0, .tail.bit b1, .tail.rest lsb tail }
367
- { .b0 b0, .b1 b1, .tail.bit b2, .tail.rest lsb tail }
368
- { { .b0 0, .b1 1, .b2 2, .tail.bit 3 } byte, .tail.rest rest };
369
-
370
- -- Unpack a 16-bit integer and plane from bits[].
371
- -- Input: bits[]
372
- -- Output: { lo: byte, hi: byte, plane: bits[] }
373
- unpack16 = inv take_byte
374
- { .byte lo, .rest take_byte high }
375
- { .lo lo, .high.byte hi, .high.rest inv trim plane };
376
-
377
- -- Bit field projections from a byte record { 0..7 }:
378
- field1 = { .0 0 };
379
- field2 = { .0 0, .1 1 };
380
- field3 = { .0 0, .1 1, .2 2 };
381
- field4 = { .0 0, .1 1, .2 2, .3 3 };
382
- field7 = { .0 0, .1 1, .2 2, .3 3, .4 4, .5 5, .6 6 };
383
-
384
- -- Convert unpacked 16-bit lo/hi bytes into an 11-bit IEEE exponent field exp11[].
385
- -- Input: { lo: byte, hi: byte, ... }
386
- -- Output: exp11[]
387
- exp_field = unpack16 {
388
- .lo.0 0, .lo.1 1, .lo.2 2, .lo.3 3, .lo.4 4, .lo.5 5, .lo.6 6, .lo.7 7,
389
- .hi.0 8, .hi.1 9, .hi.2 10
390
- };
391
-
392
- -- Unpack 52 fraction bits and lead bit from bits[].
393
- -- Input: bits[]
394
- -- Output: { fraction: frac52[], lead: bits[] }
395
- mantissa = take_byte { .byte b0, .rest take_byte tail }
396
- { .b0 b0, .tail.byte b1, .tail.rest take_byte tail }
397
- { .b0 b0, .b1 b1, .tail.byte b2, .tail.rest take_byte tail }
398
- { .b0 b0, .b1 b1, .b2 b2, .tail.byte b3, .tail.rest take_byte tail }
399
- { .b0 b0, .b1 b1, .b2 b2, .b3 b3, .tail.byte b4, .tail.rest take_byte tail }
400
- { .b0 b0, .b1 b1, .b2 b2, .b3 b3, .b4 b4, .tail.byte b5, .tail.rest take_nibble tail }
401
- { {
402
- .b0.0 0, .b0.1 1, .b0.2 2, .b0.3 3,
403
- .b0.4 4, .b0.5 5, .b0.6 6, .b0.7 7,
404
- .b1.0 8, .b1.1 9, .b1.2 10, .b1.3 11,
405
- .b1.4 12, .b1.5 13, .b1.6 14, .b1.7 15,
406
- .b2.0 16, .b2.1 17, .b2.2 18, .b2.3 19,
407
- .b2.4 20, .b2.5 21, .b2.6 22, .b2.7 23,
408
- .b3.0 24, .b3.1 25, .b3.2 26, .b3.3 27,
409
- .b3.4 28, .b3.5 29, .b3.6 30, .b3.7 31,
410
- .b4.0 32, .b4.1 33, .b4.2 34, .b4.3 35,
411
- .b4.4 36, .b4.5 37, .b4.6 38, .b4.7 39,
412
- .b5.0 40, .b5.1 41, .b5.2 42, .b5.3 43,
413
- .b5.4 44, .b5.5 45, .b5.6 46, .b5.7 47,
414
- .tail.byte.0 48, .tail.byte.1 49, .tail.byte.2 50, .tail.byte.3 51
415
- } fraction, .tail.rest lead };
416
-
417
- c52 = _ |0 |0 |1 |0 |1 |1;
418
- c400 = _ |0 |0 |0 |0 |1 |0 |0 |1 |1;
419
- c1023 = _ |1 |1 |1 |1 |1 |1 |1 |1 |1 |1;
420
- c2047 = _ |1 |1 |1 |1 |1 |1 |1 |1 |1 |1 |1;
421
-
422
- -- Extract small bitfields (1, 2, 3, 4, 7 bits) from reversed bits[].
423
- -- Input: bits[]
424
- -- Output: field struct (field1..field7)
425
- small1 = inv take_byte .byte field1;
426
- small2 = inv take_byte .byte field2;
427
- small3 = inv take_byte .byte field3;
428
- small4 = inv take_byte .byte field4;
429
- small7 = inv take_byte .byte field7;
430
-
431
- -- Parse an unsigned decimal number (leading integer digits, optional fraction).
432
- -- Input: ?string[]
433
- -- Output: ?decimal[] = { rest: string[], n: bits[], d: bits[], count: bits[] }
434
- unsigned_decimal = ?string[] scan_digit {
435
- .rest rest, .value n, 1b d, 1b count
436
- } <
437
- { .n /_ if, () then } .then,
438
- integer_digits
439
- > < fraction_first fraction_digits, () > ?decimal[];
440
-
441
- -- Parse signed decimal and optional exponent, converted to binary64 float64[].
442
- -- Input: { rest: string[], sign: sign[] }
443
- -- Output: ?result[] = { rest: string[], value: float64[] }
444
- signed_decimal = {
445
- .rest unsigned_decimal decimal, .sign sign
446
- } {
447
- .decimal decimal, .sign sign,
448
- < .decimal.rest exponent_part, { .decimal.rest rest, _ value, + sign } > exponent
449
- } {
450
- .exponent.rest rest,
451
- {
452
- .decimal.n n, .decimal.d d, .decimal.count count,
453
- .sign sign, .exponent exponent
454
- } decimal_float value
455
- } ?result[];
456
-
457
- -- Parse a decimal number with optional leading '-' or '+' sign.
458
- -- Input: ?string[]
459
- -- Output: ?result[] = { rest: string[], value: float64[] }
460
- number = ?string[] <
461
- minus_char { () rest, - sign },
462
- { () rest, + sign }
463
- > signed_decimal ?result[];
464
-
465
- -- Parse subsequent integer digits, accumulating n = n * 10 + digit.
466
- -- Input: ?decimal[]
467
- -- Output: ?decimal[]
468
- integer_digits = ?decimal[] <
469
- { .rest scan_digit digit, () decimal } {
470
- .digit.rest rest,
471
- { .decimal.n tenfold x, .digit.value y } plus n,
472
- .decimal.d d, .decimal.count inc count
473
- } integer_digits,
474
- ()
475
- > ?decimal[];
476
-
477
- -- Parse the decimal point '.' and the required first fraction digit.
478
- -- Input: ?decimal[]
479
- -- Output: ?decimal[]
480
- fraction_first = ?decimal[] {
481
- .rest dot_char scan_digit digit, () decimal
482
- } {
483
- .digit.rest rest,
484
- { .decimal.n tenfold x, .digit.value y } plus n,
485
- .decimal.d tenfold d, .decimal.count inc count
486
- };
487
-
488
- -- Parse subsequent fractional digits, accumulating n = n * 10 + digit and d = d * 10.
489
- -- Input: ?decimal[]
490
- -- Output: ?decimal[]
491
- fraction_digits = ?decimal[] <
492
- { .rest scan_digit digit, () decimal } {
493
- .digit.rest rest,
494
- { .decimal.n tenfold x, .digit.value y } plus n,
495
- .decimal.d tenfold d, .decimal.count inc count
496
- } fraction_digits,
497
- ()
498
- > ?decimal[];
499
-
500
- -- Parse the exponent component ('e' or 'E' followed by optional sign and digits).
501
- -- Input: ?string[]
502
- -- Output: { rest: string[], value: bits[], sign: sign[] }
503
- exponent_part = < e_char, E_char > <
504
- minus_char { () rest, - sign },
505
- plus_char { () rest, + sign },
506
- { () rest, + sign }
507
- > { .rest scan_digit exponent_digits digits, .sign sign }
508
- { .digits.rest rest, .digits.value value, .sign sign };
509
-
510
- -- Parse exponent digits into bits[] accumulator.
511
- -- Input: ?digit_result[] = { string[] rest, bits[] value }
512
- -- Output: ?digit_result[]
513
- exponent_digits = ?digit_result[] <
514
- { .rest scan_digit next, .value tenfold high }
515
- { .next.rest rest, { .high x, .next.value y } plus value }
516
- exponent_digits,
517
- ()
518
- > ?digit_result[];
519
-
520
- signed_zero = { .sign sign, exp0 exponent, frac0 fraction };
521
-
522
- decimal_float = <
523
- { .n /_ if, signed_zero then } .then,
524
- {
525
- () decimal,
526
- { .exponent.value x, { .count x, c400 y } plus y } cmp order
527
- } <
528
- {
529
- .order /gt a, .decimal.exponent.sign /- b,
530
- .decimal signed_zero then
531
- } .then,
532
- {
533
- .order /gt a, .decimal.exponent.sign /+ b,
534
- { .decimal.sign sign, exp_all1 exponent, frac0 fraction } then
535
- } .then,
536
- {
537
- .order </lt, /eq> if,
538
- {
539
- {
540
- .decimal.n n, .decimal.d d,
541
- .decimal.exponent.value power, .decimal.exponent.sign direction
542
- } scale_decimal ratio fields,
543
- .decimal.sign sign
544
- } { .sign sign, .fields.exponent exponent, .fields.fraction fraction } then
545
- } .then
546
- >
547
- > ?float64[];
548
-
549
- scale_decimal = ?{ bits[] n, bits[] d, bits[] power, sign[] direction } <
550
- { .power /_ if, { .n n, .d d } then } .then,
551
- <
552
- { .n tenfold n, .d d, .power dec power, .direction +? direction },
553
- { .n n, .d tenfold d, .power dec power, .direction -? direction }
554
- > scale_decimal
555
- > ?{ bits[] n, bits[] d };
556
-
557
- ratio = ?{ bits[] n, bits[] d } {
558
- .n n, .d d, .n x, .d y, c1023 exponent
559
- } align;
560
-
561
- align = ?{ bits[] n, bits[] d, bits[] x, bits[] y, bits[] exponent } <
562
- { .x /_ a, .y /_ b, { .n n, .d d, .exponent exponent } normalized then } .then,
563
- {
564
- .y /_ if,
565
- { .n n, .d inv denominator, .x steps, .exponent exponent } align_down then
566
- } .then,
567
- {
568
- .x /_ if,
569
- { .n inv numerator, .d d, .y steps, .exponent exponent } align_up then
570
- } .then,
571
- { .n n, .d d, .x </0, /1> x, .y </0, /1> y, .exponent exponent } align
572
- > ?{ exp11[] exponent, frac52[] fraction };
573
-
574
- align_down = ?{ bits[] n, bits[] denominator, bits[] steps, bits[] exponent } <
575
- {
576
- .steps /_ if,
577
- { .n n, .denominator inv d, .exponent exponent } normalized then
578
- } .then,
579
- {
580
- .exponent /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /_ if,
581
- { exp_all1 exponent, frac0 fraction } then
582
- } .then,
583
- { .n n, .denominator |0 denominator, .steps </0, /1> steps, .exponent inc exponent } align_down
584
- > ?{ exp11[] exponent, frac52[] fraction };
585
-
586
- align_up = ?{ bits[] numerator, bits[] d, bits[] steps, bits[] exponent } <
587
- {
588
- < .steps /_, .exponent /1 /_ > if,
589
- { .numerator inv n, .d d, .exponent exponent } normalized then
590
- } .then,
591
- { .numerator |0 numerator, .d d, .steps </0, /1> steps, .exponent dec exponent } align_up
592
- > ?{ exp11[] exponent, frac52[] fraction };
593
-
594
- ratio_less? = { .n x, .d y } lt?;
595
-
596
- normalized = ?{ bits[] n, bits[] d, bits[] exponent } <
597
- {
598
- ratio_less? if,
599
- .exponent /1 /_ and,
600
- { .n n, .d d, _ exponent, _ acc, c52 left } fraction_loop then
601
- } .then,
602
- {
603
- ratio_less? if,
604
- {
605
- { .n twice x, .d y } minus n,
606
- .d d, .exponent dec exponent, 1b acc, c52 left
607
- } fraction_loop then
608
- } .then,
609
- {
610
- { .n x, .d y } minus n,
611
- .d d, .exponent exponent, 1b acc, c52 left
612
- } fraction_loop
613
- > ?{ exp11[] exponent, frac52[] fraction };
614
-
615
- fraction_loop = ?{
616
- bits[] n, bits[] d, bits[] exponent, bits[] acc, bits[] left
617
- } <
618
- { .left /_ if, round then } .then,
619
- { .n twice n, .d d, .exponent exponent, .acc acc, .left dec left } <
620
- {
621
- { .n x, .d y } lt? .x n,
622
- .d d, .exponent exponent, .acc |0 acc, .left left
623
- },
624
- {
625
- { .n x, .d y } minus n,
626
- .d d, .exponent exponent, .acc |1 acc, .left left
627
- }
628
- > fraction_loop
629
- > ?{ exp11[] exponent, frac52[] fraction };
630
-
631
- inc_lsb = ?bits[] { () x, _ r } inc_rev inv;
632
-
633
- round = {
634
- () state, { .n twice x, .d y } cmp order
635
- } {
636
- .state.exponent exponent,
637
- <
638
- { .order /gt if, .state.acc inc_lsb then } .then,
639
- { .order /eq a, .state.acc /1 b, .state.acc inc_lsb then } .then,
640
- .state.acc
641
- > mantissa mantissa
642
- } {
643
- .mantissa.fraction fraction,
644
- <
645
- { .exponent /_ a, .mantissa.lead /1 b, 1b then } .then,
646
- { .mantissa.lead /0 /1 /_ if, .exponent inc then } .then,
647
- .exponent
648
- > exponent
649
- } {
650
- () fields,
651
- { .exponent x, c2047 y } cmp order
652
- } <
653
- { .order /lt if, { .fields.exponent exp_field exponent, .fields.fraction fraction } then } .then,
654
- { exp_all1 exponent, frac0 fraction }
655
- >;
656
-
657
- -- Text boundary helpers.
658
- _space_ = { 0 0, 0 1, 0 2, 0 3, 0 4, 1 5, 0 6 } |ascii;
659
- _tab_ = { 1 0, 0 1, 0 2, 1 3, 0 4, 0 5, 0 6 } |ascii;
660
- _lf_ = { 0 0, 1 1, 0 2, 1 3, 0 4, 0 5, 0 6 } |ascii;
661
- _cr_ = { 1 0, 0 1, 1 2, 1 3, 0 4, 0 5, 0 6 } |ascii;
662
- _I_ = { 1 0, 0 1, 0 2, 1 3, 0 4, 0 5, 1 6 } |ascii;
663
- _n_ = { 0 0, 1 1, 1 2, 1 3, 0 4, 1 5, 1 6 } |ascii;
664
- _f_ = { 0 0, 1 1, 1 2, 0 3, 0 4, 1 5, 1 6 } |ascii;
665
- _i_ = { 1 0, 0 1, 0 2, 1 3, 0 4, 1 5, 1 6 } |ascii;
666
- _t_ = { 0 0, 0 1, 1 2, 0 3, 1 4, 1 5, 1 6 } |ascii;
667
- _y_ = { 1 0, 0 1, 0 2, 1 3, 1 4, 1 5, 1 6 } |ascii;
668
- _N_ = { 0 0, 1 1, 1 2, 1 3, 0 4, 0 5, 1 6 } |ascii;
669
- _a_ = { 1 0, 0 1, 0 2, 0 3, 0 4, 1 5, 1 6 } |ascii;
670
-
671
- -- Match and consume specific ASCII characters from a string[].
672
- -- Input: ?string[]
673
- -- Output: ?string[] (tail of string)
674
- space = /cons { { .car x, _space_ y } eq_char? if, .cdr then } .then;
675
- tab = /cons { { .car x, _tab_ y } eq_char? if, .cdr then } .then;
676
- lf = /cons { { .car x, _lf_ y } eq_char? if, .cdr then } .then;
677
- cr = /cons { { .car x, _cr_ y } eq_char? if, .cdr then } .then;
678
- I_char = /cons { { .car x, _I_ y } eq_char? if, .cdr then } .then;
679
- n_char = /cons { { .car x, _n_ y } eq_char? if, .cdr then } .then;
680
- f_char = /cons { { .car x, _f_ y } eq_char? if, .cdr then } .then;
681
- i_char = /cons { { .car x, _i_ y } eq_char? if, .cdr then } .then;
682
- t_char = /cons { { .car x, _t_ y } eq_char? if, .cdr then } .then;
683
- y_char = /cons { { .car x, _y_ y } eq_char? if, .cdr then } .then;
684
- N_char = /cons { { .car x, _N_ y } eq_char? if, .cdr then } .then;
685
- a_char = /cons { { .car x, _a_ y } eq_char? if, .cdr then } .then;
686
-
687
- infinity_word = I_char n_char f_char i_char n_char i_char t_char y_char;
688
- nan_word = N_char a_char N_char;
689
-
690
- -- Parse special IEEE binary64 words (+/-Infinity, Infinity, +/-NaN, NaN).
691
- -- Input: ?string[]
692
- -- Output: ?result[] = { rest: string[], value: float64[] }
693
- special = <
694
- minus_char infinity_word { () rest, neg_inf value },
695
- plus_char infinity_word { () rest, pos_inf value },
696
- infinity_word { () rest, pos_inf value },
697
- < minus_char, plus_char, () > nan_word { () rest, qnan value }
698
- >;
699
-
700
- zero_text = { _0_ car, nil cdr } |cons;
701
- negative_zero_text = { _minus_ car, zero_text cdr } |cons;
702
- infinity_text = {
703
- _I_ car, { _n_ car, { _f_ car, { _i_ car, {
704
- _n_ car, { _i_ car, { _t_ car, { _y_ car, nil cdr } |cons cdr } |cons cdr } |cons cdr
705
- } |cons cdr } |cons cdr } |cons cdr } |cons cdr
706
- } |cons;
707
- negative_infinity_text = { _minus_ car, infinity_text cdr } |cons;
708
- nan_text = { _N_ car, { _a_ car, { _N_ car, nil cdr } |cons cdr } |cons cdr } |cons;
709
-
710
- c1074 = _ |0 |1 |0 |0 |1 |1 |0 |0 |0 |0 |1;
711
- c1075 = _ |1 |1 |0 |0 |1 |1 |0 |0 |0 |0 |1;
712
-
713
- exponent_bits = { () source, _ acc }
714
- { .source source,
715
- { { .source.0 0, .source.1 1, .source.2 2, .source.3 3 } x, .acc acc } push_bits4 acc
716
- }
717
- { .source source,
718
- { { .source.4 0, .source.5 1, .source.6 2, .source.7 3 } x, .acc acc } push_bits4 acc
719
- }
720
- { .source source,
721
- { { .source.8 0, .source.9 1, .source.10 2 } x, .acc acc } push_bits3 acc
722
- }
723
- .acc;
724
-
725
- fraction_bits = { () source, _ acc }
726
- { .source source,
727
- { { .source.0 0, .source.1 1, .source.2 2, .source.3 3 } x, .acc acc } push_bits4 acc
728
- }
729
- { .source source,
730
- { { .source.4 0, .source.5 1, .source.6 2, .source.7 3 } x, .acc acc } push_bits4 acc
731
- }
732
- { .source source,
733
- { { .source.8 0, .source.9 1, .source.10 2, .source.11 3 } x, .acc acc } push_bits4 acc
734
- }
735
- { .source source,
736
- { { .source.12 0, .source.13 1, .source.14 2, .source.15 3 } x, .acc acc } push_bits4 acc
737
- }
738
- { .source source,
739
- { { .source.16 0, .source.17 1, .source.18 2, .source.19 3 } x, .acc acc } push_bits4 acc
740
- }
741
- { .source source,
742
- { { .source.20 0, .source.21 1, .source.22 2, .source.23 3 } x, .acc acc } push_bits4 acc
743
- }
744
- { .source source,
745
- { { .source.24 0, .source.25 1, .source.26 2, .source.27 3 } x, .acc acc } push_bits4 acc
746
- }
747
- { .source source,
748
- { { .source.28 0, .source.29 1, .source.30 2, .source.31 3 } x, .acc acc } push_bits4 acc
749
- }
750
- { .source source,
751
- { { .source.32 0, .source.33 1, .source.34 2, .source.35 3 } x, .acc acc } push_bits4 acc
752
- }
753
- { .source source,
754
- { { .source.36 0, .source.37 1, .source.38 2, .source.39 3 } x, .acc acc } push_bits4 acc
755
- }
756
- { .source source,
757
- { { .source.40 0, .source.41 1, .source.42 2, .source.43 3 } x, .acc acc } push_bits4 acc
758
- }
759
- { .source source,
760
- { { .source.44 0, .source.45 1, .source.46 2, .source.47 3 } x, .acc acc } push_bits4 acc
761
- }
762
- { .source source,
763
- { { .source.48 0, .source.49 1, .source.50 2, .source.51 3 } x, .acc acc } push_bits4 acc
764
- }
765
- .acc;
766
-
767
- -- Decimal digit arithmetic for exact, round-trip-safe printing.
768
- list[X] = < {} nil, { X car, L cdr } cons > = L;
769
- digit_value[] = < {} 0, {} 1, {} 2, {} 3, {} 4, {} 5, {} 6, {} 7, {} 8, {} 9 >;
770
- digits[] = list[digit_value[]];
771
- factor[] = < {} 2, {} 5 >;
772
-
773
- d0 = {} |0;
774
- d1 = {} |1;
775
- d2 = {} |2;
776
- d3 = {} |3;
777
- d4 = {} |4;
778
- d5 = {} |5;
779
- d6 = {} |6;
780
- d7 = {} |7;
781
- d8 = {} |8;
782
- d9 = {} |9;
783
-
784
- digit_ascii = <
785
- /0 _0_, /1 _1_, /2 _2_, /3 _3_, /4 _4_,
786
- /5 _5_, /6 _6_, /7 _7_, /8 _8_, /9 _9_
787
- >;
788
-
789
- digit_mul2 = <
790
- { .digit /0 a, .carry /0 b } { d0 digit, d0 carry },
791
- { .digit /0 a, .carry /1 b } { d1 digit, d0 carry },
792
- { .digit /1 a, .carry /0 b } { d2 digit, d0 carry },
793
- { .digit /1 a, .carry /1 b } { d3 digit, d0 carry },
794
- { .digit /2 a, .carry /0 b } { d4 digit, d0 carry },
795
- { .digit /2 a, .carry /1 b } { d5 digit, d0 carry },
796
- { .digit /3 a, .carry /0 b } { d6 digit, d0 carry },
797
- { .digit /3 a, .carry /1 b } { d7 digit, d0 carry },
798
- { .digit /4 a, .carry /0 b } { d8 digit, d0 carry },
799
- { .digit /4 a, .carry /1 b } { d9 digit, d0 carry },
800
- { .digit /5 a, .carry /0 b } { d0 digit, d1 carry },
801
- { .digit /5 a, .carry /1 b } { d1 digit, d1 carry },
802
- { .digit /6 a, .carry /0 b } { d2 digit, d1 carry },
803
- { .digit /6 a, .carry /1 b } { d3 digit, d1 carry },
804
- { .digit /7 a, .carry /0 b } { d4 digit, d1 carry },
805
- { .digit /7 a, .carry /1 b } { d5 digit, d1 carry },
806
- { .digit /8 a, .carry /0 b } { d6 digit, d1 carry },
807
- { .digit /8 a, .carry /1 b } { d7 digit, d1 carry },
808
- { .digit /9 a, .carry /0 b } { d8 digit, d1 carry },
809
- { .digit /9 a, .carry /1 b } { d9 digit, d1 carry }
810
- >;
811
-
812
- digit_mul5 = <
813
- { .digit /0 a, .carry /0 b } { d0 digit, d0 carry },
814
- { .digit /0 a, .carry /1 b } { d1 digit, d0 carry },
815
- { .digit /0 a, .carry /2 b } { d2 digit, d0 carry },
816
- { .digit /0 a, .carry /3 b } { d3 digit, d0 carry },
817
- { .digit /0 a, .carry /4 b } { d4 digit, d0 carry },
818
- { .digit /1 a, .carry /0 b } { d5 digit, d0 carry },
819
- { .digit /1 a, .carry /1 b } { d6 digit, d0 carry },
820
- { .digit /1 a, .carry /2 b } { d7 digit, d0 carry },
821
- { .digit /1 a, .carry /3 b } { d8 digit, d0 carry },
822
- { .digit /1 a, .carry /4 b } { d9 digit, d0 carry },
823
- { .digit /2 a, .carry /0 b } { d0 digit, d1 carry },
824
- { .digit /2 a, .carry /1 b } { d1 digit, d1 carry },
825
- { .digit /2 a, .carry /2 b } { d2 digit, d1 carry },
826
- { .digit /2 a, .carry /3 b } { d3 digit, d1 carry },
827
- { .digit /2 a, .carry /4 b } { d4 digit, d1 carry },
828
- { .digit /3 a, .carry /0 b } { d5 digit, d1 carry },
829
- { .digit /3 a, .carry /1 b } { d6 digit, d1 carry },
830
- { .digit /3 a, .carry /2 b } { d7 digit, d1 carry },
831
- { .digit /3 a, .carry /3 b } { d8 digit, d1 carry },
832
- { .digit /3 a, .carry /4 b } { d9 digit, d1 carry },
833
- { .digit /4 a, .carry /0 b } { d0 digit, d2 carry },
834
- { .digit /4 a, .carry /1 b } { d1 digit, d2 carry },
835
- { .digit /4 a, .carry /2 b } { d2 digit, d2 carry },
836
- { .digit /4 a, .carry /3 b } { d3 digit, d2 carry },
837
- { .digit /4 a, .carry /4 b } { d4 digit, d2 carry },
838
- { .digit /5 a, .carry /0 b } { d5 digit, d2 carry },
839
- { .digit /5 a, .carry /1 b } { d6 digit, d2 carry },
840
- { .digit /5 a, .carry /2 b } { d7 digit, d2 carry },
841
- { .digit /5 a, .carry /3 b } { d8 digit, d2 carry },
842
- { .digit /5 a, .carry /4 b } { d9 digit, d2 carry },
843
- { .digit /6 a, .carry /0 b } { d0 digit, d3 carry },
844
- { .digit /6 a, .carry /1 b } { d1 digit, d3 carry },
845
- { .digit /6 a, .carry /2 b } { d2 digit, d3 carry },
846
- { .digit /6 a, .carry /3 b } { d3 digit, d3 carry },
847
- { .digit /6 a, .carry /4 b } { d4 digit, d3 carry },
848
- { .digit /7 a, .carry /0 b } { d5 digit, d3 carry },
849
- { .digit /7 a, .carry /1 b } { d6 digit, d3 carry },
850
- { .digit /7 a, .carry /2 b } { d7 digit, d3 carry },
851
- { .digit /7 a, .carry /3 b } { d8 digit, d3 carry },
852
- { .digit /7 a, .carry /4 b } { d9 digit, d3 carry },
853
- { .digit /8 a, .carry /0 b } { d0 digit, d4 carry },
854
- { .digit /8 a, .carry /1 b } { d1 digit, d4 carry },
855
- { .digit /8 a, .carry /2 b } { d2 digit, d4 carry },
856
- { .digit /8 a, .carry /3 b } { d3 digit, d4 carry },
857
- { .digit /8 a, .carry /4 b } { d4 digit, d4 carry },
858
- { .digit /9 a, .carry /0 b } { d5 digit, d4 carry },
859
- { .digit /9 a, .carry /1 b } { d6 digit, d4 carry },
860
- { .digit /9 a, .carry /2 b } { d7 digit, d4 carry },
861
- { .digit /9 a, .carry /3 b } { d8 digit, d4 carry },
862
- { .digit /9 a, .carry /4 b } { d9 digit, d4 carry }
863
- >;
864
-
865
- reverse_loop = ?{ list[X] x, list[X] acc } <
866
- { .x /nil if, .acc then } .then,
867
- { .x /cons .cdr x, { .x /cons .car car, .acc cdr } |cons acc } reverse_loop
868
- > ?list[X];
869
-
870
- reverse = ?list[X] { () x, nil acc } reverse_loop ?list[X];
871
-
872
- digits_multiply_loop = ?{
873
- digits[] x, factor[] factor, digit_value[] carry, digits[] acc
874
- } <
875
- { .x /nil a, .carry /0 b, .acc reverse then } .then,
876
- { .x /nil if, { .carry car, .acc cdr } |cons reverse then } .then,
877
- {
878
- .x /cons .cdr x, .factor factor, .acc acc,
879
- { .x /cons .car digit, .carry carry, .factor factor } <
880
- { .factor /2 if, { .digit digit, .carry carry } digit_mul2 then } .then,
881
- { .factor /5 if, { .digit digit, .carry carry } digit_mul5 then } .then
882
- > next
883
- } { .x x, .factor factor, .next.carry carry, { .next.digit car, .acc cdr } |cons acc }
884
- digits_multiply_loop
885
- > ?digits[];
886
-
887
- digits_multiply = ?{ digits[] digits, factor[] factor, digit_value[] carry } {
888
- .digits x, .factor factor, .carry carry, nil acc
889
- } digits_multiply_loop;
890
-
891
- binary_digits_loop = ?{ bits[] bits, digits[] digits } <
892
- { .bits /_ if, .digits then } .then,
893
- <
894
- { .bits /0 bits, { .digits digits, d2 factor, d0 carry } digits_multiply digits },
895
- { .bits /1 bits, { .digits digits, d2 factor, d1 carry } digits_multiply digits }
896
- > binary_digits_loop
897
- > ?digits[];
898
-
899
- binary_digits = ?bits[] {
900
- () bits, { d0 car, nil cdr } |cons digits
901
- } binary_digits_loop;
902
-
903
- digits_power = ?{ digits[] digits, factor[] factor, bits[] steps } <
904
- { .steps /_ if, .digits then } .then,
905
- {
906
- { .digits digits, .factor factor, d0 carry } digits_multiply digits,
907
- .factor factor,
908
- .steps dec steps
909
- } digits_power
910
- > ?digits[];
911
-
912
- -- Remove binary factors of two before generating fractional decimal places.
913
- cancel_twos = ?{ bits[] reversed, bits[] places } <
914
- { < .places /_, .reversed </1 {}, /_> > if, () then } .then,
915
- { .reversed /0 reversed, .places dec places } cancel_twos
916
- > ?{ bits[] reversed, bits[] places };
917
-
918
- fraction_digits_print = cancel_twos {
919
- .reversed inv trim binary_digits digits,
920
- d5 factor, .places steps, .places places
921
- } { { .digits digits, .factor factor, .steps steps } digits_power digits, .places places };
922
-
923
- print_ratio = ?float64[] {
924
- .fraction fraction_bits fraction,
925
- .exponent exponent_bits trim exponent
926
- } <
927
- {
928
- .exponent /_ if,
929
- { .fraction inv reversed, c1074 places } fraction_digits_print then
930
- } .then,
931
- {
932
- { .exponent x, c1075 y } lt? if,
933
- { .fraction |1 inv reversed,
934
- { c1075 x, .exponent y } minus places } fraction_digits_print then
935
- } .then,
936
- {
937
- .fraction |1 binary_digits digits, d2 factor,
938
- { .exponent x, c1075 y } minus steps
939
- } digits_power { () digits, _ places }
940
- >;
941
-
942
- -- Consume least-significant digits, prepending characters in display order.
943
- fraction_text = ?{ digits[] digits, bits[] places, string[] acc } <
944
- { .places /_ if, { .digits digits, .acc acc } then } .then,
945
- {
946
- < .digits /cons .cdr, nil > digits,
947
- .places dec places,
948
- { < .digits /cons .car, d0 > digit_ascii car, .acc cdr } |cons acc
949
- } fraction_text
950
- > ?{ digits[] digits, string[] acc };
951
-
952
- integer_text_loop = ?{ digits[] digits, string[] acc } <
953
- { .digits /nil if, .acc then } .then,
954
- {
955
- .digits /cons .cdr digits,
956
- { .digits /cons .car digit_ascii car, .acc cdr } |cons acc
957
- } integer_text_loop
958
- > ?string[];
959
-
960
- integer_text = ?{ digits[] digits, string[] acc } <
961
- { .digits /nil if, { _0_ car, .acc cdr } |cons then } .then,
962
- integer_text_loop
963
- > ?string[];
964
-
965
- format_decimal = ?{ digits[] digits, bits[] places } <
966
- { .places /_ if, { .digits digits, nil acc } integer_text then } .then,
967
- { .digits digits, .places places, nil acc } fraction_text
968
- { .digits digits, { _dot_ car, .acc cdr } |cons acc } integer_text
969
- > ?string[];