@fraczak/k 8.7.1 → 8.9.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 +28 -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/codecs/djson.mjs +2 -3
- package/codecs/int.mjs +66 -14
- package/compiler.mjs +22 -12
- package/harness/suites/poly.mjs +9 -11
- package/index.mjs +119 -50
- package/lib/djson.k +976 -288
- package/lib/generic.k +686 -0
- package/lib/ieee.k +658 -639
- package/lib/integer.k +536 -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-bigint-compare.mjs +279 -0
- 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 +16 -25
- package/tests/test-djson.mjs +272 -0
- package/tests/test-generic.mjs +237 -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/tests/test-structural-values.mjs +1 -1
- 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/scripts/perf-int.mjs
CHANGED
|
@@ -57,7 +57,7 @@ function printHelp() {
|
|
|
57
57
|
console.log(`Usage: node scripts/perf-int.mjs [options]
|
|
58
58
|
|
|
59
59
|
Big Integer Arithmetic Performance Benchmark Harness.
|
|
60
|
-
Benchmarks plus, minus, times, and squaring scale on
|
|
60
|
+
Benchmarks plus, minus, times, and squaring scale on lib/integer.k
|
|
61
61
|
across Native JS, kVM interpreter, LLVM, Wasm, and PKIR backends.
|
|
62
62
|
|
|
63
63
|
Options:
|
|
@@ -202,9 +202,9 @@ function pkirLaneName() {
|
|
|
202
202
|
return "PKIR (Wasm persistent)";
|
|
203
203
|
}
|
|
204
204
|
|
|
205
|
-
console.log("==> Initializing state and loading @fraczak/k/lib/
|
|
205
|
+
console.log("==> Initializing state and loading @fraczak/k/lib/integer.k");
|
|
206
206
|
const state = createState();
|
|
207
|
-
for (const relPath of ["
|
|
207
|
+
for (const relPath of ["core.k", "lib/integer.k"]) {
|
|
208
208
|
await evaluateInput(`:load ${fileURLToPath(import.meta.resolve(`@fraczak/k/${relPath}`))}`, state);
|
|
209
209
|
}
|
|
210
210
|
|
|
@@ -221,7 +221,7 @@ const relations = {};
|
|
|
221
221
|
for (const op of ops) {
|
|
222
222
|
const name = benchNames[op];
|
|
223
223
|
relations[op] = prepareRelation(state, name, {
|
|
224
|
-
sourceLabel: `@fraczak/k/lib/
|
|
224
|
+
sourceLabel: `@fraczak/k/lib/integer.k#${name}`
|
|
225
225
|
});
|
|
226
226
|
}
|
|
227
227
|
codes.load(state.codes);
|
|
@@ -263,7 +263,7 @@ if (runLLVM) {
|
|
|
263
263
|
inputPattern: tc.inputPattern,
|
|
264
264
|
cacheDir,
|
|
265
265
|
sourceLabel:
|
|
266
|
-
`@fraczak/k/
|
|
266
|
+
`@fraczak/k/lib/integer.k#${relation.relationName}`,
|
|
267
267
|
usePKIR: false
|
|
268
268
|
});
|
|
269
269
|
}
|
|
@@ -283,7 +283,7 @@ if (runLLVMPKIR) {
|
|
|
283
283
|
inputPattern: tc.inputPattern,
|
|
284
284
|
cacheDir,
|
|
285
285
|
sourceLabel:
|
|
286
|
-
`@fraczak/k/
|
|
286
|
+
`@fraczak/k/lib/integer.k#${relation.relationName}`,
|
|
287
287
|
usePKIR: true
|
|
288
288
|
});
|
|
289
289
|
}
|
|
@@ -444,7 +444,7 @@ function printBenchmarkDescription() {
|
|
|
444
444
|
if (runPKIR) lanes.push(pkirLaneName());
|
|
445
445
|
|
|
446
446
|
console.log("==> Benchmark description");
|
|
447
|
-
console.log(" source: @fraczak/k/
|
|
447
|
+
console.log(" source: @fraczak/k/lib/integer.k");
|
|
448
448
|
console.log(` benchmark relations: ${Object.values(benchNames).join(", ")}`);
|
|
449
449
|
console.log(` operations: ${ops.join(", ")}`);
|
|
450
450
|
console.log(` decimal inputs: ${inputTexts.join(", ")}`);
|
|
@@ -732,7 +732,7 @@ if (runPKIR && testSuite.some(tc => tc.pkirConformance !== "ok")) process.exitCo
|
|
|
732
732
|
|
|
733
733
|
// =======================================================================
|
|
734
734
|
// Squaring Scale & Memory Profiling Benchmark
|
|
735
|
-
//
|
|
735
|
+
// Expression: s = {()x,()y}times;
|
|
736
736
|
// =======================================================================
|
|
737
737
|
|
|
738
738
|
function countBitsValue(val) {
|
|
@@ -772,13 +772,7 @@ const squaringInputText = process.env.SQUARING_INPUT || "987654321";
|
|
|
772
772
|
const squaringStepTimeoutMs = parsePositiveIntEnv("SQUARING_STEP_TIMEOUT_MS", 60000);
|
|
773
773
|
|
|
774
774
|
console.log("\n=================== SQUARING SCALE & MEMORY BENCHMARK ===================");
|
|
775
|
-
console.log(`
|
|
776
|
-
|
|
777
|
-
const stateKara = createState();
|
|
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
|
-
}
|
|
781
|
-
codes.load(stateKara.codes);
|
|
775
|
+
console.log(`Algorithm: times (Karatsuba, ${squaringSteps} successive squarings doubling bit length)`);
|
|
782
776
|
|
|
783
777
|
const sInitialVal = intValue(squaringInputText);
|
|
784
778
|
const { inputWire: sInitWire, inputPattern: sInputPattern } = wireInput(sInitialVal);
|
|
@@ -787,20 +781,11 @@ const squaringAlgos = [
|
|
|
787
781
|
{
|
|
788
782
|
id: "times",
|
|
789
783
|
name: "times",
|
|
790
|
-
title: "
|
|
784
|
+
title: "Karatsuba Multiplication (times)",
|
|
791
785
|
expr: "s = {()x,()y}times;",
|
|
792
|
-
sourceLabel: "@fraczak/k/lib/
|
|
786
|
+
sourceLabel: "@fraczak/k/lib/integer.k#s",
|
|
793
787
|
state: state,
|
|
794
788
|
relName: "s"
|
|
795
|
-
},
|
|
796
|
-
{
|
|
797
|
-
id: "karatsuba",
|
|
798
|
-
name: "karatsuba",
|
|
799
|
-
title: "Karatsuba Multiplication (karatsuba)",
|
|
800
|
-
expr: "s = {()x,()y}karatsuba;",
|
|
801
|
-
sourceLabel: "@fraczak/k/lib/arithmetics.k#s",
|
|
802
|
-
state: stateKara,
|
|
803
|
-
relName: "s"
|
|
804
789
|
}
|
|
805
790
|
];
|
|
806
791
|
|
|
@@ -1049,7 +1034,7 @@ for (let step = 1; step <= squaringSteps; step++) {
|
|
|
1049
1034
|
const sep130 = "=".repeat(130);
|
|
1050
1035
|
const line130 = "-".repeat(130);
|
|
1051
1036
|
|
|
1052
|
-
if (squaringLanes.length === 1 && (squaringLanes[0].id === "llvm" || squaringLanes[0].id === "llvm-pkir")) {
|
|
1037
|
+
if (squaringAlgos.length === 2 && squaringLanes.length === 1 && (squaringLanes[0].id === "llvm" || squaringLanes[0].id === "llvm-pkir")) {
|
|
1053
1038
|
const laneId = squaringLanes[0].id;
|
|
1054
1039
|
const laneName = squaringLanes[0].name;
|
|
1055
1040
|
// LLVM Only side-by-side comparison table
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Multiplication
|
|
4
|
+
* Multiplication Performance Harness
|
|
5
5
|
*
|
|
6
|
-
*
|
|
6
|
+
* Benchmarks lib/integer.k multiplication ('times', Karatsuba)
|
|
7
7
|
* across available execution backends:
|
|
8
8
|
* - LLVM (native binary via Clang/LLVM)
|
|
9
9
|
* - WebAssembly (persistent runner via Wasm)
|
|
@@ -65,8 +65,8 @@ function hasFlag(flag) {
|
|
|
65
65
|
function printHelp() {
|
|
66
66
|
console.log(`Usage: node scripts/perf-multiplication.mjs [options]
|
|
67
67
|
|
|
68
|
-
Multiplication
|
|
69
|
-
|
|
68
|
+
Multiplication Performance Harness.
|
|
69
|
+
Benchmarks lib/integer.k multiplication ('times', Karatsuba) across
|
|
70
70
|
available execution backends (LLVM, Wasm, PKIR, kVM, JS).
|
|
71
71
|
|
|
72
72
|
Options:
|
|
@@ -80,7 +80,7 @@ Options:
|
|
|
80
80
|
(default: llvm,llvm-pkir,wasm,pkir,kvm,js)
|
|
81
81
|
--algos <csv>
|
|
82
82
|
Comma-separated list of multiplication algorithms to test:
|
|
83
|
-
times
|
|
83
|
+
times. (default: times)
|
|
84
84
|
--steps <n>
|
|
85
85
|
Number of squaring steps in squaring mode. (default: 8)
|
|
86
86
|
--input <int>
|
|
@@ -123,7 +123,7 @@ const backendsArg = getArg(
|
|
|
123
123
|
);
|
|
124
124
|
const selectedBackends = backendsArg
|
|
125
125
|
.split(",").map(b => b.trim().toLowerCase()).filter(Boolean);
|
|
126
|
-
const algosArg = getArg("--algos", "times
|
|
126
|
+
const algosArg = getArg("--algos", "times");
|
|
127
127
|
const selectedAlgos = algosArg
|
|
128
128
|
.split(",").map(a => a.trim().toLowerCase()).filter(Boolean);
|
|
129
129
|
|
|
@@ -232,9 +232,8 @@ console.log("-".repeat(72) + "\n");
|
|
|
232
232
|
|
|
233
233
|
// 1. Map algorithms to their source files
|
|
234
234
|
const arithLibChain = [
|
|
235
|
-
fileURLToPath(import.meta.resolve("../
|
|
236
|
-
fileURLToPath(import.meta.resolve("../lib/
|
|
237
|
-
fileURLToPath(import.meta.resolve("../lib/arithmetics.k"))
|
|
235
|
+
fileURLToPath(import.meta.resolve("../core.k")),
|
|
236
|
+
fileURLToPath(import.meta.resolve("../lib/integer.k"))
|
|
238
237
|
];
|
|
239
238
|
|
|
240
239
|
const cacheDir = makeCacheDir("k-mult-perf-");
|
package/scripts/perf-poly.mjs
CHANGED
|
@@ -193,22 +193,19 @@ function pkirLaneName() {
|
|
|
193
193
|
}
|
|
194
194
|
|
|
195
195
|
|
|
196
|
-
console.log("==> Loading @fraczak/k/lib/
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
source: filePath,
|
|
202
|
-
libraries: depLib ? [depLib] : []
|
|
203
|
-
}));
|
|
204
|
-
}
|
|
196
|
+
console.log("==> Loading @fraczak/k/lib/integer.k as library");
|
|
197
|
+
const integerPath = fileURLToPath(import.meta.resolve("@fraczak/k/lib/integer.k"));
|
|
198
|
+
const depLib = loadLibrary(compileLibrary(fs.readFileSync(integerPath, "utf8"), {
|
|
199
|
+
source: integerPath
|
|
200
|
+
}));
|
|
205
201
|
|
|
206
202
|
console.log("==> Compiling @fraczak/k/Examples/poly.k with library dependencies");
|
|
207
203
|
const polyPath = fileURLToPath(import.meta.resolve("@fraczak/k/Examples/poly.k"));
|
|
208
204
|
const polySource = fs.readFileSync(polyPath, "utf8");
|
|
209
205
|
const polyLib = loadLibrary(compileLibrary(polySource, {
|
|
210
206
|
source: polyPath,
|
|
211
|
-
libraries: [depLib]
|
|
207
|
+
libraries: [depLib],
|
|
208
|
+
use: ["int[] nat int pos? neg? zero? gt? inc dec"]
|
|
212
209
|
}));
|
|
213
210
|
|
|
214
211
|
const state = {
|
package/scripts/run-tests.mjs
CHANGED
|
@@ -48,9 +48,12 @@ const tests = [
|
|
|
48
48
|
["tests/test-djson-methods.mjs"]
|
|
49
49
|
],
|
|
50
50
|
["tests/test-lib.mjs", node, ["tests/test-lib.mjs"]],
|
|
51
|
+
["tests/test-generic.mjs", node, ["tests/test-generic.mjs"]],
|
|
52
|
+
["tests/test-integer.mjs", node, ["tests/test-integer.mjs"]],
|
|
53
|
+
["tests/test-ieee.mjs", node, ["tests/test-ieee.mjs"]],
|
|
54
|
+
["tests/test-djson.mjs", node, ["tests/test-djson.mjs"]],
|
|
51
55
|
["tests/test-library-isolation.mjs", node, ["tests/test-library-isolation.mjs"]],
|
|
52
56
|
["tests/test-parse-json.mjs", node, ["tests/test-parse-json.mjs"]],
|
|
53
|
-
["tests/test-ieee-text.mjs", node, ["tests/test-ieee-text.mjs"]],
|
|
54
57
|
["tests/test-deep-wire-value.mjs", node, ["tests/test-deep-wire-value.mjs"]],
|
|
55
58
|
["tests/test-k-object.mjs", node, ["tests/test-k-object.mjs"]],
|
|
56
59
|
["tests/test-kir.mjs", node, ["tests/test-kir.mjs"]],
|
|
@@ -62,7 +65,6 @@ const tests = [
|
|
|
62
65
|
["tests/test-kvm.mjs", node, ["tests/test-kvm.mjs"]],
|
|
63
66
|
["tests/test-kvm-polymorphic.mjs", node, ["tests/test-kvm-polymorphic.mjs"]],
|
|
64
67
|
["tests/test-arithmetics.mjs", node, ["tests/test-arithmetics.mjs"]],
|
|
65
|
-
["tests/test-karatsuba.mjs", node, ["tests/test-karatsuba.mjs"]],
|
|
66
68
|
["tests/test-poly.mjs", node, ["tests/test-poly.mjs"]],
|
|
67
69
|
["tests/test-polymorphic-tdd.mjs", node, ["tests/test-polymorphic-tdd.mjs"]],
|
|
68
70
|
["tests/test-access-table.mjs", node, ["tests/test-access-table.mjs"]],
|
package/scripts/test-package.mjs
CHANGED
|
@@ -45,18 +45,12 @@ 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
|
-
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]);
|
|
48
|
+
const integerKlib = path.join(scratch, "integer.klib");
|
|
53
49
|
run("k-compile", [
|
|
54
|
-
"
|
|
55
|
-
|
|
56
|
-
path.join(packageDir, "lib/arithmetics.k"),
|
|
57
|
-
arithKlib
|
|
50
|
+
path.join(packageDir, "lib/integer.k"),
|
|
51
|
+
integerKlib
|
|
58
52
|
]);
|
|
59
|
-
const incremented = run("k", ["--lib",
|
|
53
|
+
const incremented = run("k", ["--lib", integerKlib,
|
|
60
54
|
"--use", "inc", "inc"], integer);
|
|
61
55
|
assert.equal(run("k-int", ["--print"], incremented).toString().trim(), "43");
|
|
62
56
|
|
package/tests/README.md
CHANGED
|
@@ -43,9 +43,8 @@ cases; shared helpers live outside the discovered directory.
|
|
|
43
43
|
| Objects and portable export | `test-k-object.mjs`, `test-kir.mjs`, `test-validate-object.mjs` |
|
|
44
44
|
| Interactive state and browser UI | `test-repl.mjs`, `test-repl-html.mjs`, `test-tree-view.mjs` |
|
|
45
45
|
| Execution layers | `test-kvm.mjs`, `test-kvm-polymorphic.mjs`, `test-access-table.mjs`, `test-pkir.mjs` |
|
|
46
|
-
| Libraries |
|
|
47
|
-
| JSON text parsing | `test-parse-json.mjs`: grammar, Unicode escapes, binary64
|
|
48
|
-
| IEEE text conversion | `test-ieee-text.mjs`: parsing, exact printing, special values, and binary64 round-trips |
|
|
46
|
+
| Libraries | `test-generic.mjs`, `test-integer.mjs`, `test-ieee.mjs`, `test-djson.mjs`, `test-lib.mjs`, and example suites (`test-arithmetics.mjs`, `test-poly.mjs`, `test-ieee-arithmetic.mjs`, `test-djson-methods.mjs`) |
|
|
47
|
+
| JSON text parsing | `test-parse-json.mjs`: RFC 8259 grammar, Unicode escapes, binary64 rounding, Wasm, and CLI |
|
|
49
48
|
|
|
50
49
|
`test-hash-fuzz.mjs` uses a deterministic generator to check renaming and
|
|
51
50
|
definition-order invariance; it is not a general cryptographic collision test.
|
|
@@ -55,8 +54,8 @@ expected values, and case descriptions reused by supported execution paths.
|
|
|
55
54
|
A rendered payload alone can hide envelope differences, so keep pattern
|
|
56
55
|
comparisons when checking structural correctness.
|
|
57
56
|
|
|
58
|
-
Arithmetic
|
|
59
|
-
|
|
57
|
+
Arithmetic tests share CLI setup in `helpers/cli.mjs`. They run
|
|
58
|
+
`lib/integer.k` through `k.mjs` and compare numerical results with BigInt;
|
|
60
59
|
codec modules prepare and decode the wire values in process. The dedicated
|
|
61
60
|
codec and shell suites cover the codec command-line boundary.
|
|
62
61
|
|
package/tests/helpers/cli.mjs
CHANGED
|
@@ -6,10 +6,37 @@ import { spawnSync } from "node:child_process";
|
|
|
6
6
|
import { fileURLToPath } from "node:url";
|
|
7
7
|
import { parse, print } from "../../codecs/int.mjs";
|
|
8
8
|
import { encodeToWire, decodeWire } from "../../codecs/runtime/prefix-codec.mjs";
|
|
9
|
-
import { parseHeaderUses } from "../../canonical-pattern.mjs";
|
|
10
9
|
|
|
11
10
|
const root = fileURLToPath(new URL("../../", import.meta.url));
|
|
12
11
|
|
|
12
|
+
const LIBRARY_USE_SPECS = new Map([
|
|
13
|
+
["core.k", []],
|
|
14
|
+
["lib/integer.k", []],
|
|
15
|
+
["lib/generic.k", [
|
|
16
|
+
"bit[] bits[] list[X] byte[] ascii[] bmp_hi_common[] bmp_hi_private_use[] plane0[] plane_02_10[] unicode[] string[]"
|
|
17
|
+
]],
|
|
18
|
+
["lib/ieee.k", ["bit[] bits[]"]],
|
|
19
|
+
["lib/djson.k", [
|
|
20
|
+
"bit[] bits[] list[] byte[] ascii[] bmp_hi_common[] bmp_hi_private_use[] plane0[] plane_02_10[] unicode[] string[]",
|
|
21
|
+
"bool[] false? true? order[] lt? eq? gt? opt[] reverse trim_bits inc_bits btrie[] btrie_empty btrie_get btrie_set btrie_del btrie_entries_raw btrie_max_key btrie_bfs_values string_to_bits bits_to_string",
|
|
22
|
+
"sign[] float64[] pos_zero neg_zero finite_float64? ratio_to_float64"
|
|
23
|
+
]],
|
|
24
|
+
["Examples/poly.k", ["int[] nat int pos? neg? zero? gt? inc dec"]],
|
|
25
|
+
["Examples/ieee-arithmetics.k", [
|
|
26
|
+
"bit[] sign[] exp11[] frac52[] float64[] all_ones_exponent? exp0 finite_float64? frac0 inf_float64? nan_float64? neg_inf neg_zero negate_float64 pos_inf pos_zero qnan zero_exponent? zero_float64?"
|
|
27
|
+
]]
|
|
28
|
+
]);
|
|
29
|
+
|
|
30
|
+
export function useSpecsFor(sourcePath) {
|
|
31
|
+
const normalized = String(sourcePath).replace(/\\/g, "/");
|
|
32
|
+
for (const [key, specs] of LIBRARY_USE_SPECS) {
|
|
33
|
+
if (normalized === key || normalized.endsWith(`/${key}`)) {
|
|
34
|
+
return specs;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return [];
|
|
38
|
+
}
|
|
39
|
+
|
|
13
40
|
export function node(args, input) {
|
|
14
41
|
const result = spawnSync(process.execPath, args, {
|
|
15
42
|
cwd: root, input, maxBuffer: 64 * 1024 * 1024
|
|
@@ -33,12 +60,8 @@ export function withCompiledLibrary(source, test) {
|
|
|
33
60
|
const nextLibrary = path.join(dir, `library-${i}.klib`);
|
|
34
61
|
const args = ["objects/compile.mjs"];
|
|
35
62
|
if (library) args.push("--lib", library);
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
const raw = fs.readFileSync(filePath, "utf8");
|
|
39
|
-
for (const u of parseHeaderUses(raw)) {
|
|
40
|
-
args.push("--use", u.use);
|
|
41
|
-
}
|
|
63
|
+
for (const u of useSpecsFor(sources[i])) {
|
|
64
|
+
args.push("--use", u);
|
|
42
65
|
}
|
|
43
66
|
args.push(sources[i], nextLibrary);
|
|
44
67
|
succeeds(node(args), `compile ${sources[i]}`);
|
package/tests/integration.sh
CHANGED
|
@@ -85,10 +85,9 @@ compile_step() {
|
|
|
85
85
|
local prev="$1"
|
|
86
86
|
local src="$2"
|
|
87
87
|
local out="$3"
|
|
88
|
+
local use_spec="$4"
|
|
88
89
|
local uses=()
|
|
89
|
-
|
|
90
|
-
[ -n "$line" ] && uses+=(--use "$line")
|
|
91
|
-
done < <(node -e 'import("./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)
|
|
90
|
+
[ -n "$use_spec" ] && uses=(--use "$use_spec")
|
|
92
91
|
if [ -n "$prev" ]; then
|
|
93
92
|
./objects/compile.mjs --lib "$prev" "${uses[@]}" "$src" "$out"
|
|
94
93
|
else
|
|
@@ -96,13 +95,9 @@ compile_step() {
|
|
|
96
95
|
fi
|
|
97
96
|
}
|
|
98
97
|
|
|
99
|
-
compile_step ""
|
|
100
|
-
compile_step "$TMP_DIR/
|
|
101
|
-
compile_step "$TMP_DIR/
|
|
102
|
-
compile_step "$TMP_DIR/list.klib" lib/btrie.k "$TMP_DIR/btrie.klib"
|
|
103
|
-
compile_step "$TMP_DIR/btrie.klib" lib/string.k "$TMP_DIR/string.klib"
|
|
104
|
-
compile_step "$TMP_DIR/string.klib" lib/ieee.k "$TMP_DIR/ieee-types.klib"
|
|
105
|
-
compile_step "$TMP_DIR/ieee-types.klib" Examples/ieee-arithmetics.k "$TMP_DIR/ieee.klib"
|
|
98
|
+
compile_step "" core.k "$TMP_DIR/core.klib" ""
|
|
99
|
+
compile_step "$TMP_DIR/core.klib" lib/ieee.k "$TMP_DIR/ieee-types.klib" "bit[] bits[]"
|
|
100
|
+
compile_step "$TMP_DIR/ieee-types.klib" Examples/ieee-arithmetics.k "$TMP_DIR/ieee.klib" "bit[] sign[] exp11[] frac52[] float64[] all_ones_exponent? exp0 finite_float64? frac0 inf_float64? nan_float64? neg_inf neg_zero negate_float64 pos_inf pos_zero qnan zero_exponent? zero_float64?"
|
|
106
101
|
MUL=`./objects/extract-aliases.mjs "$TMP_DIR/ieee.klib" | grep '^mul = @' | sed 's/^mul = \(@[^;]*\);.*$/\1/'`
|
|
107
102
|
DIV=`./objects/extract-aliases.mjs "$TMP_DIR/ieee.klib" | grep '^div = @' | sed 's/^div = \(@[^;]*\);.*$/\1/'`
|
|
108
103
|
ARG=0.12
|
|
@@ -3,7 +3,16 @@ import { withCompiledLibrary, runLibrary, integer, succeeds } from "./helpers/cl
|
|
|
3
3
|
|
|
4
4
|
const large = "11111112222223333334444444555555666666777777788888899999900000011111122222233333344444455555";
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
function kPosInt(value) {
|
|
7
|
+
const bits = BigInt(value).toString(2);
|
|
8
|
+
let expr = "{} |_";
|
|
9
|
+
for (let i = bits.length - 1; i >= 0; i--) {
|
|
10
|
+
expr += ` |${bits[i]}`;
|
|
11
|
+
}
|
|
12
|
+
return `${expr} |+`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
withCompiledLibrary(["lib/integer.k"], (library) => {
|
|
7
16
|
const run = (aliases, source, input) => runLibrary(library, aliases, source, input);
|
|
8
17
|
const n = BigInt(large);
|
|
9
18
|
for (const [op, expected] of [
|
|
@@ -13,31 +22,31 @@ withCompiledLibrary(["lib/bit.k", "lib/bits.k", "lib/arithmetics.k", "Examples/a
|
|
|
13
22
|
String(expected), op);
|
|
14
23
|
}
|
|
15
24
|
|
|
16
|
-
succeeds(run(["dec", "
|
|
25
|
+
succeeds(run(["dec", "zero?"], "dec dec zero?", "2"),
|
|
17
26
|
"decrement normalization");
|
|
18
27
|
for (const [x, divisor] of [
|
|
19
28
|
["0", "7"], ["3", "7"], ["4", "1"], ["10", "2"], ["12", "3"],
|
|
20
29
|
["11", "3"], ["12", "12"], ["100", "7"], ["-100", "7"], ["1000", "10"]
|
|
21
30
|
]) {
|
|
22
31
|
const a = BigInt(x), b = BigInt(divisor);
|
|
23
|
-
const aliases = ["
|
|
32
|
+
const aliases = ["divmod"];
|
|
24
33
|
for (const [projection, expected] of [
|
|
25
|
-
[".
|
|
34
|
+
[".q", a / b], [".r", a % b]
|
|
26
35
|
]) {
|
|
27
|
-
const source = `{() x, divisor
|
|
36
|
+
const source = `{() x, ${kPosInt(divisor)} y} divmod ${projection}`;
|
|
28
37
|
assert.equal(integer(run(aliases, source, x), source), String(expected),
|
|
29
|
-
`
|
|
38
|
+
`divmod(${x}, ${divisor}) ${projection}`);
|
|
30
39
|
}
|
|
31
40
|
}
|
|
32
41
|
|
|
33
42
|
for (const [aliases, source] of [
|
|
34
|
-
[["
|
|
35
|
-
[["pos?",
|
|
43
|
+
[["eq?"], `{${kPosInt(2)} x, ${kPosInt(2)} y} eq? .x`],
|
|
44
|
+
[["pos?"], `${kPosInt(2)} pos?`]
|
|
36
45
|
]) succeeds(run(aliases, source), source);
|
|
37
46
|
|
|
38
47
|
for (const [aliases, source] of [
|
|
39
|
-
[["
|
|
40
|
-
[["pos?",
|
|
48
|
+
[["eq?"], `{${kPosInt(2)} x, ${kPosInt(3)} y} eq? .x`],
|
|
49
|
+
[["pos?"], `${kPosInt(0)} pos?`]
|
|
41
50
|
]) {
|
|
42
51
|
const result = run(aliases, source);
|
|
43
52
|
assert.notEqual(result.status, 0, source);
|
|
@@ -45,12 +54,10 @@ withCompiledLibrary(["lib/bit.k", "lib/bits.k", "lib/arithmetics.k", "Examples/a
|
|
|
45
54
|
}
|
|
46
55
|
|
|
47
56
|
for (const [aliases, source, expected] of [
|
|
48
|
-
[["mod"
|
|
49
|
-
[["gcd"
|
|
50
|
-
[["gcd"
|
|
51
|
-
[["lcm"
|
|
52
|
-
[["factorial", "int", "5:five"], "five int factorial", "120"],
|
|
53
|
-
[["fib", "int", "10:ten"], "ten int fib", "89"]
|
|
57
|
+
[["mod"], `{${kPosInt(10)} x, ${kPosInt(3)} y} mod`, "1"],
|
|
58
|
+
[["gcd"], `{${kPosInt(12)} x, ${kPosInt(8)} y} gcd`, "4"],
|
|
59
|
+
[["gcd"], `{${kPosInt(4)} x, ${kPosInt(0)} y} gcd`, "4"],
|
|
60
|
+
[["lcm"], `{${kPosInt(4)} x, ${kPosInt(6)} y} lcm`, "12"]
|
|
54
61
|
]) assert.equal(integer(run(aliases, source), source), expected, source);
|
|
55
62
|
});
|
|
56
63
|
|
|
@@ -3,31 +3,27 @@ import { readFileSync } from "node:fs";
|
|
|
3
3
|
import k from "../index.mjs";
|
|
4
4
|
import { Value } from "../Value.mjs";
|
|
5
5
|
import { compileLibrary, loadLibrary } from "../object.mjs";
|
|
6
|
-
import {
|
|
6
|
+
import { useSpecsFor } from "./helpers/cli.mjs";
|
|
7
7
|
import { fromJsonValue, toJsonValue } from "../codecs/djson.mjs";
|
|
8
8
|
import { textToStringValue, stringValueToText } from "../codecs/runtime/unicode-string.mjs";
|
|
9
9
|
import { parse as parseInt } from "../codecs/int.mjs";
|
|
10
10
|
|
|
11
11
|
const source = readFileSync(new URL("../lib/djson.k", import.meta.url), "utf8");
|
|
12
|
-
let
|
|
12
|
+
let prev = null;
|
|
13
13
|
for (const file of [
|
|
14
|
-
"
|
|
15
|
-
"lib/
|
|
16
|
-
"lib/list.k",
|
|
17
|
-
"lib/btrie.k",
|
|
18
|
-
"lib/string.k",
|
|
14
|
+
"core.k",
|
|
15
|
+
"lib/generic.k",
|
|
19
16
|
"lib/ieee.k"
|
|
20
17
|
]) {
|
|
21
18
|
const fileSource = readFileSync(new URL(`../${file}`, import.meta.url), "utf8");
|
|
22
|
-
const uses =
|
|
23
|
-
|
|
19
|
+
const uses = useSpecsFor(file);
|
|
20
|
+
prev = loadLibrary(compileLibrary(fileSource, {
|
|
24
21
|
source: file,
|
|
25
|
-
libraries:
|
|
22
|
+
libraries: prev ? [prev] : [],
|
|
26
23
|
use: uses
|
|
27
24
|
}));
|
|
28
25
|
}
|
|
29
|
-
const
|
|
30
|
-
const library = loadLibrary(compileLibrary(source, { libraries: [ieee], use: djsonUses }));
|
|
26
|
+
const library = loadLibrary(compileLibrary(source, { source: "lib/djson.k", libraries: [prev], use: useSpecsFor("lib/djson.k") }));
|
|
31
27
|
const run = (op, value) => k.compile(library.relAlias[op], { libraries: [library] })(value);
|
|
32
28
|
|
|
33
29
|
function method(op, json, { key, index, val } = {}) {
|
|
@@ -44,8 +40,8 @@ const doc = { name: "Alice", age: 30, tags: ["developer", "k"] };
|
|
|
44
40
|
for (const key of ["name", "age"]) {
|
|
45
41
|
assert.equal(result("get", doc, { key }), doc[key], key);
|
|
46
42
|
}
|
|
47
|
-
assert.ok(method("
|
|
48
|
-
assert.equal(method("
|
|
43
|
+
assert.ok(method("has_key?", doc, { key: "name" }));
|
|
44
|
+
assert.equal(method("has_key?", doc, { key: "missing" }), undefined);
|
|
49
45
|
const withCity = result("set", doc, { key: "city", val: "Paris" });
|
|
50
46
|
assert.deepEqual(withCity, { ...doc, city: "Paris" });
|
|
51
47
|
const withoutAge = result("del", withCity, { key: "age" });
|
|
@@ -56,15 +52,15 @@ const list = [10, 20, 30];
|
|
|
56
52
|
for (const [index, expected] of list.entries()) {
|
|
57
53
|
assert.equal(result("nth", list, { index }), expected, `index ${index}`);
|
|
58
54
|
}
|
|
59
|
-
assert.ok(method("
|
|
60
|
-
assert.equal(method("
|
|
55
|
+
assert.ok(method("has_nth?", list, { index: 1 }));
|
|
56
|
+
assert.equal(method("has_nth?", list, { index: 99 }), undefined);
|
|
61
57
|
const updated = method("set_nth", list, { index: 1, val: 99 });
|
|
62
58
|
assert.deepEqual(toJsonValue(updated), [10, 99, 30]);
|
|
63
59
|
const deleted = run("del_nth", Value.product({
|
|
64
60
|
json: updated, index: parseInt("1").value
|
|
65
61
|
}));
|
|
66
62
|
assert.deepEqual(toJsonValue(deleted), [10, 30]);
|
|
67
|
-
assert.equal(run("
|
|
63
|
+
assert.equal(run("has_nth?", Value.product({
|
|
68
64
|
json: deleted, index: parseInt("1").value
|
|
69
65
|
})), undefined);
|
|
70
66
|
assert.deepEqual(result("push", list, { val: 40 }), [10, 20, 30, 40]);
|
|
@@ -83,18 +79,13 @@ assert.deepEqual(keys, ["a", "b", "c"]);
|
|
|
83
79
|
assert.deepEqual(toJsonValue(run("values", record)), [1, 2, 3]);
|
|
84
80
|
assert.deepEqual(toJsonValue(run("values", fromJsonValue([100, 200]))), [100, 200]);
|
|
85
81
|
|
|
86
|
-
for (const [value, expected] of [
|
|
87
|
-
[{ x: 1, y: 2, z: 3, w: 4 }, 4], [["a", "b", "c"], 3], ["hello", 5]
|
|
88
|
-
]) assert.equal(toJsonValue(run("length", fromJsonValue(value))), expected);
|
|
89
|
-
|
|
90
82
|
for (const [type, value] of [
|
|
91
83
|
["object", { a: 1 }], ["list", [1, 2]], ["string", "hi"],
|
|
92
84
|
["number", 3.14], ["bool", true], ["null", null]
|
|
93
85
|
]) {
|
|
94
86
|
const input = fromJsonValue(value);
|
|
95
|
-
assert.
|
|
96
|
-
assert.ok(run(`is_${type}?`, input), type);
|
|
87
|
+
assert.ok(run(`${type}?`, input), type);
|
|
97
88
|
}
|
|
98
|
-
assert.equal(run("
|
|
99
|
-
assert.equal(run("
|
|
89
|
+
assert.equal(run("object?", fromJsonValue([1, 2])), undefined);
|
|
90
|
+
assert.equal(run("list?", fromJsonValue({ a: 1 })), undefined);
|
|
100
91
|
console.log("dynamic JSON methods: ok");
|