@fraczak/k 8.7.1 → 8.8.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/CODECS.md +42 -40
- package/DOCS/FILE_FORMATS.md +28 -25
- package/DOCS/GETTING_STARTED.md +10 -15
- package/DOCS/PATTERNS.md +3 -3
- package/DOCS/REPL.md +3 -6
- package/Examples/ieee-arithmetics.k +14 -29
- package/Examples/poly.k +18 -29
- package/README.md +14 -18
- package/backend-api.mjs +1 -3
- package/backends/llvm/src/cli.mjs +1 -2
- package/backends/llvm/tests/integration.sh +2 -6
- package/backends/wasm/tests/integration.sh +5 -10
- package/browser/shims/fs.mjs +2 -6
- package/canonical-pattern.mjs +9 -29
- package/compiler.mjs +22 -12
- package/harness/suites/poly.mjs +9 -11
- package/index.mjs +119 -50
- package/lib/djson.k +981 -295
- package/lib/generic.k +701 -0
- package/lib/ieee.k +662 -638
- package/lib/integer.k +542 -0
- package/object.mjs +19 -12
- package/objects/README.md +6 -6
- package/package.json +1 -1
- package/scripts/benchmark-idempotency.mjs +16 -17
- package/scripts/benchmark.mjs +39 -29
- package/scripts/build-repl-html.mjs +45 -35
- package/scripts/perf-ieee.mjs +1 -5
- package/scripts/perf-int.mjs +12 -27
- package/scripts/perf-multiplication.mjs +8 -9
- package/scripts/perf-poly.mjs +7 -10
- package/scripts/render-benchmark.mjs +1 -1
- package/scripts/run-tests.mjs +4 -2
- package/scripts/test-package.mjs +4 -10
- package/tests/README.md +4 -5
- package/tests/helpers/cli.mjs +30 -7
- package/tests/integration.sh +5 -10
- package/tests/test-arithmetics.mjs +23 -16
- package/tests/test-djson-methods.mjs +17 -25
- package/tests/test-djson.mjs +273 -0
- package/tests/test-generic.mjs +239 -0
- package/tests/test-ieee-arithmetic.mjs +1 -5
- package/tests/test-ieee.mjs +153 -0
- package/tests/test-integer.mjs +264 -0
- package/tests/test-k-object.mjs +12 -18
- package/tests/test-kvm-polymorphic.mjs +6 -7
- package/tests/test-lib.mjs +62 -52
- package/tests/test-library-isolation.mjs +349 -26
- package/tests/test-parse-json.mjs +15 -24
- package/tests/test-poly.mjs +21 -16
- package/tests/test-repl-html.mjs +39 -44
- package/tests/test-repl.mjs +46 -59
- package/DOCS/book-combined.md +0 -3786
- package/DOCS/dependency-graph.md +0 -144
- package/Examples/arithmetics.k +0 -108
- package/Examples/esc.k +0 -9
- package/lib/arithmetics.k +0 -286
- package/lib/bit.k +0 -68
- package/lib/bits.k +0 -227
- package/lib/btrie.k +0 -187
- package/lib/list.k +0 -49
- package/lib/string.k +0 -588
- package/tests/test-ieee-text.mjs +0 -74
- package/tests/test-karatsuba.mjs +0 -25
package/browser/shims/fs.mjs
CHANGED
|
@@ -60,12 +60,8 @@ export function getVfsFile(path) {
|
|
|
60
60
|
|
|
61
61
|
const CURATED_VFS_FILES = [
|
|
62
62
|
"core.k",
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"arithmetics.k",
|
|
66
|
-
"list.k",
|
|
67
|
-
"btrie.k",
|
|
68
|
-
"string.k",
|
|
63
|
+
"generic.k",
|
|
64
|
+
"integer.k",
|
|
69
65
|
"ieee.k",
|
|
70
66
|
"djson.k",
|
|
71
67
|
"ieee-arithmetics.k",
|
package/canonical-pattern.mjs
CHANGED
|
@@ -273,7 +273,7 @@ export function resolvePatternCanonicalInfo(name, pat, options = {}) {
|
|
|
273
273
|
}
|
|
274
274
|
const formalParams = pat.params || [];
|
|
275
275
|
const formalCount = formalParams.length;
|
|
276
|
-
const allPatterns = {
|
|
276
|
+
const allPatterns = {};
|
|
277
277
|
if (options.libraries) {
|
|
278
278
|
for (const lib of options.libraries) {
|
|
279
279
|
const lp =
|
|
@@ -281,9 +281,16 @@ export function resolvePatternCanonicalInfo(name, pat, options = {}) {
|
|
|
281
281
|
lib.defs?.patterns ||
|
|
282
282
|
lib.sourceDefs?.patterns ||
|
|
283
283
|
{};
|
|
284
|
-
Object.
|
|
284
|
+
for (const [patName, patDef] of Object.entries(lp)) {
|
|
285
|
+
if (patName.startsWith("@") && !(patName in allPatterns)) {
|
|
286
|
+
allPatterns[patName] = patDef;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
285
289
|
}
|
|
286
290
|
}
|
|
291
|
+
if (options.patterns) {
|
|
292
|
+
Object.assign(allPatterns, options.patterns);
|
|
293
|
+
}
|
|
287
294
|
if (name && pat) allPatterns[name] = pat;
|
|
288
295
|
|
|
289
296
|
let pathMap = {};
|
|
@@ -415,31 +422,6 @@ export function canonicalPatternHash(name, pat, options = {}) {
|
|
|
415
422
|
return resolvePatternCanonicalInfo(name, pat, options).hash;
|
|
416
423
|
}
|
|
417
424
|
|
|
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
425
|
function buildMapsForLibrary(lib) {
|
|
444
426
|
const relMap = {};
|
|
445
427
|
const patMap = {};
|
|
@@ -929,8 +911,6 @@ export default {
|
|
|
929
911
|
resolvePatternCanonicalInfo,
|
|
930
912
|
canonicalPatternHash,
|
|
931
913
|
buildUsePreamble,
|
|
932
|
-
parseHeaderDirectives,
|
|
933
|
-
parseHeaderUses,
|
|
934
914
|
canonicalBoundaryFilterSources,
|
|
935
915
|
instantiateCanonicalFilter
|
|
936
916
|
};
|
package/compiler.mjs
CHANGED
|
@@ -27,18 +27,28 @@ function compileTypes(representatives, rels, options = {}) {
|
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
30
|
+
if (options._useRelAlias) {
|
|
31
|
+
for (const [localName, relHash] of Object.entries(options._useRelAlias)) {
|
|
32
|
+
if (rels[localName]?._library) {
|
|
33
|
+
relAlias[localName] = relHash;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
const useSpecs = [...(options.useSpecs || options.use || [])];
|
|
38
|
+
for (const spec of useSpecs) {
|
|
39
|
+
const parts = spec.trim().split(/[\s,]+/);
|
|
40
|
+
for (const item of parts) {
|
|
41
|
+
if (!item) continue;
|
|
42
|
+
const [rawLib, rawLocal] = item.includes(":") ? item.split(":", 2) : [item, item];
|
|
43
|
+
if (/\[.*\]$/.test(rawLib.trim()) || /\[.*\]$/.test(rawLocal.trim())) continue;
|
|
44
|
+
const libName = rawLib.replace(/\[.*\]$/, "").trim();
|
|
45
|
+
const localName = rawLocal.replace(/\[.*\]$/, "").trim();
|
|
46
|
+
if (!rels[localName]?._library) continue;
|
|
47
|
+
for (const lib of options.libraries) {
|
|
48
|
+
const relHash = lib.relAlias?.[libName] || (libName.startsWith("@") ? libName : null);
|
|
49
|
+
if (relHash) {
|
|
50
|
+
relAlias[localName] = relHash;
|
|
51
|
+
}
|
|
42
52
|
}
|
|
43
53
|
}
|
|
44
54
|
}
|
package/harness/suites/poly.mjs
CHANGED
|
@@ -10,7 +10,6 @@ import {
|
|
|
10
10
|
lowerToKVM,
|
|
11
11
|
run,
|
|
12
12
|
run_converged,
|
|
13
|
-
valueForCode,
|
|
14
13
|
Value
|
|
15
14
|
} from "../../backend-api.mjs";
|
|
16
15
|
import { compileLibrary, loadLibrary, decodeObject, compileObjectBuffer } from "../../object.mjs";
|
|
@@ -19,24 +18,23 @@ import { BenchmarkSuite } from "../suite.mjs";
|
|
|
19
18
|
import { inputPatternForObjectRelation } from "../../backends/llvm/src/executable.mjs";
|
|
20
19
|
import { prepareRelation } from "../../backends/llvm/tests/perf-support.mjs";
|
|
21
20
|
|
|
21
|
+
const POLY_USE_SPECS = ["int[] nat int pos? neg? zero? gt? inc dec"];
|
|
22
|
+
|
|
22
23
|
export async function createPolySuite({ listLength = 40, cacheDir = null } = {}) {
|
|
23
24
|
const ops = ["reverse", "concat", "split_by", "get_nth", "length"];
|
|
24
25
|
const benchNames = Object.fromEntries(ops.map(op => [op, `bench_${op}`]));
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
source: filePath,
|
|
31
|
-
libraries: depLib ? [depLib] : []
|
|
32
|
-
}));
|
|
33
|
-
}
|
|
27
|
+
const integerPath = fileURLToPath(import.meta.resolve("../../lib/integer.k"));
|
|
28
|
+
const depLib = loadLibrary(compileLibrary(fs.readFileSync(integerPath, "utf8"), {
|
|
29
|
+
source: integerPath
|
|
30
|
+
}));
|
|
34
31
|
|
|
35
32
|
const polyPath = fileURLToPath(import.meta.resolve("../../Examples/poly.k"));
|
|
36
33
|
const polySource = fs.readFileSync(polyPath, "utf8");
|
|
37
34
|
const polyLib = loadLibrary(compileLibrary(polySource, {
|
|
38
35
|
source: polyPath,
|
|
39
|
-
libraries: [depLib]
|
|
36
|
+
libraries: [depLib],
|
|
37
|
+
use: POLY_USE_SPECS
|
|
40
38
|
}));
|
|
41
39
|
|
|
42
40
|
const state = {
|
|
@@ -60,7 +58,7 @@ export async function createPolySuite({ listLength = 40, cacheDir = null } = {})
|
|
|
60
58
|
}
|
|
61
59
|
|
|
62
60
|
function intValue(text) {
|
|
63
|
-
return
|
|
61
|
+
return parseIntValue(text);
|
|
64
62
|
}
|
|
65
63
|
|
|
66
64
|
function makeList(elements) {
|
package/index.mjs
CHANGED
|
@@ -35,14 +35,97 @@ function runScriptOnData(script, data, options = {}) {
|
|
|
35
35
|
runScriptOnData.doc = "Run 'script' (string) on 'data': (script,data) -> data";
|
|
36
36
|
|
|
37
37
|
|
|
38
|
+
function resolveUseImports(useSpecs, libraries) {
|
|
39
|
+
const importedPatterns = new Map();
|
|
40
|
+
const importedRels = new Map();
|
|
41
|
+
if (!libraries || useSpecs.length === 0) {
|
|
42
|
+
return { importedPatterns, importedRels };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
for (const spec of useSpecs) {
|
|
46
|
+
const parts = spec.trim().split(/[\s,]+/);
|
|
47
|
+
for (const item of parts) {
|
|
48
|
+
if (!item) continue;
|
|
49
|
+
const [rawLib, rawLocal] = item.includes(":") ? item.split(":", 2) : [item, item];
|
|
50
|
+
const hasExplicitBrackets =
|
|
51
|
+
/\[.*\]$/.test(rawLib.trim()) || /\[.*\]$/.test(rawLocal.trim());
|
|
52
|
+
const libName = rawLib.replace(/\[.*\]$/, "").trim();
|
|
53
|
+
const localName = rawLocal.replace(/\[.*\]$/, "").trim();
|
|
54
|
+
|
|
55
|
+
let matchedRel = null;
|
|
56
|
+
if (!hasExplicitBrackets && libName !== "__main__" && localName !== "__main__") {
|
|
57
|
+
for (const lib of libraries) {
|
|
58
|
+
const libRels = lib.rels || lib.defs?.rels || {};
|
|
59
|
+
const relHash = lib.relAlias?.[libName] || (libName.startsWith("@") ? libName : null);
|
|
60
|
+
const rel = (relHash && libRels[relHash]) || libRels[libName];
|
|
61
|
+
if (!rel) continue;
|
|
62
|
+
const resolvedHash = relHash || libName;
|
|
63
|
+
if (matchedRel && matchedRel.hash && resolvedHash && matchedRel.hash !== resolvedHash) {
|
|
64
|
+
console.warn(
|
|
65
|
+
`WARNING: Relation '${localName}' declared by --use is redefined; later definition wins.`
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
matchedRel = { hash: resolvedHash, rel };
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (matchedRel) {
|
|
73
|
+
if (importedRels.has(localName)) {
|
|
74
|
+
console.warn(
|
|
75
|
+
`WARNING: Relation '${localName}' declared by --use is redefined; later definition wins.`
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
importedRels.set(localName, matchedRel);
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
let matchedPat = null;
|
|
83
|
+
for (const lib of libraries) {
|
|
84
|
+
const libPatterns = lib.patterns || lib.defs?.patterns || lib.sourceDefs?.patterns || {};
|
|
85
|
+
const patHash = lib.patternAlias?.[libName] || (libName.startsWith("@") ? libName : null);
|
|
86
|
+
const pat = (patHash && libPatterns[patHash]) || libPatterns[libName];
|
|
87
|
+
if (!pat) continue;
|
|
88
|
+
const resolvedHash = patHash || pat._canonical || null;
|
|
89
|
+
if (
|
|
90
|
+
matchedPat &&
|
|
91
|
+
((matchedPat.hash && resolvedHash && matchedPat.hash !== resolvedHash) ||
|
|
92
|
+
(!matchedPat.hash && matchedPat.pat !== pat))
|
|
93
|
+
) {
|
|
94
|
+
console.warn(
|
|
95
|
+
`WARNING: Pattern '${localName}[]' declared by --use is redefined; later definition wins.`
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
matchedPat = { hash: resolvedHash, pat };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (matchedPat) {
|
|
102
|
+
if (importedPatterns.has(localName)) {
|
|
103
|
+
console.warn(
|
|
104
|
+
`WARNING: Pattern '${localName}[]' declared by --use is redefined; later definition wins.`
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
importedPatterns.set(localName, matchedPat);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return { importedPatterns, importedRels };
|
|
113
|
+
}
|
|
114
|
+
|
|
38
115
|
function annotate(script, options = {}) {
|
|
39
116
|
const { defs, exp } = parse(script);
|
|
40
117
|
|
|
41
118
|
const representatives = codes.register(defs.codes);
|
|
42
|
-
const
|
|
119
|
+
const localPatterns = { ...(defs.patterns || {}) };
|
|
120
|
+
const localRels = { ...(defs.rels || {}) };
|
|
121
|
+
const useSpecs = [...(options.useSpecs || options.use || [])];
|
|
122
|
+
const { importedPatterns, importedRels } = resolveUseImports(
|
|
123
|
+
useSpecs,
|
|
124
|
+
options.libraries
|
|
125
|
+
);
|
|
43
126
|
|
|
44
|
-
// 1. Inject library patterns
|
|
45
|
-
|
|
127
|
+
// 1. Inject library @hash patterns; friendly names only come from --use or local definitions.
|
|
128
|
+
defs.patterns = {};
|
|
46
129
|
if (options.libraries) {
|
|
47
130
|
for (const lib of options.libraries) {
|
|
48
131
|
const libPatterns =
|
|
@@ -51,44 +134,30 @@ function annotate(script, options = {}) {
|
|
|
51
134
|
lib.sourceDefs?.patterns ||
|
|
52
135
|
{};
|
|
53
136
|
for (const [name, pat] of Object.entries(libPatterns)) {
|
|
54
|
-
if (name.startsWith("@") && !(name in
|
|
55
|
-
if (!defs.patterns) defs.patterns = {};
|
|
137
|
+
if (name.startsWith("@") && !(name in defs.patterns)) {
|
|
56
138
|
defs.patterns[name] = pat;
|
|
57
139
|
}
|
|
58
140
|
}
|
|
59
141
|
}
|
|
60
142
|
}
|
|
61
143
|
|
|
62
|
-
// 2.
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const localName = rawLocal.replace(/\[.*\]$/, "").trim();
|
|
72
|
-
for (const lib of options.libraries) {
|
|
73
|
-
const libPatterns = lib.patterns || lib.defs?.patterns || lib.sourceDefs?.patterns || {};
|
|
74
|
-
const patHash = lib.patternAlias?.[libName] || (libName.startsWith("@") ? libName : null);
|
|
75
|
-
const pat = (patHash && libPatterns[patHash]) || libPatterns[libName];
|
|
76
|
-
if (pat) {
|
|
77
|
-
if (!defs.patterns) defs.patterns = {};
|
|
78
|
-
if (!(localName in defs.patterns)) {
|
|
79
|
-
defs.patterns[localName] = pat;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
144
|
+
// 2. Apply --use pattern aliases, then local pattern definitions (later wins with warning on collision).
|
|
145
|
+
for (const [localName, { pat }] of importedPatterns.entries()) {
|
|
146
|
+
defs.patterns[localName] = pat;
|
|
147
|
+
}
|
|
148
|
+
for (const [name, pat] of Object.entries(localPatterns)) {
|
|
149
|
+
if (!name.startsWith("@") && importedPatterns.has(name)) {
|
|
150
|
+
console.warn(
|
|
151
|
+
`WARNING: Pattern '${name}[]' declared by --use is redefined by local definition; later definition wins.`
|
|
152
|
+
);
|
|
84
153
|
}
|
|
154
|
+
defs.patterns[name] = pat;
|
|
85
155
|
}
|
|
86
156
|
|
|
87
|
-
validatePatternDefinitions(defs.patterns
|
|
88
|
-
expandPatternsInRels(rels, defs.patterns || {}, options);
|
|
157
|
+
validatePatternDefinitions(defs.patterns, options);
|
|
89
158
|
|
|
90
|
-
// 3. Inject library relations
|
|
91
|
-
|
|
159
|
+
// 3. Inject library @hash relations, then --use relation aliases, then local relations.
|
|
160
|
+
const rels = {};
|
|
92
161
|
if (options.libraries) {
|
|
93
162
|
for (const lib of options.libraries) {
|
|
94
163
|
const libRels = lib.rels || lib.defs?.rels || {};
|
|
@@ -100,28 +169,28 @@ function annotate(script, options = {}) {
|
|
|
100
169
|
}
|
|
101
170
|
}
|
|
102
171
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const relHash = lib.relAlias?.[libName] || (libName.startsWith("@") ? libName : null);
|
|
115
|
-
const rel = (relHash && libRels[relHash]) || libRels[libName];
|
|
116
|
-
if (rel && !(localName in rels)) {
|
|
117
|
-
rels[localName] = { ...rel, _library: true };
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}
|
|
172
|
+
const useRelHashes = {};
|
|
173
|
+
for (const [localName, { hash, rel }] of importedRels.entries()) {
|
|
174
|
+
rels[localName] = { ...rel, _library: true };
|
|
175
|
+
if (hash) useRelHashes[localName] = hash;
|
|
176
|
+
}
|
|
177
|
+
for (const [name, rel] of Object.entries(localRels)) {
|
|
178
|
+
if (!name.startsWith("@") && importedRels.has(name)) {
|
|
179
|
+
console.warn(
|
|
180
|
+
`WARNING: Relation '${name}' declared by --use is redefined by local definition; later definition wins.`
|
|
181
|
+
);
|
|
182
|
+
delete useRelHashes[name];
|
|
121
183
|
}
|
|
184
|
+
rels[name] = rel;
|
|
122
185
|
}
|
|
186
|
+
rels.__main__ = { def: exp };
|
|
187
|
+
|
|
188
|
+
expandPatternsInRels(rels, defs.patterns, options);
|
|
123
189
|
|
|
124
|
-
const { relAlias, compileStats } = compileTypes(representatives, rels,
|
|
190
|
+
const { relAlias, compileStats } = compileTypes(representatives, rels, {
|
|
191
|
+
...options,
|
|
192
|
+
_useRelAlias: useRelHashes
|
|
193
|
+
});
|
|
125
194
|
|
|
126
195
|
return {rels, representatives, relAlias, compileStats, sourceDefs: defs};
|
|
127
196
|
}
|