@fraczak/k 8.7.0 → 8.7.1

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.
@@ -286,11 +286,6 @@ export function expandPatternsInRels(rels, patterns = {}, options = {}) {
286
286
  allPatterns[name] = pat;
287
287
  }
288
288
  }
289
- for (const [name, hash] of Object.entries(lib.patternAlias || {})) {
290
- if (libPatterns[name] && !(hash in allPatterns)) {
291
- allPatterns[hash] = libPatterns[name];
292
- }
293
- }
294
289
  }
295
290
  }
296
291
 
@@ -322,13 +317,6 @@ export function validatePatternDefinitions(
322
317
  allPatterns[name] = pat;
323
318
  }
324
319
  }
325
- for (const [name, hash] of Object.entries(
326
- lib.patternAlias || {}
327
- )) {
328
- if (libPatterns[name] && !(hash in allPatterns)) {
329
- allPatterns[hash] = libPatterns[name];
330
- }
331
- }
332
320
  }
333
321
  }
334
322
 
package/object.mjs CHANGED
@@ -478,16 +478,7 @@ function collectReachable(mainName, rels, allCodes) {
478
478
  }
479
479
 
480
480
  function compileObject(script, options = {}) {
481
- let scriptToAnnotate = script;
482
- if (typeof script === "string" && options.libraries?.length > 0) {
483
- const headerUses = parseHeaderUses(script);
484
- const allSpecs = [...headerUses, ...(options.useSpecs || options.use || [])];
485
- if (allSpecs.length > 0) {
486
- const preamble = buildUsePreamble(allSpecs, options.libraries);
487
- scriptToAnnotate = preamble + script;
488
- }
489
- }
490
- const annotated = annotate(scriptToAnnotate, options);
481
+ const annotated = annotate(script, options);
491
482
  const allCodes = stripDebugFieldsFromCodes(codes.dump());
492
483
  const serialized = serializeAnnotated(annotated);
493
484
  const meta = mergeMetaEntries(
@@ -544,16 +535,7 @@ function rewriteRefsToCanonical(exp, relAlias) {
544
535
  }
545
536
 
546
537
  function compileLibrary(script, options = {}) {
547
- let scriptToAnnotate = script;
548
- if (typeof script === "string" && options.libraries?.length > 0) {
549
- const headerUses = parseHeaderUses(script);
550
- const allSpecs = [...headerUses, ...(options.useSpecs || options.use || [])];
551
- if (allSpecs.length > 0) {
552
- const preamble = buildUsePreamble(allSpecs, options.libraries);
553
- scriptToAnnotate = preamble + script;
554
- }
555
- }
556
- const annotated = annotateLibrary(scriptToAnnotate, options);
538
+ const annotated = annotateLibrary(script, options);
557
539
  const allCodes = stripDebugFieldsFromCodes(codes.dump());
558
540
  const serialized = serializeAnnotated(annotated);
559
541
  const meta = mergeMetaEntries(
@@ -583,6 +565,7 @@ function compileLibrary(script, options = {}) {
583
565
  }
584
566
  for (const [name, rel] of Object.entries(serialized.rels)) {
585
567
  if (name === "__main__") continue;
568
+ if (rel._library) continue;
586
569
  const canonicalName = libRelAlias[name] || name;
587
570
  const { varRefs, ...relWithoutVarRefs } = rel;
588
571
  libRels[canonicalName] = {
package/objects/README.md CHANGED
@@ -22,11 +22,10 @@ node objects/validate.mjs /tmp/k-ok.ko
22
22
  output filename or `--format klib`.
23
23
 
24
24
  When compiling source files, required library dependencies and friendly aliases
25
- can be declared directly in the source file header via
26
- `# --lib <dependency> --use <specs>` comment directives. The compiler
27
- automatically parses these directives and synthesizes the required bindings, so
28
- explicit CLI `--use` flags are only needed for ad-hoc snippets or command-line
29
- expressions.
25
+ can be documented directly in the source file header via
26
+ `# --lib <dependency> --use <specs>` comments. When `--lib` is provided on the
27
+ command line, all library relations and patterns are available in memory during
28
+ compilation. CLI `--use` flags allow creating in-memory aliases when needed.
30
29
 
31
30
  The [file-format reference](../DOCS/FILE_FORMATS.md) is the source for container
32
31
  fields, executable/library distinctions, header directives, aliases, and
@@ -8,9 +8,7 @@ import {
8
8
  compileLibrary,
9
9
  compileLibraryBuffer,
10
10
  decodeObject,
11
- loadLibrary,
12
- buildUsePreamble,
13
- parseHeaderUses
11
+ loadLibrary
14
12
  } from "../object.mjs";
15
13
  import { objectToKVMArtifact } from "../kvm.mjs";
16
14
 
@@ -212,17 +210,14 @@ try {
212
210
  : input.kind === "inline"
213
211
  ? input.source
214
212
  : fs.readFileSync(input.path, "utf8");
215
- const allSpecs = [...parseHeaderUses(rawSource), ...useSpecs];
216
- const preamble = buildUsePreamble(allSpecs, libraries);
217
- const source = preamble + rawSource;
218
- const opts = { source: input.sourceName, libraries };
213
+ const opts = { source: input.sourceName, libraries, useSpecs };
219
214
 
220
215
  if (outputFormat === "ko") {
221
- output = compileObjectBuffer(source, opts);
216
+ output = compileObjectBuffer(rawSource, opts);
222
217
  } else if (outputFormat === "klib") {
223
- output = compileLibraryBuffer(source, opts);
218
+ output = compileLibraryBuffer(rawSource, opts);
224
219
  } else if (outputFormat === "kvm") {
225
- const object = decodeObject(compileObjectBuffer(source, opts));
220
+ const object = decodeObject(compileObjectBuffer(rawSource, opts));
226
221
  output = objectToKVM(object);
227
222
  }
228
223
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fraczak/k",
3
- "version": "8.7.0",
3
+ "version": "8.7.1",
4
4
  "description": "k — a language for content-addressable schemas and data transformations over algebraic data types (products and tagged unions); includes runtime, contract derivation, REPL, and codec toolchain",
5
5
  "publishConfig": {
6
6
  "access": "public"
package/repl.mjs CHANGED
@@ -40,9 +40,7 @@ import {
40
40
  encodeLibrary,
41
41
  hydrateObject,
42
42
  loadLibrary,
43
- prettyRelation,
44
- buildUsePreamble,
45
- parseHeaderUses
43
+ prettyRelation
46
44
  } from "./object.mjs";
47
45
  import {
48
46
  propertyListToFilter,
@@ -1394,10 +1392,9 @@ function loadSourceOrKlib(state, targetPath, options = {}) {
1394
1392
  } else {
1395
1393
  restoreCodes(state);
1396
1394
  const source = fs.readFileSync(loadPath, "utf8");
1397
- const headerUses = parseHeaderUses(source);
1398
- const usePreamble = buildUsePreamble(headerUses, [stateLibrary(state)]);
1399
- const preamble = aliasPreamble(state, definedNamesInSource(source));
1400
- const fullSource = [preamble, usePreamble, source].filter(Boolean).join("\n");
1395
+ const usedNames = new Set(definedNamesInSource(source));
1396
+ const preamble = aliasPreamble(state, usedNames);
1397
+ const fullSource = [preamble, source].filter(Boolean).join("\n");
1401
1398
  const compileOptions = {
1402
1399
  source: targetPath,
1403
1400
  libraries: [stateLibrary(state)],
@@ -48,6 +48,7 @@ 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-library-isolation.mjs", node, ["tests/test-library-isolation.mjs"]],
51
52
  ["tests/test-parse-json.mjs", node, ["tests/test-parse-json.mjs"]],
52
53
  ["tests/test-ieee-text.mjs", node, ["tests/test-ieee-text.mjs"]],
53
54
  ["tests/test-deep-wire-value.mjs", node, ["tests/test-deep-wire-value.mjs"]],
@@ -50,7 +50,12 @@ try {
50
50
  const arithKlib = path.join(scratch, "arithmetics.klib");
51
51
  run("k-compile", [path.join(packageDir, "lib/bit.k"), bitKlib]);
52
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]);
53
+ run("k-compile", [
54
+ "--lib", bitsKlib,
55
+ "--use", "trim inv_loop inv cmp inc_bits dec_bits plus_bits minus_bits mul_bits",
56
+ path.join(packageDir, "lib/arithmetics.k"),
57
+ arithKlib
58
+ ]);
54
59
  const incremented = run("k", ["--lib", arithKlib,
55
60
  "--use", "inc", "inc"], integer);
56
61
  assert.equal(run("k-int", ["--print"], incremented).toString().trim(), "43");
@@ -6,6 +6,7 @@ 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";
9
10
 
10
11
  const root = fileURLToPath(new URL("../../", import.meta.url));
11
12
 
@@ -32,6 +33,13 @@ export function withCompiledLibrary(source, test) {
32
33
  const nextLibrary = path.join(dir, `library-${i}.klib`);
33
34
  const args = ["objects/compile.mjs"];
34
35
  if (library) args.push("--lib", library);
36
+ const filePath = path.isAbsolute(sources[i]) ? sources[i] : path.join(root, sources[i]);
37
+ if (fs.existsSync(filePath)) {
38
+ const raw = fs.readFileSync(filePath, "utf8");
39
+ for (const u of parseHeaderUses(raw)) {
40
+ args.push("--use", u.use);
41
+ }
42
+ }
35
43
  args.push(sources[i], nextLibrary);
36
44
  succeeds(node(args), `compile ${sources[i]}`);
37
45
  library = nextLibrary;
@@ -81,13 +81,28 @@ 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 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"
90
- ./objects/compile.mjs --lib "$TMP_DIR/ieee-types.klib" Examples/ieee-arithmetics.k "$TMP_DIR/ieee.klib"
84
+ compile_step() {
85
+ local prev="$1"
86
+ local src="$2"
87
+ local out="$3"
88
+ local uses=()
89
+ while IFS= read -r line; do
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)
92
+ if [ -n "$prev" ]; then
93
+ ./objects/compile.mjs --lib "$prev" "${uses[@]}" "$src" "$out"
94
+ else
95
+ ./objects/compile.mjs "${uses[@]}" "$src" "$out"
96
+ fi
97
+ }
98
+
99
+ compile_step "" lib/bit.k "$TMP_DIR/bit.klib"
100
+ compile_step "$TMP_DIR/bit.klib" lib/bits.k "$TMP_DIR/bits.klib"
101
+ compile_step "$TMP_DIR/bits.klib" lib/list.k "$TMP_DIR/list.klib"
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"
91
106
  MUL=`./objects/extract-aliases.mjs "$TMP_DIR/ieee.klib" | grep '^mul = @' | sed 's/^mul = \(@[^;]*\);.*$/\1/'`
92
107
  DIV=`./objects/extract-aliases.mjs "$TMP_DIR/ieee.klib" | grep '^div = @' | sed 's/^div = \(@[^;]*\);.*$/\1/'`
93
108
  ARG=0.12
@@ -3,6 +3,7 @@ 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 { parseHeaderUses } from "../canonical-pattern.mjs";
6
7
  import { fromJsonValue, toJsonValue } from "../codecs/djson.mjs";
7
8
  import { textToStringValue, stringValueToText } from "../codecs/runtime/unicode-string.mjs";
8
9
  import { parse as parseInt } from "../codecs/int.mjs";
@@ -17,12 +18,16 @@ for (const file of [
17
18
  "lib/string.k",
18
19
  "lib/ieee.k"
19
20
  ]) {
20
- ieee = loadLibrary(compileLibrary(readFileSync(new URL(`../${file}`, import.meta.url), "utf8"), {
21
+ const fileSource = readFileSync(new URL(`../${file}`, import.meta.url), "utf8");
22
+ const uses = parseHeaderUses(fileSource).map((u) => u.use);
23
+ ieee = loadLibrary(compileLibrary(fileSource, {
21
24
  source: file,
22
- libraries: ieee ? [ieee] : []
25
+ libraries: ieee ? [ieee] : [],
26
+ use: uses
23
27
  }));
24
28
  }
25
- const library = loadLibrary(compileLibrary(source, { libraries: [ieee] }));
29
+ const djsonUses = parseHeaderUses(source).map((u) => u.use);
30
+ const library = loadLibrary(compileLibrary(source, { libraries: [ieee], use: djsonUses }));
26
31
  const run = (op, value) => k.compile(library.relAlias[op], { libraries: [library] })(value);
27
32
 
28
33
  function method(op, json, { key, index, val } = {}) {
@@ -2,6 +2,7 @@ import assert from "node:assert/strict";
2
2
  import { readFileSync } from "node:fs";
3
3
  import k from "../index.mjs";
4
4
  import { compileLibrary, loadLibrary } from "../object.mjs";
5
+ import { parseHeaderUses } from "../canonical-pattern.mjs";
5
6
  import { compileWasmArtifact, instantiateWasmArtifact } from "../backends/wasm/src/wasm.mjs";
6
7
  import { encodeNumberToValue, decodeValueToNumber } from "../codecs/ieee.mjs";
7
8
  import { textToStringValue, stringValueToText } from "../codecs/runtime/unicode-string.mjs";
@@ -16,9 +17,12 @@ const ieeeSources = [
16
17
  ];
17
18
  let ieee = null;
18
19
  for (const file of ieeeSources) {
19
- ieee = loadLibrary(compileLibrary(readFileSync(new URL(`../${file}`, import.meta.url), "utf8"), {
20
+ const fileSource = readFileSync(new URL(`../${file}`, import.meta.url), "utf8");
21
+ const uses = parseHeaderUses(fileSource).map((u) => u.use);
22
+ ieee = loadLibrary(compileLibrary(fileSource, {
20
23
  source: file,
21
- libraries: ieee ? [ieee] : []
24
+ libraries: ieee ? [ieee] : [],
25
+ use: uses
22
26
  }));
23
27
  }
24
28
  assert.equal(ieee.relAlias.add, undefined, "Text conversion must not load IEEE arithmetic");
@@ -14,7 +14,8 @@ import {
14
14
  objectToFunction,
15
15
  runConvergedObject,
16
16
  decompileObjectBuffer,
17
- extractAliasesFromObject
17
+ extractAliasesFromObject,
18
+ parseHeaderUses
18
19
  } from "../object.mjs";
19
20
 
20
21
  const value = parseValue("{a:{b:x,c:{}}}");
@@ -109,9 +110,11 @@ const bitsLib = loadLibrary(decodeObject(compileLibraryBuffer(
109
110
  fs.readFileSync("lib/bits.k", "utf8"),
110
111
  { source: "lib/bits.k", libraries: [bitLib] }
111
112
  )));
113
+ const arithRaw = fs.readFileSync("lib/arithmetics.k", "utf8");
114
+ const arithUses = parseHeaderUses(arithRaw).map((u) => u.use);
112
115
  const arithmeticsLibraryBuffer = compileLibraryBuffer(
113
- fs.readFileSync("lib/arithmetics.k", "utf8"),
114
- { source: "lib/arithmetics.k", libraries: [bitsLib] }
116
+ arithRaw,
117
+ { source: "lib/arithmetics.k", libraries: [bitsLib], use: arithUses }
115
118
  );
116
119
  const arithmeticsDecompiledSource = decompileObjectBuffer(arithmeticsLibraryBuffer);
117
120
  const arithmeticsRoundTripSource = decompileObjectBuffer(
@@ -6,6 +6,7 @@ import {
6
6
  executeKVM
7
7
  } from "../kvm.mjs";
8
8
  import { compileObjectBuffer, decodeObject } from "../object.mjs";
9
+ import { parseHeaderUses } from "../canonical-pattern.mjs";
9
10
  import { objectToKIRP } from "../kir.mjs";
10
11
  import { fromObject, Value } from "../Value.mjs";
11
12
  import { lowerToWasm } from "../backends/wasm/src/kvm2wasm.mjs";
@@ -245,9 +246,12 @@ console.log("==> Starting Polymorphic kVM & Specialization Tests");
245
246
  let prevLib = null;
246
247
  for (const [idx, src] of ["lib/bit.k", "lib/bits.k", "lib/arithmetics.k"].entries()) {
247
248
  const nextLib = path.join(tmpDir, `step-${idx}.klib`);
249
+ const fileSource = fs.readFileSync(path.join(__dirname, "..", src), "utf8");
250
+ const uses = parseHeaderUses(fileSource).flatMap((u) => ["--use", u.use]);
248
251
  const stepRes = spawnSync(process.execPath, [
249
252
  compileScript,
250
253
  ...(prevLib ? ["--lib", prevLib] : []),
254
+ ...uses,
251
255
  src,
252
256
  nextLib
253
257
  ], {
@@ -3,6 +3,7 @@ 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 { parseHeaderUses } from "../canonical-pattern.mjs";
6
7
  import { fromJsonValue, toJsonValue } from "../codecs/djson.mjs";
7
8
  import { textToStringValue, stringValueToText } from "../codecs/runtime/unicode-string.mjs";
8
9
  import { encodeNumberToValue, decodeValueToNumber } from "../codecs/ieee.mjs";
@@ -11,26 +12,27 @@ import { parse as parseInt, parseIntStr, print as printInt } from "../codecs/int
11
12
  const libFiles = [
12
13
  "lib/bit.k",
13
14
  "lib/bits.k",
14
- "lib/arithmetics.k",
15
15
  "lib/list.k",
16
16
  "lib/btrie.k",
17
17
  "lib/string.k",
18
18
  "lib/ieee.k",
19
- "lib/djson.k"
19
+ "lib/djson.k",
20
+ "lib/arithmetics.k"
20
21
  ];
21
22
 
22
23
  const libs = [];
23
24
  for (const file of libFiles) {
24
25
  const source = readFileSync(new URL(`../${file}`, import.meta.url), "utf8");
26
+ const uses = parseHeaderUses(source).map((u) => u.use);
25
27
  const compiled = loadLibrary(
26
- compileLibrary(source, { source: file, libraries: libs })
28
+ compileLibrary(source, { source: file, libraries: libs, use: uses })
27
29
  );
28
30
  libs.push(compiled);
29
31
  }
30
32
 
31
- const arithLib = libs[2];
32
- const ieeeLib = libs[6];
33
- const djsonLib = libs[7];
33
+ const ieeeLib = libs[5];
34
+ const djsonLib = libs[6];
35
+ const arithLib = libs[7];
34
36
 
35
37
  const runArith = (op, value) =>
36
38
  k.compile(arithLib.relAlias[op], { libraries: [arithLib] })(value);