@fraczak/k 5.1.2 → 6.0.1

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 (115) hide show
  1. package/.codex +0 -0
  2. package/Code-derivation-tests/blowup.mjs +3 -3
  3. package/Code-derivation-tests/bnat.mjs +2 -2
  4. package/Code-derivation-tests/codec-witness-pattern.mjs +50 -0
  5. package/Code-derivation-tests/convergence-strategy.mjs +24 -0
  6. package/Code-derivation-tests/dot.mjs +12 -9
  7. package/Code-derivation-tests/filter-syntax.mjs +21 -0
  8. package/Code-derivation-tests/loop.mjs +8 -2
  9. package/Code-derivation-tests/nat.mjs +1 -1
  10. package/Code-derivation-tests/not-not.mjs +3 -3
  11. package/Code-derivation-tests/pattern-envelope.mjs +68 -0
  12. package/Code-derivation-tests/rlz.mjs +2 -2
  13. package/{BIG_PICTURE.md → DOCS/BIG_PICTURE.md} +103 -50
  14. package/DOCS/REPL.md +191 -0
  15. package/DOCS/a.html +398 -0
  16. package/DOCS/book/01-introduction.md +51 -0
  17. package/DOCS/book/02-syntax-and-values.md +111 -0
  18. package/DOCS/book/03-types-as-finite-automata.md +156 -0
  19. package/DOCS/book/04-partial-functions-and-composition.md +146 -0
  20. package/DOCS/book/05-typing-filters-and-normalization.md +111 -0
  21. package/DOCS/book-combined.html +3798 -0
  22. package/DOCS/book-combined.md +3745 -0
  23. package/DOCS/book-combined.pdf +0 -0
  24. package/DOCS/book-combined.tex +5682 -0
  25. package/DOCS/book.html +65 -0
  26. package/DOCS/book.md +54 -0
  27. package/DOCS/book.pdf +0 -0
  28. package/DOCS/book.tex +150 -0
  29. package/NAT.k~ +452 -0
  30. package/README.md +204 -508
  31. package/TypeGraph.mjs +43 -0
  32. package/TypePatternGraph.mjs +251 -0
  33. package/Value.mjs +192 -0
  34. package/augmentation.mjs +198 -0
  35. package/codecs/BINARY_FORMAT.html +471 -0
  36. package/codecs/BINARY_FORMAT.md +53 -0
  37. package/codecs/POLYMORPHIC_BINARY_FORMAT.html +355 -0
  38. package/codecs/POLYMORPHIC_BINARY_FORMAT.md +303 -0
  39. package/codecs/README.md +215 -0
  40. package/codecs/example-pipeline.mjs +149 -0
  41. package/codecs/ieee.mjs +140 -0
  42. package/codecs/int.mjs +104 -0
  43. package/codecs/json-codec.mjs +215 -0
  44. package/codecs/json.mjs +40 -0
  45. package/codecs/k-parse.mjs +90 -0
  46. package/codecs/k-pattern.mjs +75 -0
  47. package/codecs/k-print.mjs +46 -0
  48. package/codecs/runtime/codec.mjs +768 -0
  49. package/codecs/runtime/ieee-pattern.mjs +30 -0
  50. package/codecs/runtime/pattern-json.mjs +96 -0
  51. package/codecs/runtime/prefix-codec.mjs +425 -0
  52. package/codecs/runtime/show-value.mjs +68 -0
  53. package/codecs/runtime/typeFromValue.mjs +34 -0
  54. package/codecs/runtime/unicode-string.mjs +220 -0
  55. package/codecs/show.mjs +41 -0
  56. package/codecs/string-codec.mjs +80 -0
  57. package/codecs/unit.mjs +51 -0
  58. package/codecs/utf16.mjs +66 -0
  59. package/codecs/utf8.mjs +37 -0
  60. package/codes.mjs +146 -44
  61. package/compiler.mjs +111 -0
  62. package/convergence.mjs +197 -0
  63. package/core.k +197 -0
  64. package/export.mjs +22 -32
  65. package/hash.mjs +29 -31
  66. package/ieee.klib +0 -0
  67. package/index.mjs +41 -15
  68. package/k-vscode/.vscode/launch.json +14 -0
  69. package/k-vscode/README.md +65 -0
  70. package/k-vscode/language-configuration.json +42 -0
  71. package/k-vscode/package-lock.json +4176 -0
  72. package/k-vscode/package.json +57 -0
  73. package/k-vscode/snippets/k.json +92 -0
  74. package/k-vscode/syntaxes/k.tmLanguage.json +243 -0
  75. package/k.mjs +56 -54
  76. package/object.mjs +517 -0
  77. package/objects/README.md +38 -0
  78. package/objects/compile-lib.mjs +42 -0
  79. package/objects/compile.mjs +42 -0
  80. package/objects/decompile.mjs +32 -0
  81. package/package.json +13 -6
  82. package/parser.jison +27 -46
  83. package/parser.mjs +68 -100
  84. package/pretty.mjs +21 -40
  85. package/repl2.mjs +662 -0
  86. package/run.mjs +264 -231
  87. package/symbol-table.mjs +3 -17
  88. package/test-fingerprint.mjs +7 -7
  89. package/test-hash-fuzz.mjs +107 -0
  90. package/test-hash-normalization.mjs +134 -0
  91. package/test-k-object.mjs +74 -0
  92. package/test-repl2.mjs +106 -0
  93. package/test.mjs +45 -70
  94. package/tests.sh +10 -1
  95. package/type_registry/registry.mjs~ +105 -0
  96. package/type_registry/typeParser.jison~ +132 -0
  97. package/typing.mjs +129 -676
  98. package/unification.mjs +157 -0
  99. package/valueIO.mjs +54 -0
  100. package/valueParser.jison +143 -0
  101. package/valueParser.mjs +713 -0
  102. package/Code-derivation-tests/built-in.mjs +0 -19
  103. package/Code-derivation-tests/vector.mjs +0 -19
  104. package/Filters/bug.k +0 -3
  105. package/Filters/composing.k +0 -15
  106. package/Filters/filter.k +0 -9
  107. package/TODO.md +0 -53
  108. package/TYPING.md +0 -365
  109. package/k-repl.cast +0 -1913
  110. package/k.cast +0 -618
  111. package/my-objects.json +0 -6
  112. package/patterns.mjs +0 -543
  113. package/repl.mjs +0 -213
  114. package/test-filters.mjs +0 -45
  115. package/typing-test.mjs +0 -40
@@ -0,0 +1,220 @@
1
+ import { Product, Variant } from "../../Value.mjs";
2
+
3
+ const UNIT = new Product({});
4
+ const BIT0 = new Variant("0", UNIT);
5
+ const BIT1 = new Variant("1", UNIT);
6
+
7
+ function bitValue(bit) {
8
+ return bit === 0 ? BIT0 : BIT1;
9
+ }
10
+
11
+ function requireProduct(value, where) {
12
+ if (!(value instanceof Product)) {
13
+ throw new Error(`${where}: expected Product`);
14
+ }
15
+ return value.product;
16
+ }
17
+
18
+ function requireVariant(value, where) {
19
+ if (!(value instanceof Variant)) {
20
+ throw new Error(`${where}: expected Variant`);
21
+ }
22
+ return value;
23
+ }
24
+
25
+ function bitsProduct(width, n) {
26
+ const product = {};
27
+ for (let i = width - 1; i >= 0; i--) {
28
+ product[String(i)] = bitValue((n >> i) & 1);
29
+ }
30
+ return new Product(product);
31
+ }
32
+
33
+ function parseBitsProduct(value, maxBit, where) {
34
+ const product = requireProduct(value, where);
35
+ let n = 0;
36
+ for (let i = maxBit; i >= 0; i--) {
37
+ const entry = requireVariant(product[String(i)], `${where}.bit${i}`);
38
+ if (entry.tag !== "0" && entry.tag !== "1") {
39
+ throw new Error(`${where}.bit${i}: expected tag 0 or 1`);
40
+ }
41
+ n = (n << 1) | (entry.tag === "1" ? 1 : 0);
42
+ }
43
+ return n;
44
+ }
45
+
46
+ function byteProduct(n) {
47
+ return bitsProduct(8, n);
48
+ }
49
+
50
+ function parseByteProduct(value, where) {
51
+ return parseBitsProduct(value, 7, where);
52
+ }
53
+
54
+ function encodeBmpCommonHi(hi) {
55
+ if (hi >= 0x08 && hi <= 0x0f) return new Variant("h08_0F", bitsProduct(3, hi - 0x08));
56
+ if (hi >= 0x10 && hi <= 0x7f) return new Variant("h10_7F", bitsProduct(7, hi - 0x10));
57
+ if (hi >= 0x80 && hi <= 0xcf) return new Variant("h80_CF", bitsProduct(7, hi - 0x80));
58
+ if (hi >= 0xd0 && hi <= 0xd7) return new Variant("hD0_D7", bitsProduct(3, hi - 0xd0));
59
+ if (hi === 0xf9) return new Variant("hF9", UNIT);
60
+ if (hi >= 0xfa && hi <= 0xfb) return new Variant("hFA_FB", bitsProduct(1, hi - 0xfa));
61
+ if (hi >= 0xfc && hi <= 0xfd) return new Variant("hFC_FD", bitsProduct(1, hi - 0xfc));
62
+ if (hi >= 0xfe && hi <= 0xff) return new Variant("hFE_FF", bitsProduct(1, hi - 0xfe));
63
+ throw new Error(`Unsupported BMP common hi byte: 0x${hi.toString(16)}`);
64
+ }
65
+
66
+ function decodeBmpCommonHi(value) {
67
+ const v = requireVariant(value, "bmp_common.hi");
68
+ if (v.tag === "h08_0F") return 0x08 + parseBitsProduct(v.value, 2, "h08_0F");
69
+ if (v.tag === "h10_7F") return 0x10 + parseBitsProduct(v.value, 6, "h10_7F");
70
+ if (v.tag === "h80_CF") return 0x80 + parseBitsProduct(v.value, 6, "h80_CF");
71
+ if (v.tag === "hD0_D7") return 0xd0 + parseBitsProduct(v.value, 2, "hD0_D7");
72
+ if (v.tag === "hF9") return 0xf9;
73
+ if (v.tag === "hFA_FB") return 0xfa + parseBitsProduct(v.value, 0, "hFA_FB");
74
+ if (v.tag === "hFC_FD") return 0xfc + parseBitsProduct(v.value, 0, "hFC_FD");
75
+ if (v.tag === "hFE_FF") return 0xfe + parseBitsProduct(v.value, 0, "hFE_FF");
76
+ throw new Error(`Unsupported bmp_common.hi tag: ${v.tag}`);
77
+ }
78
+
79
+ function encodeBmpPrivateHi(hi) {
80
+ if (hi >= 0xe0 && hi <= 0xef) return new Variant("hE0_EF", bitsProduct(4, hi - 0xe0));
81
+ if (hi >= 0xf0 && hi <= 0xf7) return new Variant("hF0_F7", bitsProduct(3, hi - 0xf0));
82
+ if (hi === 0xf8) return new Variant("hF8", UNIT);
83
+ throw new Error(`Unsupported BMP private-use hi byte: 0x${hi.toString(16)}`);
84
+ }
85
+
86
+ function decodeBmpPrivateHi(value) {
87
+ const v = requireVariant(value, "bmp_private_use.hi");
88
+ if (v.tag === "hE0_EF") return 0xe0 + parseBitsProduct(v.value, 3, "hE0_EF");
89
+ if (v.tag === "hF0_F7") return 0xf0 + parseBitsProduct(v.value, 2, "hF0_F7");
90
+ if (v.tag === "hF8") return 0xf8;
91
+ throw new Error(`Unsupported bmp_private_use.hi tag: ${v.tag}`);
92
+ }
93
+
94
+ function encodePlane2To16(plane) {
95
+ if (plane >= 2 && plane <= 3) return new Variant("p02_03", bitsProduct(1, plane - 2));
96
+ if (plane >= 4 && plane <= 7) return new Variant("p04_07", bitsProduct(2, plane - 4));
97
+ if (plane >= 8 && plane <= 15) return new Variant("p08_0F", bitsProduct(3, plane - 8));
98
+ if (plane === 16) return new Variant("p10", UNIT);
99
+ throw new Error(`Plane out of range for supplementary_planes2_16: ${plane}`);
100
+ }
101
+
102
+ function decodePlane2To16(value) {
103
+ const v = requireVariant(value, "supplementary_planes2_16.plane");
104
+ if (v.tag === "p02_03") return 2 + parseBitsProduct(v.value, 0, "p02_03");
105
+ if (v.tag === "p04_07") return 4 + parseBitsProduct(v.value, 1, "p04_07");
106
+ if (v.tag === "p08_0F") return 8 + parseBitsProduct(v.value, 2, "p08_0F");
107
+ if (v.tag === "p10") return 16;
108
+ throw new Error(`Unsupported supplementary plane tag: ${v.tag}`);
109
+ }
110
+
111
+ function codePointToUnicodeValue(cp) {
112
+ if (!Number.isInteger(cp) || cp < 0 || cp > 0x10ffff) {
113
+ throw new Error(`Code point out of Unicode range: ${cp}`);
114
+ }
115
+ if (cp >= 0xd800 && cp <= 0xdfff) {
116
+ throw new Error(`Surrogate code point is not a scalar value: U+${cp.toString(16).toUpperCase()}`);
117
+ }
118
+
119
+ if (cp <= 0x7f) {
120
+ return new Variant("ascii", bitsProduct(7, cp));
121
+ }
122
+
123
+ if (cp <= 0x7ff) {
124
+ return new Variant("plane0", bitsProduct(11, cp));
125
+ }
126
+
127
+ if (cp <= 0xffff) {
128
+ const hi = (cp >> 8) & 0xff;
129
+ const lo = cp & 0xff;
130
+ if (hi >= 0xe0 && hi <= 0xf8) {
131
+ return new Variant("bmp_private_use", new Product({ hi: encodeBmpPrivateHi(hi), lo: byteProduct(lo) }));
132
+ }
133
+ return new Variant("bmp_common", new Product({ hi: encodeBmpCommonHi(hi), lo: byteProduct(lo) }));
134
+ }
135
+
136
+ const mid = (cp >> 8) & 0xff;
137
+ const lo = cp & 0xff;
138
+
139
+ if (cp <= 0x1ffff) {
140
+ return new Variant("supplementary_plane1", new Product({
141
+ mid: byteProduct(mid),
142
+ lo: byteProduct(lo)
143
+ }));
144
+ }
145
+
146
+ const plane = cp >> 16;
147
+ return new Variant("supplementary_planes2_16", new Product({
148
+ plane: encodePlane2To16(plane),
149
+ mid: byteProduct(mid),
150
+ lo: byteProduct(lo)
151
+ }));
152
+ }
153
+
154
+ function unicodeValueToCodePoint(value) {
155
+ const v = requireVariant(value, "unicode");
156
+
157
+ if (v.tag === "ascii") return parseBitsProduct(v.value, 6, "ascii");
158
+ if (v.tag === "plane0") return parseBitsProduct(v.value, 10, "plane0");
159
+
160
+ if (v.tag === "bmp_common") {
161
+ const p = requireProduct(v.value, "bmp_common");
162
+ return (decodeBmpCommonHi(p.hi) << 8) | parseByteProduct(p.lo, "bmp_common.lo");
163
+ }
164
+
165
+ if (v.tag === "bmp_private_use") {
166
+ const p = requireProduct(v.value, "bmp_private_use");
167
+ return (decodeBmpPrivateHi(p.hi) << 8) | parseByteProduct(p.lo, "bmp_private_use.lo");
168
+ }
169
+
170
+ if (v.tag === "supplementary_plane1") {
171
+ const p = requireProduct(v.value, "supplementary_plane1");
172
+ return (1 << 16) | (parseByteProduct(p.mid, "supplementary_plane1.mid") << 8) | parseByteProduct(p.lo, "supplementary_plane1.lo");
173
+ }
174
+
175
+ if (v.tag === "supplementary_planes2_16") {
176
+ const p = requireProduct(v.value, "supplementary_planes2_16");
177
+ return (decodePlane2To16(p.plane) << 16) | (parseByteProduct(p.mid, "supplementary_planes2_16.mid") << 8) | parseByteProduct(p.lo, "supplementary_planes2_16.lo");
178
+ }
179
+
180
+ throw new Error(`Unsupported unicode tag: ${v.tag}`);
181
+ }
182
+
183
+ function textToStringValue(text) {
184
+ let out = new Variant("nil", UNIT);
185
+ const chars = Array.from(text);
186
+ for (let i = chars.length - 1; i >= 0; i--) {
187
+ const cp = chars[i].codePointAt(0);
188
+ out = new Variant("cons", new Product({
189
+ car: codePointToUnicodeValue(cp),
190
+ cdr: out
191
+ }));
192
+ }
193
+ return out;
194
+ }
195
+
196
+ function stringValueToText(value) {
197
+ let out = "";
198
+ let cursor = value;
199
+
200
+ while (true) {
201
+ const list = requireVariant(cursor, "string");
202
+ if (list.tag === "nil") {
203
+ requireProduct(list.value, "string.nil");
204
+ return out;
205
+ }
206
+ if (list.tag !== "cons") {
207
+ throw new Error(`Unsupported string list tag: ${list.tag}`);
208
+ }
209
+ const pair = requireProduct(list.value, "string.cons");
210
+ out += String.fromCodePoint(unicodeValueToCodePoint(pair.car));
211
+ cursor = pair.cdr;
212
+ }
213
+ }
214
+
215
+ export {
216
+ codePointToUnicodeValue,
217
+ unicodeValueToCodePoint,
218
+ textToStringValue,
219
+ stringValueToText
220
+ };
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env node
2
+
3
+ import fs from "node:fs";
4
+ import { argv, stdin, stdout, stderr } from "node:process";
5
+ import { decodeWire } from "./runtime/prefix-codec.mjs";
6
+ import { propertyListToFilter, valueToK } from "./runtime/show-value.mjs";
7
+
8
+ function readAll(stream) {
9
+ return new Promise((resolve, reject) => {
10
+ const chunks = [];
11
+ stream.on("data", (chunk) => chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)));
12
+ stream.on("end", () => resolve(Buffer.concat(chunks)));
13
+ stream.on("error", reject);
14
+ });
15
+ }
16
+
17
+ async function main() {
18
+ const args = argv.slice(2);
19
+ let fileArg = null;
20
+ for (const arg of args) {
21
+ if (fileArg == null) fileArg = arg;
22
+ else { console.error("Usage: show.mjs [wire-file]"); process.exit(1); }
23
+ }
24
+
25
+ const buffer = fileArg ? fs.readFileSync(fileArg) : await readAll(stdin);
26
+
27
+ // forward unchanged to stdout
28
+ stdout.write(buffer);
29
+
30
+ // decode and print: value filter
31
+ const { pattern, value } = decodeWire(buffer);
32
+
33
+ const valueStr = valueToK(value);
34
+ const filterStr = propertyListToFilter(pattern);
35
+ stderr.write(`${valueStr} ?${filterStr}\n`);
36
+ }
37
+
38
+ main().catch((error) => {
39
+ console.error(error.stack || error.message || String(error));
40
+ process.exit(1);
41
+ });
@@ -0,0 +1,80 @@
1
+ import { NODE_KIND } from "./runtime/codec.mjs";
2
+ import { decodeWire, encodeToWire } from "./runtime/prefix-codec.mjs";
3
+ import { patternToPropertyList } from "./runtime/pattern-json.mjs";
4
+ import { textToStringValue, stringValueToText } from "./runtime/unicode-string.mjs";
5
+
6
+ const STRING_PATTERN = {
7
+ dictionary: [
8
+ "0", "1", "10",
9
+ "ascii", "bmp_common", "bmp_private_use",
10
+ "car", "cdr", "cons",
11
+ "h08_0F", "h10_7F", "h80_CF", "hD0_D7", "hE0_EF", "hF0_F7", "hF8", "hF9", "hFA_FB", "hFC_FD", "hFE_FF",
12
+ "hi",
13
+ "lo",
14
+ "mid",
15
+ "nil",
16
+ "p02_03", "p04_07", "p08_0F", "p10",
17
+ "plane", "plane0",
18
+ "supplementary_plane1", "supplementary_planes2_16",
19
+ "2", "3", "4", "5", "6", "7", "8", "9"
20
+ ],
21
+ nodes: [
22
+ { kind: NODE_KIND.CLOSED_UNION, edges: [ { label: "nil", target: 1 }, { label: "cons", target: 2 } ] },
23
+ { kind: NODE_KIND.CLOSED_PRODUCT, edges: [] },
24
+ { kind: NODE_KIND.CLOSED_PRODUCT, edges: [ { label: "car", target: 3 }, { label: "cdr", target: 0 } ] },
25
+ { kind: NODE_KIND.CLOSED_UNION, edges: [
26
+ { label: "ascii", target: 4 },
27
+ { label: "plane0", target: 6 },
28
+ { label: "bmp_common", target: 7 },
29
+ { label: "bmp_private_use", target: 10 },
30
+ { label: "supplementary_plane1", target: 12 },
31
+ { label: "supplementary_planes2_16", target: 13 }
32
+ ] },
33
+ { kind: NODE_KIND.CLOSED_PRODUCT, edges: [
34
+ { label: "6", target: 5 }, { label: "5", target: 5 }, { label: "4", target: 5 },
35
+ { label: "3", target: 5 }, { label: "2", target: 5 }, { label: "1", target: 5 }, { label: "0", target: 5 }
36
+ ] },
37
+ { kind: NODE_KIND.CLOSED_UNION, edges: [ { label: "0", target: 1 }, { label: "1", target: 1 } ] },
38
+ { kind: NODE_KIND.CLOSED_PRODUCT, edges: [
39
+ { label: "10", target: 5 }, { label: "9", target: 5 }, { label: "8", target: 5 }, { label: "7", target: 5 },
40
+ { label: "6", target: 5 }, { label: "5", target: 5 }, { label: "4", target: 5 }, { label: "3", target: 5 },
41
+ { label: "2", target: 5 }, { label: "1", target: 5 }, { label: "0", target: 5 }
42
+ ] },
43
+ { kind: NODE_KIND.CLOSED_PRODUCT, edges: [ { label: "hi", target: 8 }, { label: "lo", target: 9 } ] },
44
+ { kind: NODE_KIND.CLOSED_UNION, edges: [
45
+ { label: "h08_0F", target: 15 }, { label: "h10_7F", target: 16 }, { label: "h80_CF", target: 16 },
46
+ { label: "hD0_D7", target: 15 }, { label: "hF9", target: 1 },
47
+ { label: "hFA_FB", target: 17 }, { label: "hFC_FD", target: 17 }, { label: "hFE_FF", target: 17 }
48
+ ] },
49
+ { kind: NODE_KIND.CLOSED_PRODUCT, edges: [
50
+ { label: "7", target: 5 }, { label: "6", target: 5 }, { label: "5", target: 5 }, { label: "4", target: 5 },
51
+ { label: "3", target: 5 }, { label: "2", target: 5 }, { label: "1", target: 5 }, { label: "0", target: 5 }
52
+ ] },
53
+ { kind: NODE_KIND.CLOSED_PRODUCT, edges: [ { label: "hi", target: 11 }, { label: "lo", target: 9 } ] },
54
+ { kind: NODE_KIND.CLOSED_UNION, edges: [ { label: "hE0_EF", target: 18 }, { label: "hF0_F7", target: 15 }, { label: "hF8", target: 1 } ] },
55
+ { kind: NODE_KIND.CLOSED_PRODUCT, edges: [ { label: "mid", target: 9 }, { label: "lo", target: 9 } ] },
56
+ { kind: NODE_KIND.CLOSED_PRODUCT, edges: [ { label: "plane", target: 14 }, { label: "mid", target: 9 }, { label: "lo", target: 9 } ] },
57
+ { kind: NODE_KIND.CLOSED_UNION, edges: [ { label: "p02_03", target: 17 }, { label: "p04_07", target: 19 }, { label: "p08_0F", target: 15 }, { label: "p10", target: 1 } ] },
58
+ { kind: NODE_KIND.CLOSED_PRODUCT, edges: [ { label: "2", target: 5 }, { label: "1", target: 5 }, { label: "0", target: 5 } ] },
59
+ { kind: NODE_KIND.CLOSED_PRODUCT, edges: [
60
+ { label: "6", target: 5 }, { label: "5", target: 5 }, { label: "4", target: 5 },
61
+ { label: "3", target: 5 }, { label: "2", target: 5 }, { label: "1", target: 5 }, { label: "0", target: 5 }
62
+ ] },
63
+ { kind: NODE_KIND.CLOSED_PRODUCT, edges: [ { label: "0", target: 5 } ] },
64
+ { kind: NODE_KIND.CLOSED_PRODUCT, edges: [ { label: "3", target: 5 }, { label: "2", target: 5 }, { label: "1", target: 5 }, { label: "0", target: 5 } ] },
65
+ { kind: NODE_KIND.CLOSED_PRODUCT, edges: [ { label: "1", target: 5 }, { label: "0", target: 5 } ] }
66
+ ]
67
+ };
68
+
69
+ const STRING_PATTERN_PROPERTY_LIST = patternToPropertyList(STRING_PATTERN);
70
+
71
+ function encodeText(text) {
72
+ return encodeToWire(textToStringValue(text), STRING_PATTERN_PROPERTY_LIST);
73
+ }
74
+
75
+ function decodeText(buffer) {
76
+ const { value } = decodeWire(buffer);
77
+ return stringValueToText(value);
78
+ }
79
+
80
+ export { STRING_PATTERN, STRING_PATTERN_PROPERTY_LIST, textToStringValue, stringValueToText, encodeText, decodeText };
@@ -0,0 +1,51 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { stdin, stdout, argv, exit } from "node:process";
4
+ import { Product } from "../Value.mjs";
5
+ import { decodeWire, encodeToWire } from "./runtime/prefix-codec.mjs";
6
+
7
+ const UNIT_PATTERN = [
8
+ ["closed-product", []]
9
+ ];
10
+
11
+ function readAll(stream) {
12
+ return new Promise((resolve, reject) => {
13
+ const chunks = [];
14
+ stream.on("data", (chunk) => chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)));
15
+ stream.on("end", () => resolve(Buffer.concat(chunks)));
16
+ stream.on("error", reject);
17
+ });
18
+ }
19
+
20
+ function unitEncoding() {
21
+ return encodeToWire(new Product({}), UNIT_PATTERN);
22
+ }
23
+
24
+ async function main() {
25
+ const args = argv.slice(2);
26
+ if (args.length !== 1 || (args[0] !== "--parse" && args[0] !== "--print")) {
27
+ console.error("Usage: unit.mjs --parse | --print");
28
+ console.error(" --parse ignore stdin and write the unit binary pattern+value stream");
29
+ console.error(" --print validate the unit binary pattern+value stream and write {}");
30
+ exit(1);
31
+ }
32
+
33
+ const expected = unitEncoding();
34
+
35
+ if (args[0] === "--parse") {
36
+ stdout.write(expected);
37
+ return;
38
+ }
39
+
40
+ const input = await readAll(stdin);
41
+ const { value } = decodeWire(input);
42
+ if (!(value instanceof Product) || Object.keys(value.product).length !== 0) {
43
+ throw new Error("Input is not a unit value");
44
+ }
45
+ stdout.write("{}");
46
+ }
47
+
48
+ main().catch((error) => {
49
+ console.error(error.message || String(error));
50
+ exit(1);
51
+ });
@@ -0,0 +1,66 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { stdin, stdout, argv, exit } from "node:process";
4
+ import { encodeText, decodeText } from "./string-codec.mjs";
5
+
6
+ function readAll(stream) {
7
+ return new Promise((resolve, reject) => {
8
+ const chunks = [];
9
+ stream.on("data", c => chunks.push(Buffer.isBuffer(c) ? c : Buffer.from(c)));
10
+ stream.on("end", () => resolve(Buffer.concat(chunks)));
11
+ stream.on("error", reject);
12
+ });
13
+ }
14
+
15
+ function decodeUtf16Input(buf) {
16
+ if (buf.length === 0) return "";
17
+
18
+ if (buf.length >= 2 && buf[0] === 0xfe && buf[1] === 0xff) {
19
+ const src = buf.subarray(2);
20
+ if (src.length % 2 !== 0) throw new Error("UTF-16BE payload has odd byte length");
21
+ const le = Buffer.alloc(src.length);
22
+ for (let i = 0; i < src.length; i += 2) {
23
+ le[i] = src[i + 1];
24
+ le[i + 1] = src[i];
25
+ }
26
+ return le.toString("utf16le");
27
+ }
28
+
29
+ if (buf.length >= 2 && buf[0] === 0xff && buf[1] === 0xfe) {
30
+ const src = buf.subarray(2);
31
+ if (src.length % 2 !== 0) throw new Error("UTF-16LE payload has odd byte length");
32
+ return src.toString("utf16le");
33
+ }
34
+
35
+ if (buf.length % 2 !== 0) throw new Error("UTF-16 input without BOM has odd byte length");
36
+ return buf.toString("utf16le");
37
+ }
38
+
39
+ function encodeUtf16Output(text) {
40
+ const bomLe = Buffer.from([0xff, 0xfe]);
41
+ return Buffer.concat([bomLe, Buffer.from(text, "utf16le")]);
42
+ }
43
+
44
+ async function main() {
45
+ const args = argv.slice(2);
46
+ if (args.length !== 1 || (args[0] !== "--parse" && args[0] !== "--print")) {
47
+ console.error("Usage: utf16.mjs --parse | --print");
48
+ console.error(" --parse read UTF-16 text from stdin (BOM-aware), write binary pattern+value stream of k string");
49
+ console.error(" --print read binary pattern+value stream of k string, write UTF-16LE text with BOM");
50
+ exit(1);
51
+ }
52
+
53
+ const buf = await readAll(stdin);
54
+
55
+ if (args[0] === "--parse") {
56
+ const text = decodeUtf16Input(buf);
57
+ stdout.write(encodeText(text));
58
+ } else {
59
+ stdout.write(encodeUtf16Output(decodeText(buf)));
60
+ }
61
+ }
62
+
63
+ main().catch(err => {
64
+ console.error(err.message || String(err));
65
+ exit(1);
66
+ });
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { stdin, stdout, argv, exit } from "node:process";
4
+ import { encodeText, decodeText } from "./string-codec.mjs";
5
+
6
+ function readAll(stream) {
7
+ return new Promise((resolve, reject) => {
8
+ const chunks = [];
9
+ stream.on("data", c => chunks.push(Buffer.isBuffer(c) ? c : Buffer.from(c)));
10
+ stream.on("end", () => resolve(Buffer.concat(chunks)));
11
+ stream.on("error", reject);
12
+ });
13
+ }
14
+
15
+ async function main() {
16
+ const args = argv.slice(2);
17
+ if (args.length !== 1 || (args[0] !== "--parse" && args[0] !== "--print")) {
18
+ console.error("Usage: utf8.mjs --parse | --print");
19
+ console.error(" --parse read UTF-8 text from stdin, write binary pattern+value stream of k string");
20
+ console.error(" --print read binary pattern+value stream of k string, write UTF-8 text");
21
+ exit(1);
22
+ }
23
+
24
+ const buf = await readAll(stdin);
25
+
26
+ if (args[0] === "--parse") {
27
+ const text = buf.toString("utf8");
28
+ stdout.write(encodeText(text));
29
+ } else {
30
+ stdout.write(decodeText(buf));
31
+ }
32
+ }
33
+
34
+ main().catch(err => {
35
+ console.error(err.message || String(err));
36
+ exit(1);
37
+ });