@fraczak/k 8.6.1 → 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 +22 -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 +13 -7
  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 +217 -57
  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 +51 -9
  40. package/objects/README.md +10 -3
  41. package/objects/compile.mjs +10 -8
  42. package/package.json +1 -1
  43. package/repl.mjs +28 -6
  44. package/scripts/benchmark-idempotency.mjs +22 -15
  45. package/scripts/benchmark.mjs +29 -10
  46. package/scripts/build-repl-html.mjs +41 -13
  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 +50 -35
  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/lib/djson.k ADDED
@@ -0,0 +1,326 @@
1
+ # --lib lib/bits.k --use inc lt? plus minus times1024
2
+ # --lib lib/list.k --use reverse
3
+ # --lib lib/btrie.k --use btrie[V] nth_trie put_nth remove_nth obj_size:list_size obj_size obj_values_raw values_to_trie
4
+ # --lib lib/string.k --use unicode[] string[] eq_char? _quote_ _backslash_ _slash_ _bs_ _ff_ _lf_ _cr_ _tab_ n_char u_char l_char t_char r_char e_char f_char a_char s_char quote_char backslash_char slash_char b_char comma_char colon_char open_array close_array open_object close_object ws minus_char digit hex4 c1024 c55296 c56320 c57344 c65536 dot put remove str_len obj_keys_raw to_unicode
5
+ # --lib lib/ieee.k --use float64[]:number[] finite_float64? natToFloat64 scanFloat64
6
+
7
+ -- lib/djson.k — Universal dynamic JSON schema (djson[]), methods, and RFC 8259 parser
8
+
9
+ ---------------------------------------------------------------
10
+ -- §1. Base Patterns & Primitives
11
+ ---------------------------------------------------------------
12
+
13
+ bit[] = < {} 0, {} 1 >;
14
+ bool[] = < {} true, {} false >;
15
+ bits[] = < {} _, B 0, B 1 > = B;
16
+ list[X] = < {} nil, { X car, L cdr } cons > = L;
17
+
18
+ is0 = ?bit[] /0;
19
+ true_bool = {} |true ?bool[];
20
+ false_bool = {} |false ?bool[];
21
+
22
+ _ = {} |_ ?bits[];
23
+ 0b = |0 ?bits[];
24
+ 1b = |1 ?bits[];
25
+ un_ = ?bits[] /_;
26
+ un0 = ?bits[] /0;
27
+ un1 = ?bits[] /1;
28
+ _? = un_ _;
29
+ 0 = _ 0b;
30
+ 1 = _ 1b;
31
+ c0 = 0;
32
+
33
+ nil = {} |nil ?list[X];
34
+ cons = |cons ?list[X];
35
+ un_nil = ?list[X] /nil;
36
+ car = ?list[X] /cons .car;
37
+ cdr = ?list[X] /cons .cdr;
38
+
39
+ nil_trie = {} |nil ?btrie[V];
40
+ un_ascii = ?unicode[] /ascii;
41
+
42
+ ---------------------------------------------------------------
43
+ -- §2. Patterns & Primitive Constructors/Destructors
44
+ ---------------------------------------------------------------
45
+
46
+ djson[] = <
47
+ number[] number,
48
+ string[] string,
49
+ bool[] bool,
50
+ {} null,
51
+ btrie[J] list,
52
+ btrie[J] object
53
+ > = J;
54
+
55
+ json_type[] = <
56
+ {} null, {} bool, {} number, {} string, {} list, {} object
57
+ >;
58
+
59
+ string_kind[] = <
60
+ {} quote, {} escape, {} control, {} plain
61
+ >;
62
+
63
+ result[] = { string[] rest, djson[] value };
64
+ text_result[] = { string[] rest, string[] value };
65
+
66
+ -- Only the atomic primitives mention ?djson[], ?json_type[], ?string_kind[]
67
+ null = {} |null ?djson[];
68
+ number = |number ?djson[];
69
+ string = |string ?djson[];
70
+ bool = |bool ?djson[];
71
+ list = |list ?djson[];
72
+ object = |object ?djson[];
73
+
74
+ un_null = ?djson[] /null;
75
+ un_number = ?djson[] /number;
76
+ un_string = ?djson[] /string;
77
+ un_bool = ?djson[] /bool;
78
+ un_list = ?djson[] /list;
79
+ un_object = ?djson[] /object;
80
+
81
+ true = true_bool bool;
82
+ false = false_bool bool;
83
+ empty_list = nil_trie list;
84
+ empty_object = nil_trie object;
85
+
86
+ null_type = {} |null ?json_type[];
87
+ bool_type = {} |bool ?json_type[];
88
+ number_type = {} |number ?json_type[];
89
+ string_type = {} |string ?json_type[];
90
+ list_type = {} |list ?json_type[];
91
+ object_type = {} |object ?json_type[];
92
+
93
+ quote_kind = {} |quote ?string_kind[];
94
+ escape_kind = {} |escape ?string_kind[];
95
+ control_kind = {} |control ?string_kind[];
96
+ plain_kind = {} |plain ?string_kind[];
97
+
98
+ un_quote_kind = ?string_kind[] /quote;
99
+ un_escape_kind = ?string_kind[] /escape;
100
+ un_plain_kind = ?string_kind[] /plain;
101
+
102
+ ---------------------------------------------------------------
103
+ -- §3. Object & List Lookup, Update & Removal
104
+ ---------------------------------------------------------------
105
+
106
+ get = {
107
+ .key key,
108
+ .json un_object object
109
+ } dot;
110
+
111
+ nth = {
112
+ .index index,
113
+ .json un_list list
114
+ } nth_trie;
115
+
116
+ has_key? = { get if, () then } .then;
117
+ has? = has_key?;
118
+ has_index? = { nth if, () then } .then;
119
+ has_nth? = has_index?;
120
+
121
+ set = {
122
+ .key key,
123
+ .val val,
124
+ .json un_object object
125
+ } put object;
126
+
127
+ del = {
128
+ .key key,
129
+ .json un_object object
130
+ } remove object;
131
+
132
+ set_nth = {
133
+ .index index,
134
+ .val val,
135
+ .json un_list list
136
+ } put_nth list;
137
+
138
+ del_nth = {
139
+ .index index,
140
+ .json un_list list
141
+ } remove_nth list;
142
+
143
+ ---------------------------------------------------------------
144
+ -- §4. Cardinality, Keys & Values Extraction
145
+ ---------------------------------------------------------------
146
+
147
+ length_nat = <
148
+ un_object obj_size,
149
+ un_list list_size,
150
+ un_string str_len
151
+ >;
152
+
153
+ length = length_nat natToFloat64 number;
154
+
155
+ push = {
156
+ .json un_list list_size index,
157
+ .val val,
158
+ .json json
159
+ } set_nth;
160
+
161
+ keys_raw = un_object obj_keys_raw;
162
+
163
+ keys_to_trie = <
164
+ { .klist un_nil a, .index <_?, un0, un1> b, .acc then } .then,
165
+ {
166
+ .klist cdr klist,
167
+ .index inc index,
168
+ {
169
+ .index index,
170
+ .klist car string val,
171
+ .acc list
172
+ } put_nth acc
173
+ } keys_to_trie
174
+ >;
175
+
176
+ keys = un_object obj_keys_raw
177
+ { () klist, c0 index, nil_trie acc } keys_to_trie
178
+ list;
179
+
180
+ values_raw = <
181
+ un_object obj_values_raw,
182
+ un_list obj_values_raw
183
+ >;
184
+
185
+ values = <
186
+ un_object obj_values_raw
187
+ { () klist, c0 index, nil_trie acc } values_to_trie list,
188
+ ?djson[]
189
+ >;
190
+
191
+ ---------------------------------------------------------------
192
+ -- §5. Type Predicates
193
+ ---------------------------------------------------------------
194
+
195
+ is_null? = { un_null if, () then } .then;
196
+ is_bool? = { un_bool if, () then } .then;
197
+ is_number? = { un_number if, () then } .then;
198
+ is_string? = { un_string if, () then } .then;
199
+ is_list? = { un_list if, () then } .then;
200
+ is_object? = { un_object if, () then } .then;
201
+
202
+ type_of = <
203
+ un_null null_type,
204
+ un_bool bool_type,
205
+ un_number number_type,
206
+ un_string string_type,
207
+ un_list list_type,
208
+ un_object object_type
209
+ >;
210
+
211
+ ---------------------------------------------------------------
212
+ -- §6. JSON Text Decoder (RFC 8259)
213
+ ---------------------------------------------------------------
214
+
215
+ string_kind = ?unicode[] <
216
+ { { () x, _quote_ y } eq_char? if, quote_kind then } .then,
217
+ { { () x, _backslash_ y } eq_char? if, escape_kind then } .then,
218
+ { un_ascii { .6 is0 a, .5 is0 b } if, control_kind then } .then,
219
+ plain_kind
220
+ >;
221
+
222
+ surrogate_pair = {
223
+ { .value x, c55296 y } minus high,
224
+ .rest backslash_char u_char hex4 low
225
+ } {
226
+ { .high x, c1024 y } lt? a,
227
+ { .low.value x, c57344 y } lt? b,
228
+ .high times1024 x,
229
+ { .low.value x, c56320 y } minus y,
230
+ .low.rest rest
231
+ } {
232
+ { { .x x, .y y } plus x, c65536 y } plus to_unicode value,
233
+ .rest rest
234
+ };
235
+
236
+ escape = <
237
+ quote_char { () rest, _quote_ value },
238
+ backslash_char { () rest, _backslash_ value },
239
+ slash_char { () rest, _slash_ value },
240
+ b_char { () rest, _bs_ value },
241
+ f_char { () rest, _ff_ value },
242
+ n_char { () rest, _lf_ value },
243
+ r_char { () rest, _cr_ value },
244
+ t_char { () rest, _tab_ value },
245
+ u_char hex4 <
246
+ surrogate_pair,
247
+ { .rest rest, .value to_unicode value }
248
+ >
249
+ >;
250
+
251
+ string_loop = {
252
+ .rest cdr rest,
253
+ .rest car car,
254
+ .acc acc,
255
+ .rest car string_kind kind
256
+ } <
257
+ {
258
+ .kind un_quote_kind if,
259
+ { .rest rest, .acc reverse value } then
260
+ } .then,
261
+ <
262
+ {
263
+ .rest rest,
264
+ { { .kind un_plain_kind a, .car b } .b car, .acc cdr } cons acc
265
+ },
266
+ { { .kind un_escape_kind a, .rest escape b } .b escaped, .acc acc }
267
+ { .escaped.rest rest, { .escaped.value car, .acc cdr } cons acc }
268
+ > string_loop
269
+ >;
270
+
271
+ json_string = quote_char { () rest, nil acc } string_loop;
272
+
273
+ json_number = {
274
+ < minus_char {}, digit {} > if, () then
275
+ } .then scanFloat64 {
276
+ .rest rest, .value finite_float64? number value
277
+ };
278
+
279
+ json_value = ws <
280
+ n_char u_char l_char l_char { () rest, null value },
281
+ t_char r_char u_char e_char { () rest, true value },
282
+ f_char a_char l_char s_char e_char { () rest, false value },
283
+ json_string { .rest rest, .value string value },
284
+ json_array,
285
+ json_object,
286
+ json_number
287
+ >;
288
+
289
+ array_item = {
290
+ .rest json_value item, .tree tree, .index index
291
+ } {
292
+ .item.rest ws rest,
293
+ { .index index, .item.value val, .tree list } put_nth tree,
294
+ .index inc index
295
+ } <
296
+ { .rest close_array rest, .tree list value },
297
+ { .rest comma_char rest, .tree tree, .index index } array_item
298
+ >;
299
+
300
+ json_array = open_array ws <
301
+ close_array { () rest, empty_list value },
302
+ { () rest, nil_trie tree, c0 index } array_item
303
+ >;
304
+
305
+ object_item = {
306
+ .rest ws json_string key, .tree tree
307
+ } {
308
+ .key.rest ws colon_char json_value item,
309
+ .key.value key, .tree tree
310
+ } {
311
+ .item.rest ws rest,
312
+ { .key key, .item.value val, .tree object } put tree
313
+ } <
314
+ { .rest close_object rest, .tree object value },
315
+ { .rest comma_char rest, .tree tree } object_item
316
+ >;
317
+
318
+ json_object = open_object ws <
319
+ close_object { () rest, empty_object value },
320
+ { () rest, nil_trie tree } object_item
321
+ >;
322
+
323
+ parseJson = json_value {
324
+ .rest ws un_nil end,
325
+ .value value
326
+ } .value;