@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.
Files changed (71) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/DOCS/CODECS.md +20 -14
  3. package/DOCS/FILE_FORMATS.md +79 -12
  4. package/DOCS/GETTING_STARTED.md +17 -8
  5. package/DOCS/PATTERNS.md +17 -0
  6. package/DOCS/REPL.md +15 -8
  7. package/Examples/arithmetics.k +81 -468
  8. package/Examples/ieee-arithmetics.k +37 -33
  9. package/Examples/poly.k +107 -104
  10. package/README.md +19 -11
  11. package/backend-api.mjs +3 -1
  12. package/backends/llvm/README.md +1 -1
  13. package/backends/llvm/bin/k-llvm-build.mjs +5 -5
  14. package/backends/llvm/bin/k-llvm-compile.mjs +4 -4
  15. package/backends/llvm/bin/k-llvm.mjs +6 -6
  16. package/backends/llvm/src/cli.mjs +17 -12
  17. package/backends/llvm/tests/integration.sh +4 -1
  18. package/backends/wasm/README.md +3 -3
  19. package/backends/wasm/bin/k-wasm-compile.mjs +4 -4
  20. package/backends/wasm/bin/k-wasm.mjs +4 -4
  21. package/backends/wasm/src/cli.mjs +12 -10
  22. package/backends/wasm/src/wasm.mjs +6 -2
  23. package/backends/wasm/tests/integration.sh +8 -3
  24. package/browser/shims/fs.mjs +19 -3
  25. package/canonical-pattern.mjs +351 -82
  26. package/core.k +2 -1
  27. package/harness/suites/poly.mjs +14 -30
  28. package/k.mjs +17 -13
  29. package/kvm.mjs +13 -9
  30. package/lib/arithmetics.k +287 -0
  31. package/lib/bit.k +68 -0
  32. package/lib/bits.k +221 -0
  33. package/lib/btrie.k +187 -0
  34. package/lib/djson.k +326 -0
  35. package/lib/ieee.k +709 -0
  36. package/lib/list.k +49 -0
  37. package/lib/string.k +588 -0
  38. package/named-patterns.mjs +2 -2
  39. package/object.mjs +52 -10
  40. package/objects/README.md +10 -3
  41. package/objects/compile.mjs +10 -8
  42. package/package.json +1 -1
  43. package/repl.mjs +227 -22
  44. package/scripts/benchmark-idempotency.mjs +22 -15
  45. package/scripts/benchmark.mjs +29 -10
  46. package/scripts/build-repl-html.mjs +49 -14
  47. package/scripts/perf-ieee.mjs +9 -1
  48. package/scripts/perf-int.mjs +10 -17
  49. package/scripts/perf-multiplication.mjs +9 -15
  50. package/scripts/perf-poly.mjs +16 -32
  51. package/scripts/run-tests.mjs +2 -2
  52. package/scripts/test-package.mjs +8 -2
  53. package/tests/helpers/cli.mjs +11 -3
  54. package/tests/integration.sh +9 -4
  55. package/tests/test-arithmetics.mjs +4 -2
  56. package/tests/test-djson-methods.mjs +16 -12
  57. package/tests/test-ieee-arithmetic.mjs +11 -3
  58. package/tests/test-ieee-text.mjs +15 -4
  59. package/tests/test-k-object.mjs +11 -3
  60. package/tests/test-karatsuba.mjs +1 -1
  61. package/tests/test-kvm-polymorphic.mjs +54 -34
  62. package/tests/test-lib.mjs +115 -0
  63. package/tests/test-named-patterns.mjs +12 -3
  64. package/tests/test-parse-json.mjs +33 -13
  65. package/tests/test-poly.mjs +10 -4
  66. package/tests/test-repl-html.mjs +18 -7
  67. package/tests/test-repl.mjs +154 -54
  68. package/Examples/djson.k +0 -894
  69. package/Examples/ieee.k +0 -969
  70. package/Examples/karatsuba-mult.k +0 -227
  71. package/Examples/list.k +0 -10
@@ -5,7 +5,7 @@ import {
5
5
  compileLibrary,
6
6
  decodeObject,
7
7
  loadLibrary,
8
- buildExportPreamble
8
+ buildUsePreamble
9
9
  } from "@fraczak/k/backend-api.mjs";
10
10
  import {
11
11
  compileWasmArtifact,
@@ -27,7 +27,7 @@ function readAll(stream) {
27
27
  });
28
28
  }
29
29
 
30
- export { buildExportPreamble };
30
+ export { buildUsePreamble };
31
31
 
32
32
  export function loadLibraryFromPath(libPath) {
33
33
  const ext = path.extname(libPath).toLowerCase();
@@ -43,7 +43,7 @@ export function loadLibraryFromPath(libPath) {
43
43
 
44
44
  function parseCompileOptions(args) {
45
45
  const libraries = [];
46
- const exportSpecs = [];
46
+ const useSpecs = [];
47
47
  let loadedLibrary = false;
48
48
  let usePKIR = false;
49
49
 
@@ -61,15 +61,15 @@ function parseCompileOptions(args) {
61
61
  if (!libPath) throw new Error("--lib requires a file argument");
62
62
  libraries.push(loadLibraryFromPath(libPath));
63
63
  loadedLibrary = true;
64
- } else if (args[0] === "--export") {
64
+ } else if (args[0] === "--use") {
65
65
  args.shift();
66
66
  const spec = args.shift();
67
67
  if (!spec) {
68
68
  throw new Error(
69
- "--export requires a spec argument (name or libname:localname)"
69
+ "--use requires a spec argument (name or libname:localname)"
70
70
  );
71
71
  }
72
- exportSpecs.push(spec);
72
+ useSpecs.push(spec);
73
73
  } else if (args[0] === "--pkir") {
74
74
  args.shift();
75
75
  usePKIR = true;
@@ -80,7 +80,7 @@ function parseCompileOptions(args) {
80
80
  }
81
81
  }
82
82
 
83
- return { libraries, exportSpecs, usePKIR };
83
+ return { libraries, useSpecs, usePKIR };
84
84
  }
85
85
 
86
86
  function fileInputType(filePath) {
@@ -126,16 +126,18 @@ async function compileProgramInput(
126
126
  input,
127
127
  {
128
128
  libraries = [],
129
+ useSpecs = [],
129
130
  exportSpecs = [],
130
131
  stdin = null,
131
132
  inputEnvelopePattern = null,
132
133
  usePKIR = false
133
134
  } = {}
134
135
  ) {
136
+ const specs = useSpecs.length > 0 ? useSpecs : exportSpecs;
135
137
  if (input.kind === "inline-source") {
136
138
  return compileWasmArtifact(input.source, {
137
139
  libraries,
138
- exports: exportSpecs,
140
+ use: specs,
139
141
  source: input.sourceName,
140
142
  inputEnvelopePattern,
141
143
  usePKIR
@@ -148,7 +150,7 @@ async function compileProgramInput(
148
150
  }
149
151
  return compileWasmArtifact((await readAll(stdin)).toString("utf8"), {
150
152
  libraries,
151
- exports: exportSpecs,
153
+ use: specs,
152
154
  source: input.sourceName,
153
155
  inputEnvelopePattern,
154
156
  usePKIR
@@ -158,7 +160,7 @@ async function compileProgramInput(
158
160
  if (input.kind === "source-file") {
159
161
  return compileWasmArtifact(fs.readFileSync(input.path, "utf8"), {
160
162
  libraries,
161
- exports: exportSpecs,
163
+ use: specs,
162
164
  source: input.sourceName,
163
165
  inputEnvelopePattern,
164
166
  usePKIR
@@ -3,7 +3,8 @@ import path from "node:path";
3
3
  import { fileURLToPath } from "node:url";
4
4
  import {
5
5
  annotate,
6
- buildExportPreamble,
6
+ buildUsePreamble,
7
+ parseHeaderUses,
7
8
  decodeWire,
8
9
  encodeToWire,
9
10
  exportPatternGraph,
@@ -642,13 +643,16 @@ async function compileWasmArtifact(
642
643
  source,
643
644
  {
644
645
  libraries = [],
646
+ use = [],
645
647
  exports = [],
646
648
  source: sourceName = null,
647
649
  inputEnvelopePattern = null,
648
650
  usePKIR = false
649
651
  } = {}
650
652
  ) {
651
- const preamble = buildExportPreamble(exports, libraries);
653
+ const specs = use.length > 0 ? use : exports;
654
+ const allSpecs = [...parseHeaderUses(source), ...specs];
655
+ const preamble = buildUsePreamble(allSpecs, libraries);
652
656
  const fullSource = preamble + source;
653
657
  const options = {
654
658
  libraries,
@@ -172,13 +172,18 @@ printf '{"x":{"a":{}},"y":{"b":{}}}\n' |
172
172
  node "$K_ROOT/codecs/k-print.mjs" |
173
173
  grep -qx '"b"'
174
174
 
175
- node "$K_ROOT/objects/compile.mjs" --lib "$K_ROOT/core.k" "$K_ROOT/Examples/ieee.k" "$TMP_DIR/ieee-types.klib"
175
+ node "$K_ROOT/objects/compile.mjs" "$K_ROOT/lib/bit.k" "$TMP_DIR/bit.klib"
176
+ node "$K_ROOT/objects/compile.mjs" --lib "$TMP_DIR/bit.klib" "$K_ROOT/lib/bits.k" "$TMP_DIR/bits.klib"
177
+ node "$K_ROOT/objects/compile.mjs" --lib "$TMP_DIR/bits.klib" "$K_ROOT/lib/list.k" "$TMP_DIR/list.klib"
178
+ node "$K_ROOT/objects/compile.mjs" --lib "$TMP_DIR/list.klib" "$K_ROOT/lib/btrie.k" "$TMP_DIR/btrie.klib"
179
+ node "$K_ROOT/objects/compile.mjs" --lib "$TMP_DIR/btrie.klib" "$K_ROOT/lib/string.k" "$TMP_DIR/string.klib"
180
+ node "$K_ROOT/objects/compile.mjs" --lib "$TMP_DIR/string.klib" "$K_ROOT/lib/ieee.k" "$TMP_DIR/ieee-types.klib"
176
181
  node "$K_ROOT/objects/compile.mjs" --lib "$TMP_DIR/ieee-types.klib" "$K_ROOT/Examples/ieee-arithmetics.k" "$TMP_DIR/ieee.klib"
177
- if node ./bin/k-wasm-compile.mjs --lib "$TMP_DIR/ieee.klib" --lib "$TMP_DIR/ieee.klib" --export mul:times "{()x,()y} times .result" "$TMP_DIR/repeated-lib.wasm" >/dev/null 2>&1; then
182
+ if node ./bin/k-wasm-compile.mjs --lib "$TMP_DIR/ieee.klib" --lib "$TMP_DIR/ieee.klib" --use mul:times "{()x,()y} times .result" "$TMP_DIR/repeated-lib.wasm" >/dev/null 2>&1; then
178
183
  echo "--lib unexpectedly accepted more than once" >&2
179
184
  exit 1
180
185
  fi
181
- node ./bin/k-wasm-compile.mjs --lib "$TMP_DIR/ieee.klib" --export mul:times "{()x,()y} times .result" "$TMP_DIR/ieee-mul.wasm"
186
+ node ./bin/k-wasm-compile.mjs --lib "$TMP_DIR/ieee.klib" --use mul:times "{()x,()y} times .result" "$TMP_DIR/ieee-mul.wasm"
182
187
  echo 0.12 |
183
188
  node "$K_ROOT/codecs/ieee.mjs" --parse |
184
189
  node ./bin/k-wasm-run.mjs "$TMP_DIR/ieee-mul.wasm" |
@@ -12,6 +12,8 @@ function cleanPath(p) {
12
12
  let s = (p instanceof URL ? p.pathname : (p?.pathname || String(p))).replace(/\\/g, "/");
13
13
  if (s.startsWith("file://")) s = s.replace(/^file:\/\//, "");
14
14
  if (s.endsWith("runtime.wat")) return "backends/wasm/runtime.wat";
15
+ const libIdx = s.lastIndexOf("lib/");
16
+ if (libIdx !== -1) return s.slice(libIdx);
15
17
  const exIdx = s.lastIndexOf("Examples/");
16
18
  if (exIdx !== -1) return s.slice(exIdx);
17
19
  const wasmIdx = s.lastIndexOf("backends/wasm/");
@@ -32,10 +34,17 @@ export function setVfsFile(path, content) {
32
34
  export function getVfsFile(path) {
33
35
  const cleaned = cleanPath(path);
34
36
  if (vfs.has(cleaned)) return vfs.get(cleaned);
37
+ if (cleaned.startsWith("lib/")) {
38
+ const stripped = cleaned.slice("lib/".length);
39
+ if (vfs.has(stripped)) return vfs.get(stripped);
40
+ } else if (!cleaned.startsWith("Examples/")) {
41
+ const withLib = `lib/${cleaned}`;
42
+ if (vfs.has(withLib)) return vfs.get(withLib);
43
+ }
35
44
  if (cleaned.startsWith("Examples/")) {
36
45
  const stripped = cleaned.slice("Examples/".length);
37
46
  if (vfs.has(stripped)) return vfs.get(stripped);
38
- } else {
47
+ } else if (!cleaned.startsWith("lib/")) {
39
48
  const withEx = `Examples/${cleaned}`;
40
49
  if (vfs.has(withEx)) return vfs.get(withEx);
41
50
  }
@@ -51,12 +60,19 @@ export function getVfsFile(path) {
51
60
 
52
61
  const CURATED_VFS_FILES = [
53
62
  "core.k",
63
+ "bit.k",
64
+ "bits.k",
54
65
  "arithmetics.k",
66
+ "list.k",
67
+ "btrie.k",
68
+ "string.k",
55
69
  "ieee.k",
70
+ "djson.k",
56
71
  "ieee-arithmetics.k",
57
72
  "poly.k",
58
- "karatsuba-mult.k",
73
+ "nat.k",
59
74
  "json.mjs",
75
+ "djson.mjs",
60
76
  "utf8.mjs",
61
77
  "int.mjs",
62
78
  "unit.mjs",
@@ -72,7 +88,7 @@ export function getAllVfsFiles() {
72
88
  }
73
89
  for (const key of vfs.keys()) {
74
90
  if (key.endsWith("runtime.wat") || key.startsWith("backends/")) continue;
75
- if (key.startsWith("Examples/") || key.startsWith("codecs/")) continue;
91
+ if (key.startsWith("lib/") || key.startsWith("Examples/") || key.startsWith("codecs/")) continue;
76
92
  result.add(key);
77
93
  }
78
94
  return Array.from(result);