@fraczak/k 6.7.0 → 6.9.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 +10 -0
- package/DOCS/CODECS.md +80 -181
- package/DOCS/CODEC_ARCHITECTURE.md +194 -0
- package/DOCS/REPL.md +49 -60
- package/Examples/arithmetics.k +1 -0
- package/Examples/poly.k +46 -0
- package/TODO.md +112 -0
- package/browser/repl-browser.mjs +112 -733
- package/browser/shims/process.mjs +7 -0
- package/browser/tree-view.mjs +5 -2
- package/browser/vfs-data.mjs +14 -12
- package/codecs/README.md +7 -0
- package/codecs/ieee.mjs +50 -75
- package/codecs/int.mjs +83 -62
- package/codecs/json-codec.mjs +5 -77
- package/codecs/json.mjs +12 -56
- package/codecs/k-parse.mjs +3 -19
- package/codecs/runtime/codec-sdk.mjs +94 -0
- package/codecs/runtime/pattern-k.mjs +45 -0
- package/codecs/string-codec.mjs +48 -74
- package/codecs/unit.mjs +13 -61
- package/codecs/utf16.mjs +13 -56
- package/codecs/utf8.mjs +11 -55
- package/package.json +1 -1
- package/patterns/from-k.mjs +2 -22
- package/repl-codecs.mjs +78 -184
- package/repl.mjs +168 -226
- package/scripts/build-repl-html.mjs +273 -424
- package/scripts/run-tests.mjs +1 -0
- package/tests/test-codecs.mjs +186 -0
- package/tests/test-poly.mjs +44 -0
- package/tests/test-repl-html.mjs +190 -53
- package/tests/test-repl.mjs +150 -114
- package/tests/test-structural-values.mjs +17 -0
- package/tests/test-tree-view.mjs +3 -0
- package/codecs/example-pipeline.mjs +0 -163
- package/codecs/runtime/ieee-pattern.mjs +0 -30
- package/codecs/runtime/test-codec.mjs +0 -89
- package/codecs/runtime/typeFromValue.mjs +0 -34
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* Test the binary codec with basic k types
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { deriveClosedPattern } from './codec.mjs';
|
|
7
|
-
import { encodeToWire, decodeWire } from './prefix-codec.mjs';
|
|
8
|
-
import { patternToPropertyList } from './pattern-json.mjs';
|
|
9
|
-
import { Value, isProduct, isVariant } from '../../Value.mjs';
|
|
10
|
-
import hash from '../../hash.mjs';
|
|
11
|
-
|
|
12
|
-
// Type repository
|
|
13
|
-
const types = {};
|
|
14
|
-
|
|
15
|
-
// Define unit type: {}
|
|
16
|
-
const unitTypeDef = { code: 'product', product: {} };
|
|
17
|
-
const unitTypeStr = '$C0={};';
|
|
18
|
-
const unitTypeName = hash(unitTypeStr);
|
|
19
|
-
types[unitTypeName] = unitTypeDef;
|
|
20
|
-
|
|
21
|
-
console.log('Unit type name:', unitTypeName);
|
|
22
|
-
|
|
23
|
-
// Define bool type: <{} false, {} true>
|
|
24
|
-
const boolTypeDef = {
|
|
25
|
-
code: 'union',
|
|
26
|
-
union: {
|
|
27
|
-
false: unitTypeName,
|
|
28
|
-
true: unitTypeName
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
const boolTypeStr = `$C0=<${unitTypeName}"false",${unitTypeName}"true">;`;
|
|
32
|
-
const boolTypeName = hash(boolTypeStr);
|
|
33
|
-
types[boolTypeName] = boolTypeDef;
|
|
34
|
-
|
|
35
|
-
console.log('Bool type name:', boolTypeName);
|
|
36
|
-
|
|
37
|
-
// Resolver function
|
|
38
|
-
function resolveType(typeName) {
|
|
39
|
-
if (!types[typeName]) {
|
|
40
|
-
throw new Error(`Unknown type: ${typeName}`);
|
|
41
|
-
}
|
|
42
|
-
return types[typeName];
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Test 1: Unit value
|
|
46
|
-
console.log('\n=== Test 1: Unit ===');
|
|
47
|
-
const unitValue = Value.product({});
|
|
48
|
-
console.log('Encoding unit:', unitValue.toString());
|
|
49
|
-
|
|
50
|
-
const unitPattern = patternToPropertyList(deriveClosedPattern(unitTypeName, unitTypeDef, resolveType));
|
|
51
|
-
const unitEncoded = encodeToWire(unitValue, unitPattern);
|
|
52
|
-
console.log('Encoded bytes:', unitEncoded.length, 'bytes');
|
|
53
|
-
console.log('Hex:', unitEncoded.toString('hex'));
|
|
54
|
-
|
|
55
|
-
const unitDecoded = decodeWire(unitEncoded);
|
|
56
|
-
console.log('Decoded value:', unitDecoded.value.toString());
|
|
57
|
-
console.log('Match:', isProduct(unitDecoded.value) &&
|
|
58
|
-
Object.keys(unitDecoded.value.product).length === 0);
|
|
59
|
-
|
|
60
|
-
// Test 2: Bool false
|
|
61
|
-
console.log('\n=== Test 2: Bool (false) ===');
|
|
62
|
-
const falseValue = Value.variant('false', Value.product({}));
|
|
63
|
-
console.log('Encoding false:', falseValue.toString());
|
|
64
|
-
|
|
65
|
-
const boolPattern = patternToPropertyList(deriveClosedPattern(boolTypeName, boolTypeDef, resolveType));
|
|
66
|
-
const falseEncoded = encodeToWire(falseValue, boolPattern);
|
|
67
|
-
console.log('Encoded bytes:', falseEncoded.length, 'bytes');
|
|
68
|
-
console.log('Hex:', falseEncoded.toString('hex'));
|
|
69
|
-
|
|
70
|
-
const falseDecoded = decodeWire(falseEncoded);
|
|
71
|
-
console.log('Decoded value:', falseDecoded.value.toString());
|
|
72
|
-
console.log('Match:', isVariant(falseDecoded.value) &&
|
|
73
|
-
falseDecoded.value.tag === 'false');
|
|
74
|
-
|
|
75
|
-
// Test 3: Bool true
|
|
76
|
-
console.log('\n=== Test 3: Bool (true) ===');
|
|
77
|
-
const trueValue = Value.variant('true', Value.product({}));
|
|
78
|
-
console.log('Encoding true:', trueValue.toString());
|
|
79
|
-
|
|
80
|
-
const trueEncoded = encodeToWire(trueValue, boolPattern);
|
|
81
|
-
console.log('Encoded bytes:', trueEncoded.length, 'bytes');
|
|
82
|
-
console.log('Hex:', trueEncoded.toString('hex'));
|
|
83
|
-
|
|
84
|
-
const trueDecoded = decodeWire(trueEncoded);
|
|
85
|
-
console.log('Decoded value:', trueDecoded.value.toString());
|
|
86
|
-
console.log('Match:', isVariant(trueDecoded.value) &&
|
|
87
|
-
trueDecoded.value.tag === 'true');
|
|
88
|
-
|
|
89
|
-
console.log('\n=== All tests complete ===');
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { isProduct, isVariant } from "../../Value.mjs";
|
|
2
|
-
|
|
3
|
-
function typeDefsFromValue(value) {
|
|
4
|
-
let nextId = 0;
|
|
5
|
-
const defs = {};
|
|
6
|
-
|
|
7
|
-
function visit(node) {
|
|
8
|
-
const name = `:auto${nextId++}`;
|
|
9
|
-
if (isProduct(node)) {
|
|
10
|
-
const labels = Object.keys(node.product).sort();
|
|
11
|
-
const product = {};
|
|
12
|
-
for (const label of labels) {
|
|
13
|
-
product[label] = visit(node.product[label]);
|
|
14
|
-
}
|
|
15
|
-
defs[name] = { code: "product", product };
|
|
16
|
-
return name;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (isVariant(node)) {
|
|
20
|
-
const childName = visit(node.value);
|
|
21
|
-
defs[name] = { code: "union", union: { [node.tag]: childName } };
|
|
22
|
-
return name;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
throw new Error(`Unsupported runtime value node: ${node?.constructor?.name || typeof node}`);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const root = visit(value);
|
|
29
|
-
defs.__root__ = { code: "ref", ref: `@${root}` };
|
|
30
|
-
return { defs, root };
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export { typeDefsFromValue };
|
|
34
|
-
export default { typeDefsFromValue };
|