@fraczak/k 8.9.0 → 9.0.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 +17 -0
- package/DOCS/ARCHITECTURE.md +5 -4
- package/DOCS/DICTIONARY.md +2 -2
- package/DOCS/FILE_FORMATS.md +11 -9
- package/DOCS/KIR_V1.md +5 -1
- package/DOCS/KTVM_SPEC.md +902 -0
- package/DOCS/KVM_EXECUTION_MODEL.md +3 -2
- package/DOCS/KVM_VS_KTVM_COMPARATIVE_STUDY.md +127 -0
- package/DOCS/PROFILING_AND_TRACING.md +6 -4
- package/DOCS/README.md +3 -2
- package/DOCS/REPL.md +16 -12
- package/README.md +132 -2
- package/backend-api.mjs +31 -16
- package/backends/llvm/README.md +4 -5
- package/backends/llvm/bin/k-llvm-build.mjs +7 -19
- package/backends/llvm/bin/k-llvm-compile.mjs +6 -18
- package/backends/llvm/bin/k-llvm.mjs +11 -33
- package/backends/llvm/src/cli.mjs +2 -12
- package/backends/llvm/src/executable.mjs +7 -95
- package/backends/llvm/src/kvm2llvm.mjs +1 -5
- package/backends/llvm/src/llvm.mjs +4 -111
- package/backends/llvm/tests/k-llvm-run.mjs +1 -6
- package/backends/llvm/tests/perf-support.mjs +3 -5
- package/backends/llvm/tests/test-llvm.mjs +0 -16
- package/backends/wasm/README.md +2 -3
- package/backends/wasm/bin/k-wasm-compile.mjs +4 -7
- package/backends/wasm/bin/k-wasm.mjs +4 -6
- package/backends/wasm/src/cli.mjs +8 -24
- package/backends/wasm/src/wasm.mjs +10 -56
- package/browser/repl-browser.mjs +9 -8
- package/browser/shims/buffer.mjs +31 -2
- package/browser/shims/child_process.mjs +26 -0
- package/browser/shims/fs.mjs +3 -1
- package/conformance/run.mjs +17 -2
- package/harness/adapters/ktvm-llvm.mjs +118 -0
- package/harness/adapters/ktvm-wasm.mjs +104 -0
- package/harness/adapters/ktvm.mjs +133 -0
- package/harness/registry.mjs +9 -0
- package/ktvm/ast.mjs +218 -0
- package/ktvm/bit-deque.mjs +482 -0
- package/ktvm/fixtures/integer-streaming.k +477 -0
- package/ktvm/index.mjs +119 -0
- package/ktvm/llvm/index.mjs +202 -0
- package/ktvm/llvm/lower-c.mjs +1258 -0
- package/ktvm/llvm/runtime.h +3542 -0
- package/ktvm/llvm/test-llvm.mjs +476 -0
- package/ktvm/lower-kir.mjs +2221 -0
- package/ktvm/optimize/fuse-kernel.mjs +2111 -0
- package/ktvm/optimize/index.mjs +116 -0
- package/ktvm/optimize/mwtr.mjs +283 -0
- package/ktvm/optimize/simplify-procs.mjs +5250 -0
- package/ktvm/optimize/synthesize-tables.mjs +1105 -0
- package/ktvm/parser.mjs +1101 -0
- package/ktvm/printer.mjs +281 -0
- package/ktvm/schema.mjs +543 -0
- package/ktvm/specialize.mjs +538 -0
- package/ktvm/test-conformance.mjs +597 -0
- package/ktvm/test-ktvm.mjs +1376 -0
- package/ktvm/test-optimize.mjs +598 -0
- package/ktvm/validator.mjs +1280 -0
- package/ktvm/vm.mjs +2225 -0
- package/ktvm/wasm/index.mjs +331 -0
- package/ktvm/wasm/lower-wasm.mjs +1272 -0
- package/ktvm/wasm/runtime.wat +4413 -0
- package/ktvm/wasm/test-wasm.mjs +426 -0
- package/ktvm/wire.mjs +489 -0
- package/package.json +1 -9
- package/repl.mjs +101 -20
- package/scripts/build-repl-html.mjs +40 -18
- package/scripts/perf-bigint-compare.mjs +12 -16
- package/scripts/perf-ieee.mjs +9 -229
- package/scripts/perf-int.mjs +14 -272
- package/scripts/perf-multiplication.mjs +8 -54
- package/scripts/perf-poly.mjs +9 -263
- package/scripts/run-tests.mjs +5 -2
- package/scripts/test-package.mjs +15 -3
- package/tests/README.md +3 -3
- package/tests/test-repl-html.mjs +72 -15
- package/tests/test-repl.mjs +24 -17
- package/DOCS/POLYMORPHIC_IR.md +0 -86
- package/access-table.mjs +0 -343
- package/pkir.mjs +0 -772
- package/tests/test-access-table.mjs +0 -213
- package/tests/test-pkir.mjs +0 -530
package/ktvm/wire.mjs
ADDED
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Strictly iterative k-wire boundary codecs and resumable 1-state DPDA steppers.
|
|
3
|
+
*
|
|
4
|
+
* Guarantees zero host call-stack recursion (handles arbitrarily deep 4,096+ bit
|
|
5
|
+
* values cleanly) and 100% bit-exact conformance with codecs/WIRE_FORMAT.md and
|
|
6
|
+
* codecs/runtime/prefix-codec.mjs.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { Value, isProduct, isVariant } from "../Value.mjs";
|
|
10
|
+
import {
|
|
11
|
+
encodeToWire,
|
|
12
|
+
decodeWire,
|
|
13
|
+
choiceWidth,
|
|
14
|
+
propertyListToPattern,
|
|
15
|
+
patternToPropertyList,
|
|
16
|
+
CORE_PATTERN_PROPERTY_LIST
|
|
17
|
+
} from "../codecs/runtime/prefix-codec.mjs";
|
|
18
|
+
import {
|
|
19
|
+
textToStringValue,
|
|
20
|
+
stringValueToText
|
|
21
|
+
} from "../codecs/runtime/unicode-string.mjs";
|
|
22
|
+
import { BitDeque } from "./bit-deque.mjs";
|
|
23
|
+
import { compareLabels } from "./schema.mjs";
|
|
24
|
+
|
|
25
|
+
export const DPDA_RESULT = Object.freeze({
|
|
26
|
+
DONE: "DONE",
|
|
27
|
+
NEED_BITS: "NEED_BITS",
|
|
28
|
+
FAILED: "FAILED"
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Strictly iterative Value -> BitDeque encoder under closed property-list `plist`.
|
|
33
|
+
*/
|
|
34
|
+
export function encodePayloadToDeque(value, plist, deque, rootId = 0) {
|
|
35
|
+
const stack = [{ val: value, nodeId: rootId }];
|
|
36
|
+
while (stack.length > 0) {
|
|
37
|
+
const { val, nodeId } = stack.pop();
|
|
38
|
+
const [kind, edges] = plist[nodeId];
|
|
39
|
+
if (kind === "closed-product") {
|
|
40
|
+
if (!isProduct(val)) {
|
|
41
|
+
throw new Error(`Expected product value at schema node ${nodeId}`);
|
|
42
|
+
}
|
|
43
|
+
// Push children in reverse canonical order so LIFO emits them in canonical order.
|
|
44
|
+
for (let i = edges.length - 1; i >= 0; i--) {
|
|
45
|
+
const [label, targetId] = edges[i];
|
|
46
|
+
if (!(label in val.product)) {
|
|
47
|
+
throw new Error(`Product value missing field '${label}' at schema node ${nodeId}`);
|
|
48
|
+
}
|
|
49
|
+
stack.push({ val: val.product[label], nodeId: targetId });
|
|
50
|
+
}
|
|
51
|
+
} else if (kind === "closed-union") {
|
|
52
|
+
if (!isVariant(val)) {
|
|
53
|
+
throw new Error(`Expected variant value at schema node ${nodeId}`);
|
|
54
|
+
}
|
|
55
|
+
const ord = edges.findIndex(([l]) => l === val.tag);
|
|
56
|
+
if (ord < 0) {
|
|
57
|
+
throw new Error(`Variant tag '${val.tag}' not in closed union at node ${nodeId}`);
|
|
58
|
+
}
|
|
59
|
+
const w = choiceWidth(edges.length);
|
|
60
|
+
if (w > 0) deque.pushBits(ord, w);
|
|
61
|
+
stack.push({ val: val.value, nodeId: edges[ord][1] });
|
|
62
|
+
} else {
|
|
63
|
+
throw new Error(`Cannot encode payload under non-closed schema kind '${kind}'`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Strictly iterative BitDeque -> Value decoder under closed property-list `plist`.
|
|
70
|
+
* Rejects truncation, invalid union ordinals, and leftover unconsumed bits.
|
|
71
|
+
*/
|
|
72
|
+
export function decodeDequeToValue(deque, plist, rootId = 0, { exact = true } = {}) {
|
|
73
|
+
const results = [];
|
|
74
|
+
// Frames: { kind: "decode", nodeId } | { kind: "build_product", labels } | { kind: "build_variant", tag }
|
|
75
|
+
const frames = [{ kind: "decode", nodeId: rootId }];
|
|
76
|
+
|
|
77
|
+
while (frames.length > 0) {
|
|
78
|
+
const frame = frames.pop();
|
|
79
|
+
if (frame.kind === "build_product") {
|
|
80
|
+
const n = frame.labels.length;
|
|
81
|
+
const vals = new Array(n);
|
|
82
|
+
for (let i = n - 1; i >= 0; i--) {
|
|
83
|
+
vals[i] = results.pop();
|
|
84
|
+
}
|
|
85
|
+
const fields = {};
|
|
86
|
+
for (let i = 0; i < n; i++) {
|
|
87
|
+
fields[frame.labels[i]] = vals[i];
|
|
88
|
+
}
|
|
89
|
+
results.push(Value.product(fields));
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (frame.kind === "build_variant") {
|
|
93
|
+
const payload = results.pop();
|
|
94
|
+
results.push(Value.variant(frame.tag, payload));
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const [kind, edges] = plist[frame.nodeId];
|
|
99
|
+
if (kind === "closed-product") {
|
|
100
|
+
if (edges.length === 0) {
|
|
101
|
+
results.push(Value.product({}));
|
|
102
|
+
} else {
|
|
103
|
+
frames.push({ kind: "build_product", labels: edges.map(([l]) => l) });
|
|
104
|
+
for (let i = edges.length - 1; i >= 0; i--) {
|
|
105
|
+
frames.push({ kind: "decode", nodeId: edges[i][1] });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
} else if (kind === "closed-union") {
|
|
109
|
+
if (edges.length === 0) {
|
|
110
|
+
throw new Error("Cannot decode empty union");
|
|
111
|
+
}
|
|
112
|
+
const w = choiceWidth(edges.length);
|
|
113
|
+
let ord = 0;
|
|
114
|
+
if (w > 0) {
|
|
115
|
+
if (!deque.hasBits(w)) {
|
|
116
|
+
throw new Error(`Truncated k-wire stream: needed ${w} bits at union node ${frame.nodeId}`);
|
|
117
|
+
}
|
|
118
|
+
ord = deque.shiftBits(w);
|
|
119
|
+
}
|
|
120
|
+
if (ord >= edges.length) {
|
|
121
|
+
throw new Error(`Invalid union ordinal ${ord} (expected < ${edges.length}) at node ${frame.nodeId}`);
|
|
122
|
+
}
|
|
123
|
+
const [tag, targetId] = edges[ord];
|
|
124
|
+
frames.push({ kind: "build_variant", tag });
|
|
125
|
+
frames.push({ kind: "decode", nodeId: targetId });
|
|
126
|
+
} else {
|
|
127
|
+
throw new Error(`Cannot decode payload under non-closed schema kind '${kind}'`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (exact && deque.length !== 0) {
|
|
132
|
+
throw new Error(`Trailing unconsumed bits in BitDeque (${deque.length} bits remaining)`);
|
|
133
|
+
}
|
|
134
|
+
return results[0];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Push a byte buffer into a BitDeque (MSB-first per byte), stripping trailing
|
|
139
|
+
* zero-padding beyond `totalBits` if specified.
|
|
140
|
+
*/
|
|
141
|
+
export function writeBytesToDeque(bytes, deque, totalBits = bytes.length * 8) {
|
|
142
|
+
let rem = totalBits;
|
|
143
|
+
for (let i = 0; i < bytes.length && rem > 0; i++) {
|
|
144
|
+
if (rem >= 8) {
|
|
145
|
+
deque.pushBits(bytes[i], 8);
|
|
146
|
+
rem -= 8;
|
|
147
|
+
} else {
|
|
148
|
+
deque.pushBits(bytes[i] >>> (8 - rem), rem);
|
|
149
|
+
rem = 0;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Drain a BitDeque into a zero-padded Uint8Array (exact k-wire byte framing).
|
|
156
|
+
*/
|
|
157
|
+
export function dequeToPaddedBytes(deque) {
|
|
158
|
+
const totalBits = deque.length;
|
|
159
|
+
const byteLen = Math.ceil(totalBits / 8);
|
|
160
|
+
const out = Buffer.alloc(byteLen);
|
|
161
|
+
let byteIdx = 0;
|
|
162
|
+
let rem = totalBits;
|
|
163
|
+
while (rem >= 8) {
|
|
164
|
+
out[byteIdx++] = deque.shiftBits(8);
|
|
165
|
+
rem -= 8;
|
|
166
|
+
}
|
|
167
|
+
if (rem > 0) {
|
|
168
|
+
out[byteIdx++] = (deque.shiftBits(rem) << (8 - rem)) & 0xff;
|
|
169
|
+
}
|
|
170
|
+
return out;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Decode ONLY the k-wire envelope header prefix (`encode(pattern_value)`) without
|
|
175
|
+
* running a JS DPDA over the payload bits. Returns `{ plist, headerBitLen, canonicalOrder }`
|
|
176
|
+
* so Wasm/native runners can validate and stream the remaining payload bits directly.
|
|
177
|
+
*/
|
|
178
|
+
export function splitWireHeaderOnly(buffer, { quota = null } = {}) {
|
|
179
|
+
const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
|
|
180
|
+
const full = new BitDeque(-1, { quota });
|
|
181
|
+
const totalBits = bytes.length * 8;
|
|
182
|
+
writeBytesToDeque(bytes, full, totalBits);
|
|
183
|
+
|
|
184
|
+
const patternValue = decodeDequeToValue(full, CORE_PATTERN_PROPERTY_LIST, 0, { exact: false });
|
|
185
|
+
const headerBitLen = totalBits - full.length;
|
|
186
|
+
full.release();
|
|
187
|
+
|
|
188
|
+
const rawPlist = patternValueToPropertyList(patternValue);
|
|
189
|
+
let canonicalOrder = true;
|
|
190
|
+
const plist = rawPlist.map(([kind, edges]) => {
|
|
191
|
+
for (let i = 1; i < edges.length; i++) {
|
|
192
|
+
if (compareLabels(edges[i - 1][0], edges[i][0]) >= 0) {
|
|
193
|
+
canonicalOrder = false;
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (kind === "open-union") return ["closed-union", edges];
|
|
198
|
+
if (kind === "open-product") return ["closed-product", edges];
|
|
199
|
+
return [kind, edges];
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
return { plist, headerBitLen, canonicalOrder };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Decode an enveloped k-wire byte buffer (`encode(pattern_value) encode(payload)`)
|
|
207
|
+
* strictly iteratively into `{ plist, payloadDeque }`.
|
|
208
|
+
*
|
|
209
|
+
* Rejects malformed headers, truncated streams, invalid ordinals, and nonzero
|
|
210
|
+
* trailing padding bits (matching WIRE_FORMAT.md).
|
|
211
|
+
*/
|
|
212
|
+
export function splitEnvelopedWire(buffer, { quota = null } = {}) {
|
|
213
|
+
const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
|
|
214
|
+
const full = new BitDeque(-1, { quota });
|
|
215
|
+
writeBytesToDeque(bytes, full, bytes.length * 8);
|
|
216
|
+
|
|
217
|
+
// Decode the pattern header using K's CORE_PATTERN_PROPERTY_LIST
|
|
218
|
+
const patternValue = decodeDequeToValue(full, CORE_PATTERN_PROPERTY_LIST, 0, { exact: false });
|
|
219
|
+
const rawPlist = patternValueToPropertyList(patternValue);
|
|
220
|
+
// Concrete k-wire headers may tag value-derived variants/products as `open-union` /
|
|
221
|
+
// `open-product` (see prefix-codec.mjs `deriveClosedPatternFromValue`), whose wire
|
|
222
|
+
// encoding is identical to `closed-union` / `closed-product` over their listed edges.
|
|
223
|
+
const plist = rawPlist.map(([kind, edges]) => {
|
|
224
|
+
if (kind === "open-union") return ["closed-union", edges];
|
|
225
|
+
if (kind === "open-product") return ["closed-product", edges];
|
|
226
|
+
return [kind, edges];
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
// Validate and extract exact payload bits by running DPDA copy into payloadDeque,
|
|
230
|
+
// then verify all remaining bits (< 8) are zero trailing byte padding.
|
|
231
|
+
const payloadDeque = new BitDeque(-2, { prodSchema: plist, consSchema: plist, quota });
|
|
232
|
+
const copyState = createDpdaCopy(plist, 0);
|
|
233
|
+
const res = stepDpdaCopy(full, payloadDeque, copyState);
|
|
234
|
+
if (res !== DPDA_RESULT.DONE) {
|
|
235
|
+
full.release();
|
|
236
|
+
payloadDeque.release();
|
|
237
|
+
throw new Error("Malformed k-wire envelope payload (truncated or invalid union ordinal)");
|
|
238
|
+
}
|
|
239
|
+
if (full.length >= 8) {
|
|
240
|
+
const extra = full.length;
|
|
241
|
+
full.release();
|
|
242
|
+
payloadDeque.release();
|
|
243
|
+
throw new Error(`Malformed k-wire envelope: ${extra} trailing bits after payload`);
|
|
244
|
+
}
|
|
245
|
+
if (full.length > 0) {
|
|
246
|
+
const pad = full.shiftBits(full.length);
|
|
247
|
+
if (pad !== 0) {
|
|
248
|
+
full.release();
|
|
249
|
+
payloadDeque.release();
|
|
250
|
+
throw new Error(`Malformed k-wire envelope: nonzero trailing padding bits (0b${pad.toString(2)})`);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
full.release();
|
|
254
|
+
payloadDeque.close();
|
|
255
|
+
return { plist, payloadDeque };
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Frame a closed `plist` + payload `BitDeque` into a complete enveloped k-wire
|
|
260
|
+
* `Uint8Array`, 100% bit-exact with `encodeToWire` in `codecs/runtime/prefix-codec.mjs`.
|
|
261
|
+
*/
|
|
262
|
+
export function frameEnvelopedWire(plist, payloadDeque) {
|
|
263
|
+
const headerVal = propertyListToPatternValue(plist);
|
|
264
|
+
const out = new BitDeque(-3);
|
|
265
|
+
encodePayloadToDeque(headerVal, CORE_PATTERN_PROPERTY_LIST, out, 0);
|
|
266
|
+
out.pushFromDeque(payloadDeque);
|
|
267
|
+
const bytes = dequeToPaddedBytes(out);
|
|
268
|
+
out.release();
|
|
269
|
+
return bytes;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Convenience bridge: encode a K `Value` under closed `plist` to enveloped k-wire bytes
|
|
274
|
+
* using K's canonical `encodeToWire`.
|
|
275
|
+
*/
|
|
276
|
+
export function encodeValueToEnvelopedWire(value, plist) {
|
|
277
|
+
return encodeToWire(value, plist);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export function decodeEnvelopedWireToValue(bytes) {
|
|
281
|
+
return decodeWire(Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes));
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// --- Pattern Value <-> Property List helpers (using K's canonical pattern codec) ---
|
|
285
|
+
|
|
286
|
+
function patternValueToPropertyList(patternValue) {
|
|
287
|
+
// Reuse K's canonical decoder by encoding only header? Better: convert directly!
|
|
288
|
+
// Pattern value matches CORE_PATTERN_PROPERTY_LIST (list of node variants).
|
|
289
|
+
const nodes = [];
|
|
290
|
+
let cur = patternValue;
|
|
291
|
+
while (isVariant(cur) && cur.tag === "cons") {
|
|
292
|
+
const nodeVal = cur.value.product.car;
|
|
293
|
+
const kind = nodeVal.tag;
|
|
294
|
+
const edges = [];
|
|
295
|
+
if (kind !== "any") {
|
|
296
|
+
let ecur = nodeVal.value;
|
|
297
|
+
while (isVariant(ecur) && ecur.tag === "cons") {
|
|
298
|
+
const edgeProd = ecur.value.product.car.product;
|
|
299
|
+
const label = decodeKStringValue(edgeProd.label);
|
|
300
|
+
const target = decodeKBitsUint(edgeProd.target);
|
|
301
|
+
edges.push([label, target]);
|
|
302
|
+
ecur = ecur.value.product.cdr;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
nodes.push([kind, edges]);
|
|
306
|
+
cur = cur.value.product.cdr;
|
|
307
|
+
}
|
|
308
|
+
return nodes;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function propertyListToPatternValue(plist) {
|
|
312
|
+
// Use K's own `encodeToWire` header or direct conversion via `patternToPropertyList`.
|
|
313
|
+
// Build canonical pattern[] Value directly matching CORE_PATTERN_PROPERTY_LIST!
|
|
314
|
+
let listVal = Value.variant("nil", Value.product({}));
|
|
315
|
+
for (let i = plist.length - 1; i >= 0; i--) {
|
|
316
|
+
const [kind, edges] = plist[i];
|
|
317
|
+
let nodeVal;
|
|
318
|
+
if (kind === "any") {
|
|
319
|
+
nodeVal = Value.variant("any", Value.product({}));
|
|
320
|
+
} else {
|
|
321
|
+
let edgeList = Value.variant("nil", Value.product({}));
|
|
322
|
+
for (let j = edges.length - 1; j >= 0; j--) {
|
|
323
|
+
const [label, target] = edges[j];
|
|
324
|
+
const edgeProd = Value.product({
|
|
325
|
+
label: encodeKStringValue(label),
|
|
326
|
+
target: encodeKBitsUint(target)
|
|
327
|
+
});
|
|
328
|
+
edgeList = Value.variant("cons", Value.product({ car: edgeProd, cdr: edgeList }));
|
|
329
|
+
}
|
|
330
|
+
nodeVal = Value.variant(kind, edgeList);
|
|
331
|
+
}
|
|
332
|
+
listVal = Value.variant("cons", Value.product({ car: nodeVal, cdr: listVal }));
|
|
333
|
+
}
|
|
334
|
+
return listVal;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function decodeKBitsUint(bitsVal) {
|
|
338
|
+
// `bits[]` target indices in WIRE_FORMAT.md / prefix-codec.mjs are LSB-first (`01_` = 2)
|
|
339
|
+
let acc = 0;
|
|
340
|
+
let place = 1;
|
|
341
|
+
let cur = bitsVal;
|
|
342
|
+
while (isVariant(cur) && cur.tag !== "_") {
|
|
343
|
+
if (cur.tag === "1") acc += place;
|
|
344
|
+
place *= 2;
|
|
345
|
+
cur = cur.value;
|
|
346
|
+
}
|
|
347
|
+
return acc;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function encodeKBitsUint(n) {
|
|
351
|
+
const unit = Value.product({});
|
|
352
|
+
if (n === 0) return Value.variant("_", unit);
|
|
353
|
+
const bits = [];
|
|
354
|
+
let v = n;
|
|
355
|
+
while (v > 0) {
|
|
356
|
+
bits.push(v & 1 ? "1" : "0");
|
|
357
|
+
v = Math.floor(v / 2);
|
|
358
|
+
}
|
|
359
|
+
let cur = Value.variant("_", unit);
|
|
360
|
+
for (let i = bits.length - 1; i >= 0; i--) {
|
|
361
|
+
cur = Value.variant(bits[i], cur);
|
|
362
|
+
}
|
|
363
|
+
return cur;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function decodeKStringValue(strVal) {
|
|
367
|
+
return stringValueToText(strVal);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function encodeKStringValue(str) {
|
|
371
|
+
return textToStringValue(str);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// --- Strictly Iterative, Resumable 1-State Pushdown Steppers ---
|
|
375
|
+
|
|
376
|
+
export function createDpdaSkip(plist, rootId = 0) {
|
|
377
|
+
return { plist, stack: [rootId] };
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
export function stepDpdaSkip(srcDeque, state) {
|
|
381
|
+
const { plist, stack } = state;
|
|
382
|
+
while (stack.length > 0) {
|
|
383
|
+
const nodeId = stack[stack.length - 1];
|
|
384
|
+
const [kind, edges] = plist[nodeId];
|
|
385
|
+
if (kind === "closed-product") {
|
|
386
|
+
stack.pop();
|
|
387
|
+
for (let i = edges.length - 1; i >= 0; i--) {
|
|
388
|
+
stack.push(edges[i][1]);
|
|
389
|
+
}
|
|
390
|
+
} else if (kind === "closed-union") {
|
|
391
|
+
if (edges.length === 0) return DPDA_RESULT.FAILED;
|
|
392
|
+
const w = choiceWidth(edges.length);
|
|
393
|
+
let ord = 0;
|
|
394
|
+
if (w > 0) {
|
|
395
|
+
if (!srcDeque.hasBits(w)) {
|
|
396
|
+
return srcDeque.isOpen ? DPDA_RESULT.NEED_BITS : DPDA_RESULT.FAILED;
|
|
397
|
+
}
|
|
398
|
+
ord = srcDeque.shiftBits(w);
|
|
399
|
+
srcDeque.aligned = false;
|
|
400
|
+
}
|
|
401
|
+
if (ord >= edges.length) return DPDA_RESULT.FAILED;
|
|
402
|
+
stack.pop();
|
|
403
|
+
stack.push(edges[ord][1]);
|
|
404
|
+
} else {
|
|
405
|
+
return DPDA_RESULT.FAILED;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
return DPDA_RESULT.DONE;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
export function createDpdaCopy(plist, rootId = 0) {
|
|
412
|
+
return { plist, stack: [rootId] };
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
export function stepDpdaCopy(srcDeque, dstDeque, state) {
|
|
416
|
+
const { plist, stack } = state;
|
|
417
|
+
while (stack.length > 0) {
|
|
418
|
+
const nodeId = stack[stack.length - 1];
|
|
419
|
+
const [kind, edges] = plist[nodeId];
|
|
420
|
+
if (kind === "closed-product") {
|
|
421
|
+
stack.pop();
|
|
422
|
+
for (let i = edges.length - 1; i >= 0; i--) {
|
|
423
|
+
stack.push(edges[i][1]);
|
|
424
|
+
}
|
|
425
|
+
} else if (kind === "closed-union") {
|
|
426
|
+
if (edges.length === 0) return DPDA_RESULT.FAILED;
|
|
427
|
+
const w = choiceWidth(edges.length);
|
|
428
|
+
let ord = 0;
|
|
429
|
+
if (w > 0) {
|
|
430
|
+
if (!srcDeque.hasBits(w)) {
|
|
431
|
+
return srcDeque.isOpen ? DPDA_RESULT.NEED_BITS : DPDA_RESULT.FAILED;
|
|
432
|
+
}
|
|
433
|
+
ord = srcDeque.shiftBits(w);
|
|
434
|
+
srcDeque.aligned = false;
|
|
435
|
+
}
|
|
436
|
+
if (ord >= edges.length) return DPDA_RESULT.FAILED;
|
|
437
|
+
if (w > 0) dstDeque.pushBits(ord, w);
|
|
438
|
+
stack.pop();
|
|
439
|
+
stack.push(edges[ord][1]);
|
|
440
|
+
} else {
|
|
441
|
+
return DPDA_RESULT.FAILED;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return DPDA_RESULT.DONE;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
export function createDpdaCoerce(srcPlist, srcRoot, dstPlist, dstRoot) {
|
|
448
|
+
return { srcPlist, dstPlist, stack: [{ s: srcRoot, d: dstRoot }] };
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
export function stepDpdaCoerce(srcDeque, dstDeque, state) {
|
|
452
|
+
const { srcPlist, dstPlist, stack } = state;
|
|
453
|
+
while (stack.length > 0) {
|
|
454
|
+
const { s, d } = stack[stack.length - 1];
|
|
455
|
+
const [sKind, sEdges] = srcPlist[s];
|
|
456
|
+
const [dKind, dEdges] = dstPlist[d];
|
|
457
|
+
|
|
458
|
+
if (sKind === "closed-product" && dKind === "closed-product") {
|
|
459
|
+
if (sEdges.length !== dEdges.length) return DPDA_RESULT.FAILED;
|
|
460
|
+
stack.pop();
|
|
461
|
+
for (let i = sEdges.length - 1; i >= 0; i--) {
|
|
462
|
+
if (sEdges[i][0] !== dEdges[i][0]) return DPDA_RESULT.FAILED;
|
|
463
|
+
stack.push({ s: sEdges[i][1], d: dEdges[i][1] });
|
|
464
|
+
}
|
|
465
|
+
} else if (sKind === "closed-union" && dKind === "closed-union") {
|
|
466
|
+
if (sEdges.length === 0 || dEdges.length === 0) return DPDA_RESULT.FAILED;
|
|
467
|
+
const sw = choiceWidth(sEdges.length);
|
|
468
|
+
let sOrd = 0;
|
|
469
|
+
if (sw > 0) {
|
|
470
|
+
if (!srcDeque.hasBits(sw)) {
|
|
471
|
+
return srcDeque.isOpen ? DPDA_RESULT.NEED_BITS : DPDA_RESULT.FAILED;
|
|
472
|
+
}
|
|
473
|
+
sOrd = srcDeque.shiftBits(sw);
|
|
474
|
+
srcDeque.aligned = false;
|
|
475
|
+
}
|
|
476
|
+
if (sOrd >= sEdges.length) return DPDA_RESULT.FAILED;
|
|
477
|
+
const [label, sNext] = sEdges[sOrd];
|
|
478
|
+
const dOrd = dEdges.findIndex(([l]) => l === label);
|
|
479
|
+
if (dOrd < 0) return DPDA_RESULT.FAILED;
|
|
480
|
+
const dw = choiceWidth(dEdges.length);
|
|
481
|
+
if (dw > 0) dstDeque.pushBits(dOrd, dw);
|
|
482
|
+
stack.pop();
|
|
483
|
+
stack.push({ s: sNext, d: dEdges[dOrd][1] });
|
|
484
|
+
} else {
|
|
485
|
+
return DPDA_RESULT.FAILED;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
return DPDA_RESULT.DONE;
|
|
489
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fraczak/k",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.0.0",
|
|
4
4
|
"description": "k — a language for content-addressable schemas and data transformations over algebraic data types (products and tagged unions); includes runtime, contract derivation, REPL, and codec toolchain",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -21,17 +21,9 @@
|
|
|
21
21
|
"benchmark:html": "node scripts/render-benchmark.mjs",
|
|
22
22
|
"perf:ieee": "node scripts/perf-ieee.mjs",
|
|
23
23
|
"perf:ieee:full": "node scripts/perf-ieee.mjs --full",
|
|
24
|
-
"perf:ieee:pkir": "BACKENDS=pkir node scripts/perf-ieee.mjs",
|
|
25
|
-
"perf:ieee:llvm-pkir": "BACKENDS=llvm-pkir node scripts/perf-ieee.mjs",
|
|
26
24
|
"perf:int": "node scripts/perf-int.mjs",
|
|
27
|
-
"perf:int:pkir": "BACKENDS=pkir node scripts/perf-int.mjs",
|
|
28
|
-
"perf:int:llvm-pkir": "BACKENDS=llvm-pkir node scripts/perf-int.mjs",
|
|
29
25
|
"perf:mult": "node scripts/perf-multiplication.mjs",
|
|
30
|
-
"perf:mult:pkir": "node scripts/perf-multiplication.mjs --backends pkir",
|
|
31
|
-
"perf:mult:llvm-pkir": "node scripts/perf-multiplication.mjs --backends llvm-pkir",
|
|
32
26
|
"perf:poly": "node scripts/perf-poly.mjs",
|
|
33
|
-
"perf:poly:pkir": "BACKENDS=pkir node scripts/perf-poly.mjs",
|
|
34
|
-
"perf:poly:llvm-pkir": "BACKENDS=llvm-pkir node scripts/perf-poly.mjs",
|
|
35
27
|
"perf:poly:trace": "node scripts/perf-poly.mjs --trace",
|
|
36
28
|
"perf:idempotency": "node scripts/benchmark-idempotency.mjs",
|
|
37
29
|
"deps:graph": "node scripts/generate-dependency-graph.mjs",
|