@skillstech/thunderlang 1.0.4 → 1.2.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 (81) hide show
  1. package/CHANGELOG.md +221 -0
  2. package/README.md +4 -1
  3. package/dist/core.cjs +2568 -462
  4. package/dist/index.cjs +7562 -5084
  5. package/dist/learn.cjs +16994 -8339
  6. package/intent-graph.schema.json +11 -0
  7. package/package.json +6 -2
  8. package/src/cli-home.mjs +242 -0
  9. package/src/cli.mjs +241 -89
  10. package/src/codegen.mjs +145 -55
  11. package/src/conformance.mjs +15 -6
  12. package/src/core/check.mjs +725 -0
  13. package/src/core/cli.mjs +184 -0
  14. package/src/core/diagnostics.mjs +82 -0
  15. package/src/core/engine-core.mjs +572 -0
  16. package/src/core/engine-values.mjs +1054 -0
  17. package/src/core/engine-worker.mjs +15 -0
  18. package/src/core/engine.mjs +63 -0
  19. package/src/core/format.mjs +20 -0
  20. package/src/core/from-python.mjs +645 -0
  21. package/src/core/index.mjs +39 -0
  22. package/src/core/lexer.mjs +115 -0
  23. package/src/core/parser.mjs +742 -0
  24. package/src/core/project.mjs +177 -0
  25. package/src/core/round-trip.mjs +150 -0
  26. package/src/core/runtime-python.mjs +801 -0
  27. package/src/core/runtime-typescript.mjs +119 -0
  28. package/src/core/target-python.mjs +487 -0
  29. package/src/core/target-typescript.mjs +466 -0
  30. package/src/core/typescript-run.mjs +114 -0
  31. package/src/core.d.ts +5 -2
  32. package/src/core.mjs +5 -2
  33. package/src/data-schema.mjs +8 -1
  34. package/src/emit.mjs +1 -1
  35. package/src/evidence.mjs +1 -1
  36. package/src/expr.mjs +81 -4
  37. package/src/form-words.mjs +2 -0
  38. package/src/index.d.ts +8 -1
  39. package/src/index.mjs +5 -1
  40. package/src/intent-schema.mjs +1 -0
  41. package/src/learn-lessons.mjs +245 -1
  42. package/src/learn.d.mts +8 -0
  43. package/src/learn.mjs +40 -4
  44. package/src/mcp.mjs +23 -0
  45. package/src/meaning.mjs +45 -6
  46. package/src/model.d.mts +7 -3
  47. package/src/model.mjs +19 -6
  48. package/src/money.d.mts +18 -0
  49. package/src/money.mjs +124 -0
  50. package/src/plain-contract.mjs +18 -2
  51. package/src/read-level.mjs +146 -0
  52. package/src/read.d.mts +11 -0
  53. package/src/read.mjs +40 -6
  54. package/src/review-comment.d.mts +16 -0
  55. package/src/review-comment.mjs +145 -0
  56. package/src/say-less.mjs +1432 -0
  57. package/src/system.mjs +153 -44
  58. package/src/target-cs.mjs +39 -34
  59. package/src/target-elixir.mjs +40 -30
  60. package/src/target-go.mjs +41 -36
  61. package/src/target-java.mjs +34 -27
  62. package/src/target-kotlin.mjs +43 -35
  63. package/src/target-py.mjs +57 -55
  64. package/src/target-result.d.mts +48 -0
  65. package/src/target-result.mjs +112 -0
  66. package/src/target-rust.mjs +42 -38
  67. package/src/target-scala.mjs +45 -35
  68. package/src/target-ts.mjs +37 -45
  69. package/src/target-types.mjs +41 -5
  70. package/src/target-util.mjs +55 -2
  71. package/src/term.mjs +89 -0
  72. package/src/trace-cli.mjs +58 -0
  73. package/src/trace-fs.mjs +76 -0
  74. package/src/trace.mjs +261 -0
  75. package/src/translate.d.mts +3 -0
  76. package/src/translate.mjs +62 -32
  77. package/src/try-cli.mjs +225 -0
  78. package/src/why-cli.mjs +41 -0
  79. package/src/why.d.mts +38 -0
  80. package/src/why.mjs +613 -0
  81. package/src/write-steps.mjs +22 -5
@@ -0,0 +1,15 @@
1
+ // Runs the Thunder engine on its own thread, with a stack deep enough for the recursion Python allows (about 1000
2
+ // calls). engine.mjs starts it and waits for the answer (see runSync there).
3
+ import { workerData } from 'node:worker_threads';
4
+ import { runInProcess, testInProcess } from './engine.mjs';
5
+
6
+ const { shared, port, job } = workerData;
7
+ let result;
8
+ try {
9
+ result = job.tests ? testInProcess(job) : runInProcess(job);
10
+ } catch (e) {
11
+ result = { status: 70, stdout: '', stderr: `The Thunder engine failed: ${e?.stack ?? e}\n` };
12
+ }
13
+ port.postMessage(result);
14
+ Atomics.store(shared, 0, 1);
15
+ Atomics.notify(shared, 0);
@@ -0,0 +1,63 @@
1
+ // The Thunder engine: runs Thunder Core directly, with no Python (thunder run --engine thunder).
2
+ //
3
+ // It runs the SAME checked program the Python target compiles (compileCore -> program + info), so there is one
4
+ // meaning of Thunder, not two. The language itself lives in engine-core.mjs (no Node APIs, so it also runs in a
5
+ // browser); this file adds what a command line needs: reading files from disk, and a worker thread with a deep stack
6
+ // for programs that nest calls deeper than the calling thread allows. compiler/test/core-engine.test.mjs runs every
7
+ // Core test program on both engines and compares the output, errors included. A difference is a bug in one of them.
8
+ //
9
+ // A program that uses a Python module ("Use csv as csv") is not run here: Thunder never imitates a library.
10
+
11
+ import { readFileSync } from 'node:fs';
12
+ import { Worker, MessageChannel, receiveMessageOnPort } from 'node:worker_threads';
13
+ import { resolve as resolvePath } from 'node:path';
14
+ import { ENGINES, pythonModulesUsed, refusalText, runInProcess as runCore, testInProcess as testCore, ThunderErr, PyErr } from './engine-core.mjs';
15
+
16
+ export { ENGINES, pythonModulesUsed, refusalText, ThunderErr, PyErr };
17
+
18
+ /** Files a program reads come from disk, relative to the folder it runs in. */
19
+ const readText = (path, cwd) => readFileSync(resolvePath(cwd, path), 'utf8');
20
+
21
+ /**
22
+ * Run a program with the Thunder engine. `entry` is a compileCore result. Returns { status, stdout, stderr }, the way
23
+ * `python main.py` would end: status 0, or 1 after an error reported in Thunder words. On this thread first (fast, no
24
+ * worker); only a program that nests calls deeper than this thread's stack allows is run again on a worker with a
25
+ * deep stack. Running again is safe: the engine only collects output and reads.
26
+ */
27
+ export function runWithEngine({ entry, cwd = process.cwd() } = {}) {
28
+ const uses = pythonModulesUsed([entry]);
29
+ if (uses.length) return { status: 2, stdout: '', stderr: `${refusalText(uses)}\n`, refused: uses };
30
+ const r = runCore({ entry, cwd, deeper: true, readText });
31
+ return r.needsDeeperStack ? runSync({ entry, cwd }) : r;
32
+ }
33
+
34
+ /** The same, on the calling thread only. */
35
+ export function runInProcess({ entry, cwd = process.cwd(), deeper = false } = {}) { return runCore({ entry, cwd, deeper, readText }); }
36
+
37
+ /** Run every Test block, the way run_tests.py does: one line per test, then the totals. */
38
+ export function testWithEngine({ results, cwd = process.cwd() } = {}) {
39
+ const uses = pythonModulesUsed(results);
40
+ if (uses.length) return { status: 2, stdout: '', stderr: `${refusalText(uses)}\n`, refused: uses };
41
+ const r = testCore({ results, cwd, deeper: true, readText });
42
+ return r.needsDeeperStack ? runSync({ results, cwd, tests: true }) : r;
43
+ }
44
+
45
+ /** The same, on the calling thread only. */
46
+ export function testInProcess({ results, cwd = process.cwd(), deeper = false } = {}) { return testCore({ results, cwd, deeper, readText }); }
47
+
48
+ function runSync(job) {
49
+ const shared = new Int32Array(new SharedArrayBuffer(4));
50
+ const { port1, port2 } = new MessageChannel();
51
+ const worker = new Worker(new URL('./engine-worker.mjs', import.meta.url), {
52
+ workerData: { shared, port: port2, job }, transferList: [port2], resourceLimits: { stackSizeMb: 256 }, stdout: false, stderr: false,
53
+ });
54
+ try {
55
+ while (Atomics.wait(shared, 0, 0, 1000) === 'timed-out') { if (!worker.threadId) break; }
56
+ const msg = receiveMessageOnPort(port1);
57
+ return msg ? msg.message : { status: 70, stdout: '', stderr: 'The Thunder engine stopped without an answer.\n' };
58
+ } finally {
59
+ worker.terminate();
60
+ port1.close();
61
+ }
62
+ }
63
+
@@ -0,0 +1,20 @@
1
+ // thunder format for Thunder Core: two spaces per block level, no trailing spaces, at most one blank line in a
2
+ // row, and a newline at the end. It changes only layout, never words, so it cannot change what a program means.
3
+
4
+ export function formatCore(source) {
5
+ const lines = source.replace(/\r\n?/g, '\n').split('\n');
6
+ const out = [];
7
+ const stack = [0]; // original indents of open blocks
8
+ let blank = 0;
9
+ for (const raw of lines) {
10
+ const text = raw.replace(/\s+$/, '');
11
+ if (!text.trim()) { blank++; continue; }
12
+ const indent = text.match(/^ */)[0].length;
13
+ while (stack.length > 1 && indent < stack[stack.length - 1]) stack.pop();
14
+ if (indent > stack[stack.length - 1]) stack.push(indent);
15
+ if (blank && out.length) out.push('');
16
+ blank = 0;
17
+ out.push(`${' '.repeat(stack.length - 1)}${text.trim()}`);
18
+ }
19
+ return out.length ? `${out.join('\n')}\n` : '';
20
+ }