@fraczak/k 7.2.0 → 8.1.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 (46) hide show
  1. package/CHANGELOG.md +90 -0
  2. package/DOCS/BIG_PICTURE.md +54 -7
  3. package/DOCS/CODECS.md +21 -13
  4. package/DOCS/CODEC_ARCHITECTURE.md +1 -1
  5. package/DOCS/DICTIONARY.md +13 -4
  6. package/DOCS/FILE_FORMATS.md +3 -2
  7. package/DOCS/JSON_APPROACHES_COMPARISON.md +302 -0
  8. package/DOCS/NAMED_PATTERNS.md +317 -0
  9. package/DOCS/PATTERNS.md +59 -11
  10. package/DOCS/REPL.md +13 -2
  11. package/DOCS/WEB_REPL_REQUIREMENTS.md +3 -2
  12. package/Examples/arithmetics.k +22 -17
  13. package/Examples/bnat.k +21 -11
  14. package/Examples/buda.k +37 -20
  15. package/Examples/byte.k +3 -1
  16. package/Examples/code.k +38 -39
  17. package/Examples/djson.k +789 -0
  18. package/Examples/esc.k +1 -1
  19. package/Examples/karatsuba-mult.k +16 -13
  20. package/Examples/list.k +8 -4
  21. package/Examples/nat.k +17 -11
  22. package/Examples/poly.k +11 -3
  23. package/Examples/vhdl_to_k.k +43 -31
  24. package/README.md +40 -16
  25. package/browser/repl-browser.mjs +2 -0
  26. package/browser/tree-view.mjs +35 -16
  27. package/browser/vfs-data.mjs +10 -8
  28. package/codecs/README.md +1 -1
  29. package/codecs/djson.mjs +375 -84
  30. package/core.k +79 -115
  31. package/index.mjs +3 -0
  32. package/named-patterns.mjs +267 -0
  33. package/object.mjs +31 -3
  34. package/package.json +1 -1
  35. package/parser.jison +68 -5
  36. package/parser.mjs +111 -60
  37. package/pretty.mjs +5 -0
  38. package/repl.mjs +188 -22
  39. package/scripts/build-repl-html.mjs +58 -4
  40. package/scripts/run-tests.mjs +10 -0
  41. package/symbol-table.mjs +20 -2
  42. package/tests/test-codecs.mjs +30 -17
  43. package/tests/test-djson-methods.mjs +285 -0
  44. package/tests/test-named-patterns.mjs +189 -0
  45. package/tests/test-repl-html.mjs +47 -4
  46. package/tests/test-repl.mjs +149 -0
package/Examples/bnat.k CHANGED
@@ -1,6 +1,7 @@
1
1
  -- Standard binary encoding of natural numbers
2
2
 
3
- bnat? = ?< {} _, bnat? 0, bnat? 1 > = bnat?;
3
+ bnat[] = < {} _, B 0, B 1 > = B;
4
+ bnat? = ? bnat[];
4
5
 
5
6
  _ = {}|_;
6
7
  _1 = |1;
@@ -23,7 +24,7 @@ remove_leading_zeros = < /0 remove_leading_zeros, () >;
23
24
  reverse = {() x, _ result} _reverse;
24
25
 
25
26
  _reverse =
26
- ?{ <X 0, X 1, {} _>=X x, ... }
27
+ ?{ bnat[] = X x, ... }
27
28
  <
28
29
  {.x/_ if, .result then} .then,
29
30
  {.x/0 x, .result _0 result} _reverse,
@@ -52,7 +53,7 @@ plus =
52
53
 
53
54
  _plus =
54
55
  -- least significant bit first addition with carry
55
- ?{ <X 0, X 1, {} _>=X x, ... }
56
+ ?{ bnat[] = X x, ... }
56
57
  <
57
58
  {.x/_ if1, .y/_ if2, <.c/0 bnat_0, bnat_1> then} .then,
58
59
  {.x/_ if1, .c/0 if2, .y then} .then,
@@ -88,7 +89,7 @@ minus =
88
89
 
89
90
  _minus =
90
91
  -- least significat bit first substraction with carry
91
- ?{ <X 0, X 1, {} _>=X x, <Y 0, Y 1, {} _>=Y y, ... }
92
+ ?{ bnat[] = X x, bnat[] = Y y, ... }
92
93
  <
93
94
  {.x x, .y/_ y, .c/0 if} .x,
94
95
  {.x/1 x, .y/_ y, .c/1 if} .x _0,
@@ -116,10 +117,10 @@ times =
116
117
  ;
117
118
 
118
119
  _times =
119
- ?{ <Y 0, Y 1, {} _>=Y y, ... }
120
+ ?{ bnat[] = Y y, ... }
120
121
  <
121
122
  { .y/_ if, .result then } .then,
122
- { .result _0 result, ._x _x, .y/0 y } _times,
123
+ { .result _0 result, ._x _x, .y/0 y } _times,
123
124
  { { ._x x, .result _0 y, c0 c } _plus result, ._x _x, .y/1 y } _times
124
125
  >
125
126
  ;
@@ -149,7 +150,7 @@ align =
149
150
  ;
150
151
 
151
152
  _align =
152
- ?{ <X 0, X 1, {} _>=X _x, <Y 0, Y 1, {} _>=Y _y, ... }
153
+ ?{ bnat[] = X _x, bnat[] = Y _y, ... }
153
154
  <
154
155
  {._x/_ if1, ._y/_ if2, {.x x, .y y} then} .then,
155
156
  {._x </0, /_ _> _x, ._y </0, /_ _> _y, .x _0 x, .y _0 y} _align,
@@ -166,7 +167,7 @@ gt = {} |gt;
166
167
 
167
168
  compare = align _compare;
168
169
  _compare =
169
- ?{ <X 0, X 1, {} _>=X x, <Y 0, Y 1, {} _>=Y y, ... }
170
+ ?{ bnat[] = X x, bnat[] = Y y, ... }
170
171
  <
171
172
  {.x/_ if1, .y/_ if2 } eq,
172
173
  {.x/0 x, .y/0 y} _compare,
@@ -201,7 +202,11 @@ log2_floor =
201
202
  _log2_floor =
202
203
  <
203
204
  {{.step x, .x y} gt? if, .log then} .then,
204
- {.x x, {.step x, bnat_0 y} gt? .x append_0 step, .log inc log} _log2_floor
205
+ {
206
+ .x x,
207
+ {.step x, bnat_0 y} gt? .x append_0 step,
208
+ .log inc log
209
+ } _log2_floor
205
210
  >
206
211
  ;
207
212
 
@@ -260,7 +265,11 @@ pow =
260
265
  <
261
266
  .exponent zero? bnat_1,
262
267
  {.base base, .exponent half half}
263
- {.base base, .half.rem extra, {.base base, .half.div exponent} pow {()x,()y} times pow}
268
+ {
269
+ .base base,
270
+ .half.rem extra,
271
+ {.base base, .half.div exponent} pow {()x, ()y} times pow
272
+ }
264
273
  <
265
274
  {.extra zero? if, .pow then} .then,
266
275
  {.base x, .pow y} times
@@ -281,7 +290,8 @@ _sqrt =
281
290
  { () aux, {.input.x x, .middle y} div .div middle }
282
291
  {
283
292
  .aux.input.x x,
284
- { .aux.middle x, .middle y } < gt?, {.x y, .y x} > {.y min, .x max} range
293
+ { .aux.middle x, .middle y }
294
+ < gt?, {.x y, .y x} > {.y min, .x max} range
285
295
  } _sqrt
286
296
  >
287
297
  ;
package/Examples/buda.k CHANGED
@@ -1,44 +1,61 @@
1
- // we will use arithmetics.k
1
+ -- Examples/buda.k: Relational list algorithms using named patterns
2
+ -- Uses arbitrary-precision integers from Examples/arithmetics.k.
2
3
 
3
- nths = ?{$int n, LIST list}
4
+ bits[] = < {} _, B 0, B 1 > = B;
5
+ int[] = < bits[] "+", bits[] "-" >;
6
+
7
+ list[X] = < {} nil, { X car, Y cdr } cons > = Y;
8
+ list? = ? list[X];
9
+
10
+ nil = {} | nil list?;
11
+ cons = { .car car, .cdr cdr } | cons list?;
12
+ car = list? /cons .car;
13
+ cdr = list? /cons .cdr;
14
+
15
+ nths = ?{ int[] n, list[X] list }
4
16
  <
5
- {.n pos? dec n, .list cdr list } nths,
6
- {.n zero_int? if, .list list? car then} .then
17
+ { .n pos? dec n, .list cdr list } nths,
18
+ { .n zero_int? if, .list car then } .then
7
19
  >;
8
20
 
9
- reverse = ? LIST list?
10
- {nil ? LIST reversed, () list } _reverse;
21
+ reverse = ? list[X]
22
+ { nil reversed, () list } _reverse;
11
23
 
12
- _reverse = ?{ LIST reversed, LIST list}
24
+ _reverse = ?{ list[X] reversed, list[X] list }
13
25
  <
14
- {{.list car car, .reversed cdr} cons reversed, .list cdr list } _reverse,
26
+ {
27
+ { .list car car, .reversed cdr } cons reversed,
28
+ .list cdr list
29
+ } _reverse,
15
30
  .reversed
16
31
  >
17
32
  ;
18
33
 
19
- split = ? LIST
20
- { nil ? LIST odd, nil ? LIST even, reverse list } _split
34
+ split = ? list[X]
35
+ { nil odd, nil even, reverse list } _split
21
36
  ;
22
37
 
23
- _split = ? { LIST odd, LIST even, LIST list }
38
+ _split = ?{ list[X] odd, list[X] even, list[X] list }
24
39
  <
25
40
  {
26
- {.list car car, .odd cdr} cons odd,
27
- .even even,
28
- .list cdr list
41
+ { .list car car, .odd cdr } cons odd,
42
+ .even even,
43
+ .list cdr list
29
44
  } split_,
30
45
 
31
- {.odd odd, .even even}
46
+ { .odd odd, .even even }
32
47
  >
33
48
  ;
34
49
 
35
- split_ = ? { LIST odd, LIST even, LIST list }
50
+ split_ = ?{ list[X] odd, list[X] even, list[X] list }
36
51
  <
37
52
  {
38
- .odd odd,
39
- {.list car car, .even cdr} cons even,
40
- .list cdr list
53
+ .odd odd,
54
+ { .list car car, .even cdr } cons even,
55
+ .list cdr list
41
56
  } _split,
42
- {.odd odd, .even even}
57
+ { .odd odd, .even even }
43
58
  >
44
59
  ;
60
+
61
+ ()
package/Examples/byte.k CHANGED
@@ -11,7 +11,9 @@ map = <
11
11
  _
12
12
  > | ascii;
13
13
 
14
- filter = ?< {} nil, { (...) car, S cdr } cons > = S <
14
+ list[X] = < {} nil, { X car, Y cdr } cons > = Y;
15
+
16
+ filter = ? list[(...)] <
15
17
  /cons {.car map car, .cdr filter cdr} | cons,
16
18
  ()
17
19
  >;
package/Examples/code.k CHANGED
@@ -1,44 +1,43 @@
1
- $ code = < fields product, fields union, chars ref >;
2
- $ fields = < {} nil, { chars car, fields cdr } cons >;
3
- $ chars = <
4
- chars" ", chars"!", chars'"', chars"#", chars"$", chars"%", chars"&", chars"'",
5
- chars"(", chars")", chars"*", chars"+", chars",", chars"-", chars".", chars"/",
6
- chars"0", chars"1", chars"2", chars"3", chars"4", chars"5", chars"6", chars"7",
7
- chars"8", chars"9", chars":", chars";", chars"<", chars"=", chars">", chars"?",
8
- chars"@", chars"A", chars"B", chars"C", chars"D", chars"E", chars"F", chars"G",
9
- chars"H", chars"I", chars"J", chars"K", chars"L", chars"M", chars"N", chars"O",
10
- chars"P", chars"Q", chars"R", chars"S", chars"T", chars"U", chars"V", chars"W",
11
- chars"X", chars"Y", chars"Z", chars"[", chars"\\", chars"]", chars"^", chars"_",
12
- chars"`", chars"a", chars"b", chars"c", chars"d", chars"e", chars"f", chars"g",
13
- chars"h", chars"i", chars"j", chars"k", chars"l", chars"m", chars"n", chars"o",
14
- chars"p", chars"q", chars"r", chars"s", chars"t", chars"u", chars"v", chars"w",
15
- chars"x", chars"y", chars"z", chars"{", chars"|", chars"}", chars"~",
16
- {} end
17
- >;
18
-
19
- $ char = <
20
- {}" ", {}"!", {}'"', {}"#", {}"$", {}"%", {}"&", {}"'",
21
- {}"(", {}")", {}"*", {}"+", {}",", {}"-", {}".", {}"/",
22
- {}"0", {}"1", {}"2", {}"3", {}"4", {}"5", {}"6", {}"7",
23
- {}"8", {}"9", {}":", {}";", {}"<", {}"=", {}">", {}"?",
24
- {}"@", {}"A", {}"B", {}"C", {}"D", {}"E", {}"F", {}"G",
25
- {}"H", {}"I", {}"J", {}"K", {}"L", {}"M", {}"N", {}"O",
26
- {}"P", {}"Q", {}"R", {}"S", {}"T", {}"U", {}"V", {}"W",
27
- {}"X", {}"Y", {}"Z", {}"[", {}"\\", {}"]", {}"^", {}"_",
28
- {}"`", {}"a", {}"b", {}"c", {}"d", {}"e", {}"f", {}"g",
29
- {}"h", {}"i", {}"j", {}"k", {}"l", {}"m", {}"n", {}"o",
30
- {}"p", {}"q", {}"r", {}"s", {}"t", {}"u", {}"v", {}"w",
31
- {}"x", {}"y", {}"z", {}"{", {}"|", {}"}", {}"~"
32
- >;
1
+ -- Examples/code.k: Pattern representation and encoding
33
2
 
3
+ chars[] = <
4
+ C " ", C "!", C '"', C "#", C "$", C "%", C "&", C "'",
5
+ C "(", C ")", C "*", C "+", C ",", C "-", C ".", C "/",
6
+ C "0", C "1", C "2", C "3", C "4", C "5", C "6", C "7",
7
+ C "8", C "9", C ":", C ";", C "<", C "=", C ">", C "?",
8
+ C "@", C "A", C "B", C "C", C "D", C "E", C "F", C "G",
9
+ C "H", C "I", C "J", C "K", C "L", C "M", C "N", C "O",
10
+ C "P", C "Q", C "R", C "S", C "T", C "U", C "V", C "W",
11
+ C "X", C "Y", C "Z", C "[", C "\\", C "]", C "^", C "_",
12
+ C "`", C "a", C "b", C "c", C "d", C "e", C "f", C "g",
13
+ C "h", C "i", C "j", C "k", C "l", C "m", C "n", C "o",
14
+ C "p", C "q", C "r", C "s", C "t", C "u", C "v", C "w",
15
+ C "x", C "y", C "z", C "{", C "|", C "}", C "~",
16
+ {} end
17
+ > = C;
34
18
 
35
- nil = {}|nil;
36
- _ = {}|end $chars;
19
+ char[] = <
20
+ {} " ", {} "!", {} '"', {} "#", {} "$", {} "%", {} "&", {} "'",
21
+ {} "(", {} ")", {} "*", {} "+", {} ",", {} "-", {} ".", {} "/",
22
+ {} "0", {} "1", {} "2", {} "3", {} "4", {} "5", {} "6", {} "7",
23
+ {} "8", {} "9", {} ":", {} ";", {} "<", {} "=", {} ">", {} "?",
24
+ {} "@", {} "A", {} "B", {} "C", {} "D", {} "E", {} "F", {} "G",
25
+ {} "H", {} "I", {} "J", {} "K", {} "L", {} "M", {} "N", {} "O",
26
+ {} "P", {} "Q", {} "R", {} "S", {} "T", {} "U", {} "V", {} "W",
27
+ {} "X", {} "Y", {} "Z", {} "[", {} "\\", {} "]", {} "^", {} "_",
28
+ {} "`", {} "a", {} "b", {} "c", {} "d", {} "e", {} "f", {} "g",
29
+ {} "h", {} "i", {} "j", {} "k", {} "l", {} "m", {} "n", {} "o",
30
+ {} "p", {} "q", {} "r", {} "s", {} "t", {} "u", {} "v", {} "w",
31
+ {} "x", {} "y", {} "z", {} "{", {} "|", {} "}", {} "~"
32
+ >;
37
33
 
38
- KL = _|L|K $chars;
39
-
40
- bool =
41
- { KL car, { KL car, nil cdr } | cons cdr } | cons | union
42
- $code;
34
+ fields[] = < {} nil, { chars[] car, F cdr } cons > = F;
43
35
 
36
+ code[] = < fields[] product, fields[] union, chars[] ref >;
37
+
38
+ nil = {} | nil;
39
+ _ = ({} | end) ? chars[];
40
+ KL = (_ | L | K) ? chars[];
41
+
42
+ bool = { KL car, { KL car, nil cdr } | cons cdr } | cons | union ? code[];
44
43
  bool