@fraczak/k 8.6.1 → 8.7.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 +22 -0
- package/DOCS/CODECS.md +20 -14
- package/DOCS/FILE_FORMATS.md +79 -12
- package/DOCS/GETTING_STARTED.md +17 -8
- package/DOCS/PATTERNS.md +17 -0
- package/DOCS/REPL.md +13 -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 -12
- package/backends/llvm/tests/integration.sh +4 -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 +6 -2
- package/backends/wasm/tests/integration.sh +8 -3
- package/browser/shims/fs.mjs +19 -3
- package/canonical-pattern.mjs +217 -57
- package/core.k +2 -1
- package/harness/suites/poly.mjs +14 -30
- package/k.mjs +17 -13
- package/kvm.mjs +13 -9
- package/lib/arithmetics.k +287 -0
- package/lib/bit.k +68 -0
- package/lib/bits.k +221 -0
- package/lib/btrie.k +187 -0
- package/lib/djson.k +326 -0
- package/lib/ieee.k +709 -0
- package/lib/list.k +49 -0
- package/lib/string.k +588 -0
- package/named-patterns.mjs +2 -2
- package/object.mjs +51 -9
- package/objects/README.md +10 -3
- package/objects/compile.mjs +10 -8
- package/package.json +1 -1
- package/repl.mjs +28 -6
- 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 +2 -2
- package/scripts/test-package.mjs +8 -2
- package/tests/helpers/cli.mjs +11 -3
- package/tests/integration.sh +9 -4
- package/tests/test-arithmetics.mjs +4 -2
- package/tests/test-djson-methods.mjs +16 -12
- package/tests/test-ieee-arithmetic.mjs +11 -3
- package/tests/test-ieee-text.mjs +15 -4
- package/tests/test-k-object.mjs +11 -3
- package/tests/test-karatsuba.mjs +1 -1
- package/tests/test-kvm-polymorphic.mjs +54 -34
- package/tests/test-lib.mjs +115 -0
- package/tests/test-named-patterns.mjs +12 -3
- package/tests/test-parse-json.mjs +33 -13
- 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,8 @@ import path from "node:path";
|
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
4
|
import {
|
|
5
5
|
annotate,
|
|
6
|
-
|
|
6
|
+
buildUsePreamble,
|
|
7
|
+
parseHeaderUses,
|
|
7
8
|
decodeWire,
|
|
8
9
|
encodeToWire,
|
|
9
10
|
exportPatternGraph,
|
|
@@ -642,13 +643,16 @@ async function compileWasmArtifact(
|
|
|
642
643
|
source,
|
|
643
644
|
{
|
|
644
645
|
libraries = [],
|
|
646
|
+
use = [],
|
|
645
647
|
exports = [],
|
|
646
648
|
source: sourceName = null,
|
|
647
649
|
inputEnvelopePattern = null,
|
|
648
650
|
usePKIR = false
|
|
649
651
|
} = {}
|
|
650
652
|
) {
|
|
651
|
-
const
|
|
653
|
+
const specs = use.length > 0 ? use : exports;
|
|
654
|
+
const allSpecs = [...parseHeaderUses(source), ...specs];
|
|
655
|
+
const preamble = buildUsePreamble(allSpecs, libraries);
|
|
652
656
|
const fullSource = preamble + source;
|
|
653
657
|
const options = {
|
|
654
658
|
libraries,
|
|
@@ -172,13 +172,18 @@ printf '{"x":{"a":{}},"y":{"b":{}}}\n' |
|
|
|
172
172
|
node "$K_ROOT/codecs/k-print.mjs" |
|
|
173
173
|
grep -qx '"b"'
|
|
174
174
|
|
|
175
|
-
node "$K_ROOT/objects/compile.mjs"
|
|
175
|
+
node "$K_ROOT/objects/compile.mjs" "$K_ROOT/lib/bit.k" "$TMP_DIR/bit.klib"
|
|
176
|
+
node "$K_ROOT/objects/compile.mjs" --lib "$TMP_DIR/bit.klib" "$K_ROOT/lib/bits.k" "$TMP_DIR/bits.klib"
|
|
177
|
+
node "$K_ROOT/objects/compile.mjs" --lib "$TMP_DIR/bits.klib" "$K_ROOT/lib/list.k" "$TMP_DIR/list.klib"
|
|
178
|
+
node "$K_ROOT/objects/compile.mjs" --lib "$TMP_DIR/list.klib" "$K_ROOT/lib/btrie.k" "$TMP_DIR/btrie.klib"
|
|
179
|
+
node "$K_ROOT/objects/compile.mjs" --lib "$TMP_DIR/btrie.klib" "$K_ROOT/lib/string.k" "$TMP_DIR/string.klib"
|
|
180
|
+
node "$K_ROOT/objects/compile.mjs" --lib "$TMP_DIR/string.klib" "$K_ROOT/lib/ieee.k" "$TMP_DIR/ieee-types.klib"
|
|
176
181
|
node "$K_ROOT/objects/compile.mjs" --lib "$TMP_DIR/ieee-types.klib" "$K_ROOT/Examples/ieee-arithmetics.k" "$TMP_DIR/ieee.klib"
|
|
177
|
-
if node ./bin/k-wasm-compile.mjs --lib "$TMP_DIR/ieee.klib" --lib "$TMP_DIR/ieee.klib" --
|
|
182
|
+
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
183
|
echo "--lib unexpectedly accepted more than once" >&2
|
|
179
184
|
exit 1
|
|
180
185
|
fi
|
|
181
|
-
node ./bin/k-wasm-compile.mjs --lib "$TMP_DIR/ieee.klib" --
|
|
186
|
+
node ./bin/k-wasm-compile.mjs --lib "$TMP_DIR/ieee.klib" --use mul:times "{()x,()y} times .result" "$TMP_DIR/ieee-mul.wasm"
|
|
182
187
|
echo 0.12 |
|
|
183
188
|
node "$K_ROOT/codecs/ieee.mjs" --parse |
|
|
184
189
|
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,144 @@ 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
|
+
if (trimmed.match(/^[a-zA-Z0-9_?!+-]+(?:\[[^\]]*\])?\s*=\s*@[a-zA-Z0-9_-]+(?:\[[^\]]*\])?\s*;/)) {
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
break;
|
|
438
|
+
}
|
|
439
|
+
return directives;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
export function parseHeaderUses(source) {
|
|
443
|
+
return parseHeaderDirectives(source);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function buildMapsForLibrary(lib) {
|
|
420
447
|
const relMap = {};
|
|
421
448
|
const patMap = {};
|
|
422
|
-
for (const
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
449
|
+
for (const [name, hash] of Object.entries(lib.relAlias || {})) {
|
|
450
|
+
if (name !== "__main__") relMap[name] = { hash };
|
|
451
|
+
}
|
|
452
|
+
for (const [name, hash] of Object.entries(lib.patternAlias || {})) {
|
|
453
|
+
patMap[name] = {
|
|
454
|
+
hash,
|
|
455
|
+
params:
|
|
456
|
+
lib.patterns?.[name]?.params ||
|
|
457
|
+
lib.patterns?.[name]?._canonicalParams ||
|
|
458
|
+
lib.patterns?.[hash]?.params ||
|
|
459
|
+
[],
|
|
460
|
+
aliasArgs:
|
|
461
|
+
lib.patterns?.[name]?._aliasArgs ||
|
|
462
|
+
lib.meta?.[hash]?.origins?.find((o) => o.name === name)
|
|
463
|
+
?.aliasArgs ||
|
|
464
|
+
lib.patterns?.[name]?.params ||
|
|
465
|
+
[]
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
for (const [name, pat] of Object.entries(lib.patterns || {})) {
|
|
469
|
+
if (!patMap[name]) {
|
|
427
470
|
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
|
-
[]
|
|
471
|
+
hash: pat._canonical || lib.patternAlias?.[name] || name,
|
|
472
|
+
params: pat.params || [],
|
|
473
|
+
aliasArgs: pat._aliasArgs || pat.params || []
|
|
440
474
|
};
|
|
441
475
|
}
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
476
|
+
}
|
|
477
|
+
for (const [hash, entry] of Object.entries(lib.meta || {})) {
|
|
478
|
+
for (const origin of entry?.origins || []) {
|
|
479
|
+
const type = origin?.type || entry?.type;
|
|
480
|
+
if (type === "rel") {
|
|
481
|
+
if (origin?.name && origin.name !== "__main__") {
|
|
482
|
+
relMap[origin.name] = { hash };
|
|
483
|
+
}
|
|
484
|
+
} else if (type === "pattern") {
|
|
485
|
+
if (origin?.name) {
|
|
486
|
+
patMap[origin.name] = {
|
|
487
|
+
hash,
|
|
488
|
+
params: origin.params || entry.params || [],
|
|
489
|
+
aliasArgs:
|
|
490
|
+
origin.aliasArgs ||
|
|
491
|
+
entry.aliasArgs ||
|
|
492
|
+
origin.params ||
|
|
493
|
+
[]
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
return { relMap, patMap };
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
function findMatchingLibrary(libPath, libraries) {
|
|
503
|
+
if (!libPath || !libraries || libraries.length === 0) return null;
|
|
504
|
+
const baseName = libPath.split("/").pop();
|
|
505
|
+
for (const lib of libraries) {
|
|
506
|
+
if (lib.source) {
|
|
507
|
+
if (
|
|
508
|
+
lib.source === libPath ||
|
|
509
|
+
lib.source.endsWith("/" + libPath) ||
|
|
510
|
+
lib.source.split("/").pop() === baseName
|
|
511
|
+
) {
|
|
512
|
+
return lib;
|
|
449
513
|
}
|
|
450
514
|
}
|
|
451
|
-
|
|
452
|
-
for (const
|
|
453
|
-
const
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
515
|
+
if (lib.meta) {
|
|
516
|
+
for (const entry of Object.values(lib.meta)) {
|
|
517
|
+
for (const origin of entry?.origins || []) {
|
|
518
|
+
if (
|
|
519
|
+
origin.source &&
|
|
520
|
+
(origin.source === libPath ||
|
|
521
|
+
origin.source.split("/").pop() === baseName)
|
|
522
|
+
) {
|
|
523
|
+
return lib;
|
|
457
524
|
}
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
return null;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
function matchesSource(actualSource, requestedSource) {
|
|
533
|
+
if (!actualSource || !requestedSource) return false;
|
|
534
|
+
if (actualSource === requestedSource) return true;
|
|
535
|
+
const reqBase = requestedSource.split("/").pop();
|
|
536
|
+
const actBase = actualSource.split("/").pop();
|
|
537
|
+
return reqBase === actBase;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
function findFromMeta(libPath, name, type, libraries) {
|
|
541
|
+
for (const lib of libraries) {
|
|
542
|
+
if (!lib.meta) continue;
|
|
543
|
+
for (const [hash, entry] of Object.entries(lib.meta)) {
|
|
544
|
+
if (entry?.type !== type && entry?.type !== "any") continue;
|
|
545
|
+
for (const origin of entry?.origins || []) {
|
|
546
|
+
if (origin?.name === name && (origin.type == null || origin.type === type)) {
|
|
547
|
+
if (!libPath || matchesSource(origin.source, libPath)) {
|
|
548
|
+
return {
|
|
461
549
|
hash,
|
|
462
550
|
params: origin.params || entry.params || [],
|
|
463
551
|
aliasArgs:
|
|
464
552
|
origin.aliasArgs ||
|
|
465
553
|
entry.aliasArgs ||
|
|
466
554
|
origin.params ||
|
|
555
|
+
entry.params ||
|
|
467
556
|
[]
|
|
468
557
|
};
|
|
469
558
|
}
|
|
@@ -471,9 +560,45 @@ export function buildExportPreamble(exports = [], libraries = []) {
|
|
|
471
560
|
}
|
|
472
561
|
}
|
|
473
562
|
}
|
|
563
|
+
return null;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
export function buildUsePreamble(exports = [], libraries = []) {
|
|
567
|
+
if (exports.length === 0) return "";
|
|
568
|
+
const globalRelMap = {};
|
|
569
|
+
const globalPatMap = {};
|
|
570
|
+
const libMap = new Map();
|
|
571
|
+
|
|
572
|
+
for (const lib of libraries) {
|
|
573
|
+
const maps = buildMapsForLibrary(lib);
|
|
574
|
+
libMap.set(lib, maps);
|
|
575
|
+
Object.assign(globalRelMap, maps.relMap);
|
|
576
|
+
Object.assign(globalPatMap, maps.patMap);
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
const flatItems = [];
|
|
580
|
+
for (const entry of exports) {
|
|
581
|
+
if (!entry) continue;
|
|
582
|
+
if (typeof entry === "string") {
|
|
583
|
+
const trimmed = entry.trim();
|
|
584
|
+
if (!trimmed) continue;
|
|
585
|
+
for (const spec of trimmed.split(/[\s,]+/).filter(Boolean)) {
|
|
586
|
+
flatItems.push({ lib: null, spec });
|
|
587
|
+
}
|
|
588
|
+
} else if (typeof entry === "object" && entry.use) {
|
|
589
|
+
const trimmed = entry.use.trim();
|
|
590
|
+
if (!trimmed) continue;
|
|
591
|
+
for (const spec of trimmed.split(/[\s,]+/).filter(Boolean)) {
|
|
592
|
+
flatItems.push({ lib: entry.lib || null, spec });
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
}
|
|
474
596
|
|
|
475
597
|
const lines = [];
|
|
476
|
-
|
|
598
|
+
const seenRel = new Set();
|
|
599
|
+
const seenPat = new Set();
|
|
600
|
+
for (const item of flatItems) {
|
|
601
|
+
const spec = item.spec;
|
|
477
602
|
const [rawLib, rawLocal] = spec.includes(":")
|
|
478
603
|
? spec.split(":", 2)
|
|
479
604
|
: [spec, spec];
|
|
@@ -482,43 +607,76 @@ export function buildExportPreamble(exports = [], libraries = []) {
|
|
|
482
607
|
const libName = rawLib.replace(/\[.*\]$/, "").trim();
|
|
483
608
|
const localName = rawLocal.replace(/\[.*\]$/, "").trim();
|
|
484
609
|
|
|
610
|
+
let relTarget = null;
|
|
611
|
+
let patTarget = null;
|
|
612
|
+
|
|
613
|
+
if (!isExplicitPattern) {
|
|
614
|
+
if (item.lib) {
|
|
615
|
+
relTarget = findFromMeta(item.lib, libName, "rel", libraries);
|
|
616
|
+
}
|
|
617
|
+
if (!relTarget) {
|
|
618
|
+
const matchedLib = item.lib ? findMatchingLibrary(item.lib, libraries) : null;
|
|
619
|
+
if (matchedLib && libMap.get(matchedLib)?.relMap[libName]) {
|
|
620
|
+
relTarget = libMap.get(matchedLib).relMap[libName];
|
|
621
|
+
} else if (globalRelMap[libName]) {
|
|
622
|
+
relTarget = globalRelMap[libName];
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
if (item.lib) {
|
|
628
|
+
patTarget = findFromMeta(item.lib, libName, "pattern", libraries);
|
|
629
|
+
}
|
|
630
|
+
if (!patTarget) {
|
|
631
|
+
const matchedLib = item.lib ? findMatchingLibrary(item.lib, libraries) : null;
|
|
632
|
+
if (matchedLib && libMap.get(matchedLib)?.patMap[libName]) {
|
|
633
|
+
patTarget = libMap.get(matchedLib).patMap[libName];
|
|
634
|
+
} else if (globalPatMap[libName]) {
|
|
635
|
+
patTarget = globalPatMap[libName];
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
485
639
|
let emitted = false;
|
|
486
|
-
if (!isExplicitPattern &&
|
|
487
|
-
const
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
640
|
+
if (!isExplicitPattern && relTarget) {
|
|
641
|
+
const body = relTarget.hash.startsWith("@")
|
|
642
|
+
? relTarget.hash.slice(1)
|
|
643
|
+
: relTarget.hash;
|
|
644
|
+
if (!seenRel.has(localName)) {
|
|
645
|
+
lines.push(`${localName} = @${body};`);
|
|
646
|
+
seenRel.add(localName);
|
|
647
|
+
}
|
|
492
648
|
emitted = true;
|
|
493
649
|
}
|
|
494
|
-
if (
|
|
495
|
-
if (isExplicitPattern || !emitted ||
|
|
496
|
-
const
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
: target.hash;
|
|
650
|
+
if (patTarget) {
|
|
651
|
+
if (isExplicitPattern || !emitted || relTarget) {
|
|
652
|
+
const body = patTarget.hash.startsWith("@")
|
|
653
|
+
? patTarget.hash.slice(1)
|
|
654
|
+
: patTarget.hash;
|
|
500
655
|
const paramStr =
|
|
501
|
-
|
|
502
|
-
? `[${
|
|
656
|
+
patTarget.params && patTarget.params.length > 0
|
|
657
|
+
? `[${patTarget.params.join(", ")}]`
|
|
503
658
|
: "[]";
|
|
504
|
-
const args =
|
|
659
|
+
const args = patTarget.aliasArgs || patTarget.params;
|
|
505
660
|
const argStr =
|
|
506
661
|
args && args.length > 0
|
|
507
662
|
? `[${args.join(", ")}]`
|
|
508
663
|
: paramStr;
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
664
|
+
if (!seenPat.has(localName)) {
|
|
665
|
+
lines.push(
|
|
666
|
+
`${localName}${paramStr} = @${body}${argStr};`
|
|
667
|
+
);
|
|
668
|
+
seenPat.add(localName);
|
|
669
|
+
}
|
|
512
670
|
emitted = true;
|
|
513
671
|
}
|
|
514
672
|
}
|
|
515
673
|
if (!emitted) {
|
|
516
674
|
throw new Error(
|
|
517
|
-
`--
|
|
675
|
+
`--use: '${rawLib}' not found in loaded libraries`
|
|
518
676
|
);
|
|
519
677
|
}
|
|
520
678
|
}
|
|
521
|
-
return lines.join("\n") + "\n";
|
|
679
|
+
return lines.length > 0 ? lines.join("\n") + "\n" : "";
|
|
522
680
|
}
|
|
523
681
|
|
|
524
682
|
const DEFAULT_MAX_PAT_LENGTH = 45;
|
|
@@ -773,7 +931,9 @@ export function canonicalBoundaryFilterSources(
|
|
|
773
931
|
export default {
|
|
774
932
|
resolvePatternCanonicalInfo,
|
|
775
933
|
canonicalPatternHash,
|
|
776
|
-
|
|
934
|
+
buildUsePreamble,
|
|
935
|
+
parseHeaderDirectives,
|
|
936
|
+
parseHeaderUses,
|
|
777
937
|
canonicalBoundaryFilterSources,
|
|
778
938
|
instantiateCanonicalFilter
|
|
779
939
|
};
|
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;
|