@fraczak/k 6.0.2 → 6.0.4
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/DOCS/KIR_SPECIALIZATION_DESIGN.md +458 -0
- package/DOCS/OBJECT_FILE_AND_PATTERN.md +133 -43
- package/DOCS/REPL.md +10 -7
- package/README.md +16 -6
- package/UU/ta.k +1 -0
- package/UU/ta.k~ +0 -0
- package/UU/tb.k +0 -0
- package/UU/tc.k +0 -0
- package/codecs/README.md +4 -0
- package/core.json +1 -0
- package/core.klib +1 -0
- package/core.klib~ +0 -0
- package/export.mjs +1 -1
- package/ieee.klib +0 -0
- package/index.mjs +1 -1
- package/kkk.k +197 -0
- package/kkk.klib +0 -0
- package/kkk.klib~ +0 -0
- package/object.mjs +452 -57
- package/objects/README.md +28 -1
- package/objects/compile-lib.mjs +27 -2
- package/objects/compile.mjs +27 -2
- package/objects/decompile.mjs +25 -2
- package/objects/extract-aliases.mjs +59 -0
- package/p.json +5475 -0
- package/package.json +2 -1
- package/repl2.mjs +106 -38
- package/run.mjs +172 -2
- package/test-k-object.mjs +129 -2
- package/test-repl2.mjs +10 -0
package/objects/README.md
CHANGED
|
@@ -8,6 +8,7 @@ CLI tools for compiling and inspecting k object files (`.ko`) and library files
|
|
|
8
8
|
|
|
9
9
|
Compiles a `.k` source file into an executable object file (`.ko`).
|
|
10
10
|
The output contains only the codes and relations reachable from `main`.
|
|
11
|
+
Each stored relation includes `typeDerivation.status` for backend eligibility.
|
|
11
12
|
|
|
12
13
|
```bash
|
|
13
14
|
./objects/compile.mjs [--lib lib-file]... [k-file [object-file]]
|
|
@@ -16,8 +17,12 @@ The output contains only the codes and relations reachable from `main`.
|
|
|
16
17
|
### compile-lib.mjs
|
|
17
18
|
|
|
18
19
|
Compiles a `.k` source file into a library file (`.klib`).
|
|
19
|
-
The output
|
|
20
|
+
The output is plain JSON with no binary header. It contains the library closure
|
|
21
|
+
formed from loaded `--lib` dependencies plus the compiled source definitions,
|
|
22
|
+
with `main: null`.
|
|
20
23
|
Use `--lib` to build on top of existing libraries.
|
|
24
|
+
Each stored relation includes `typeDerivation.status`; libraries may contain a
|
|
25
|
+
mix of converged and non-converged relations.
|
|
21
26
|
|
|
22
27
|
```bash
|
|
23
28
|
./objects/compile-lib.mjs [--lib lib-file]... [k-file [lib-file]]
|
|
@@ -31,6 +36,28 @@ Decompiles a `.ko` or `.klib` back into human-readable k source.
|
|
|
31
36
|
./objects/decompile.mjs [object-file [k-file]]
|
|
32
37
|
```
|
|
33
38
|
|
|
39
|
+
### extract-aliases.mjs
|
|
40
|
+
|
|
41
|
+
Extracts metadata aliases from a `.ko` or `.klib` as a valid k definition
|
|
42
|
+
snippet. Output is grouped by metadata type (`code`, then `rel`), then sorted by
|
|
43
|
+
alias name and `compiledAt`.
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
./objects/extract-aliases.mjs [object-file [k-file]]
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Current Format Notes
|
|
50
|
+
|
|
51
|
+
- `.klib` files are plain UTF-8 JSON, not binary containers.
|
|
52
|
+
- `.ko` files use the `KOBJ\n` binary header followed by a JSON payload.
|
|
53
|
+
- There is no object payload version field in either format.
|
|
54
|
+
- `typeDerivation` belongs only to relations and currently stores only
|
|
55
|
+
`status`.
|
|
56
|
+
- Source `start` / `end` ranges live on `meta[hash].origins[]` entries.
|
|
57
|
+
- Origin entries do not have `kind`; the metadata entry has `type: "code"` or
|
|
58
|
+
`type: "rel"`.
|
|
59
|
+
- Stored relation bodies do not include generated input/output boundary filters.
|
|
60
|
+
|
|
34
61
|
## Further reading
|
|
35
62
|
|
|
36
63
|
- [DOCS/OBJECT_FILE_AND_PATTERN.md](../DOCS/OBJECT_FILE_AND_PATTERN.md) — object file format and pattern encoding
|
package/objects/compile-lib.mjs
CHANGED
|
@@ -4,8 +4,28 @@ import fs from "node:fs";
|
|
|
4
4
|
import { argv, exit, stdin, stdout } from "node:process";
|
|
5
5
|
import { compileLibraryBuffer, decodeObject, loadLibrary } from "../object.mjs";
|
|
6
6
|
|
|
7
|
-
function
|
|
8
|
-
|
|
7
|
+
function helpText() {
|
|
8
|
+
return [
|
|
9
|
+
"Compile a k source file into a plain-JSON .klib library object.",
|
|
10
|
+
"The library records type-derivation convergence for each stored relation.",
|
|
11
|
+
"",
|
|
12
|
+
`Usage: ${argv[1]} [ --lib lib-file ]... [ k-file [ lib-file ] ]`,
|
|
13
|
+
"",
|
|
14
|
+
"Arguments:",
|
|
15
|
+
" k-file Source .k file to compile. Reads UTF-8 source from stdin when omitted.",
|
|
16
|
+
" lib-file Output .klib path. Writes plain JSON library data to stdout when omitted.",
|
|
17
|
+
"",
|
|
18
|
+
"Options:",
|
|
19
|
+
" --lib file Load a .klib dependency before compiling. May be repeated.",
|
|
20
|
+
" -h, --help Show this help.",
|
|
21
|
+
"",
|
|
22
|
+
"Example:",
|
|
23
|
+
` ${argv[1]} --lib core.klib library.k library.klib`
|
|
24
|
+
].join("\n");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function usage(stream = console.error) {
|
|
28
|
+
stream(helpText());
|
|
9
29
|
}
|
|
10
30
|
|
|
11
31
|
async function readStdinText() {
|
|
@@ -18,6 +38,11 @@ async function readStdinText() {
|
|
|
18
38
|
|
|
19
39
|
try {
|
|
20
40
|
const args = argv.slice(2);
|
|
41
|
+
if (args.includes("-h") || args.includes("--help")) {
|
|
42
|
+
usage(console.log);
|
|
43
|
+
exit(0);
|
|
44
|
+
}
|
|
45
|
+
|
|
21
46
|
const libraries = [];
|
|
22
47
|
while (args.length > 0 && args[0] === "--lib") {
|
|
23
48
|
args.shift();
|
package/objects/compile.mjs
CHANGED
|
@@ -4,8 +4,28 @@ import fs from "node:fs";
|
|
|
4
4
|
import { argv, exit, stdin, stdout } from "node:process";
|
|
5
5
|
import { compileObjectBuffer, decodeObject, loadLibrary } from "../object.mjs";
|
|
6
6
|
|
|
7
|
-
function
|
|
8
|
-
|
|
7
|
+
function helpText() {
|
|
8
|
+
return [
|
|
9
|
+
"Compile a k source file into an executable .ko object.",
|
|
10
|
+
"The object records type-derivation convergence for each stored relation.",
|
|
11
|
+
"",
|
|
12
|
+
`Usage: ${argv[1]} [ --lib lib-file ]... [ k-file [ object-file ] ]`,
|
|
13
|
+
"",
|
|
14
|
+
"Arguments:",
|
|
15
|
+
" k-file Source .k file to compile. Reads UTF-8 source from stdin when omitted.",
|
|
16
|
+
" object-file Output .ko path. Writes binary object data to stdout when omitted.",
|
|
17
|
+
"",
|
|
18
|
+
"Options:",
|
|
19
|
+
" --lib file Load a .klib dependency before compiling. May be repeated.",
|
|
20
|
+
" -h, --help Show this help.",
|
|
21
|
+
"",
|
|
22
|
+
"Example:",
|
|
23
|
+
` ${argv[1]} --lib core.klib program.k program.ko`
|
|
24
|
+
].join("\n");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function usage(stream = console.error) {
|
|
28
|
+
stream(helpText());
|
|
9
29
|
}
|
|
10
30
|
|
|
11
31
|
async function readStdinText() {
|
|
@@ -18,6 +38,11 @@ async function readStdinText() {
|
|
|
18
38
|
|
|
19
39
|
try {
|
|
20
40
|
const args = argv.slice(2);
|
|
41
|
+
if (args.includes("-h") || args.includes("--help")) {
|
|
42
|
+
usage(console.log);
|
|
43
|
+
exit(0);
|
|
44
|
+
}
|
|
45
|
+
|
|
21
46
|
const libraries = [];
|
|
22
47
|
while (args.length > 0 && args[0] === "--lib") {
|
|
23
48
|
args.shift();
|
package/objects/decompile.mjs
CHANGED
|
@@ -4,8 +4,26 @@ import fs from "node:fs";
|
|
|
4
4
|
import { argv, exit, stdin, stdout } from "node:process";
|
|
5
5
|
import { decompileObjectBuffer } from "../object.mjs";
|
|
6
6
|
|
|
7
|
-
function
|
|
8
|
-
|
|
7
|
+
function helpText() {
|
|
8
|
+
return [
|
|
9
|
+
"Decompile a .ko or .klib object into readable k source.",
|
|
10
|
+
"",
|
|
11
|
+
`Usage: ${argv[1]} [ object-file [ k-file ] ]`,
|
|
12
|
+
"",
|
|
13
|
+
"Arguments:",
|
|
14
|
+
" object-file Input .ko or .klib path. Reads object data from stdin when omitted.",
|
|
15
|
+
" k-file Output .k path. Writes source to stdout when omitted.",
|
|
16
|
+
"",
|
|
17
|
+
"Options:",
|
|
18
|
+
" -h, --help Show this help.",
|
|
19
|
+
"",
|
|
20
|
+
"Example:",
|
|
21
|
+
` ${argv[1]} program.ko program.decompiled.k`
|
|
22
|
+
].join("\n");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function usage(stream = console.error) {
|
|
26
|
+
stream(helpText());
|
|
9
27
|
}
|
|
10
28
|
|
|
11
29
|
async function readStdinBuffer() {
|
|
@@ -18,6 +36,11 @@ async function readStdinBuffer() {
|
|
|
18
36
|
|
|
19
37
|
try {
|
|
20
38
|
const [, , objectPath, outputPath] = argv;
|
|
39
|
+
if (objectPath === "-h" || objectPath === "--help") {
|
|
40
|
+
usage(console.log);
|
|
41
|
+
exit(0);
|
|
42
|
+
}
|
|
43
|
+
|
|
21
44
|
const objectBuffer = objectPath == null ? await readStdinBuffer() : fs.readFileSync(objectPath);
|
|
22
45
|
const source = decompileObjectBuffer(objectBuffer);
|
|
23
46
|
if (outputPath == null) {
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import { argv, exit, stdin, stdout } from "node:process";
|
|
5
|
+
import { extractAliasesFromObjectBuffer } from "../object.mjs";
|
|
6
|
+
|
|
7
|
+
function helpText() {
|
|
8
|
+
return [
|
|
9
|
+
"Extract metadata aliases from a .ko or .klib as a valid k source snippet.",
|
|
10
|
+
"",
|
|
11
|
+
`Usage: ${argv[1]} [ object-file [ k-file ] ]`,
|
|
12
|
+
"",
|
|
13
|
+
"Output is grouped by metadata type (code, then rel), then sorted by alias",
|
|
14
|
+
"name and compiledAt. Earlier definitions of the same alias are commented",
|
|
15
|
+
"out so only the latest one remains active.",
|
|
16
|
+
"",
|
|
17
|
+
"Arguments:",
|
|
18
|
+
" object-file Input .ko or .klib path. Reads object data from stdin when omitted.",
|
|
19
|
+
" k-file Output .k path. Writes source to stdout when omitted.",
|
|
20
|
+
"",
|
|
21
|
+
"Options:",
|
|
22
|
+
" -h, --help Show this help.",
|
|
23
|
+
"",
|
|
24
|
+
"Example:",
|
|
25
|
+
` ${argv[1]} core.klib aliases.k`
|
|
26
|
+
].join("\n");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function usage(stream = console.error) {
|
|
30
|
+
stream(helpText());
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function readStdinBuffer() {
|
|
34
|
+
const chunks = [];
|
|
35
|
+
for await (const chunk of stdin) {
|
|
36
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
37
|
+
}
|
|
38
|
+
return Buffer.concat(chunks);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
const [, , objectPath, outputPath] = argv;
|
|
43
|
+
if (objectPath === "-h" || objectPath === "--help") {
|
|
44
|
+
usage(console.log);
|
|
45
|
+
exit(0);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const objectBuffer = objectPath == null ? await readStdinBuffer() : fs.readFileSync(objectPath);
|
|
49
|
+
const source = extractAliasesFromObjectBuffer(objectBuffer);
|
|
50
|
+
if (outputPath == null) {
|
|
51
|
+
stdout.write(source);
|
|
52
|
+
} else {
|
|
53
|
+
fs.writeFileSync(outputPath, source);
|
|
54
|
+
}
|
|
55
|
+
} catch (error) {
|
|
56
|
+
console.error(error);
|
|
57
|
+
usage();
|
|
58
|
+
exit(1);
|
|
59
|
+
}
|