@fraczak/k 8.6.0 → 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 +44 -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 +15 -8
- 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 +351 -82
- 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 +52 -10
- package/objects/README.md +10 -3
- package/objects/compile.mjs +10 -8
- package/package.json +1 -1
- package/repl.mjs +227 -22
- package/scripts/benchmark-idempotency.mjs +22 -15
- package/scripts/benchmark.mjs +29 -10
- package/scripts/build-repl-html.mjs +49 -14
- 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 +154 -54
- 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
package/scripts/benchmark.mjs
CHANGED
|
@@ -159,15 +159,32 @@ function sourceNameForSuite(suite) {
|
|
|
159
159
|
}
|
|
160
160
|
|
|
161
161
|
let ieeeDependencies;
|
|
162
|
+
let arithDependencies;
|
|
162
163
|
function dependenciesForSuite(suite) {
|
|
163
|
-
if (suite
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
164
|
+
if (suite === "ieee") {
|
|
165
|
+
if (!ieeeDependencies) {
|
|
166
|
+
let prev = null;
|
|
167
|
+
for (const f of ["lib/bit.k", "lib/bits.k", "lib/list.k", "lib/btrie.k", "lib/string.k", "lib/ieee.k"]) {
|
|
168
|
+
prev = loadLibrary(compileLibrary(fs.readFileSync(path.join(kRoot, f), "utf8"), {
|
|
169
|
+
source: f,
|
|
170
|
+
libraries: prev ? [prev] : []
|
|
171
|
+
}));
|
|
172
|
+
}
|
|
173
|
+
ieeeDependencies = [prev];
|
|
174
|
+
}
|
|
175
|
+
return ieeeDependencies;
|
|
169
176
|
}
|
|
170
|
-
|
|
177
|
+
if (!arithDependencies) {
|
|
178
|
+
let prev = null;
|
|
179
|
+
for (const f of ["lib/bit.k", "lib/bits.k", "lib/arithmetics.k"]) {
|
|
180
|
+
prev = loadLibrary(compileLibrary(fs.readFileSync(path.join(kRoot, f), "utf8"), {
|
|
181
|
+
source: f,
|
|
182
|
+
libraries: prev ? [prev] : []
|
|
183
|
+
}));
|
|
184
|
+
}
|
|
185
|
+
arithDependencies = [prev];
|
|
186
|
+
}
|
|
187
|
+
return arithDependencies;
|
|
171
188
|
}
|
|
172
189
|
|
|
173
190
|
function stripTrailingUnitMain(source) {
|
|
@@ -472,9 +489,11 @@ async function prepareBackendRunner(job, relation, inputVal, state) {
|
|
|
472
489
|
|
|
473
490
|
async function prepareState(suite) {
|
|
474
491
|
const state = createState();
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
492
|
+
const deps = suite === "ieee"
|
|
493
|
+
? ["lib/bit.k", "lib/bits.k", "lib/list.k", "lib/btrie.k", "lib/string.k", "lib/ieee.k"]
|
|
494
|
+
: ["lib/bit.k", "lib/bits.k", "lib/arithmetics.k"];
|
|
495
|
+
for (const dep of deps) {
|
|
496
|
+
await evaluateInput(`:load ${path.join(kRoot, dep)}`, state);
|
|
478
497
|
}
|
|
479
498
|
await evaluateInput(`:load ${suiteSourcePath(suite)}`, state);
|
|
480
499
|
return state;
|
|
@@ -78,21 +78,41 @@ if (fs.existsSync(coreKPath)) {
|
|
|
78
78
|
vfsFiles["core.k"] = fs.readFileSync(coreKPath, "utf8");
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
// Add verified k-code files
|
|
82
|
-
const
|
|
81
|
+
// Add verified lib/ k-code files
|
|
82
|
+
const verifiedLibs = [
|
|
83
|
+
"bit.k",
|
|
84
|
+
"bits.k",
|
|
83
85
|
"arithmetics.k",
|
|
86
|
+
"list.k",
|
|
87
|
+
"btrie.k",
|
|
88
|
+
"string.k",
|
|
84
89
|
"ieee.k",
|
|
85
|
-
"ieee-arithmetics.k",
|
|
86
|
-
"poly.k",
|
|
87
|
-
"karatsuba-mult.k",
|
|
88
90
|
"djson.k"
|
|
89
91
|
];
|
|
92
|
+
const libDir = path.join(projectRoot, "lib");
|
|
93
|
+
for (const filename of verifiedLibs) {
|
|
94
|
+
const fullPath = path.join(libDir, filename);
|
|
95
|
+
if (fs.existsSync(fullPath) && fs.statSync(fullPath).isFile()) {
|
|
96
|
+
const content = fs.readFileSync(fullPath, "utf8");
|
|
97
|
+
vfsFiles[filename] = content;
|
|
98
|
+
vfsFiles[`lib/${filename}`] = content;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Add verified Examples/ k-code files
|
|
103
|
+
const verifiedExamples = [
|
|
104
|
+
"arithmetics.k",
|
|
105
|
+
"ieee-arithmetics.k",
|
|
106
|
+
"poly.k"
|
|
107
|
+
];
|
|
90
108
|
const examplesDir = path.join(projectRoot, "Examples");
|
|
91
109
|
for (const filename of verifiedExamples) {
|
|
92
110
|
const fullPath = path.join(examplesDir, filename);
|
|
93
111
|
if (fs.existsSync(fullPath) && fs.statSync(fullPath).isFile()) {
|
|
94
112
|
const content = fs.readFileSync(fullPath, "utf8");
|
|
95
|
-
|
|
113
|
+
if (!(filename in vfsFiles)) {
|
|
114
|
+
vfsFiles[filename] = content;
|
|
115
|
+
}
|
|
96
116
|
vfsFiles[`Examples/${filename}`] = content;
|
|
97
117
|
}
|
|
98
118
|
}
|
|
@@ -1688,14 +1708,22 @@ const htmlContent = `<!DOCTYPE html>
|
|
|
1688
1708
|
<div class="control-group">
|
|
1689
1709
|
<select id="example-select" title="Choose an example or expression">
|
|
1690
1710
|
<option value="">-- Example / Expression --</option>
|
|
1691
|
-
<optgroup label="Standard
|
|
1711
|
+
<optgroup label="Standard Libraries (lib/)">
|
|
1712
|
+
<option value="lib/bit.k">lib/bit.k</option>
|
|
1713
|
+
<option value="lib/bits.k">lib/bits.k</option>
|
|
1714
|
+
<option value="lib/arithmetics.k">lib/arithmetics.k</option>
|
|
1715
|
+
<option value="lib/list.k">lib/list.k</option>
|
|
1716
|
+
<option value="lib/btrie.k">lib/btrie.k</option>
|
|
1717
|
+
<option value="lib/string.k">lib/string.k</option>
|
|
1718
|
+
<option value="lib/ieee.k">lib/ieee.k</option>
|
|
1719
|
+
<option value="lib/djson.k">lib/djson.k</option>
|
|
1720
|
+
</optgroup>
|
|
1721
|
+
<optgroup label="Examples">
|
|
1692
1722
|
<option value="core.k">core.k</option>
|
|
1693
|
-
<option value="
|
|
1694
|
-
<option value="
|
|
1695
|
-
<option value="
|
|
1696
|
-
<option value="
|
|
1697
|
-
<option value="karatsuba-mult.k">karatsuba-mult.k</option>
|
|
1698
|
-
<option value="djson.k">djson.k</option>
|
|
1723
|
+
<option value="Examples/nat.k">Examples/nat.k</option>
|
|
1724
|
+
<option value="Examples/arithmetics.k">Examples/arithmetics.k</option>
|
|
1725
|
+
<option value="Examples/poly.k">Examples/poly.k</option>
|
|
1726
|
+
<option value="Examples/ieee-arithmetics.k">Examples/ieee-arithmetics.k</option>
|
|
1699
1727
|
</optgroup>
|
|
1700
1728
|
<optgroup label="Quick Expressions">
|
|
1701
1729
|
<option value="expr:{} | ok">{} | ok</option>
|
|
@@ -1936,10 +1964,17 @@ const htmlContent = `<!DOCTYPE html>
|
|
|
1936
1964
|
(alias <code>:pats</code>)
|
|
1937
1965
|
</td>
|
|
1938
1966
|
</tr>
|
|
1967
|
+
<tr>
|
|
1968
|
+
<td><code>:all-patterns</code></td>
|
|
1969
|
+
<td>
|
|
1970
|
+
List canonical patterns keyed by hash with their aliases
|
|
1971
|
+
(alias <code>:all-pat</code>)
|
|
1972
|
+
</td>
|
|
1973
|
+
</tr>
|
|
1939
1974
|
<tr><td><code>:rel <name></code></td><td>Display definition of relation <code><name></code></td></tr>
|
|
1940
1975
|
<tr><td><code>:rels</code></td><td>List all relation aliases</td></tr>
|
|
1941
1976
|
<tr><td><code>:limit [num|none]</code></td><td>Display or set maximum characters for value output</td></tr>
|
|
1942
|
-
<tr><td><code>:max-pat-length [num|none]</code></td><td>Display or set max inline pattern length in <code>:rel</code> (default <code>
|
|
1977
|
+
<tr><td><code>:max-pat-length [num|none]</code></td><td>Display or set max inline pattern length in <code>:rel</code> (default <code>45</code>, alias <code>:maxPatLength</code>)</td></tr>
|
|
1943
1978
|
<tr><td><code>:codecs</code></td><td>List all loaded format codecs</td></tr>
|
|
1944
1979
|
<tr><td><code>:codec load <file.mjs></code></td><td>Load a codec from file (or load through Files)</td></tr>
|
|
1945
1980
|
<tr><td><code>:codec unload <file.mjs></code></td><td>Unload a loaded codec</td></tr>
|
package/scripts/perf-ieee.mjs
CHANGED
|
@@ -223,7 +223,15 @@ function pkirLaneName() {
|
|
|
223
223
|
|
|
224
224
|
console.log("==> Initializing IEEE types and arithmetic");
|
|
225
225
|
const state = createState();
|
|
226
|
-
for (const name of [
|
|
226
|
+
for (const name of [
|
|
227
|
+
"lib/bit.k",
|
|
228
|
+
"lib/bits.k",
|
|
229
|
+
"lib/list.k",
|
|
230
|
+
"lib/btrie.k",
|
|
231
|
+
"lib/string.k",
|
|
232
|
+
"lib/ieee.k",
|
|
233
|
+
"Examples/ieee-arithmetics.k"
|
|
234
|
+
]) {
|
|
227
235
|
await evaluateInput(`:load ${fileURLToPath(import.meta.resolve(`@fraczak/k/${name}`))}`, state);
|
|
228
236
|
}
|
|
229
237
|
|
package/scripts/perf-int.mjs
CHANGED
|
@@ -202,10 +202,11 @@ function pkirLaneName() {
|
|
|
202
202
|
return "PKIR (Wasm persistent)";
|
|
203
203
|
}
|
|
204
204
|
|
|
205
|
-
console.log("==> Initializing state and loading @fraczak/k/
|
|
205
|
+
console.log("==> Initializing state and loading @fraczak/k/lib/arithmetics.k");
|
|
206
206
|
const state = createState();
|
|
207
|
-
const
|
|
208
|
-
await evaluateInput(`:load ${
|
|
207
|
+
for (const relPath of ["lib/bit.k", "lib/bits.k", "lib/arithmetics.k"]) {
|
|
208
|
+
await evaluateInput(`:load ${fileURLToPath(import.meta.resolve(`@fraczak/k/${relPath}`))}`, state);
|
|
209
|
+
}
|
|
209
210
|
|
|
210
211
|
for (const op of ops) {
|
|
211
212
|
await evaluateInput(`${benchNames[op]} = {inc x, () y} ${op};`, state);
|
|
@@ -217,13 +218,10 @@ function intValue(text) {
|
|
|
217
218
|
|
|
218
219
|
console.log("==> Preparing relations");
|
|
219
220
|
const relations = {};
|
|
220
|
-
const arithmeticsSource = fs.readFileSync(arithmeticsPath, "utf8");
|
|
221
|
-
const arithmeticsLibrarySource = arithmeticsSource.replace(/\s*\(\)\s*$/, "\n");
|
|
222
221
|
for (const op of ops) {
|
|
223
222
|
const name = benchNames[op];
|
|
224
223
|
relations[op] = prepareRelation(state, name, {
|
|
225
|
-
|
|
226
|
-
sourceLabel: `@fraczak/k/Examples/arithmetics.k#${name}`
|
|
224
|
+
sourceLabel: `@fraczak/k/lib/arithmetics.k#${name}`
|
|
227
225
|
});
|
|
228
226
|
}
|
|
229
227
|
codes.load(state.codes);
|
|
@@ -776,12 +774,10 @@ const squaringStepTimeoutMs = parsePositiveIntEnv("SQUARING_STEP_TIMEOUT_MS", 60
|
|
|
776
774
|
console.log("\n=================== SQUARING SCALE & MEMORY BENCHMARK ===================");
|
|
777
775
|
console.log(`Algorithms: times vs karatsuba (${squaringSteps} successive squarings doubling bit length)`);
|
|
778
776
|
|
|
779
|
-
// Load and initialize Karatsuba multiplication relation
|
|
780
|
-
const karatsubaPath = fileURLToPath(import.meta.resolve("@fraczak/k/Examples/karatsuba-mult.k"));
|
|
781
|
-
const karatsubaSource = fs.readFileSync(karatsubaPath, "utf8");
|
|
782
|
-
const karatsubaLibrarySource = karatsubaSource.replace(/\s*\(\)\s*$/, "\n");
|
|
783
777
|
const stateKara = createState();
|
|
784
|
-
|
|
778
|
+
for (const relPath of ["lib/bit.k", "lib/bits.k", "lib/arithmetics.k"]) {
|
|
779
|
+
await evaluateInput(`:load ${fileURLToPath(import.meta.resolve(`@fraczak/k/${relPath}`))}`, stateKara);
|
|
780
|
+
}
|
|
785
781
|
codes.load(stateKara.codes);
|
|
786
782
|
|
|
787
783
|
const sInitialVal = intValue(squaringInputText);
|
|
@@ -793,8 +789,7 @@ const squaringAlgos = [
|
|
|
793
789
|
name: "times",
|
|
794
790
|
title: "Long Multiplication (times)",
|
|
795
791
|
expr: "s = {()x,()y}times;",
|
|
796
|
-
|
|
797
|
-
sourceLabel: "@fraczak/k/Examples/arithmetics.k#s",
|
|
792
|
+
sourceLabel: "@fraczak/k/lib/arithmetics.k#s",
|
|
798
793
|
state: state,
|
|
799
794
|
relName: "s"
|
|
800
795
|
},
|
|
@@ -803,8 +798,7 @@ const squaringAlgos = [
|
|
|
803
798
|
name: "karatsuba",
|
|
804
799
|
title: "Karatsuba Multiplication (karatsuba)",
|
|
805
800
|
expr: "s = {()x,()y}karatsuba;",
|
|
806
|
-
|
|
807
|
-
sourceLabel: "@fraczak/k/Examples/karatsuba-mult.k#s",
|
|
801
|
+
sourceLabel: "@fraczak/k/lib/arithmetics.k#s",
|
|
808
802
|
state: stateKara,
|
|
809
803
|
relName: "s"
|
|
810
804
|
}
|
|
@@ -815,7 +809,6 @@ for (const algo of squaringAlgos) {
|
|
|
815
809
|
await evaluateInput(algo.expr, algo.state);
|
|
816
810
|
codes.load(algo.state.codes);
|
|
817
811
|
algo.rel = prepareRelation(algo.state, algo.relName, {
|
|
818
|
-
source: `${algo.librarySource}\n${algo.expr}\n${algo.relName}`,
|
|
819
812
|
sourceLabel: algo.sourceLabel
|
|
820
813
|
});
|
|
821
814
|
|
|
@@ -231,14 +231,11 @@ console.log(
|
|
|
231
231
|
console.log("-".repeat(72) + "\n");
|
|
232
232
|
|
|
233
233
|
// 1. Map algorithms to their source files
|
|
234
|
-
const
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
)
|
|
238
|
-
|
|
239
|
-
import.meta.resolve("../Examples/karatsuba-mult.k")
|
|
240
|
-
)
|
|
241
|
-
};
|
|
234
|
+
const arithLibChain = [
|
|
235
|
+
fileURLToPath(import.meta.resolve("../lib/bit.k")),
|
|
236
|
+
fileURLToPath(import.meta.resolve("../lib/bits.k")),
|
|
237
|
+
fileURLToPath(import.meta.resolve("../lib/arithmetics.k"))
|
|
238
|
+
];
|
|
242
239
|
|
|
243
240
|
const cacheDir = makeCacheDir("k-mult-perf-");
|
|
244
241
|
|
|
@@ -250,12 +247,12 @@ const algoServers = { llvm: {}, "llvm-pkir": {}, wasm: {}, pkir: {} };
|
|
|
250
247
|
let commonState = null;
|
|
251
248
|
|
|
252
249
|
for (const algo of selectedAlgos) {
|
|
253
|
-
const filePath =
|
|
254
|
-
const rawSource = fs.readFileSync(filePath, "utf8");
|
|
255
|
-
const libSource = rawSource.replace(/\s*\(\)\s*$/, "\n");
|
|
250
|
+
const filePath = arithLibChain[arithLibChain.length - 1];
|
|
256
251
|
|
|
257
252
|
const algoState = createState();
|
|
258
|
-
|
|
253
|
+
for (const libPath of arithLibChain) {
|
|
254
|
+
await evaluateInput(`:load ${libPath}`, algoState);
|
|
255
|
+
}
|
|
259
256
|
codes.load(algoState.codes);
|
|
260
257
|
|
|
261
258
|
if (!commonState) commonState = algoState;
|
|
@@ -264,10 +261,7 @@ for (const algo of selectedAlgos) {
|
|
|
264
261
|
await evaluateInput(`${relName} = {()x, ()y} ${algo};`, algoState);
|
|
265
262
|
codes.load(algoState.codes);
|
|
266
263
|
|
|
267
|
-
const relSource =
|
|
268
|
-
`${libSource}\n${relName} = {()x, ()y} ${algo};\n${relName}`;
|
|
269
264
|
const prepared = prepareRelation(algoState, relName, {
|
|
270
|
-
source: relSource,
|
|
271
265
|
sourceLabel: `${filePath}#${relName}`
|
|
272
266
|
});
|
|
273
267
|
prepared.state = algoState;
|
package/scripts/perf-poly.mjs
CHANGED
|
@@ -193,48 +193,32 @@ function pkirLaneName() {
|
|
|
193
193
|
}
|
|
194
194
|
|
|
195
195
|
|
|
196
|
-
console.log("==> Loading @fraczak/k/
|
|
197
|
-
|
|
198
|
-
const
|
|
199
|
-
const
|
|
196
|
+
console.log("==> Loading @fraczak/k/lib/{bit,bits,arithmetics,list}.k as libraries");
|
|
197
|
+
let depLib = null;
|
|
198
|
+
for (const relPath of ["lib/bit.k", "lib/bits.k", "lib/arithmetics.k", "lib/list.k"]) {
|
|
199
|
+
const filePath = fileURLToPath(import.meta.resolve(`@fraczak/k/${relPath}`));
|
|
200
|
+
depLib = loadLibrary(compileLibrary(fs.readFileSync(filePath, "utf8"), {
|
|
201
|
+
source: filePath,
|
|
202
|
+
libraries: depLib ? [depLib] : []
|
|
203
|
+
}));
|
|
204
|
+
}
|
|
200
205
|
|
|
201
|
-
console.log("==> Compiling @fraczak/k/Examples/poly.k with
|
|
206
|
+
console.log("==> Compiling @fraczak/k/Examples/poly.k with library dependencies");
|
|
202
207
|
const polyPath = fileURLToPath(import.meta.resolve("@fraczak/k/Examples/poly.k"));
|
|
203
208
|
const polySource = fs.readFileSync(polyPath, "utf8");
|
|
204
|
-
|
|
205
|
-
function buildExportPreamble(exports, libraries) {
|
|
206
|
-
const aliasMap = {};
|
|
207
|
-
for (const lib of libraries) {
|
|
208
|
-
for (const [name, hash] of Object.entries(lib.relAlias || {})) {
|
|
209
|
-
if (name !== "__main__") aliasMap[name] = hash;
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
const lines = [];
|
|
213
|
-
for (const spec of exports) {
|
|
214
|
-
const [libName, localName] = spec.includes(":") ? spec.split(":", 2) : [spec, spec];
|
|
215
|
-
const hash = aliasMap[libName];
|
|
216
|
-
if (!hash) throw new Error(`--export: '${libName}' not found in loaded libraries`);
|
|
217
|
-
const body = hash.startsWith("@") ? hash.slice(1) : hash;
|
|
218
|
-
lines.push(`${localName} = @${body};`);
|
|
219
|
-
}
|
|
220
|
-
return lines.join("\n") + "\n";
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
const neededExports = ["0", "int", "inc", "dec", "nat", "zero_int?", "neg?", "GT"];
|
|
224
|
-
const exportPreamble = buildExportPreamble(neededExports, [arithmeticsLib]);
|
|
225
|
-
const polyLib = loadLibrary(compileLibrary(exportPreamble + polySource, {
|
|
209
|
+
const polyLib = loadLibrary(compileLibrary(polySource, {
|
|
226
210
|
source: polyPath,
|
|
227
|
-
libraries: [
|
|
211
|
+
libraries: [depLib]
|
|
228
212
|
}));
|
|
229
213
|
|
|
230
214
|
const state = {
|
|
231
|
-
codes: { ...
|
|
232
|
-
rels: { ...
|
|
215
|
+
codes: { ...depLib.codes, ...polyLib.codes },
|
|
216
|
+
rels: { ...depLib.rels, ...polyLib.rels },
|
|
233
217
|
typeAliases: {},
|
|
234
218
|
relAliases: { ...polyLib.relAlias },
|
|
235
|
-
meta: { ...
|
|
219
|
+
meta: { ...depLib.meta, ...polyLib.meta }
|
|
236
220
|
};
|
|
237
|
-
for (const [hash, entry] of Object.entries(
|
|
221
|
+
for (const [hash, entry] of Object.entries(depLib.meta)) {
|
|
238
222
|
if (entry.type === "code") {
|
|
239
223
|
for (const origin of entry.origins || []) {
|
|
240
224
|
if (origin.name) state.typeAliases[origin.name] = hash;
|
package/scripts/run-tests.mjs
CHANGED
|
@@ -43,11 +43,11 @@ const tests = [
|
|
|
43
43
|
["tests/test-hash-normalization.mjs", node, ["tests/test-hash-normalization.mjs"]],
|
|
44
44
|
["tests/test-structural-values.mjs", node, ["tests/test-structural-values.mjs"]],
|
|
45
45
|
["tests/test-codecs.mjs", node, ["tests/test-codecs.mjs"]],
|
|
46
|
-
[
|
|
47
|
-
"tests/test-djson-methods.mjs",
|
|
46
|
+
["tests/test-djson-methods.mjs",
|
|
48
47
|
node,
|
|
49
48
|
["tests/test-djson-methods.mjs"]
|
|
50
49
|
],
|
|
50
|
+
["tests/test-lib.mjs", node, ["tests/test-lib.mjs"]],
|
|
51
51
|
["tests/test-parse-json.mjs", node, ["tests/test-parse-json.mjs"]],
|
|
52
52
|
["tests/test-ieee-text.mjs", node, ["tests/test-ieee-text.mjs"]],
|
|
53
53
|
["tests/test-deep-wire-value.mjs", node, ["tests/test-deep-wire-value.mjs"]],
|
package/scripts/test-package.mjs
CHANGED
|
@@ -45,8 +45,14 @@ try {
|
|
|
45
45
|
assert.equal(run("k-print", [], hello).toString().trim(), '"ok"');
|
|
46
46
|
|
|
47
47
|
const integer = run("k-int", ["--parse"], "42\n");
|
|
48
|
-
const
|
|
49
|
-
|
|
48
|
+
const bitKlib = path.join(scratch, "bit.klib");
|
|
49
|
+
const bitsKlib = path.join(scratch, "bits.klib");
|
|
50
|
+
const arithKlib = path.join(scratch, "arithmetics.klib");
|
|
51
|
+
run("k-compile", [path.join(packageDir, "lib/bit.k"), bitKlib]);
|
|
52
|
+
run("k-compile", ["--lib", bitKlib, path.join(packageDir, "lib/bits.k"), bitsKlib]);
|
|
53
|
+
run("k-compile", ["--lib", bitsKlib, path.join(packageDir, "lib/arithmetics.k"), arithKlib]);
|
|
54
|
+
const incremented = run("k", ["--lib", arithKlib,
|
|
55
|
+
"--use", "inc", "inc"], integer);
|
|
50
56
|
assert.equal(run("k-int", ["--print"], incremented).toString().trim(), "43");
|
|
51
57
|
|
|
52
58
|
const object = path.join(scratch, "ok.ko");
|
package/tests/helpers/cli.mjs
CHANGED
|
@@ -25,9 +25,17 @@ export function succeeds(result, label) {
|
|
|
25
25
|
|
|
26
26
|
export function withCompiledLibrary(source, test) {
|
|
27
27
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "k-library-test-"));
|
|
28
|
-
const
|
|
28
|
+
const sources = Array.isArray(source) ? source : [source];
|
|
29
|
+
let library = null;
|
|
29
30
|
try {
|
|
30
|
-
|
|
31
|
+
for (let i = 0; i < sources.length; i++) {
|
|
32
|
+
const nextLibrary = path.join(dir, `library-${i}.klib`);
|
|
33
|
+
const args = ["objects/compile.mjs"];
|
|
34
|
+
if (library) args.push("--lib", library);
|
|
35
|
+
args.push(sources[i], nextLibrary);
|
|
36
|
+
succeeds(node(args), `compile ${sources[i]}`);
|
|
37
|
+
library = nextLibrary;
|
|
38
|
+
}
|
|
31
39
|
test(library);
|
|
32
40
|
} finally {
|
|
33
41
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
@@ -36,7 +44,7 @@ export function withCompiledLibrary(source, test) {
|
|
|
36
44
|
|
|
37
45
|
export function runLibrary(library, aliases, source, input = "0") {
|
|
38
46
|
return node(["k.mjs", "--lib", library,
|
|
39
|
-
...aliases.flatMap((alias) => ["--
|
|
47
|
+
...aliases.flatMap((alias) => ["--use", alias]), source],
|
|
40
48
|
encodeToWire(parse(input)));
|
|
41
49
|
}
|
|
42
50
|
|
package/tests/integration.sh
CHANGED
|
@@ -33,10 +33,10 @@ grep -q '"__main__"' "$TMP_DIR/stdin.kvm"
|
|
|
33
33
|
grep -q '"layer": "KVM-P"' "$TMP_DIR/poly-object.kvm"
|
|
34
34
|
grep -q '"isPolymorphic": true' "$TMP_DIR/poly-object.kvm"
|
|
35
35
|
./objects/compile.mjs 'id = ();' "$TMP_DIR/export.klib"
|
|
36
|
-
./objects/compile.mjs --lib "$TMP_DIR/export.klib" --
|
|
36
|
+
./objects/compile.mjs --lib "$TMP_DIR/export.klib" --use id:alias 'alias' "$TMP_DIR/export.ko"
|
|
37
37
|
./objects/decompile.mjs "$TMP_DIR/export.ko" | grep -q '^----- main -----$'
|
|
38
|
-
node ./codecs/unit.mjs --parse | ./k.mjs --lib "$TMP_DIR/export.klib" --
|
|
39
|
-
node ./codecs/unit.mjs --parse | ./kvm.mjs --lib "$TMP_DIR/export.klib" --
|
|
38
|
+
node ./codecs/unit.mjs --parse | ./k.mjs --lib "$TMP_DIR/export.klib" --use id:alias 'alias' | node ./codecs/unit.mjs --print | grep -qx '{}'
|
|
39
|
+
node ./codecs/unit.mjs --parse | ./kvm.mjs --lib "$TMP_DIR/export.klib" --use id:alias 'alias' | node ./codecs/unit.mjs --print | grep -qx '{}'
|
|
40
40
|
node ./codecs/unit.mjs --parse | ./k.mjs "$TMP_DIR/export.ko" | node ./codecs/unit.mjs --print | grep -qx '{}'
|
|
41
41
|
node ./codecs/unit.mjs --parse | ./kvm.mjs "$TMP_DIR/export.ko" | node ./codecs/unit.mjs --print | grep -qx '{}'
|
|
42
42
|
if ./k.mjs --lib "$TMP_DIR/export.klib" --lib "$TMP_DIR/export.klib" '()' > "$TMP_DIR/two-lib.out" 2> "$TMP_DIR/two-lib.err"; then
|
|
@@ -81,7 +81,12 @@ if ./kvm.mjs -k Examples/nat.k > "$TMP_DIR/dash-k-kvm.out" 2> "$TMP_DIR/dash-k-k
|
|
|
81
81
|
fi
|
|
82
82
|
grep -q -- '-k is no longer supported' "$TMP_DIR/dash-k-kvm.err"
|
|
83
83
|
echo
|
|
84
|
-
./objects/compile.mjs
|
|
84
|
+
./objects/compile.mjs lib/bit.k "$TMP_DIR/bit.klib"
|
|
85
|
+
./objects/compile.mjs --lib "$TMP_DIR/bit.klib" lib/bits.k "$TMP_DIR/bits.klib"
|
|
86
|
+
./objects/compile.mjs --lib "$TMP_DIR/bits.klib" lib/list.k "$TMP_DIR/list.klib"
|
|
87
|
+
./objects/compile.mjs --lib "$TMP_DIR/list.klib" lib/btrie.k "$TMP_DIR/btrie.klib"
|
|
88
|
+
./objects/compile.mjs --lib "$TMP_DIR/btrie.klib" lib/string.k "$TMP_DIR/string.klib"
|
|
89
|
+
./objects/compile.mjs --lib "$TMP_DIR/string.klib" lib/ieee.k "$TMP_DIR/ieee-types.klib"
|
|
85
90
|
./objects/compile.mjs --lib "$TMP_DIR/ieee-types.klib" Examples/ieee-arithmetics.k "$TMP_DIR/ieee.klib"
|
|
86
91
|
MUL=`./objects/extract-aliases.mjs "$TMP_DIR/ieee.klib" | grep '^mul = @' | sed 's/^mul = \(@[^;]*\);.*$/\1/'`
|
|
87
92
|
DIV=`./objects/extract-aliases.mjs "$TMP_DIR/ieee.klib" | grep '^div = @' | sed 's/^div = \(@[^;]*\);.*$/\1/'`
|
|
@@ -3,7 +3,7 @@ import { withCompiledLibrary, runLibrary, integer, succeeds } from "./helpers/cl
|
|
|
3
3
|
|
|
4
4
|
const large = "11111112222223333334444444555555666666777777788888899999900000011111122222233333344444455555";
|
|
5
5
|
|
|
6
|
-
withCompiledLibrary("Examples/arithmetics.k", (library) => {
|
|
6
|
+
withCompiledLibrary(["lib/bit.k", "lib/bits.k", "lib/arithmetics.k", "Examples/arithmetics.k"], (library) => {
|
|
7
7
|
const run = (aliases, source, input) => runLibrary(library, aliases, source, input);
|
|
8
8
|
const n = BigInt(large);
|
|
9
9
|
for (const [op, expected] of [
|
|
@@ -48,7 +48,9 @@ withCompiledLibrary("Examples/arithmetics.k", (library) => {
|
|
|
48
48
|
[["mod", "int", "10:ten", "3:three"], "{ten x, three y} mod int", "1"],
|
|
49
49
|
[["gcd", "int", "12:twelve", "8:eight"], "{twelve x, eight y} gcd int", "4"],
|
|
50
50
|
[["gcd", "int", "4:four", "_:zero_b"], "{four x, zero_b y} gcd int", "4"],
|
|
51
|
-
[["lcm", "int", "4:four", "6:six"], "{four x, six y} lcm int", "12"]
|
|
51
|
+
[["lcm", "int", "4:four", "6:six"], "{four x, six y} lcm int", "12"],
|
|
52
|
+
[["factorial", "int", "5:five"], "five int factorial", "120"],
|
|
53
|
+
[["fib", "int", "10:ten"], "ten int fib", "89"]
|
|
52
54
|
]) assert.equal(integer(run(aliases, source), source), expected, source);
|
|
53
55
|
});
|
|
54
56
|
|
|
@@ -7,18 +7,22 @@ import { fromJsonValue, toJsonValue } from "../codecs/djson.mjs";
|
|
|
7
7
|
import { textToStringValue, stringValueToText } from "../codecs/runtime/unicode-string.mjs";
|
|
8
8
|
import { parse as parseInt } from "../codecs/int.mjs";
|
|
9
9
|
|
|
10
|
-
const source = readFileSync(new URL("../
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
10
|
+
const source = readFileSync(new URL("../lib/djson.k", import.meta.url), "utf8");
|
|
11
|
+
let ieee = null;
|
|
12
|
+
for (const file of [
|
|
13
|
+
"lib/bit.k",
|
|
14
|
+
"lib/bits.k",
|
|
15
|
+
"lib/list.k",
|
|
16
|
+
"lib/btrie.k",
|
|
17
|
+
"lib/string.k",
|
|
18
|
+
"lib/ieee.k"
|
|
19
|
+
]) {
|
|
20
|
+
ieee = loadLibrary(compileLibrary(readFileSync(new URL(`../${file}`, import.meta.url), "utf8"), {
|
|
21
|
+
source: file,
|
|
22
|
+
libraries: ieee ? [ieee] : []
|
|
23
|
+
}));
|
|
24
|
+
}
|
|
25
|
+
const library = loadLibrary(compileLibrary(source, { libraries: [ieee] }));
|
|
22
26
|
const run = (op, value) => k.compile(library.relAlias[op], { libraries: [library] })(value);
|
|
23
27
|
|
|
24
28
|
function method(op, json, { key, index, val } = {}) {
|
|
@@ -7,9 +7,17 @@ import { parse as parseFloat64, print as printFloat64 } from "../codecs/ieee.mjs
|
|
|
7
7
|
import { closedPatternToCodeHash, valueForCode } from "../repl-codecs.mjs";
|
|
8
8
|
|
|
9
9
|
const state = createState({ engine: "js" });
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
for (const file of [
|
|
11
|
+
"lib/bit.k",
|
|
12
|
+
"lib/bits.k",
|
|
13
|
+
"lib/list.k",
|
|
14
|
+
"lib/btrie.k",
|
|
15
|
+
"lib/string.k",
|
|
16
|
+
"lib/ieee.k",
|
|
17
|
+
"Examples/ieee-arithmetics.k"
|
|
18
|
+
]) {
|
|
19
|
+
await evaluateInput(`:load ${file}`, state);
|
|
20
|
+
}
|
|
13
21
|
|
|
14
22
|
for (const name of ["add", "sub", "mul", "div"]) {
|
|
15
23
|
assert.ok(state.relAliases[name], `Expected public IEEE relation '${name}'`);
|
package/tests/test-ieee-text.mjs
CHANGED
|
@@ -6,10 +6,21 @@ import { compileWasmArtifact, instantiateWasmArtifact } from "../backends/wasm/s
|
|
|
6
6
|
import { encodeNumberToValue, decodeValueToNumber } from "../codecs/ieee.mjs";
|
|
7
7
|
import { textToStringValue, stringValueToText } from "../codecs/runtime/unicode-string.mjs";
|
|
8
8
|
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
const ieeeSources = [
|
|
10
|
+
"lib/bit.k",
|
|
11
|
+
"lib/bits.k",
|
|
12
|
+
"lib/list.k",
|
|
13
|
+
"lib/btrie.k",
|
|
14
|
+
"lib/string.k",
|
|
15
|
+
"lib/ieee.k"
|
|
16
|
+
];
|
|
17
|
+
let ieee = null;
|
|
18
|
+
for (const file of ieeeSources) {
|
|
19
|
+
ieee = loadLibrary(compileLibrary(readFileSync(new URL(`../${file}`, import.meta.url), "utf8"), {
|
|
20
|
+
source: file,
|
|
21
|
+
libraries: ieee ? [ieee] : []
|
|
22
|
+
}));
|
|
23
|
+
}
|
|
13
24
|
assert.equal(ieee.relAlias.add, undefined, "Text conversion must not load IEEE arithmetic");
|
|
14
25
|
const parser = await instantiateWasmArtifact(await compileWasmArtifact(ieee.relAlias.parseFloat64, { libraries: [ieee] }));
|
|
15
26
|
const printer = await instantiateWasmArtifact(await compileWasmArtifact(ieee.relAlias.printFloat64, { libraries: [ieee] }));
|
package/tests/test-k-object.mjs
CHANGED
|
@@ -101,13 +101,21 @@ const recursiveNatPattern = new RegExp(
|
|
|
101
101
|
assert.equal(natRoundTripSource, natDecompiledSource);
|
|
102
102
|
assert.match(natDecompiledSource, recursiveNatPattern);
|
|
103
103
|
|
|
104
|
+
const bitLib = loadLibrary(decodeObject(compileLibraryBuffer(
|
|
105
|
+
fs.readFileSync("lib/bit.k", "utf8"),
|
|
106
|
+
{ source: "lib/bit.k" }
|
|
107
|
+
)));
|
|
108
|
+
const bitsLib = loadLibrary(decodeObject(compileLibraryBuffer(
|
|
109
|
+
fs.readFileSync("lib/bits.k", "utf8"),
|
|
110
|
+
{ source: "lib/bits.k", libraries: [bitLib] }
|
|
111
|
+
)));
|
|
104
112
|
const arithmeticsLibraryBuffer = compileLibraryBuffer(
|
|
105
|
-
fs.readFileSync("
|
|
106
|
-
{ source: "
|
|
113
|
+
fs.readFileSync("lib/arithmetics.k", "utf8"),
|
|
114
|
+
{ source: "lib/arithmetics.k", libraries: [bitsLib] }
|
|
107
115
|
);
|
|
108
116
|
const arithmeticsDecompiledSource = decompileObjectBuffer(arithmeticsLibraryBuffer);
|
|
109
117
|
const arithmeticsRoundTripSource = decompileObjectBuffer(
|
|
110
|
-
compileLibraryBuffer(arithmeticsDecompiledSource, { source: "
|
|
118
|
+
compileLibraryBuffer(arithmeticsDecompiledSource, { source: "lib/arithmetics.decompiled.k" })
|
|
111
119
|
);
|
|
112
120
|
assert.equal(arithmeticsRoundTripSource, arithmeticsDecompiledSource);
|
|
113
121
|
|
package/tests/test-karatsuba.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import { withCompiledLibrary, runLibrary, integer } from "./helpers/cli.mjs";
|
|
|
3
3
|
|
|
4
4
|
const large = "11111112222223333334444444555555666666777777788888899999900000011111122222233333344444455555";
|
|
5
5
|
|
|
6
|
-
withCompiledLibrary("
|
|
6
|
+
withCompiledLibrary(["lib/bit.k", "lib/bits.k", "lib/arithmetics.k"], (library) => {
|
|
7
7
|
const multiply = (source, input) =>
|
|
8
8
|
integer(runLibrary(library, ["karatsuba"], source, input), source);
|
|
9
9
|
|