@fraczak/k 8.5.0 → 8.6.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.
- package/CHANGELOG.md +35 -0
- package/DOCS/CODECS.md +54 -0
- package/DOCS/REPL.md +1 -0
- package/Examples/djson.k +443 -328
- package/Examples/ieee-arithmetics.k +7183 -0
- package/Examples/ieee.k +895 -7374
- package/Examples/poly.k +1 -1
- package/README.md +3 -2
- package/backends/llvm/src/kvm2llvm.mjs +5 -4
- package/backends/llvm/src/llvm.mjs +2 -1
- package/backends/llvm/tests/integration.sh +7 -0
- package/backends/llvm/tests/test-llvm.mjs +20 -0
- package/backends/wasm/src/kvm2wasm.mjs +64 -18
- package/backends/wasm/tests/integration.sh +7 -1
- package/backends/wasm/tests/test-wasm.mjs +86 -2
- package/browser/shims/fs.mjs +1 -0
- package/canonical-pattern.mjs +231 -2
- package/object.mjs +18 -9
- package/package.json +1 -1
- package/repl.mjs +91 -9
- package/scripts/benchmark-idempotency.mjs +20 -5
- package/scripts/benchmark.mjs +42 -10
- package/scripts/build-repl-html.mjs +4 -0
- package/scripts/perf-ieee.mjs +9 -16
- package/scripts/render-benchmark.mjs +1 -1
- package/scripts/run-tests.mjs +2 -0
- package/tests/README.md +2 -0
- package/tests/integration.sh +2 -1
- package/tests/test-djson-methods.mjs +11 -1
- package/tests/test-ieee-arithmetic.mjs +3 -1
- package/tests/test-ieee-text.mjs +59 -0
- package/tests/test-k-object.mjs +9 -0
- package/tests/test-parse-json.mjs +96 -0
- package/tests/test-repl-html.mjs +21 -8
- package/tests/test-repl.mjs +105 -2
package/Examples/djson.k
CHANGED
|
@@ -5,89 +5,62 @@
|
|
|
5
5
|
-- §1. Named Pattern Definitions & Component Schemas
|
|
6
6
|
---------------------------------------------------------------
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
plane0[] = {
|
|
63
|
-
bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4,
|
|
64
|
-
bit[] 5, bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
plane_02_10[] = <
|
|
68
|
-
{ bit[] 0 } p02_03,
|
|
69
|
-
{ bit[] 0, bit[] 1 } p04_07,
|
|
70
|
-
{ bit[] 0, bit[] 1, bit[] 2 } p08_0F,
|
|
71
|
-
{} p10
|
|
72
|
-
>;
|
|
73
|
-
|
|
74
|
-
unicode[] = <
|
|
75
|
-
ascii[] ascii,
|
|
76
|
-
{ bmp_hi_common[] hi, byte[] lo } bmp_common,
|
|
77
|
-
{ bmp_hi_private_use[] hi, byte[] lo } bmp_private_use,
|
|
78
|
-
plane0[] plane0,
|
|
79
|
-
{ byte[] mid, byte[] lo } supplementary_plane1,
|
|
80
|
-
{ plane_02_10[] plane, byte[] mid, byte[] lo } supplementary_planes2_16
|
|
81
|
-
>;
|
|
82
|
-
|
|
83
|
-
string[] = < {} nil, { unicode[] car, S cdr } cons > = S;
|
|
8
|
+
-- Defined in 'core.k':
|
|
9
|
+
bits[] = @VtPHxGf5GNMzzyVFxtv7gegFfJRYapBGtCeyV56bs5Zb[];
|
|
10
|
+
unicode[] = @AhRir67FipnHnJfDU7fSQ3Xhcg6BTJYwtq6HGsWwZtbi[];
|
|
11
|
+
string[] = @Pt3MwQwzUPxQKu58zXR3qUBHWTzfycx5bSZGzcK6EZQK[];
|
|
12
|
+
|
|
13
|
+
-- Defined in 'ieee.k':
|
|
14
|
+
number[] = @AR4sFGMwXgjj5p7dhi8sJKDWNbrXxLefQUCBRNm5fP6t[];
|
|
15
|
+
list[X] = @JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe[X];
|
|
16
|
+
|
|
17
|
+
-- Aliases to definitions imported from 'ieee.k':
|
|
18
|
+
0b = @9sQsLXUsbMiGLXkQgJ6yaTLUBQGLgViQjKE3hXAYyRyv;
|
|
19
|
+
_a_ = @CSQsWPGdgDer9h4vkQrpyxPAx3HHpq9e3tBCmFt5JBft;
|
|
20
|
+
_cr_ = @mWxEf9tNC5Zza2n2asRACakK6wbaFty5FTeymgxNCtKh;
|
|
21
|
+
_e_ = @Cp2mSUTm7hs9NqXJUQCXmqjiNbcCWAEmTrYr3erBrcTy;
|
|
22
|
+
_E_ = @EMwpiv7Tq8gBC2YiNK39za9v9fNXzgShuDH4JiAaQKPJ;
|
|
23
|
+
_f_ = @W5EyUxJ2QtepSwEkMKXCZAeUVctfNXG9xAJtGiffHxpg;
|
|
24
|
+
_lf_ = @DdFQhsDcSVY6kcLFYhFRRwV4AMY7FnWQrdAVC9W3u757;
|
|
25
|
+
_tab_ = @HSry2A9J4g5d7KL4x7cbnfLPsAWgjS4fuvzfiHdVWtps;
|
|
26
|
+
a_char = @LBFH7KivecqSzUZSvF6h9pcsYLzrg6GdfLJbxydvP8kD;
|
|
27
|
+
b0 = @EHvJE7XLDHFp85gcBQGN5j3EDqUi3ChEdMtByVxF7mk9;
|
|
28
|
+
b1 = @yhn2hGiP2VyS2BeYjrSrtrucugXJNDqWpNEYGMCjQQqV;
|
|
29
|
+
c1023 = @6QziDQ3acyiQV7XQsX77uhPbTDvpdVGALzKAcxhwj5zQ;
|
|
30
|
+
digit = @BGtG3xygJZDwEzkPwUZgNVasnCMJnuRiFxXUsFb9W7wW;
|
|
31
|
+
e_char = @JPp573QKGFNnkGdbZNeRTwFDZU3wpNuhTYhwdsAVaM3A;
|
|
32
|
+
eq_char? = @H7x5yhf73jnDkJmKDsGtgng2AdBWT7cydc9aiP99Ppny;
|
|
33
|
+
f_char = @5t5Jd89TCk3WE9bFb6f5HR9tfwQ5PFTVk6iwBrJi5ASd;
|
|
34
|
+
field7 = @SFuC8GpSvwemLh82Kz4t6h9Bar5ieF4v28ibNZjQb48a;
|
|
35
|
+
finite_float64? = @eDmk2p9N36L83zS9SHZFgu3zxpSydaj8vi9GqykygVdJ;
|
|
36
|
+
inc = @3n3LEgaACKTzgLhEFH6r8PaGbNiPyV3WGKkLhpyf3FUs;
|
|
37
|
+
inv = @9HG3xPV4skXm8TRWTQZtpMCkLvMuuppUuRWsPNQsVsqK;
|
|
38
|
+
inv_loop = @DWXKJJnBu3bXPpNdp2i8S9yyd6RMaafpg4UNk7KiY8mJ;
|
|
39
|
+
lt? = @paDd2Hc35YnSGKVxQnG3hseDVFeY8K85M73ZpBj2bGvz;
|
|
40
|
+
minus = @3Q9Gfy4iXs5cEdTUBNPb2Lz554djkNQVwLR4iKhytHKL;
|
|
41
|
+
minus_char = @GLrjZ6x7by7X6Q9ZBBgWQvQPdpaDdpGy7cVyTTG7r44y;
|
|
42
|
+
n_char = @YtiKcZQ9rry3cueGvu9yx4VmVkeQUfNkmQmfspWrzdvp;
|
|
43
|
+
natToFloat64 = @JsjYdrgdYfgctaEhYbNrsRmDmHwV8NnwPiuSLqQeQWGb;
|
|
44
|
+
plus = @h3uz7hegVewstX8QqxwRNzTJ9Rmf5Lzcb9bXxAQXY3bb;
|
|
45
|
+
push_bit = @kjzD5wy3vVihSyEmpqUTiiQaTc2vikejKC66D7s5DfE2;
|
|
46
|
+
push_bits1 = @5xxBWEQkTGDnHkuyA4hPaqswq6DDymPR53Z6vX6DANyn;
|
|
47
|
+
push_bits2 = @agFtk8Zs8KrTR6zu9Nx5KDJCHTU8biAR6LyhGdd9gGqR;
|
|
48
|
+
push_bits3 = @B9yHHLyYAyUfQfvXzghbPutyN852tXraqbhbPpCBHRmW;
|
|
49
|
+
push_bits4 = @iz9bg6NQF7RXzM6ibW3MfBrUAYYXyUKhFWTHJjHn9Fvy;
|
|
50
|
+
reverse = @MugpktbC3D4hLzaRVyRH2W64CK9ZuhZ29AkJBEjprV72;
|
|
51
|
+
scanFloat64 = @GMczQMpsqVR7qdHgQEaqYuxR84hrYxizgcsZ2axayY8H;
|
|
52
|
+
small1 = @FASMJ5EfHNmaMqy88FTYZJy7T8ps6uCzZetJ55zsEPgz;
|
|
53
|
+
small2 = @fLHcGBbuAm73evJwKH2maEt3AK3VzMQuFbfyfUKXRBfD;
|
|
54
|
+
small3 = @3rbHuQNth7JyLehzdQzTYG8gBE5JGUwTQdPYiAkWnkQ4;
|
|
55
|
+
small4 = @M2vX2c8M5EgaQyEctasyssWd6rcn25hjb2MW5UWvbJtY;
|
|
56
|
+
small7 = @yxeyAa4XY88QP5gRcuKzmnCKrrnSDd7M2p6QGjjGnS2N;
|
|
57
|
+
t_char = @Z86xynhBGBTpSNMrXKbZwi2xwvCrpMR5VcCBVTgspFZY;
|
|
58
|
+
trim = @89p3L4zjJpYP8qadbebPKXAszHmqKm9sk5jYzTga8gKK;
|
|
59
|
+
unpack16 = @YCBHPaRPdxbgQ93gwvvTMhqXCtPaiuBLgpRek6LLt7pE;
|
|
60
|
+
ws = @Hd35uZ2UMYiPdg9dxVNXxnjFWq6gDYPLiaZGr99dkixt;
|
|
84
61
|
|
|
85
62
|
bool[] = < {} true, {} false >;
|
|
86
63
|
|
|
87
|
-
bits[] = < {} _, B 0, B 1 > = B;
|
|
88
|
-
|
|
89
|
-
list[X] = < {} nil, { X car, L cdr } cons > = L;
|
|
90
|
-
|
|
91
64
|
opt[V] = < {} nil, V val >;
|
|
92
65
|
|
|
93
66
|
btrie[V] = <
|
|
@@ -112,7 +85,6 @@ djson[] = <
|
|
|
112
85
|
-- §2. Constructors and Projections
|
|
113
86
|
---------------------------------------------------------------
|
|
114
87
|
|
|
115
|
-
0b = {} | _;
|
|
116
88
|
0 = 0b | 0;
|
|
117
89
|
1 = 0b | 1;
|
|
118
90
|
|
|
@@ -225,10 +197,12 @@ dot = ?{ K key, T object } { .key ?string[] key, .object ?btrie[V] t } <
|
|
|
225
197
|
>;
|
|
226
198
|
|
|
227
199
|
-- Low-level lookup ?bits[] index in list trie: { index, list } -> val
|
|
228
|
-
|
|
200
|
+
nth_trie = ?{ bits[] index, btrie[V] list } <
|
|
229
201
|
{ .index /_ if, .list /trie .val /val then } .then,
|
|
230
|
-
|
|
231
|
-
|
|
202
|
+
<
|
|
203
|
+
{ .index /0 index, .list /trie .0 list },
|
|
204
|
+
{ .index /1 index, .list /trie .1 list }
|
|
205
|
+
> nth_trie
|
|
232
206
|
>;
|
|
233
207
|
|
|
234
208
|
-- Primary accessor: lookup string key in ?djson[] object -> ?djson[] val
|
|
@@ -241,7 +215,7 @@ get = ?{ string[] key, djson[] json } {
|
|
|
241
215
|
nth = ?{ K index, djson[] json } {
|
|
242
216
|
.index ?bits[] index,
|
|
243
217
|
.json /list list
|
|
244
|
-
}
|
|
218
|
+
} nth_trie;
|
|
245
219
|
|
|
246
220
|
-- Filter: succeeds as identity if ?djson[] object contains string key
|
|
247
221
|
has_key? = { get if, () then } .then;
|
|
@@ -255,15 +229,6 @@ has_nth? = has_index?;
|
|
|
255
229
|
-- §5. Trie Update & Removal (put, remove, put_nth, remove_nth)
|
|
256
230
|
---------------------------------------------------------------
|
|
257
231
|
|
|
258
|
-
push_bit = ?{ < {} 0, {} 1 > b, < {} _, BITS 0, BITS 1 >=BITS acc } <
|
|
259
|
-
{ .b /0 if, .acc |0 then } .then,
|
|
260
|
-
{ .b /1 if, .acc |1 then } .then
|
|
261
|
-
> ?bits[];
|
|
262
|
-
|
|
263
|
-
push_bits1 = { .x .0 b, .acc acc } push_bit;
|
|
264
|
-
push_bits2 = { .x .1 b, { .x x, .acc acc } push_bits1 acc } push_bit;
|
|
265
|
-
push_bits3 = { .x .2 b, { .x x, .acc acc } push_bits2 acc } push_bit;
|
|
266
|
-
push_bits4 = { .x .3 b, { .x x, .acc acc } push_bits3 acc } push_bit;
|
|
267
232
|
push_bits5 = { .x .4 b, { .x x, .acc acc } push_bits4 acc } push_bit;
|
|
268
233
|
push_bits6 = { .x .5 b, { .x x, .acc acc } push_bits5 acc } push_bit;
|
|
269
234
|
push_bits7 = { .x .6 b, { .x x, .acc acc } push_bits6 acc } push_bit;
|
|
@@ -320,11 +285,16 @@ prepend_char_bits = <
|
|
|
320
285
|
} push_plane2_16 |1 |1 |1 |1 |1
|
|
321
286
|
> ?bits[];
|
|
322
287
|
|
|
323
|
-
|
|
324
|
-
/nil
|
|
325
|
-
|
|
288
|
+
str_to_bits_loop = ?{ string[] str, bits[] acc } <
|
|
289
|
+
{ .str /nil if, .acc then } .then,
|
|
290
|
+
{
|
|
291
|
+
.str /cons .cdr str,
|
|
292
|
+
{ .str /cons .car char, .acc acc } prepend_char_bits acc
|
|
293
|
+
} str_to_bits_loop
|
|
326
294
|
> ?bits[];
|
|
327
295
|
|
|
296
|
+
str_to_bits = ?string[] { reverse str, 0b acc } str_to_bits_loop;
|
|
297
|
+
|
|
328
298
|
ensure_trie = ?btrie[V] < /nil { nil val, nil 0, nil 1 }, /trie () >;
|
|
329
299
|
|
|
330
300
|
-- Insert or replace { index, val, list } -> list
|
|
@@ -356,7 +326,7 @@ put_nth = ?{ K index, V val, T list } = X {
|
|
|
356
326
|
> ?btrie[V];
|
|
357
327
|
|
|
358
328
|
-- Remove entry at ?bits[] index from trie: { index, list } -> list
|
|
359
|
-
remove_nth = ?{ K index, T list } = X
|
|
329
|
+
remove_nth = ?{ K index, T list } = X
|
|
360
330
|
{ .index index, .list t } <
|
|
361
331
|
{ .t /nil if, nil then } .then,
|
|
362
332
|
{
|
|
@@ -395,7 +365,7 @@ remove = ?{ K key, O object } {
|
|
|
395
365
|
} remove_nth;
|
|
396
366
|
|
|
397
367
|
-- Set string key in ?djson[] object: { key, val, json } -> json
|
|
398
|
-
set = {.key key, .val val, .json /object object}
|
|
368
|
+
set = { .key key, .val val, .json /object object }
|
|
399
369
|
put |object ?djson[];
|
|
400
370
|
|
|
401
371
|
-- Remove string key from ?djson[] object: { key, json } -> json
|
|
@@ -421,47 +391,28 @@ del_nth = ?{ bits[] index, djson[] json } {
|
|
|
421
391
|
-- §6. Cardinality & Length Relations
|
|
422
392
|
---------------------------------------------------------------
|
|
423
393
|
|
|
424
|
-
|
|
425
|
-
{ .x /_ if, .r then } .then,
|
|
426
|
-
{ .x /0 x, .r |0 r } _inv,
|
|
427
|
-
{ .x /1 x, .r |1 r } _inv
|
|
428
|
-
> ?bits[];
|
|
429
|
-
_inc = ?{ < {} _, B 0, B 1 >=B x, B y } <
|
|
430
|
-
{ .x /_ if, .y |1 then } .then,
|
|
431
|
-
{ .x /0 x, .y |1 r } _inv,
|
|
432
|
-
{ .x /1 x, .y |0 y } _inc
|
|
433
|
-
> ?bits[];
|
|
434
|
-
inc_nat = { { () x, 0b r } _inv x, 0b y } _inc;
|
|
435
|
-
|
|
436
|
-
_trie_size = ?{
|
|
437
|
-
< {} nil, { < {} nil, V val > val, T 0, T 1 } trie >=T t,
|
|
438
|
-
< {} _, B 0, B 1 >=B acc
|
|
439
|
-
} <
|
|
394
|
+
trie_size = ?{ btrie[V] t, bits[] acc } <
|
|
440
395
|
{ .t /nil if, .acc then } .then,
|
|
441
396
|
{
|
|
442
|
-
.t /trie .
|
|
443
|
-
{
|
|
444
|
-
.t /trie .1 t,
|
|
445
|
-
{ .t /trie .0 t, .acc acc } _trie_size acc
|
|
446
|
-
} _trie_size then
|
|
447
|
-
} .then,
|
|
448
|
-
{
|
|
449
|
-
.t /trie .val /val if,
|
|
397
|
+
.t /trie .1 t,
|
|
450
398
|
{
|
|
451
|
-
.t /trie .
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
399
|
+
.t /trie .0 t,
|
|
400
|
+
<
|
|
401
|
+
{ .t /trie .val /nil if, .acc then } .then,
|
|
402
|
+
{ .t /trie .val /val if, .acc inc then } .then
|
|
403
|
+
> acc
|
|
404
|
+
} trie_size acc
|
|
405
|
+
} trie_size
|
|
455
406
|
> ?bits[];
|
|
456
407
|
|
|
457
|
-
list_size = { ?btrie[V] t, 0 acc }
|
|
458
|
-
obj_size = { ?btrie[V] t, 0 acc }
|
|
408
|
+
list_size = { ?btrie[V] t, 0 acc } trie_size;
|
|
409
|
+
obj_size = { ?btrie[V] t, 0 acc } trie_size;
|
|
459
410
|
|
|
460
|
-
|
|
411
|
+
str_len_loop = ?{ string[] str, bits[] acc } <
|
|
461
412
|
{ .str /nil if, .acc then } .then,
|
|
462
|
-
{ .str /cons .cdr str, .acc
|
|
463
|
-
> ?
|
|
464
|
-
str_len = { ?string[] str, 0 acc }
|
|
413
|
+
{ .str /cons .cdr str, .acc inc acc } str_len_loop
|
|
414
|
+
> ?bits[];
|
|
415
|
+
str_len = { ?string[] str, 0 acc } str_len_loop;
|
|
465
416
|
|
|
466
417
|
-- Count items in ?djson[] (object entries, list elements, or string chars)
|
|
467
418
|
length_nat = ?djson[] <
|
|
@@ -470,130 +421,8 @@ length_nat = ?djson[] <
|
|
|
470
421
|
/string str_len
|
|
471
422
|
>;
|
|
472
423
|
|
|
473
|
-
pos_sign = {} |"+";
|
|
474
|
-
bit0 = {} |0;
|
|
475
|
-
bit1 = {} |1;
|
|
476
|
-
|
|
477
|
-
pos_zero = {
|
|
478
|
-
pos_sign sign,
|
|
479
|
-
{
|
|
480
|
-
bit0 0, bit0 1, bit0 2, bit0 3, bit0 4,
|
|
481
|
-
bit0 5, bit0 6, bit0 7, bit0 8, bit0 9, bit0 10
|
|
482
|
-
} exponent,
|
|
483
|
-
{
|
|
484
|
-
bit0 0, bit0 1, bit0 2, bit0 3, bit0 4,
|
|
485
|
-
bit0 5, bit0 6, bit0 7, bit0 8, bit0 9,
|
|
486
|
-
bit0 10, bit0 11, bit0 12, bit0 13, bit0 14,
|
|
487
|
-
bit0 15, bit0 16, bit0 17, bit0 18, bit0 19,
|
|
488
|
-
bit0 20, bit0 21, bit0 22, bit0 23, bit0 24,
|
|
489
|
-
bit0 25, bit0 26, bit0 27, bit0 28, bit0 29,
|
|
490
|
-
bit0 30, bit0 31, bit0 32, bit0 33, bit0 34,
|
|
491
|
-
bit0 35, bit0 36, bit0 37, bit0 38, bit0 39,
|
|
492
|
-
bit0 40, bit0 41, bit0 42, bit0 43, bit0 44,
|
|
493
|
-
bit0 45, bit0 46, bit0 47, bit0 48, bit0 49,
|
|
494
|
-
bit0 50, bit0 51
|
|
495
|
-
} fraction
|
|
496
|
-
};
|
|
497
|
-
|
|
498
|
-
exp_bias = {
|
|
499
|
-
bit1 0, bit1 1, bit1 2, bit1 3, bit1 4,
|
|
500
|
-
bit1 5, bit1 6, bit1 7, bit1 8, bit1 9, bit0 10
|
|
501
|
-
};
|
|
502
|
-
|
|
503
|
-
exp_half_adder = <
|
|
504
|
-
{ .a /0 a, .c_in /0 c_in } { bit0 sum, bit0 carry },
|
|
505
|
-
{ .a /0 a, .c_in /1 c_in } { bit1 sum, bit0 carry },
|
|
506
|
-
{ .a /1 a, .c_in /0 c_in } { bit1 sum, bit0 carry },
|
|
507
|
-
{ .a /1 a, .c_in /1 c_in } { bit0 sum, bit1 carry }
|
|
508
|
-
>;
|
|
509
|
-
|
|
510
|
-
inc_exp = {
|
|
511
|
-
{ .0 a, bit1 c_in } exp_half_adder e0,
|
|
512
|
-
.1 b1, .2 b2, .3 b3, .4 b4, .5 b5, .6 b6, .7 b7, .8 b8, .9 b9, .10 b10
|
|
513
|
-
} {
|
|
514
|
-
.e0.sum 0,
|
|
515
|
-
{ .b1 a, .e0.carry c_in } exp_half_adder e1,
|
|
516
|
-
.b2 b2, .b3 b3, .b4 b4, .b5 b5, .b6 b6, .b7 b7, .b8 b8, .b9 b9, .b10 b10
|
|
517
|
-
} {
|
|
518
|
-
.0 0, .e1.sum 1,
|
|
519
|
-
{ .b2 a, .e1.carry c_in } exp_half_adder e2,
|
|
520
|
-
.b3 b3, .b4 b4, .b5 b5, .b6 b6, .b7 b7, .b8 b8, .b9 b9, .b10 b10
|
|
521
|
-
} {
|
|
522
|
-
.0 0, .1 1, .e2.sum 2,
|
|
523
|
-
{ .b3 a, .e2.carry c_in } exp_half_adder e3,
|
|
524
|
-
.b4 b4, .b5 b5, .b6 b6, .b7 b7, .b8 b8, .b9 b9, .b10 b10
|
|
525
|
-
} {
|
|
526
|
-
.0 0, .1 1, .2 2, .e3.sum 3,
|
|
527
|
-
{ .b4 a, .e3.carry c_in } exp_half_adder e4,
|
|
528
|
-
.b5 b5, .b6 b6, .b7 b7, .b8 b8, .b9 b9, .b10 b10
|
|
529
|
-
} {
|
|
530
|
-
.0 0, .1 1, .2 2, .3 3, .e4.sum 4,
|
|
531
|
-
{ .b5 a, .e4.carry c_in } exp_half_adder e5,
|
|
532
|
-
.b6 b6, .b7 b7, .b8 b8, .b9 b9, .b10 b10
|
|
533
|
-
} {
|
|
534
|
-
.0 0, .1 1, .2 2, .3 3, .4 4, .e5.sum 5,
|
|
535
|
-
{ .b6 a, .e5.carry c_in } exp_half_adder e6,
|
|
536
|
-
.b7 b7, .b8 b8, .b9 b9, .b10 b10
|
|
537
|
-
} {
|
|
538
|
-
.0 0, .1 1, .2 2, .3 3, .4 4, .5 5, .e6.sum 6,
|
|
539
|
-
{ .b7 a, .e6.carry c_in } exp_half_adder e7,
|
|
540
|
-
.b8 b8, .b9 b9, .b10 b10
|
|
541
|
-
} {
|
|
542
|
-
.0 0, .1 1, .2 2, .3 3, .4 4, .5 5, .6 6, .e7.sum 7,
|
|
543
|
-
{ .b8 a, .e7.carry c_in } exp_half_adder e8,
|
|
544
|
-
.b9 b9, .b10 b10
|
|
545
|
-
} {
|
|
546
|
-
.0 0, .1 1, .2 2, .3 3, .4 4, .5 5, .6 6, .7 7, .e8.sum 8,
|
|
547
|
-
{ .b9 a, .e8.carry c_in } exp_half_adder e9,
|
|
548
|
-
.b10 b10
|
|
549
|
-
} {
|
|
550
|
-
.0 0, .1 1, .2 2, .3 3, .4 4, .5 5, .6 6, .7 7, .8 8, .e9.sum 9,
|
|
551
|
-
{ .b10 a, .e9.carry c_in } exp_half_adder e10
|
|
552
|
-
} {
|
|
553
|
-
.0 0, .1 1, .2 2, .3 3, .4 4, .5 5, .6 6, .7 7, .8 8, .9 9, .e10.sum 10
|
|
554
|
-
};
|
|
555
|
-
|
|
556
|
-
shift_frac = ?{ bit[] b, fraction[] f } {
|
|
557
|
-
.f.1 0, .f.2 1, .f.3 2, .f.4 3, .f.5 4,
|
|
558
|
-
.f.6 5, .f.7 6, .f.8 7, .f.9 8, .f.10 9,
|
|
559
|
-
.f.11 10, .f.12 11, .f.13 12, .f.14 13, .f.15 14,
|
|
560
|
-
.f.16 15, .f.17 16, .f.18 17, .f.19 18, .f.20 19,
|
|
561
|
-
.f.21 20, .f.22 21, .f.23 22, .f.24 23, .f.25 24,
|
|
562
|
-
.f.26 25, .f.27 26, .f.28 27, .f.29 28, .f.30 29,
|
|
563
|
-
.f.31 30, .f.32 31, .f.33 32, .f.34 33, .f.35 34,
|
|
564
|
-
.f.36 35, .f.37 36, .f.38 37, .f.39 38, .f.40 39,
|
|
565
|
-
.f.41 40, .f.42 41, .f.43 42, .f.44 43, .f.45 44,
|
|
566
|
-
.f.46 45, .f.47 46, .f.48 47, .f.49 48, .f.50 49,
|
|
567
|
-
.f.51 50, .b 51
|
|
568
|
-
};
|
|
569
|
-
|
|
570
|
-
_nat_step = ?{ bits[] bits, exponent[] exp, fraction[] frac } <
|
|
571
|
-
{
|
|
572
|
-
.bits /_ if,
|
|
573
|
-
{ pos_sign sign, .exp exponent, .frac fraction } then
|
|
574
|
-
} .then,
|
|
575
|
-
{
|
|
576
|
-
.bits /0 bits,
|
|
577
|
-
.exp inc_exp exp,
|
|
578
|
-
{ bit0 b, .frac f } shift_frac frac
|
|
579
|
-
} _nat_step,
|
|
580
|
-
{
|
|
581
|
-
.bits /1 bits,
|
|
582
|
-
.exp inc_exp exp,
|
|
583
|
-
{ bit1 b, .frac f } shift_frac frac
|
|
584
|
-
} _nat_step
|
|
585
|
-
>;
|
|
586
|
-
|
|
587
|
-
inv_bits = { () x, 0b r } _inv;
|
|
588
|
-
|
|
589
|
-
nat_to_float64 = ?bits[] <
|
|
590
|
-
/_ { pos_zero then } .then,
|
|
591
|
-
/0 { pos_zero then } .then,
|
|
592
|
-
/1 { () inv_bits bits, exp_bias exp, pos_zero .fraction frac } _nat_step
|
|
593
|
-
>;
|
|
594
|
-
|
|
595
424
|
-- Count items in ?djson[] -> ?djson[] number
|
|
596
|
-
length = length_nat
|
|
425
|
+
length = length_nat natToFloat64 |number ?djson[];
|
|
597
426
|
|
|
598
427
|
-- Append element to ?djson[] list: { val, json } -> json
|
|
599
428
|
push = ?{ djson[] val, djson[] json } {
|
|
@@ -607,8 +436,8 @@ push = ?{ djson[] val, djson[] json } {
|
|
|
607
436
|
---------------------------------------------------------------
|
|
608
437
|
|
|
609
438
|
read_bit = ?bits[] <
|
|
610
|
-
/0 {
|
|
611
|
-
/1 {
|
|
439
|
+
/0 { b0 bit, () rest },
|
|
440
|
+
/1 { b1 bit, () rest }
|
|
612
441
|
>;
|
|
613
442
|
|
|
614
443
|
read_bits7 =
|
|
@@ -631,52 +460,44 @@ read_char = ?bits[] <
|
|
|
631
460
|
/0 read_bits7
|
|
632
461
|
>;
|
|
633
462
|
|
|
634
|
-
|
|
635
|
-
/_
|
|
636
|
-
|
|
463
|
+
bits_to_str_loop = ?{ bits[] bits, string[] acc } <
|
|
464
|
+
{ .bits /_ if, .acc reverse then } .then,
|
|
465
|
+
{ .bits read_char next, .acc acc }
|
|
466
|
+
{ .next.rest bits, { .next.val car, .acc cdr } |cons acc }
|
|
467
|
+
bits_to_str_loop
|
|
637
468
|
> ?string[];
|
|
638
469
|
|
|
639
|
-
|
|
470
|
+
bits_to_str = ?bits[] { () bits, nil acc } bits_to_str_loop;
|
|
471
|
+
|
|
472
|
+
collect_keys = ?{
|
|
640
473
|
btrie[V] tree,
|
|
641
474
|
bits[] rev_path,
|
|
642
475
|
list[string[]] acc
|
|
643
476
|
} <
|
|
644
477
|
{ .tree /nil if, .acc then } .then,
|
|
645
478
|
{
|
|
646
|
-
.tree /trie .
|
|
479
|
+
.tree /trie .0 tree,
|
|
480
|
+
.rev_path |0 rev_path,
|
|
647
481
|
{
|
|
648
|
-
.tree /trie .
|
|
649
|
-
.rev_path |
|
|
650
|
-
|
|
651
|
-
.tree /trie .
|
|
652
|
-
.rev_path |1 rev_path,
|
|
653
|
-
.acc acc
|
|
654
|
-
} _collect_keys acc
|
|
655
|
-
} _collect_keys then
|
|
656
|
-
} .then,
|
|
657
|
-
{
|
|
658
|
-
.tree /trie .val /val if,
|
|
659
|
-
{
|
|
660
|
-
.tree /trie .0 tree,
|
|
661
|
-
.rev_path |0 rev_path,
|
|
662
|
-
{
|
|
663
|
-
.tree /trie .1 tree,
|
|
664
|
-
.rev_path |1 rev_path,
|
|
482
|
+
.tree /trie .1 tree,
|
|
483
|
+
.rev_path |1 rev_path,
|
|
484
|
+
<
|
|
485
|
+
{ .tree /trie .val /nil if, .acc then } .then,
|
|
665
486
|
{
|
|
666
|
-
|
|
667
|
-
.acc cdr
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
> ?
|
|
487
|
+
.tree /trie .val /val if,
|
|
488
|
+
{ .rev_path inv bits_to_str car, .acc cdr } |cons then
|
|
489
|
+
} .then
|
|
490
|
+
> acc
|
|
491
|
+
} collect_keys acc
|
|
492
|
+
} collect_keys
|
|
493
|
+
> ?list[string[]];
|
|
673
494
|
|
|
674
|
-
|
|
495
|
+
obj_keys_raw = { () tree, 0b rev_path, nil acc } collect_keys;
|
|
675
496
|
|
|
676
497
|
-- Extract all keys from ?djson[] object as raw list: ?djson[] -> list[string[]]
|
|
677
|
-
keys_raw = ?djson[] /object
|
|
498
|
+
keys_raw = ?djson[] /object obj_keys_raw;
|
|
678
499
|
|
|
679
|
-
|
|
500
|
+
keys_to_trie = ?{
|
|
680
501
|
list[string[]] klist,
|
|
681
502
|
bits[] index,
|
|
682
503
|
btrie[V] acc
|
|
@@ -684,53 +505,46 @@ _keys_to_trie = ?{
|
|
|
684
505
|
{ .klist /nil if, .acc then } .then,
|
|
685
506
|
{
|
|
686
507
|
.klist /cons .cdr klist,
|
|
687
|
-
.index
|
|
508
|
+
.index inc index,
|
|
688
509
|
{
|
|
689
510
|
.index index,
|
|
690
511
|
.klist /cons .car |string val,
|
|
691
512
|
.acc list
|
|
692
513
|
} put_nth acc
|
|
693
|
-
}
|
|
514
|
+
} keys_to_trie
|
|
694
515
|
>;
|
|
695
516
|
|
|
696
517
|
-- Extract all keys from ?djson[] object as a ?djson[] list: ?djson[] -> ?djson[]
|
|
697
|
-
keys = ?djson[] /object
|
|
698
|
-
{ () klist, 0 index,
|
|
518
|
+
keys = ?djson[] /object obj_keys_raw
|
|
519
|
+
{ () klist, 0 index, nil acc } keys_to_trie
|
|
699
520
|
|list ?djson[];
|
|
700
521
|
|
|
701
|
-
|
|
522
|
+
collect_values = ?{
|
|
702
523
|
btrie[V] tree,
|
|
703
524
|
list[V] acc
|
|
704
525
|
} <
|
|
705
526
|
{ .tree /nil if, .acc then } .then,
|
|
706
527
|
{
|
|
707
|
-
.tree /trie .
|
|
528
|
+
.tree /trie .0 tree,
|
|
708
529
|
{
|
|
709
|
-
.tree /trie .
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
{
|
|
718
|
-
.tree /trie .1 tree,
|
|
719
|
-
{ .tree /trie .val /val car, .acc cdr } |cons acc
|
|
720
|
-
} _collect_values acc
|
|
721
|
-
} _collect_values then
|
|
722
|
-
} .then
|
|
723
|
-
> ? list[V];
|
|
530
|
+
.tree /trie .1 tree,
|
|
531
|
+
<
|
|
532
|
+
{ .tree /trie .val /nil if, .acc then } .then,
|
|
533
|
+
{ .tree /trie .val /val car, .acc cdr } |cons
|
|
534
|
+
> acc
|
|
535
|
+
} collect_values acc
|
|
536
|
+
} collect_values
|
|
537
|
+
> ?list[V];
|
|
724
538
|
|
|
725
|
-
|
|
539
|
+
obj_values_raw = { () tree, nil acc } collect_values;
|
|
726
540
|
|
|
727
541
|
-- Extract all values from ?djson[] object or list as raw list
|
|
728
542
|
values_raw = ?djson[] <
|
|
729
|
-
/object
|
|
730
|
-
/list
|
|
543
|
+
/object obj_values_raw,
|
|
544
|
+
/list obj_values_raw
|
|
731
545
|
>;
|
|
732
546
|
|
|
733
|
-
|
|
547
|
+
values_to_trie = ?{
|
|
734
548
|
list[(...)] klist,
|
|
735
549
|
bits[] index,
|
|
736
550
|
btrie[(...)] acc
|
|
@@ -738,20 +552,19 @@ _values_to_trie = ?{
|
|
|
738
552
|
{ .klist /nil if, .acc then } .then,
|
|
739
553
|
{
|
|
740
554
|
.klist /cons .cdr klist,
|
|
741
|
-
.index
|
|
555
|
+
.index inc index,
|
|
742
556
|
{
|
|
743
557
|
.index index,
|
|
744
558
|
.klist /cons .car val,
|
|
745
559
|
.acc list
|
|
746
560
|
} put_nth acc
|
|
747
|
-
}
|
|
561
|
+
} values_to_trie
|
|
748
562
|
>;
|
|
749
563
|
|
|
750
564
|
-- Extract all values from ?djson[] as a ?djson[] list: ?djson[] -> ?djson[]
|
|
751
|
-
values =
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
{ () klist, 0 index, {} |nil acc } _values_to_trie |list,
|
|
565
|
+
values = <
|
|
566
|
+
/object obj_values_raw
|
|
567
|
+
{ () klist, 0 index, nil acc } values_to_trie |list,
|
|
755
568
|
?djson[]
|
|
756
569
|
>;
|
|
757
570
|
|
|
@@ -759,12 +572,12 @@ values =
|
|
|
759
572
|
-- §8. Type Predicates
|
|
760
573
|
---------------------------------------------------------------
|
|
761
574
|
|
|
762
|
-
is_null? = ?djson[] {/null if, () then} .then;
|
|
763
|
-
is_bool? = ?djson[] {/bool if, () then} .then;
|
|
764
|
-
is_number? = ?djson[] {/number if, () then} .then;
|
|
765
|
-
is_string? = ?djson[] {/string if, () then} .then;
|
|
766
|
-
is_list? = ?djson[] {/list if, () then} .then;
|
|
767
|
-
is_object? = ?djson[] {/object if, () then} .then;
|
|
575
|
+
is_null? = ?djson[] { /null if, () then } .then;
|
|
576
|
+
is_bool? = ?djson[] { /bool if, () then } .then;
|
|
577
|
+
is_number? = ?djson[] { /number if, () then } .then;
|
|
578
|
+
is_string? = ?djson[] { /string if, () then } .then;
|
|
579
|
+
is_list? = ?djson[] { /list if, () then } .then;
|
|
580
|
+
is_object? = ?djson[] { /object if, () then } .then;
|
|
768
581
|
|
|
769
582
|
json_type[] = <
|
|
770
583
|
{} null, {} bool, {} number, {} string, {} list, {} object
|
|
@@ -776,4 +589,306 @@ type_of = ?djson[] <
|
|
|
776
589
|
/string {} |string,
|
|
777
590
|
/list {} |list,
|
|
778
591
|
/object {} |object
|
|
779
|
-
> ?
|
|
592
|
+
> ?json_type[];
|
|
593
|
+
|
|
594
|
+
---------------------------------------------------------------
|
|
595
|
+
-- §9. JSON Text Decoder
|
|
596
|
+
---------------------------------------------------------------
|
|
597
|
+
|
|
598
|
+
-- Parse exactly one JSON document. Invalid text, non-finite numbers,
|
|
599
|
+
-- and unpaired surrogate escapes are undefined, as in k-djson.
|
|
600
|
+
parseJson = ?string[] json_value {
|
|
601
|
+
.rest ws /nil end,
|
|
602
|
+
.value value
|
|
603
|
+
} .value ?djson[];
|
|
604
|
+
|
|
605
|
+
result[] = { string[] rest, djson[] value };
|
|
606
|
+
text_result[] = { string[] rest, string[] value };
|
|
607
|
+
|
|
608
|
+
json_value = ?string[] ws <
|
|
609
|
+
n_char u_char l_char l_char { () rest, null value },
|
|
610
|
+
t_char r_char u_char e_char { () rest, true value },
|
|
611
|
+
f_char a_char l_char s_char e_char { () rest, false value },
|
|
612
|
+
json_string { .rest rest, .value |string value },
|
|
613
|
+
json_array,
|
|
614
|
+
json_object,
|
|
615
|
+
json_number
|
|
616
|
+
> ?result[];
|
|
617
|
+
|
|
618
|
+
_quote_ = { b0 0, b1 1, b0 2, b0 3, b0 4, b1 5, b0 6 } |ascii;
|
|
619
|
+
_backslash_ = { b0 0, b0 1, b1 2, b1 3, b1 4, b0 5, b1 6 } |ascii;
|
|
620
|
+
_slash_ = { b1 0, b1 1, b1 2, b1 3, b0 4, b1 5, b0 6 } |ascii;
|
|
621
|
+
_b_ = { b0 0, b1 1, b0 2, b0 3, b0 4, b1 5, b1 6 } |ascii;
|
|
622
|
+
_bs_ = { b0 0, b0 1, b0 2, b1 3, b0 4, b0 5, b0 6 } |ascii;
|
|
623
|
+
_ff_ = { b0 0, b0 1, b1 2, b1 3, b0 4, b0 5, b0 6 } |ascii;
|
|
624
|
+
_l_ = { b0 0, b0 1, b1 2, b1 3, b0 4, b1 5, b1 6 } |ascii;
|
|
625
|
+
_r_ = { b0 0, b1 1, b0 2, b0 3, b1 4, b1 5, b1 6 } |ascii;
|
|
626
|
+
_s_ = { b1 0, b1 1, b0 2, b0 3, b1 4, b1 5, b1 6 } |ascii;
|
|
627
|
+
_u_ = { b1 0, b0 1, b1 2, b0 3, b1 4, b1 5, b1 6 } |ascii;
|
|
628
|
+
_comma_ = { b0 0, b0 1, b1 2, b1 3, b0 4, b1 5, b0 6 } |ascii;
|
|
629
|
+
_colon_ = { b0 0, b1 1, b0 2, b1 3, b1 4, b1 5, b0 6 } |ascii;
|
|
630
|
+
_open_array_ = { b1 0, b1 1, b0 2, b1 3, b1 4, b0 5, b1 6 } |ascii;
|
|
631
|
+
_close_array_ = { b1 0, b0 1, b1 2, b1 3, b1 4, b0 5, b1 6 } |ascii;
|
|
632
|
+
_open_object_ = { b1 0, b1 1, b0 2, b1 3, b1 4, b1 5, b1 6 } |ascii;
|
|
633
|
+
_close_object_ = { b1 0, b0 1, b1 2, b1 3, b1 4, b1 5, b1 6 } |ascii;
|
|
634
|
+
|
|
635
|
+
_A_ = { b1 0, b0 1, b0 2, b0 3, b0 4, b0 5, b1 6 } |ascii;
|
|
636
|
+
_B_ = { b0 0, b1 1, b0 2, b0 3, b0 4, b0 5, b1 6 } |ascii;
|
|
637
|
+
_C_ = { b1 0, b1 1, b0 2, b0 3, b0 4, b0 5, b1 6 } |ascii;
|
|
638
|
+
_D_ = { b0 0, b0 1, b1 2, b0 3, b0 4, b0 5, b1 6 } |ascii;
|
|
639
|
+
_F_ = { b0 0, b1 1, b1 2, b0 3, b0 4, b0 5, b1 6 } |ascii;
|
|
640
|
+
_c_ = { b1 0, b1 1, b0 2, b0 3, b0 4, b1 5, b1 6 } |ascii;
|
|
641
|
+
_d_ = { b0 0, b0 1, b1 2, b0 3, b0 4, b1 5, b1 6 } |ascii;
|
|
642
|
+
|
|
643
|
+
json_string = ?string[] quote_char { () rest, nil acc } string_loop;
|
|
644
|
+
string_kind = ?unicode[] <
|
|
645
|
+
{ { () x, _quote_ y } eq_char? if, {} |quote then } .then,
|
|
646
|
+
{ { () x, _backslash_ y } eq_char? if, {} |escape then } .then,
|
|
647
|
+
{ /ascii { .6 /0 a, .5 /0 b } if, {} |control then } .then,
|
|
648
|
+
{} |plain
|
|
649
|
+
>;
|
|
650
|
+
string_loop = ?{ string[] rest, string[] acc } {
|
|
651
|
+
.rest /cons head, .acc acc, .rest /cons .car string_kind kind
|
|
652
|
+
} <
|
|
653
|
+
{
|
|
654
|
+
.kind /quote if,
|
|
655
|
+
{ .head.cdr rest, .acc reverse value } then
|
|
656
|
+
} .then,
|
|
657
|
+
<
|
|
658
|
+
{
|
|
659
|
+
.head.cdr rest,
|
|
660
|
+
{ { .kind /plain a, .head.car b } .b car, .acc cdr } |cons acc
|
|
661
|
+
},
|
|
662
|
+
{ { .kind /escape a, .head.cdr escape b } .b escaped, .acc acc }
|
|
663
|
+
{ .escaped.rest rest, { .escaped.value car, .acc cdr } |cons acc }
|
|
664
|
+
> string_loop
|
|
665
|
+
> ?text_result[];
|
|
666
|
+
|
|
667
|
+
json_array = open_array ws <
|
|
668
|
+
close_array { () rest, empty_list value },
|
|
669
|
+
{ () rest, nil tree, 0 index } array_item
|
|
670
|
+
>;
|
|
671
|
+
array_item = ?{ string[] rest, btrie[djson[]] tree, bits[] index } {
|
|
672
|
+
.rest json_value item, .tree tree, .index index
|
|
673
|
+
} {
|
|
674
|
+
.item.rest ws rest,
|
|
675
|
+
{ .index index, .item.value val, .tree list } put_nth tree,
|
|
676
|
+
.index inc index
|
|
677
|
+
} <
|
|
678
|
+
{ .rest close_array rest, .tree |list value },
|
|
679
|
+
{ .rest comma_char rest, .tree tree, .index index } array_item
|
|
680
|
+
> ?result[];
|
|
681
|
+
|
|
682
|
+
json_object = open_object ws <
|
|
683
|
+
close_object { () rest, empty_object value },
|
|
684
|
+
{ () rest, nil tree } object_item
|
|
685
|
+
>;
|
|
686
|
+
object_item = ?{ string[] rest, btrie[djson[]] tree } {
|
|
687
|
+
.rest ws json_string key, .tree tree
|
|
688
|
+
} {
|
|
689
|
+
.key.rest ws colon_char json_value item,
|
|
690
|
+
.key.value key, .tree tree
|
|
691
|
+
} {
|
|
692
|
+
.item.rest ws rest,
|
|
693
|
+
{ .key key, .item.value val, .tree object } put tree
|
|
694
|
+
} <
|
|
695
|
+
{ .rest close_object rest, .tree |object value },
|
|
696
|
+
{ .rest comma_char rest, .tree tree } object_item
|
|
697
|
+
> ?result[];
|
|
698
|
+
|
|
699
|
+
-- ASCII consumers return the unconsumed input (a_char, e_char, f_char, n_char, t_char are imported from ieee.k).
|
|
700
|
+
quote_char = /cons { { .car x, _quote_ y } eq_char? if, .cdr then } .then;
|
|
701
|
+
backslash_char = /cons { { .car x, _backslash_ y } eq_char? if, .cdr then } .then;
|
|
702
|
+
slash_char = /cons { { .car x, _slash_ y } eq_char? if, .cdr then } .then;
|
|
703
|
+
b_char = /cons { { .car x, _b_ y } eq_char? if, .cdr then } .then;
|
|
704
|
+
l_char = /cons { { .car x, _l_ y } eq_char? if, .cdr then } .then;
|
|
705
|
+
r_char = /cons { { .car x, _r_ y } eq_char? if, .cdr then } .then;
|
|
706
|
+
s_char = /cons { { .car x, _s_ y } eq_char? if, .cdr then } .then;
|
|
707
|
+
u_char = /cons { { .car x, _u_ y } eq_char? if, .cdr then } .then;
|
|
708
|
+
comma_char = /cons { { .car x, _comma_ y } eq_char? if, .cdr then } .then;
|
|
709
|
+
colon_char = /cons { { .car x, _colon_ y } eq_char? if, .cdr then } .then;
|
|
710
|
+
open_array = /cons { { .car x, _open_array_ y } eq_char? if, .cdr then } .then;
|
|
711
|
+
close_array = /cons { { .car x, _close_array_ y } eq_char? if, .cdr then } .then;
|
|
712
|
+
open_object = /cons { { .car x, _open_object_ y } eq_char? if, .cdr then } .then;
|
|
713
|
+
close_object = /cons { { .car x, _close_object_ y } eq_char? if, .cdr then } .then;
|
|
714
|
+
|
|
715
|
+
hex_letter = ?unicode[] <
|
|
716
|
+
{ () x, _a_ y } eq_char? 0b |0 |1 |0 |1,
|
|
717
|
+
{ () x, _A_ y } eq_char? 0b |0 |1 |0 |1,
|
|
718
|
+
{ () x, _b_ y } eq_char? 0b |1 |1 |0 |1,
|
|
719
|
+
{ () x, _B_ y } eq_char? 0b |1 |1 |0 |1,
|
|
720
|
+
{ () x, _c_ y } eq_char? 0b |0 |0 |1 |1,
|
|
721
|
+
{ () x, _C_ y } eq_char? 0b |0 |0 |1 |1,
|
|
722
|
+
{ () x, _d_ y } eq_char? 0b |1 |0 |1 |1,
|
|
723
|
+
{ () x, _D_ y } eq_char? 0b |1 |0 |1 |1,
|
|
724
|
+
{ () x, _e_ y } eq_char? 0b |0 |1 |1 |1,
|
|
725
|
+
{ () x, _E_ y } eq_char? 0b |0 |1 |1 |1,
|
|
726
|
+
{ () x, _f_ y } eq_char? 0b |1 |1 |1 |1,
|
|
727
|
+
{ () x, _F_ y } eq_char? 0b |1 |1 |1 |1
|
|
728
|
+
>;
|
|
729
|
+
-- Parse a single hexadecimal digit ('0'..'9', 'a'..'f', 'A'..'F') from a character string.
|
|
730
|
+
-- Input: string[]
|
|
731
|
+
-- Output: { rest: string[], value: bits[] } where value is in 0..15
|
|
732
|
+
hex = < digit, /cons { .cdr rest, .car hex_letter value } >;
|
|
733
|
+
|
|
734
|
+
-- Unicode escape decoding.
|
|
735
|
+
|
|
736
|
+
c2 = 0b |0 |1;
|
|
737
|
+
c4 = 0b |0 |0 |1;
|
|
738
|
+
c8 = 0b |0 |0 |0 |1;
|
|
739
|
+
c16 = 0b |0 |0 |0 |0 |1;
|
|
740
|
+
c128 = 0b |0 |0 |0 |0 |0 |0 |0 |1;
|
|
741
|
+
c208 = 0b |0 |0 |0 |0 |1 |0 |1 |1;
|
|
742
|
+
c216 = 0b |0 |0 |0 |1 |1 |0 |1 |1;
|
|
743
|
+
c224 = 0b |0 |0 |0 |0 |0 |1 |1 |1;
|
|
744
|
+
c240 = 0b |0 |0 |0 |0 |1 |1 |1 |1;
|
|
745
|
+
c248 = 0b |0 |0 |0 |1 |1 |1 |1 |1;
|
|
746
|
+
c249 = 0b |1 |0 |0 |1 |1 |1 |1 |1;
|
|
747
|
+
c250 = 0b |0 |1 |0 |1 |1 |1 |1 |1;
|
|
748
|
+
c252 = 0b |0 |0 |1 |1 |1 |1 |1 |1;
|
|
749
|
+
c254 = 0b |0 |1 |1 |1 |1 |1 |1 |1;
|
|
750
|
+
c1024 = c1023 inc;
|
|
751
|
+
c55296 = 0b |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |1 |1 |0 |1 |1;
|
|
752
|
+
c56320 = 0b |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |1 |1 |1 |0 |1 |1;
|
|
753
|
+
c57344 = 0b |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |1 |1 |1;
|
|
754
|
+
c65536 = 0b |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 |1;
|
|
755
|
+
|
|
756
|
+
-- Reconstruct the same Unicode partitions used by core.k and k-utf8.
|
|
757
|
+
to_unicode = ?bits[] unpack16 <
|
|
758
|
+
{ .plane /_ if, () bmp then } .then,
|
|
759
|
+
{
|
|
760
|
+
.plane /1 /_ if,
|
|
761
|
+
{ .hi mid, .lo lo } |supplementary_plane1 then
|
|
762
|
+
} .then,
|
|
763
|
+
{ .plane plane plane, .hi mid, .lo lo } |supplementary_planes2_16
|
|
764
|
+
> ?unicode[];
|
|
765
|
+
bmp = {
|
|
766
|
+
() parts, .hi { () x, 0b acc } push_bits8 trim high
|
|
767
|
+
} <
|
|
768
|
+
{ .high /_ a, .parts.lo.7 /0 b, .parts.lo field7 |ascii then } .then,
|
|
769
|
+
{
|
|
770
|
+
{ .high x, c8 y } lt? if,
|
|
771
|
+
{
|
|
772
|
+
.parts.lo.0 0, .parts.lo.1 1, .parts.lo.2 2, .parts.lo.3 3,
|
|
773
|
+
.parts.lo.4 4, .parts.lo.5 5, .parts.lo.6 6, .parts.lo.7 7,
|
|
774
|
+
.parts.hi.0 8, .parts.hi.1 9, .parts.hi.2 10
|
|
775
|
+
} |plane0 then
|
|
776
|
+
} .then,
|
|
777
|
+
{ .high bmp_hi high, .parts.lo lo } <
|
|
778
|
+
{ .high /common hi, .lo lo } |bmp_common,
|
|
779
|
+
{ .high /private hi, .lo lo } |bmp_private_use
|
|
780
|
+
>
|
|
781
|
+
>;
|
|
782
|
+
bmp_hi = ?bits[] <
|
|
783
|
+
{
|
|
784
|
+
{ () x, c16 y } lt? if,
|
|
785
|
+
{ () x, c8 y } minus small3 |h08_0F |common then
|
|
786
|
+
} .then,
|
|
787
|
+
{
|
|
788
|
+
{ () x, c128 y } lt? if,
|
|
789
|
+
{ () x, c16 y } minus small7 |h10_7F |common then
|
|
790
|
+
} .then,
|
|
791
|
+
{
|
|
792
|
+
{ () x, c208 y } lt? if,
|
|
793
|
+
{ () x, c128 y } minus small7 |h80_CF |common then
|
|
794
|
+
} .then,
|
|
795
|
+
{
|
|
796
|
+
{ () x, c216 y } lt? if,
|
|
797
|
+
{ () x, c208 y } minus small3 |hD0_D7 |common then
|
|
798
|
+
} .then,
|
|
799
|
+
{
|
|
800
|
+
{ () x, c240 y } lt? if,
|
|
801
|
+
{ () x, c224 y } minus small4 |hE0_EF |private then
|
|
802
|
+
} .then,
|
|
803
|
+
{
|
|
804
|
+
{ () x, c248 y } lt? if,
|
|
805
|
+
{ () x, c240 y } minus small3 |hF0_F7 |private then
|
|
806
|
+
} .then,
|
|
807
|
+
{ () x, c248 y } minus /_ {} |hF8 |private,
|
|
808
|
+
{ () x, c249 y } minus /_ {} |hF9 |common,
|
|
809
|
+
{
|
|
810
|
+
{ () x, c252 y } lt? if,
|
|
811
|
+
{ () x, c250 y } minus small1 |hFA_FB |common then
|
|
812
|
+
} .then,
|
|
813
|
+
{
|
|
814
|
+
{ () x, c254 y } lt? if,
|
|
815
|
+
{ () x, c252 y } minus small1 |hFC_FD |common then
|
|
816
|
+
} .then,
|
|
817
|
+
{ () x, c254 y } minus small1 |hFE_FF |common
|
|
818
|
+
>;
|
|
819
|
+
plane = ?bits[] <
|
|
820
|
+
{
|
|
821
|
+
{ () x, c4 y } lt? if,
|
|
822
|
+
{ () x, c2 y } minus small1 |p02_03 then
|
|
823
|
+
} .then,
|
|
824
|
+
{
|
|
825
|
+
{ () x, c8 y } lt? if,
|
|
826
|
+
{ () x, c4 y } minus small2 |p04_07 then
|
|
827
|
+
} .then,
|
|
828
|
+
{
|
|
829
|
+
{ () x, c16 y } lt? if,
|
|
830
|
+
{ () x, c8 y } minus small3 |p08_0F then
|
|
831
|
+
} .then,
|
|
832
|
+
/1 /0 /0 /0 /0 /_ {} |p10
|
|
833
|
+
>;
|
|
834
|
+
|
|
835
|
+
times16 = ?bits[] <
|
|
836
|
+
/_ 0b,
|
|
837
|
+
{ inv |0 |0 |0 |0 x, 0b r } inv_loop
|
|
838
|
+
> ?bits[];
|
|
839
|
+
|
|
840
|
+
times1024 = ?bits[] <
|
|
841
|
+
/_ 0b,
|
|
842
|
+
{ inv |0 |0 |0 |0 |0 |0 |0 |0 |0 |0 x, 0b r } inv_loop
|
|
843
|
+
> ?bits[];
|
|
844
|
+
|
|
845
|
+
-- Parse 4 hexadecimal digits from string[], accumulating a 16-bit code point.
|
|
846
|
+
-- Input: string[]
|
|
847
|
+
-- Output: { rest: string[], value: bits[] }
|
|
848
|
+
hex4 = hex
|
|
849
|
+
{ .rest hex next, .value times16 high }
|
|
850
|
+
{ .next.rest rest, { .high x, .next.value y } plus value }
|
|
851
|
+
{ .rest hex next, .value times16 high }
|
|
852
|
+
{ .next.rest rest, { .high x, .next.value y } plus value }
|
|
853
|
+
{ .rest hex next, .value times16 high }
|
|
854
|
+
{ .next.rest rest, { .high x, .next.value y } plus value };
|
|
855
|
+
|
|
856
|
+
-- Parse a JSON string escape sequence (\", \\, \/, \b, \f, \n, \r, \t, \uXXXX) after the backslash.
|
|
857
|
+
-- Input: string[]
|
|
858
|
+
-- Output: { rest: string[], value: unicode[] }
|
|
859
|
+
escape = <
|
|
860
|
+
quote_char { () rest, _quote_ value },
|
|
861
|
+
backslash_char { () rest, _backslash_ value },
|
|
862
|
+
slash_char { () rest, _slash_ value },
|
|
863
|
+
b_char { () rest, _bs_ value },
|
|
864
|
+
f_char { () rest, _ff_ value },
|
|
865
|
+
n_char { () rest, _lf_ value },
|
|
866
|
+
r_char { () rest, _cr_ value },
|
|
867
|
+
t_char { () rest, _tab_ value },
|
|
868
|
+
u_char hex4 <
|
|
869
|
+
surrogate_pair,
|
|
870
|
+
{ .rest rest, .value to_unicode value }
|
|
871
|
+
>
|
|
872
|
+
>;
|
|
873
|
+
surrogate_pair = {
|
|
874
|
+
{ .value x, c55296 y } minus high,
|
|
875
|
+
.rest backslash_char u_char hex4 low
|
|
876
|
+
} {
|
|
877
|
+
{ .high x, c1024 y } lt? a,
|
|
878
|
+
{ .low.value x, c57344 y } lt? b,
|
|
879
|
+
.high times1024 x,
|
|
880
|
+
{ .low.value x, c56320 y } minus y,
|
|
881
|
+
.low.rest rest
|
|
882
|
+
} {
|
|
883
|
+
{ { .x x, .y y } plus x, c65536 y } plus to_unicode value,
|
|
884
|
+
.rest rest
|
|
885
|
+
};
|
|
886
|
+
|
|
887
|
+
-- JSON numbers use IEEE's exact scanner, with JSON's lexical/finite guards (RFC 8259).
|
|
888
|
+
-- Input: ?string[]
|
|
889
|
+
-- Output: ?result[] = { rest: string[], value: djson[] } (where value is |number)
|
|
890
|
+
json_number = ?string[] {
|
|
891
|
+
< minus_char {}, digit {} > if, () then
|
|
892
|
+
} .then scanFloat64 {
|
|
893
|
+
.rest rest, .value finite_float64? |number value
|
|
894
|
+
} ?result[];
|