@fraczak/k 8.2.0 → 8.4.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 +73 -0
- package/DOCS/CANONICAL_PATTERNS.md +349 -0
- package/DOCS/OBJECT_FILE_AND_PATTERN.md +12 -4
- package/DOCS/REPL.md +30 -3
- package/Examples/arithmetics.k +31 -14
- package/Examples/bnat.k +13 -6
- package/Examples/buda.k +9 -4
- package/Examples/byte.k +23 -9
- package/Examples/ieee.k +102 -34
- package/Examples/karatsuba-mult.k +23 -9
- package/Examples/nat.k +3 -2
- package/Examples/poly.k +30 -16
- package/Examples/vhdl_to_k.k +7 -2
- package/README.md +33 -5
- package/backend-api.mjs +6 -1
- package/backends/llvm/src/cli.mjs +3 -33
- package/backends/wasm/src/cli.mjs +3 -33
- package/backends/wasm/src/wasm.mjs +1 -29
- package/browser/vfs-data.mjs +8 -8
- package/canonical-pattern.mjs +407 -0
- package/k.mjs +12 -29
- package/kvm.mjs +2 -28
- package/named-patterns.mjs +35 -2
- package/object.mjs +137 -20
- package/objects/compile.mjs +2 -29
- package/objects/validate.mjs +13 -1
- package/package.json +1 -1
- package/parser.jison +72 -10
- package/parser.mjs +114 -49
- package/pretty.mjs +19 -4
- package/repl.mjs +319 -104
- package/scripts/build-repl-html.mjs +1 -1
- package/tests/test-k-object.mjs +5 -1
- package/tests/test-named-patterns.mjs +439 -0
- package/tests/test-repl.mjs +163 -12
|
@@ -4,7 +4,8 @@ import {
|
|
|
4
4
|
compileLibrary,
|
|
5
5
|
compileObjectBuffer,
|
|
6
6
|
decodeObject,
|
|
7
|
-
loadLibrary
|
|
7
|
+
loadLibrary,
|
|
8
|
+
buildExportPreamble
|
|
8
9
|
} from "@fraczak/k/object.mjs";
|
|
9
10
|
|
|
10
11
|
export function readAll(stream) {
|
|
@@ -138,38 +139,7 @@ export function resolveProgramInput(args, { allowStdinSource = false } = {}) {
|
|
|
138
139
|
};
|
|
139
140
|
}
|
|
140
141
|
|
|
141
|
-
export
|
|
142
|
-
if (exports.length === 0) return "";
|
|
143
|
-
const aliasMap = {};
|
|
144
|
-
for (const lib of libraries) {
|
|
145
|
-
for (const [name, hash] of Object.entries(lib.relAlias || {})) {
|
|
146
|
-
if (name !== "__main__") aliasMap[name] = hash;
|
|
147
|
-
}
|
|
148
|
-
for (const [hash, entry] of Object.entries(lib.meta || {})) {
|
|
149
|
-
if (entry?.type !== "rel") continue;
|
|
150
|
-
for (const origin of entry?.origins || []) {
|
|
151
|
-
if (origin?.name && origin.name !== "__main__") {
|
|
152
|
-
aliasMap[origin.name] = hash;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
const lines = [];
|
|
158
|
-
for (const spec of exports) {
|
|
159
|
-
const [libName, localName] = spec.includes(":")
|
|
160
|
-
? spec.split(":", 2)
|
|
161
|
-
: [spec, spec];
|
|
162
|
-
const hash = aliasMap[libName];
|
|
163
|
-
if (!hash) {
|
|
164
|
-
throw new Error(
|
|
165
|
-
`--export: '${libName}' not found in loaded libraries`
|
|
166
|
-
);
|
|
167
|
-
}
|
|
168
|
-
const body = hash.startsWith("@") ? hash.slice(1) : hash;
|
|
169
|
-
lines.push(`${localName} = @${body};`);
|
|
170
|
-
}
|
|
171
|
-
return lines.join("\n") + "\n";
|
|
172
|
-
}
|
|
142
|
+
export { buildExportPreamble };
|
|
173
143
|
|
|
174
144
|
export async function compileProgramInputToObject(
|
|
175
145
|
input,
|
|
@@ -4,7 +4,8 @@ import path from "node:path";
|
|
|
4
4
|
import {
|
|
5
5
|
compileLibrary,
|
|
6
6
|
decodeObject,
|
|
7
|
-
loadLibrary
|
|
7
|
+
loadLibrary,
|
|
8
|
+
buildExportPreamble
|
|
8
9
|
} from "@fraczak/k/backend-api.mjs";
|
|
9
10
|
import {
|
|
10
11
|
compileWasmArtifact,
|
|
@@ -26,38 +27,7 @@ function readAll(stream) {
|
|
|
26
27
|
});
|
|
27
28
|
}
|
|
28
29
|
|
|
29
|
-
export
|
|
30
|
-
if (exports.length === 0) return "";
|
|
31
|
-
const aliasMap = {};
|
|
32
|
-
for (const lib of libraries) {
|
|
33
|
-
for (const [name, hash] of Object.entries(lib.relAlias || {})) {
|
|
34
|
-
if (name !== "__main__") aliasMap[name] = hash;
|
|
35
|
-
}
|
|
36
|
-
for (const [hash, entry] of Object.entries(lib.meta || {})) {
|
|
37
|
-
if (entry?.type !== "rel") continue;
|
|
38
|
-
for (const origin of entry?.origins || []) {
|
|
39
|
-
if (origin?.name && origin.name !== "__main__") {
|
|
40
|
-
aliasMap[origin.name] = hash;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
const lines = [];
|
|
46
|
-
for (const spec of exports) {
|
|
47
|
-
const [libName, localName] = spec.includes(":")
|
|
48
|
-
? spec.split(":", 2)
|
|
49
|
-
: [spec, spec];
|
|
50
|
-
const hash = aliasMap[libName];
|
|
51
|
-
if (!hash) {
|
|
52
|
-
throw new Error(
|
|
53
|
-
`--export: '${libName}' not found in loaded libraries`
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
const body = hash.startsWith("@") ? hash.slice(1) : hash;
|
|
57
|
-
lines.push(`${localName} = @${body};`);
|
|
58
|
-
}
|
|
59
|
-
return lines.join("\n") + "\n";
|
|
60
|
-
}
|
|
30
|
+
export { buildExportPreamble };
|
|
61
31
|
|
|
62
32
|
export function loadLibraryFromPath(libPath) {
|
|
63
33
|
const ext = path.extname(libPath).toLowerCase();
|
|
@@ -3,6 +3,7 @@ import path from "node:path";
|
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
4
|
import {
|
|
5
5
|
annotate,
|
|
6
|
+
buildExportPreamble,
|
|
6
7
|
decodeWire,
|
|
7
8
|
encodeToWire,
|
|
8
9
|
exportPatternGraph,
|
|
@@ -478,35 +479,6 @@ function validateInputEnvelope(inputPattern, valuePattern) {
|
|
|
478
479
|
intersectInputEnvelope(inputPattern, valuePattern);
|
|
479
480
|
}
|
|
480
481
|
|
|
481
|
-
function buildExportPreamble(exportSpecs, libraries) {
|
|
482
|
-
if (exportSpecs.length === 0) return "";
|
|
483
|
-
|
|
484
|
-
const aliasMap = {};
|
|
485
|
-
for (const lib of libraries) {
|
|
486
|
-
for (const [name, hash] of Object.entries(lib.relAlias || {})) {
|
|
487
|
-
if (name !== "__main__") aliasMap[name] = hash;
|
|
488
|
-
}
|
|
489
|
-
for (const [hash, entry] of Object.entries(lib.meta || {})) {
|
|
490
|
-
if (entry?.type !== "rel") continue;
|
|
491
|
-
for (const origin of entry?.origins || []) {
|
|
492
|
-
if (origin?.name && origin.name !== "__main__") {
|
|
493
|
-
aliasMap[origin.name] = hash;
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
const lines = [];
|
|
500
|
-
for (const spec of exportSpecs) {
|
|
501
|
-
const [libName, localName] = spec.includes(":") ? spec.split(":", 2) : [spec, spec];
|
|
502
|
-
const hash = aliasMap[libName];
|
|
503
|
-
if (!hash) throw new Error(`--export: '${libName}' not found in loaded libraries`);
|
|
504
|
-
const body = hash.startsWith("@") ? hash.slice(1) : hash;
|
|
505
|
-
lines.push(`${localName} = @${body};`);
|
|
506
|
-
}
|
|
507
|
-
return lines.join("\n") + "\n";
|
|
508
|
-
}
|
|
509
|
-
|
|
510
482
|
async function compileWasmArtifactFromDefs(
|
|
511
483
|
defs,
|
|
512
484
|
mainRelName = "__main__",
|