@fraczak/k 8.6.1 → 8.7.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.
- package/CHANGELOG.md +37 -0
- package/DOCS/CODECS.md +20 -14
- package/DOCS/FILE_FORMATS.md +53 -12
- package/DOCS/GETTING_STARTED.md +15 -8
- package/DOCS/PATTERNS.md +10 -0
- package/DOCS/REPL.md +12 -7
- package/Examples/arithmetics.k +81 -468
- package/Examples/ieee-arithmetics.k +37 -33
- package/Examples/poly.k +107 -104
- package/README.md +19 -11
- package/backend-api.mjs +3 -1
- package/backends/llvm/README.md +1 -1
- package/backends/llvm/bin/k-llvm-build.mjs +5 -5
- package/backends/llvm/bin/k-llvm-compile.mjs +4 -4
- package/backends/llvm/bin/k-llvm.mjs +6 -6
- package/backends/llvm/src/cli.mjs +17 -15
- package/backends/llvm/tests/integration.sh +6 -1
- package/backends/wasm/README.md +3 -3
- package/backends/wasm/bin/k-wasm-compile.mjs +4 -4
- package/backends/wasm/bin/k-wasm.mjs +4 -4
- package/backends/wasm/src/cli.mjs +12 -10
- package/backends/wasm/src/wasm.mjs +4 -4
- package/backends/wasm/tests/integration.sh +24 -4
- package/browser/shims/fs.mjs +19 -3
- package/canonical-pattern.mjs +214 -57
- package/compiler.mjs +26 -0
- package/core.k +2 -1
- package/harness/suites/poly.mjs +14 -30
- package/index.mjs +67 -2
- package/k.mjs +14 -18
- package/kir.mjs +3 -1
- package/kvm.mjs +11 -11
- package/lib/arithmetics.k +286 -0
- package/lib/bit.k +68 -0
- package/lib/bits.k +227 -0
- package/lib/btrie.k +187 -0
- package/lib/djson.k +327 -0
- package/lib/ieee.k +709 -0
- package/lib/list.k +49 -0
- package/lib/string.k +588 -0
- package/named-patterns.mjs +2 -14
- package/object.mjs +32 -7
- package/objects/README.md +9 -3
- package/objects/compile.mjs +11 -14
- package/package.json +1 -1
- package/repl.mjs +24 -5
- package/scripts/benchmark-idempotency.mjs +22 -15
- package/scripts/benchmark.mjs +29 -10
- package/scripts/build-repl-html.mjs +41 -13
- package/scripts/perf-ieee.mjs +9 -1
- package/scripts/perf-int.mjs +10 -17
- package/scripts/perf-multiplication.mjs +9 -15
- package/scripts/perf-poly.mjs +16 -32
- package/scripts/run-tests.mjs +3 -2
- package/scripts/test-package.mjs +13 -2
- package/tests/helpers/cli.mjs +19 -3
- package/tests/integration.sh +25 -5
- package/tests/test-arithmetics.mjs +4 -2
- package/tests/test-djson-methods.mjs +21 -12
- package/tests/test-ieee-arithmetic.mjs +11 -3
- package/tests/test-ieee-text.mjs +19 -4
- package/tests/test-k-object.mjs +15 -4
- package/tests/test-karatsuba.mjs +1 -1
- package/tests/test-kvm-polymorphic.mjs +58 -34
- package/tests/test-lib.mjs +117 -0
- package/tests/test-library-isolation.mjs +381 -0
- package/tests/test-named-patterns.mjs +21 -3
- package/tests/test-parse-json.mjs +44 -14
- package/tests/test-poly.mjs +10 -4
- package/tests/test-repl-html.mjs +18 -7
- package/tests/test-repl.mjs +50 -35
- package/Examples/djson.k +0 -894
- package/Examples/ieee.k +0 -969
- package/Examples/karatsuba-mult.k +0 -227
- package/Examples/list.k +0 -10
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
compileLibrary,
|
|
6
6
|
decodeObject,
|
|
7
7
|
loadLibrary,
|
|
8
|
-
|
|
8
|
+
buildUsePreamble
|
|
9
9
|
} from "@fraczak/k/backend-api.mjs";
|
|
10
10
|
import {
|
|
11
11
|
compileWasmArtifact,
|
|
@@ -27,7 +27,7 @@ function readAll(stream) {
|
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
export {
|
|
30
|
+
export { buildUsePreamble };
|
|
31
31
|
|
|
32
32
|
export function loadLibraryFromPath(libPath) {
|
|
33
33
|
const ext = path.extname(libPath).toLowerCase();
|
|
@@ -43,7 +43,7 @@ export function loadLibraryFromPath(libPath) {
|
|
|
43
43
|
|
|
44
44
|
function parseCompileOptions(args) {
|
|
45
45
|
const libraries = [];
|
|
46
|
-
const
|
|
46
|
+
const useSpecs = [];
|
|
47
47
|
let loadedLibrary = false;
|
|
48
48
|
let usePKIR = false;
|
|
49
49
|
|
|
@@ -61,15 +61,15 @@ function parseCompileOptions(args) {
|
|
|
61
61
|
if (!libPath) throw new Error("--lib requires a file argument");
|
|
62
62
|
libraries.push(loadLibraryFromPath(libPath));
|
|
63
63
|
loadedLibrary = true;
|
|
64
|
-
} else if (args[0] === "--
|
|
64
|
+
} else if (args[0] === "--use") {
|
|
65
65
|
args.shift();
|
|
66
66
|
const spec = args.shift();
|
|
67
67
|
if (!spec) {
|
|
68
68
|
throw new Error(
|
|
69
|
-
"--
|
|
69
|
+
"--use requires a spec argument (name or libname:localname)"
|
|
70
70
|
);
|
|
71
71
|
}
|
|
72
|
-
|
|
72
|
+
useSpecs.push(spec);
|
|
73
73
|
} else if (args[0] === "--pkir") {
|
|
74
74
|
args.shift();
|
|
75
75
|
usePKIR = true;
|
|
@@ -80,7 +80,7 @@ function parseCompileOptions(args) {
|
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
return { libraries,
|
|
83
|
+
return { libraries, useSpecs, usePKIR };
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
function fileInputType(filePath) {
|
|
@@ -126,16 +126,18 @@ async function compileProgramInput(
|
|
|
126
126
|
input,
|
|
127
127
|
{
|
|
128
128
|
libraries = [],
|
|
129
|
+
useSpecs = [],
|
|
129
130
|
exportSpecs = [],
|
|
130
131
|
stdin = null,
|
|
131
132
|
inputEnvelopePattern = null,
|
|
132
133
|
usePKIR = false
|
|
133
134
|
} = {}
|
|
134
135
|
) {
|
|
136
|
+
const specs = useSpecs.length > 0 ? useSpecs : exportSpecs;
|
|
135
137
|
if (input.kind === "inline-source") {
|
|
136
138
|
return compileWasmArtifact(input.source, {
|
|
137
139
|
libraries,
|
|
138
|
-
|
|
140
|
+
use: specs,
|
|
139
141
|
source: input.sourceName,
|
|
140
142
|
inputEnvelopePattern,
|
|
141
143
|
usePKIR
|
|
@@ -148,7 +150,7 @@ async function compileProgramInput(
|
|
|
148
150
|
}
|
|
149
151
|
return compileWasmArtifact((await readAll(stdin)).toString("utf8"), {
|
|
150
152
|
libraries,
|
|
151
|
-
|
|
153
|
+
use: specs,
|
|
152
154
|
source: input.sourceName,
|
|
153
155
|
inputEnvelopePattern,
|
|
154
156
|
usePKIR
|
|
@@ -158,7 +160,7 @@ async function compileProgramInput(
|
|
|
158
160
|
if (input.kind === "source-file") {
|
|
159
161
|
return compileWasmArtifact(fs.readFileSync(input.path, "utf8"), {
|
|
160
162
|
libraries,
|
|
161
|
-
|
|
163
|
+
use: specs,
|
|
162
164
|
source: input.sourceName,
|
|
163
165
|
inputEnvelopePattern,
|
|
164
166
|
usePKIR
|
|
@@ -3,7 +3,6 @@ import path from "node:path";
|
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
4
|
import {
|
|
5
5
|
annotate,
|
|
6
|
-
buildExportPreamble,
|
|
7
6
|
decodeWire,
|
|
8
7
|
encodeToWire,
|
|
9
8
|
exportPatternGraph,
|
|
@@ -642,19 +641,20 @@ async function compileWasmArtifact(
|
|
|
642
641
|
source,
|
|
643
642
|
{
|
|
644
643
|
libraries = [],
|
|
644
|
+
use = [],
|
|
645
645
|
exports = [],
|
|
646
646
|
source: sourceName = null,
|
|
647
647
|
inputEnvelopePattern = null,
|
|
648
648
|
usePKIR = false
|
|
649
649
|
} = {}
|
|
650
650
|
) {
|
|
651
|
-
const
|
|
652
|
-
const fullSource = preamble + source;
|
|
651
|
+
const specs = use.length > 0 ? use : exports;
|
|
653
652
|
const options = {
|
|
654
653
|
libraries,
|
|
654
|
+
useSpecs: specs,
|
|
655
655
|
...(sourceName ? { source: sourceName } : {})
|
|
656
656
|
};
|
|
657
|
-
let defs = annotate(
|
|
657
|
+
let defs = annotate(source, options);
|
|
658
658
|
|
|
659
659
|
let typingMode = "generic";
|
|
660
660
|
if (inputEnvelopePattern) {
|
|
@@ -172,13 +172,33 @@ printf '{"x":{"a":{}},"y":{"b":{}}}\n' |
|
|
|
172
172
|
node "$K_ROOT/codecs/k-print.mjs" |
|
|
173
173
|
grep -qx '"b"'
|
|
174
174
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
175
|
+
compile_step() {
|
|
176
|
+
local prev="$1"
|
|
177
|
+
local src="$2"
|
|
178
|
+
local out="$3"
|
|
179
|
+
local uses=()
|
|
180
|
+
while IFS= read -r line; do
|
|
181
|
+
[ -n "$line" ] && uses+=(--use "$line")
|
|
182
|
+
done < <(node -e 'import("'$K_ROOT'/canonical-pattern.mjs").then(m => { for (const u of m.parseHeaderUses(fs.readFileSync(process.argv[1],"utf8"))) console.log(u.use); })' "$src" 2>/dev/null || true)
|
|
183
|
+
if [ -n "$prev" ]; then
|
|
184
|
+
node "$K_ROOT/objects/compile.mjs" --lib "$prev" "${uses[@]}" "$src" "$out"
|
|
185
|
+
else
|
|
186
|
+
node "$K_ROOT/objects/compile.mjs" "${uses[@]}" "$src" "$out"
|
|
187
|
+
fi
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
compile_step "" "$K_ROOT/lib/bit.k" "$TMP_DIR/bit.klib"
|
|
191
|
+
compile_step "$TMP_DIR/bit.klib" "$K_ROOT/lib/bits.k" "$TMP_DIR/bits.klib"
|
|
192
|
+
compile_step "$TMP_DIR/bits.klib" "$K_ROOT/lib/list.k" "$TMP_DIR/list.klib"
|
|
193
|
+
compile_step "$TMP_DIR/list.klib" "$K_ROOT/lib/btrie.k" "$TMP_DIR/btrie.klib"
|
|
194
|
+
compile_step "$TMP_DIR/btrie.klib" "$K_ROOT/lib/string.k" "$TMP_DIR/string.klib"
|
|
195
|
+
compile_step "$TMP_DIR/string.klib" "$K_ROOT/lib/ieee.k" "$TMP_DIR/ieee-types.klib"
|
|
196
|
+
compile_step "$TMP_DIR/ieee-types.klib" "$K_ROOT/Examples/ieee-arithmetics.k" "$TMP_DIR/ieee.klib"
|
|
197
|
+
if node ./bin/k-wasm-compile.mjs --lib "$TMP_DIR/ieee.klib" --lib "$TMP_DIR/ieee.klib" --use mul:times "{()x,()y} times .result" "$TMP_DIR/repeated-lib.wasm" >/dev/null 2>&1; then
|
|
178
198
|
echo "--lib unexpectedly accepted more than once" >&2
|
|
179
199
|
exit 1
|
|
180
200
|
fi
|
|
181
|
-
node ./bin/k-wasm-compile.mjs --lib "$TMP_DIR/ieee.klib" --
|
|
201
|
+
node ./bin/k-wasm-compile.mjs --lib "$TMP_DIR/ieee.klib" --use mul:times "{()x,()y} times .result" "$TMP_DIR/ieee-mul.wasm"
|
|
182
202
|
echo 0.12 |
|
|
183
203
|
node "$K_ROOT/codecs/ieee.mjs" --parse |
|
|
184
204
|
node ./bin/k-wasm-run.mjs "$TMP_DIR/ieee-mul.wasm" |
|
package/browser/shims/fs.mjs
CHANGED
|
@@ -12,6 +12,8 @@ function cleanPath(p) {
|
|
|
12
12
|
let s = (p instanceof URL ? p.pathname : (p?.pathname || String(p))).replace(/\\/g, "/");
|
|
13
13
|
if (s.startsWith("file://")) s = s.replace(/^file:\/\//, "");
|
|
14
14
|
if (s.endsWith("runtime.wat")) return "backends/wasm/runtime.wat";
|
|
15
|
+
const libIdx = s.lastIndexOf("lib/");
|
|
16
|
+
if (libIdx !== -1) return s.slice(libIdx);
|
|
15
17
|
const exIdx = s.lastIndexOf("Examples/");
|
|
16
18
|
if (exIdx !== -1) return s.slice(exIdx);
|
|
17
19
|
const wasmIdx = s.lastIndexOf("backends/wasm/");
|
|
@@ -32,10 +34,17 @@ export function setVfsFile(path, content) {
|
|
|
32
34
|
export function getVfsFile(path) {
|
|
33
35
|
const cleaned = cleanPath(path);
|
|
34
36
|
if (vfs.has(cleaned)) return vfs.get(cleaned);
|
|
37
|
+
if (cleaned.startsWith("lib/")) {
|
|
38
|
+
const stripped = cleaned.slice("lib/".length);
|
|
39
|
+
if (vfs.has(stripped)) return vfs.get(stripped);
|
|
40
|
+
} else if (!cleaned.startsWith("Examples/")) {
|
|
41
|
+
const withLib = `lib/${cleaned}`;
|
|
42
|
+
if (vfs.has(withLib)) return vfs.get(withLib);
|
|
43
|
+
}
|
|
35
44
|
if (cleaned.startsWith("Examples/")) {
|
|
36
45
|
const stripped = cleaned.slice("Examples/".length);
|
|
37
46
|
if (vfs.has(stripped)) return vfs.get(stripped);
|
|
38
|
-
} else {
|
|
47
|
+
} else if (!cleaned.startsWith("lib/")) {
|
|
39
48
|
const withEx = `Examples/${cleaned}`;
|
|
40
49
|
if (vfs.has(withEx)) return vfs.get(withEx);
|
|
41
50
|
}
|
|
@@ -51,12 +60,19 @@ export function getVfsFile(path) {
|
|
|
51
60
|
|
|
52
61
|
const CURATED_VFS_FILES = [
|
|
53
62
|
"core.k",
|
|
63
|
+
"bit.k",
|
|
64
|
+
"bits.k",
|
|
54
65
|
"arithmetics.k",
|
|
66
|
+
"list.k",
|
|
67
|
+
"btrie.k",
|
|
68
|
+
"string.k",
|
|
55
69
|
"ieee.k",
|
|
70
|
+
"djson.k",
|
|
56
71
|
"ieee-arithmetics.k",
|
|
57
72
|
"poly.k",
|
|
58
|
-
"
|
|
73
|
+
"nat.k",
|
|
59
74
|
"json.mjs",
|
|
75
|
+
"djson.mjs",
|
|
60
76
|
"utf8.mjs",
|
|
61
77
|
"int.mjs",
|
|
62
78
|
"unit.mjs",
|
|
@@ -72,7 +88,7 @@ export function getAllVfsFiles() {
|
|
|
72
88
|
}
|
|
73
89
|
for (const key of vfs.keys()) {
|
|
74
90
|
if (key.endsWith("runtime.wat") || key.startsWith("backends/")) continue;
|
|
75
|
-
if (key.startsWith("Examples/") || key.startsWith("codecs/")) continue;
|
|
91
|
+
if (key.startsWith("lib/") || key.startsWith("Examples/") || key.startsWith("codecs/")) continue;
|
|
76
92
|
result.add(key);
|
|
77
93
|
}
|
|
78
94
|
return Array.from(result);
|
package/canonical-pattern.mjs
CHANGED
|
@@ -415,55 +415,141 @@ export function canonicalPatternHash(name, pat, options = {}) {
|
|
|
415
415
|
return resolvePatternCanonicalInfo(name, pat, options).hash;
|
|
416
416
|
}
|
|
417
417
|
|
|
418
|
-
export function
|
|
419
|
-
if (
|
|
418
|
+
export function parseHeaderDirectives(source) {
|
|
419
|
+
if (!source || typeof source !== "string") return [];
|
|
420
|
+
const directives = [];
|
|
421
|
+
const lines = source.split(/\r?\n/);
|
|
422
|
+
for (const line of lines) {
|
|
423
|
+
const trimmed = line.trim();
|
|
424
|
+
if (!trimmed || trimmed.startsWith("--") || trimmed.startsWith("//") || trimmed.startsWith("%")) {
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
if (trimmed.startsWith("#")) {
|
|
428
|
+
const match = trimmed.match(/^#\s*--?lib\s+(\S+)\s+--use\s+(.+)$/);
|
|
429
|
+
if (match) {
|
|
430
|
+
directives.push({ lib: match[1], use: match[2].trim() });
|
|
431
|
+
}
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
434
|
+
break;
|
|
435
|
+
}
|
|
436
|
+
return directives;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
export function parseHeaderUses(source) {
|
|
440
|
+
return parseHeaderDirectives(source);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function buildMapsForLibrary(lib) {
|
|
420
444
|
const relMap = {};
|
|
421
445
|
const patMap = {};
|
|
422
|
-
for (const
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
446
|
+
for (const [name, hash] of Object.entries(lib.relAlias || {})) {
|
|
447
|
+
if (name !== "__main__") relMap[name] = { hash };
|
|
448
|
+
}
|
|
449
|
+
for (const [name, hash] of Object.entries(lib.patternAlias || {})) {
|
|
450
|
+
patMap[name] = {
|
|
451
|
+
hash,
|
|
452
|
+
params:
|
|
453
|
+
lib.patterns?.[name]?.params ||
|
|
454
|
+
lib.patterns?.[name]?._canonicalParams ||
|
|
455
|
+
lib.patterns?.[hash]?.params ||
|
|
456
|
+
[],
|
|
457
|
+
aliasArgs:
|
|
458
|
+
lib.patterns?.[name]?._aliasArgs ||
|
|
459
|
+
lib.meta?.[hash]?.origins?.find((o) => o.name === name)
|
|
460
|
+
?.aliasArgs ||
|
|
461
|
+
lib.patterns?.[name]?.params ||
|
|
462
|
+
[]
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
for (const [name, pat] of Object.entries(lib.patterns || {})) {
|
|
466
|
+
if (!patMap[name]) {
|
|
427
467
|
patMap[name] = {
|
|
428
|
-
hash,
|
|
429
|
-
params:
|
|
430
|
-
|
|
431
|
-
lib.patterns?.[name]?._canonicalParams ||
|
|
432
|
-
lib.patterns?.[hash]?.params ||
|
|
433
|
-
[],
|
|
434
|
-
aliasArgs:
|
|
435
|
-
lib.patterns?.[name]?._aliasArgs ||
|
|
436
|
-
lib.meta?.[hash]?.origins?.find((o) => o.name === name)
|
|
437
|
-
?.aliasArgs ||
|
|
438
|
-
lib.patterns?.[name]?.params ||
|
|
439
|
-
[]
|
|
468
|
+
hash: pat._canonical || lib.patternAlias?.[name] || name,
|
|
469
|
+
params: pat.params || [],
|
|
470
|
+
aliasArgs: pat._aliasArgs || pat.params || []
|
|
440
471
|
};
|
|
441
472
|
}
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
473
|
+
}
|
|
474
|
+
for (const [hash, entry] of Object.entries(lib.meta || {})) {
|
|
475
|
+
for (const origin of entry?.origins || []) {
|
|
476
|
+
const type = origin?.type || entry?.type;
|
|
477
|
+
if (type === "rel") {
|
|
478
|
+
if (origin?.name && origin.name !== "__main__") {
|
|
479
|
+
relMap[origin.name] = { hash };
|
|
480
|
+
}
|
|
481
|
+
} else if (type === "pattern") {
|
|
482
|
+
if (origin?.name) {
|
|
483
|
+
patMap[origin.name] = {
|
|
484
|
+
hash,
|
|
485
|
+
params: origin.params || entry.params || [],
|
|
486
|
+
aliasArgs:
|
|
487
|
+
origin.aliasArgs ||
|
|
488
|
+
entry.aliasArgs ||
|
|
489
|
+
origin.params ||
|
|
490
|
+
[]
|
|
491
|
+
};
|
|
492
|
+
}
|
|
449
493
|
}
|
|
450
494
|
}
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
495
|
+
}
|
|
496
|
+
return { relMap, patMap };
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
function findMatchingLibrary(libPath, libraries) {
|
|
500
|
+
if (!libPath || !libraries || libraries.length === 0) return null;
|
|
501
|
+
const baseName = libPath.split("/").pop();
|
|
502
|
+
for (const lib of libraries) {
|
|
503
|
+
if (lib.source) {
|
|
504
|
+
if (
|
|
505
|
+
lib.source === libPath ||
|
|
506
|
+
lib.source.endsWith("/" + libPath) ||
|
|
507
|
+
lib.source.split("/").pop() === baseName
|
|
508
|
+
) {
|
|
509
|
+
return lib;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
if (lib.meta) {
|
|
513
|
+
for (const entry of Object.values(lib.meta)) {
|
|
514
|
+
for (const origin of entry?.origins || []) {
|
|
515
|
+
if (
|
|
516
|
+
origin.source &&
|
|
517
|
+
(origin.source === libPath ||
|
|
518
|
+
origin.source.split("/").pop() === baseName)
|
|
519
|
+
) {
|
|
520
|
+
return lib;
|
|
457
521
|
}
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
return null;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
function matchesSource(actualSource, requestedSource) {
|
|
530
|
+
if (!actualSource || !requestedSource) return false;
|
|
531
|
+
if (actualSource === requestedSource) return true;
|
|
532
|
+
const reqBase = requestedSource.split("/").pop();
|
|
533
|
+
const actBase = actualSource.split("/").pop();
|
|
534
|
+
return reqBase === actBase;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
function findFromMeta(libPath, name, type, libraries) {
|
|
538
|
+
for (const lib of libraries) {
|
|
539
|
+
if (!lib.meta) continue;
|
|
540
|
+
for (const [hash, entry] of Object.entries(lib.meta)) {
|
|
541
|
+
if (entry?.type !== type && entry?.type !== "any") continue;
|
|
542
|
+
for (const origin of entry?.origins || []) {
|
|
543
|
+
if (origin?.name === name && (origin.type == null || origin.type === type)) {
|
|
544
|
+
if (!libPath || matchesSource(origin.source, libPath)) {
|
|
545
|
+
return {
|
|
461
546
|
hash,
|
|
462
547
|
params: origin.params || entry.params || [],
|
|
463
548
|
aliasArgs:
|
|
464
549
|
origin.aliasArgs ||
|
|
465
550
|
entry.aliasArgs ||
|
|
466
551
|
origin.params ||
|
|
552
|
+
entry.params ||
|
|
467
553
|
[]
|
|
468
554
|
};
|
|
469
555
|
}
|
|
@@ -471,9 +557,45 @@ export function buildExportPreamble(exports = [], libraries = []) {
|
|
|
471
557
|
}
|
|
472
558
|
}
|
|
473
559
|
}
|
|
560
|
+
return null;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
export function buildUsePreamble(exports = [], libraries = []) {
|
|
564
|
+
if (exports.length === 0) return "";
|
|
565
|
+
const globalRelMap = {};
|
|
566
|
+
const globalPatMap = {};
|
|
567
|
+
const libMap = new Map();
|
|
568
|
+
|
|
569
|
+
for (const lib of libraries) {
|
|
570
|
+
const maps = buildMapsForLibrary(lib);
|
|
571
|
+
libMap.set(lib, maps);
|
|
572
|
+
Object.assign(globalRelMap, maps.relMap);
|
|
573
|
+
Object.assign(globalPatMap, maps.patMap);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
const flatItems = [];
|
|
577
|
+
for (const entry of exports) {
|
|
578
|
+
if (!entry) continue;
|
|
579
|
+
if (typeof entry === "string") {
|
|
580
|
+
const trimmed = entry.trim();
|
|
581
|
+
if (!trimmed) continue;
|
|
582
|
+
for (const spec of trimmed.split(/[\s,]+/).filter(Boolean)) {
|
|
583
|
+
flatItems.push({ lib: null, spec });
|
|
584
|
+
}
|
|
585
|
+
} else if (typeof entry === "object" && entry.use) {
|
|
586
|
+
const trimmed = entry.use.trim();
|
|
587
|
+
if (!trimmed) continue;
|
|
588
|
+
for (const spec of trimmed.split(/[\s,]+/).filter(Boolean)) {
|
|
589
|
+
flatItems.push({ lib: entry.lib || null, spec });
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
}
|
|
474
593
|
|
|
475
594
|
const lines = [];
|
|
476
|
-
|
|
595
|
+
const seenRel = new Set();
|
|
596
|
+
const seenPat = new Set();
|
|
597
|
+
for (const item of flatItems) {
|
|
598
|
+
const spec = item.spec;
|
|
477
599
|
const [rawLib, rawLocal] = spec.includes(":")
|
|
478
600
|
? spec.split(":", 2)
|
|
479
601
|
: [spec, spec];
|
|
@@ -482,43 +604,76 @@ export function buildExportPreamble(exports = [], libraries = []) {
|
|
|
482
604
|
const libName = rawLib.replace(/\[.*\]$/, "").trim();
|
|
483
605
|
const localName = rawLocal.replace(/\[.*\]$/, "").trim();
|
|
484
606
|
|
|
607
|
+
let relTarget = null;
|
|
608
|
+
let patTarget = null;
|
|
609
|
+
|
|
610
|
+
if (!isExplicitPattern) {
|
|
611
|
+
if (item.lib) {
|
|
612
|
+
relTarget = findFromMeta(item.lib, libName, "rel", libraries);
|
|
613
|
+
}
|
|
614
|
+
if (!relTarget) {
|
|
615
|
+
const matchedLib = item.lib ? findMatchingLibrary(item.lib, libraries) : null;
|
|
616
|
+
if (matchedLib && libMap.get(matchedLib)?.relMap[libName]) {
|
|
617
|
+
relTarget = libMap.get(matchedLib).relMap[libName];
|
|
618
|
+
} else if (globalRelMap[libName]) {
|
|
619
|
+
relTarget = globalRelMap[libName];
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
if (item.lib) {
|
|
625
|
+
patTarget = findFromMeta(item.lib, libName, "pattern", libraries);
|
|
626
|
+
}
|
|
627
|
+
if (!patTarget) {
|
|
628
|
+
const matchedLib = item.lib ? findMatchingLibrary(item.lib, libraries) : null;
|
|
629
|
+
if (matchedLib && libMap.get(matchedLib)?.patMap[libName]) {
|
|
630
|
+
patTarget = libMap.get(matchedLib).patMap[libName];
|
|
631
|
+
} else if (globalPatMap[libName]) {
|
|
632
|
+
patTarget = globalPatMap[libName];
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
|
|
485
636
|
let emitted = false;
|
|
486
|
-
if (!isExplicitPattern &&
|
|
487
|
-
const
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
637
|
+
if (!isExplicitPattern && relTarget) {
|
|
638
|
+
const body = relTarget.hash.startsWith("@")
|
|
639
|
+
? relTarget.hash.slice(1)
|
|
640
|
+
: relTarget.hash;
|
|
641
|
+
if (!seenRel.has(localName)) {
|
|
642
|
+
lines.push(`${localName} = @${body};`);
|
|
643
|
+
seenRel.add(localName);
|
|
644
|
+
}
|
|
492
645
|
emitted = true;
|
|
493
646
|
}
|
|
494
|
-
if (
|
|
495
|
-
if (isExplicitPattern || !emitted ||
|
|
496
|
-
const
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
: target.hash;
|
|
647
|
+
if (patTarget) {
|
|
648
|
+
if (isExplicitPattern || !emitted || relTarget) {
|
|
649
|
+
const body = patTarget.hash.startsWith("@")
|
|
650
|
+
? patTarget.hash.slice(1)
|
|
651
|
+
: patTarget.hash;
|
|
500
652
|
const paramStr =
|
|
501
|
-
|
|
502
|
-
? `[${
|
|
653
|
+
patTarget.params && patTarget.params.length > 0
|
|
654
|
+
? `[${patTarget.params.join(", ")}]`
|
|
503
655
|
: "[]";
|
|
504
|
-
const args =
|
|
656
|
+
const args = patTarget.aliasArgs || patTarget.params;
|
|
505
657
|
const argStr =
|
|
506
658
|
args && args.length > 0
|
|
507
659
|
? `[${args.join(", ")}]`
|
|
508
660
|
: paramStr;
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
661
|
+
if (!seenPat.has(localName)) {
|
|
662
|
+
lines.push(
|
|
663
|
+
`${localName}${paramStr} = @${body}${argStr};`
|
|
664
|
+
);
|
|
665
|
+
seenPat.add(localName);
|
|
666
|
+
}
|
|
512
667
|
emitted = true;
|
|
513
668
|
}
|
|
514
669
|
}
|
|
515
670
|
if (!emitted) {
|
|
516
671
|
throw new Error(
|
|
517
|
-
`--
|
|
672
|
+
`--use: '${rawLib}' not found in loaded libraries`
|
|
518
673
|
);
|
|
519
674
|
}
|
|
520
675
|
}
|
|
521
|
-
return lines.join("\n") + "\n";
|
|
676
|
+
return lines.length > 0 ? lines.join("\n") + "\n" : "";
|
|
522
677
|
}
|
|
523
678
|
|
|
524
679
|
const DEFAULT_MAX_PAT_LENGTH = 45;
|
|
@@ -773,7 +928,9 @@ export function canonicalBoundaryFilterSources(
|
|
|
773
928
|
export default {
|
|
774
929
|
resolvePatternCanonicalInfo,
|
|
775
930
|
canonicalPatternHash,
|
|
776
|
-
|
|
931
|
+
buildUsePreamble,
|
|
932
|
+
parseHeaderDirectives,
|
|
933
|
+
parseHeaderUses,
|
|
777
934
|
canonicalBoundaryFilterSources,
|
|
778
935
|
instantiateCanonicalFilter
|
|
779
936
|
};
|
package/compiler.mjs
CHANGED
|
@@ -18,6 +18,32 @@ function compileTypes(representatives, rels, options = {}) {
|
|
|
18
18
|
// }
|
|
19
19
|
|
|
20
20
|
const relAlias = {};
|
|
21
|
+
if (options.libraries) {
|
|
22
|
+
for (const lib of options.libraries) {
|
|
23
|
+
const lr = lib.rels || lib.defs?.rels || {};
|
|
24
|
+
for (const k of Object.keys(lr)) {
|
|
25
|
+
if (k.startsWith("@")) {
|
|
26
|
+
relAlias[k] = k;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const useSpecs = [...(options.useSpecs || options.use || [])];
|
|
31
|
+
for (const spec of useSpecs) {
|
|
32
|
+
const parts = spec.trim().split(/[\s,]+/);
|
|
33
|
+
for (const item of parts) {
|
|
34
|
+
if (!item) continue;
|
|
35
|
+
const [rawLib, rawLocal] = item.includes(":") ? item.split(":", 2) : [item, item];
|
|
36
|
+
const libName = rawLib.replace(/\[.*\]$/, "").trim();
|
|
37
|
+
const localName = rawLocal.replace(/\[.*\]$/, "").trim();
|
|
38
|
+
for (const lib of options.libraries) {
|
|
39
|
+
const relHash = lib.relAlias?.[libName] || (libName.startsWith("@") ? libName : null);
|
|
40
|
+
if (relHash) {
|
|
41
|
+
relAlias[localName] = relHash;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
21
47
|
const compileStats = {
|
|
22
48
|
sccs: []
|
|
23
49
|
};
|
package/core.k
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
bit[] = < {} 0, {} 1 >;
|
|
13
13
|
bits[] = < {} _, B 0, B 1 > = B;
|
|
14
|
+
list[X] = < {} nil, {X car, L cdr} cons> = L;
|
|
14
15
|
|
|
15
16
|
---------------------------------------------------------------
|
|
16
17
|
-- §2. Unicode and String
|
|
@@ -73,7 +74,7 @@ unicode[] = <
|
|
|
73
74
|
>;
|
|
74
75
|
|
|
75
76
|
-- Linked-list string: nil or (unicode car, string cdr).
|
|
76
|
-
string[] =
|
|
77
|
+
string[] = list[unicode[]];
|
|
77
78
|
|
|
78
79
|
---------------------------------------------------------------
|
|
79
80
|
-- §3. Pattern
|
package/harness/suites/poly.mjs
CHANGED
|
@@ -23,47 +23,31 @@ export async function createPolySuite({ listLength = 40, cacheDir = null } = {})
|
|
|
23
23
|
const ops = ["reverse", "concat", "split_by", "get_nth", "length"];
|
|
24
24
|
const benchNames = Object.fromEntries(ops.map(op => [op, `bench_${op}`]));
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
const
|
|
28
|
-
|
|
26
|
+
let depLib = null;
|
|
27
|
+
for (const relPath of ["../../lib/bit.k", "../../lib/bits.k", "../../lib/arithmetics.k", "../../lib/list.k"]) {
|
|
28
|
+
const filePath = fileURLToPath(import.meta.resolve(relPath));
|
|
29
|
+
depLib = loadLibrary(compileLibrary(fs.readFileSync(filePath, "utf8"), {
|
|
30
|
+
source: filePath,
|
|
31
|
+
libraries: depLib ? [depLib] : []
|
|
32
|
+
}));
|
|
33
|
+
}
|
|
29
34
|
|
|
30
35
|
const polyPath = fileURLToPath(import.meta.resolve("../../Examples/poly.k"));
|
|
31
36
|
const polySource = fs.readFileSync(polyPath, "utf8");
|
|
32
|
-
|
|
33
|
-
function buildExportPreamble(exports, libraries) {
|
|
34
|
-
const aliasMap = {};
|
|
35
|
-
for (const lib of libraries) {
|
|
36
|
-
for (const [name, hash] of Object.entries(lib.relAlias || {})) {
|
|
37
|
-
if (name !== "__main__") aliasMap[name] = hash;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
const lines = [];
|
|
41
|
-
for (const spec of exports) {
|
|
42
|
-
const [libName, localName] = spec.includes(":") ? spec.split(":", 2) : [spec, spec];
|
|
43
|
-
const hash = aliasMap[libName];
|
|
44
|
-
if (!hash) throw new Error(`--export: '${libName}' not found in loaded libraries`);
|
|
45
|
-
const body = hash.startsWith("@") ? hash.slice(1) : hash;
|
|
46
|
-
lines.push(`${localName} = @${body};`);
|
|
47
|
-
}
|
|
48
|
-
return lines.join("\n") + "\n";
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const neededExports = ["0", "int", "inc", "dec", "nat", "zero_int?"];
|
|
52
|
-
const exportPreamble = buildExportPreamble(neededExports, [arithmeticsLib]);
|
|
53
|
-
const polyLib = loadLibrary(compileLibrary(exportPreamble + polySource, {
|
|
37
|
+
const polyLib = loadLibrary(compileLibrary(polySource, {
|
|
54
38
|
source: polyPath,
|
|
55
|
-
libraries: [
|
|
39
|
+
libraries: [depLib]
|
|
56
40
|
}));
|
|
57
41
|
|
|
58
42
|
const state = {
|
|
59
|
-
codes: { ...
|
|
60
|
-
rels: { ...
|
|
43
|
+
codes: { ...depLib.codes, ...polyLib.codes },
|
|
44
|
+
rels: { ...depLib.rels, ...polyLib.rels },
|
|
61
45
|
typeAliases: {},
|
|
62
46
|
relAliases: { ...polyLib.relAlias },
|
|
63
|
-
meta: { ...
|
|
47
|
+
meta: { ...depLib.meta, ...polyLib.meta }
|
|
64
48
|
};
|
|
65
49
|
|
|
66
|
-
for (const [hash, entry] of Object.entries(
|
|
50
|
+
for (const [hash, entry] of Object.entries(depLib.meta)) {
|
|
67
51
|
if (entry.type === "code") {
|
|
68
52
|
for (const origin of entry.origins || []) {
|
|
69
53
|
if (origin.name) state.typeAliases[origin.name] = hash;
|