@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.
- package/CHANGELOG.md +221 -0
- package/README.md +4 -1
- package/dist/core.cjs +2568 -462
- package/dist/index.cjs +7562 -5084
- package/dist/learn.cjs +16994 -8339
- package/intent-graph.schema.json +11 -0
- package/package.json +6 -2
- package/src/cli-home.mjs +242 -0
- package/src/cli.mjs +241 -89
- package/src/codegen.mjs +145 -55
- package/src/conformance.mjs +15 -6
- package/src/core/check.mjs +725 -0
- package/src/core/cli.mjs +184 -0
- package/src/core/diagnostics.mjs +82 -0
- package/src/core/engine-core.mjs +572 -0
- package/src/core/engine-values.mjs +1054 -0
- package/src/core/engine-worker.mjs +15 -0
- package/src/core/engine.mjs +63 -0
- package/src/core/format.mjs +20 -0
- package/src/core/from-python.mjs +645 -0
- package/src/core/index.mjs +39 -0
- package/src/core/lexer.mjs +115 -0
- package/src/core/parser.mjs +742 -0
- package/src/core/project.mjs +177 -0
- package/src/core/round-trip.mjs +150 -0
- package/src/core/runtime-python.mjs +801 -0
- package/src/core/runtime-typescript.mjs +119 -0
- package/src/core/target-python.mjs +487 -0
- package/src/core/target-typescript.mjs +466 -0
- package/src/core/typescript-run.mjs +114 -0
- package/src/core.d.ts +5 -2
- package/src/core.mjs +5 -2
- package/src/data-schema.mjs +8 -1
- package/src/emit.mjs +1 -1
- package/src/evidence.mjs +1 -1
- package/src/expr.mjs +81 -4
- package/src/form-words.mjs +2 -0
- package/src/index.d.ts +8 -1
- package/src/index.mjs +5 -1
- package/src/intent-schema.mjs +1 -0
- package/src/learn-lessons.mjs +245 -1
- package/src/learn.d.mts +8 -0
- package/src/learn.mjs +40 -4
- package/src/mcp.mjs +23 -0
- package/src/meaning.mjs +45 -6
- package/src/model.d.mts +7 -3
- package/src/model.mjs +19 -6
- package/src/money.d.mts +18 -0
- package/src/money.mjs +124 -0
- package/src/plain-contract.mjs +18 -2
- package/src/read-level.mjs +146 -0
- package/src/read.d.mts +11 -0
- package/src/read.mjs +40 -6
- package/src/review-comment.d.mts +16 -0
- package/src/review-comment.mjs +145 -0
- package/src/say-less.mjs +1432 -0
- package/src/system.mjs +153 -44
- package/src/target-cs.mjs +39 -34
- package/src/target-elixir.mjs +40 -30
- package/src/target-go.mjs +41 -36
- package/src/target-java.mjs +34 -27
- package/src/target-kotlin.mjs +43 -35
- package/src/target-py.mjs +57 -55
- package/src/target-result.d.mts +48 -0
- package/src/target-result.mjs +112 -0
- package/src/target-rust.mjs +42 -38
- package/src/target-scala.mjs +45 -35
- package/src/target-ts.mjs +37 -45
- package/src/target-types.mjs +41 -5
- package/src/target-util.mjs +55 -2
- package/src/term.mjs +89 -0
- package/src/trace-cli.mjs +58 -0
- package/src/trace-fs.mjs +76 -0
- package/src/trace.mjs +261 -0
- package/src/translate.d.mts +3 -0
- package/src/translate.mjs +62 -32
- package/src/try-cli.mjs +225 -0
- package/src/why-cli.mjs +41 -0
- package/src/why.d.mts +38 -0
- package/src/why.mjs +613 -0
- package/src/write-steps.mjs +22 -5
package/src/core/cli.mjs
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
// The Thunder Core commands: thunder new | run | build | test | check | format.
|
|
2
|
+
//
|
|
3
|
+
// When does a command mean Thunder Core (and not a mission .thunder file)? Deterministically, never by guessing
|
|
4
|
+
// from the file's contents:
|
|
5
|
+
// - thunder new <dir> always creates a Thunder Core project (missions: thunder init <Name>)
|
|
6
|
+
// - no file given, and a thunder.toml is in this folder or above it
|
|
7
|
+
// - the file is a folder that holds a thunder.toml, or a .thunder file under one
|
|
8
|
+
// - --core is given (a single file with no project)
|
|
9
|
+
// Everything else falls through to the mission commands, unchanged.
|
|
10
|
+
|
|
11
|
+
import { readFileSync, writeFileSync, existsSync, statSync } from 'node:fs';
|
|
12
|
+
import { resolve, relative, join } from 'node:path';
|
|
13
|
+
import { findProjectRoot, buildProject, findPython, runPython, newProject, TARGETS } from './project.mjs';
|
|
14
|
+
import { runTypeScript } from './typescript-run.mjs';
|
|
15
|
+
import { formatDiagnostic } from './diagnostics.mjs';
|
|
16
|
+
import { formatCore } from './format.mjs';
|
|
17
|
+
import { paint } from '../term.mjs';
|
|
18
|
+
import { runWithEngine, testWithEngine, ENGINES } from './engine.mjs';
|
|
19
|
+
|
|
20
|
+
const CORE_COMMANDS = new Set(['new', 'run', 'build', 'test', 'check', 'format', 'fmt']);
|
|
21
|
+
|
|
22
|
+
function parse(argv) {
|
|
23
|
+
const a = { _: [], core: false, json: false, write: false, check: false, force: false, mission: false, engine: null, target: null };
|
|
24
|
+
for (let i = 0; i < argv.length; i++) {
|
|
25
|
+
const x = argv[i];
|
|
26
|
+
if (x === '--target') a.target = argv[++i] ?? '';
|
|
27
|
+
else if (x.startsWith('--target=')) a.target = x.slice('--target='.length);
|
|
28
|
+
else if (x === '--engine') a.engine = argv[++i] ?? '';
|
|
29
|
+
else if (x.startsWith('--engine=')) a.engine = x.slice('--engine='.length);
|
|
30
|
+
else if (x === '--core') a.core = true;
|
|
31
|
+
else if (x === '--json') a.json = true;
|
|
32
|
+
else if (x === '--write' || x === '-w') a.write = true;
|
|
33
|
+
else if (x === '--check') a.check = true;
|
|
34
|
+
else if (x === '--force') a.force = true;
|
|
35
|
+
else if (x === '--mission') a.mission = true;
|
|
36
|
+
else if (x === '--run') a.run = true;
|
|
37
|
+
else if (x.startsWith('--')) a.other = true;
|
|
38
|
+
else a._.push(x);
|
|
39
|
+
}
|
|
40
|
+
return a;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Decide whether this invocation is a Thunder Core one: { root, file } or null. */
|
|
44
|
+
export function coreTarget(cmd, argv, cwd = process.cwd()) {
|
|
45
|
+
if (!CORE_COMMANDS.has(cmd)) return null;
|
|
46
|
+
const a = parse(argv);
|
|
47
|
+
if (cmd === 'new') return a.mission || a._.length === 0 || a.other ? null : { new: a._[0] };
|
|
48
|
+
const file = a._[0];
|
|
49
|
+
if (!file) { const root = findProjectRoot(cwd); return root && !a.other ? { root, file: null } : null; }
|
|
50
|
+
const p = resolve(cwd, file);
|
|
51
|
+
if (existsSync(p) && statSync(p).isDirectory()) { const root = existsSync(join(p, 'thunder.toml')) ? p : null; return root ? { root, file: null } : null; }
|
|
52
|
+
if (!file.endsWith('.thunder')) return null;
|
|
53
|
+
const root = findProjectRoot(p);
|
|
54
|
+
if (root || a.core) return { root, file: p };
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const say = (s = '') => process.stdout.write(`${paint(s)}\n`);
|
|
59
|
+
const warn = (s = '') => process.stderr.write(`${s}\n`);
|
|
60
|
+
|
|
61
|
+
function report(build, cwd) {
|
|
62
|
+
let n = 0;
|
|
63
|
+
for (const r of build.results) for (const d of r.diagnostics) { warn(formatDiagnostic(d, relative(cwd, r.path) || r.rel)); n++; }
|
|
64
|
+
return n;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Run a Thunder Core command. Returns the process exit code, or null when it is not a Core invocation. */
|
|
68
|
+
export function coreMain(cmd, argv, cwd = process.cwd()) {
|
|
69
|
+
const t = coreTarget(cmd, argv, cwd);
|
|
70
|
+
if (!t) return null;
|
|
71
|
+
const a = parse(argv);
|
|
72
|
+
try {
|
|
73
|
+
if (cmd === 'new') {
|
|
74
|
+
const root = newProject(resolve(cwd, t.new), { force: a.force });
|
|
75
|
+
const rel = relative(cwd, root) || '.';
|
|
76
|
+
say(`Created ${rel}, a new Thunder program.`);
|
|
77
|
+
say('');
|
|
78
|
+
say('Run it:');
|
|
79
|
+
say(` thunder run ${rel}`);
|
|
80
|
+
say('');
|
|
81
|
+
say(`Then change ${join(rel, 'src', 'main.thunder')} and run it again. Check it works with: thunder test ${rel}`);
|
|
82
|
+
say(`(Wanted a mission file instead? thunder init ${t.new})`);
|
|
83
|
+
if (!a.run) return 0;
|
|
84
|
+
say('');
|
|
85
|
+
return coreMain('run', [root], cwd);
|
|
86
|
+
}
|
|
87
|
+
if (cmd === 'format' || cmd === 'fmt') return format(t, a, cwd);
|
|
88
|
+
if (a.engine !== null && !ENGINES.includes(a.engine)) { warn(`thunder ${cmd}: there is no engine "${a.engine}". Use --engine python (the default) or --engine thunder.`); return 2; }
|
|
89
|
+
if (a.engine !== null && cmd !== 'run' && cmd !== 'test') { warn(`thunder ${cmd}: --engine is for thunder run and thunder test.`); return 2; }
|
|
90
|
+
if (a.target !== null && !TARGETS.includes(a.target)) { warn(`thunder ${cmd}: there is no target "${a.target}". Use --target python (the default) or --target typescript.`); return 2; }
|
|
91
|
+
if (a.target === 'typescript' && a.engine === 'thunder') { warn(`thunder ${cmd}: choose one: --target typescript runs compiled TypeScript, --engine thunder runs Thunder directly.`); return 2; }
|
|
92
|
+
const build = buildProject({ root: t.root, single: t.file, includeTests: cmd === 'test' || cmd === 'check', write: cmd !== 'check', target: a.target });
|
|
93
|
+
if (cmd === 'check') {
|
|
94
|
+
if (a.json) { say(JSON.stringify({ ok: build.ok, files: build.results.map((r) => ({ file: relative(cwd, r.path), diagnostics: r.diagnostics })) }, null, 2)); return build.ok ? 0 : 1; }
|
|
95
|
+
const n = report(build, cwd);
|
|
96
|
+
if (n) { warn(`thunder check: ${n} problem${n === 1 ? '' : 's'} in ${build.results.filter((r) => !r.ok).length} of ${build.results.length} file${build.results.length === 1 ? '' : 's'}.`); return 1; }
|
|
97
|
+
say(`thunder check: no problems in ${build.results.length} Thunder Core file${build.results.length === 1 ? '' : 's'}.`);
|
|
98
|
+
return 0;
|
|
99
|
+
}
|
|
100
|
+
if (!build.ok) { const n = report(build, cwd); warn(`thunder ${cmd}: ${n} problem${n === 1 ? '' : 's'}; nothing was ${cmd === 'build' ? 'built' : 'run'}.`); return 1; }
|
|
101
|
+
const out = relative(cwd, build.outDir) || '.';
|
|
102
|
+
if (cmd === 'build' && build.target === 'typescript') {
|
|
103
|
+
say(`thunder build: ${build.results.length} file${build.results.length === 1 ? '' : 's'} -> ${out}/ (TypeScript)`);
|
|
104
|
+
for (const r of build.results) say(` ${relative(cwd, r.path)} -> ${out}/${r.module}.ts`);
|
|
105
|
+
say(` ${out}/thunder_runtime.mjs (the small runtime the generated code uses), with its types and a tsconfig.json`);
|
|
106
|
+
say(` run it without Thunder: node ${out}/${build.entryModule}.ts (Node 22.6 or newer; check the types with: tsc -p ${out})`);
|
|
107
|
+
return 0;
|
|
108
|
+
}
|
|
109
|
+
if (cmd === 'build') {
|
|
110
|
+
say(`thunder build: ${build.results.length} file${build.results.length === 1 ? '' : 's'} -> ${out}/ (Python 3.10+)`);
|
|
111
|
+
for (const r of build.results) say(` ${relative(cwd, r.path)} -> ${out}/${r.module}.py`);
|
|
112
|
+
say(` ${out}/thunder_runtime.py (the small runtime the generated code uses)`);
|
|
113
|
+
say(` run it without Thunder: cd ${build.base === cwd ? '.' : relative(cwd, build.base)} && python3 ${relative(build.base, build.outDir)}/${build.entryModule}.py`);
|
|
114
|
+
return 0;
|
|
115
|
+
}
|
|
116
|
+
if (a.engine === 'thunder') return runOnEngine(cmd, build, warn, say);
|
|
117
|
+
if (build.target === 'typescript') return runCompiledTypeScript(cmd, build, warn, say);
|
|
118
|
+
const python = findPython();
|
|
119
|
+
if (python.error) { warn(`thunder ${cmd}: ${python.error}${['run', 'test'].includes(cmd) ? `\nOr run it without Python, on the Thunder engine: thunder ${cmd} ${argv.filter((x) => !x.startsWith('--engine')).join(' ')}${argv.length ? ' ' : ''}--engine thunder`.replace(/ +/g, ' ') : ''}`); return 2; }
|
|
120
|
+
if (cmd === 'run') {
|
|
121
|
+
if (!build.entryModule) { warn('thunder run: there is no program to run (no .thunder files in src/).'); return 1; }
|
|
122
|
+
const r = runPython(python, build.outDir, `${build.entryModule}.py`, build.base);
|
|
123
|
+
return r.status ?? 1;
|
|
124
|
+
}
|
|
125
|
+
if (cmd === 'test') {
|
|
126
|
+
const tests = build.results.reduce((n, r) => n + r.info.tests.length, 0);
|
|
127
|
+
if (!tests) { say('thunder test: no Test blocks found. Write one: Test "what it checks": then Expect … lines.'); return 0; }
|
|
128
|
+
const r = runPython(python, build.outDir, 'run_tests.py', build.base);
|
|
129
|
+
return r.status ?? 1;
|
|
130
|
+
}
|
|
131
|
+
} catch (e) {
|
|
132
|
+
warn(`thunder ${cmd}: ${e.message}`);
|
|
133
|
+
return 2;
|
|
134
|
+
}
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** thunder run / test with the TypeScript target: the compiled TypeScript runs on Node. */
|
|
139
|
+
function runCompiledTypeScript(cmd, build, warn, say) {
|
|
140
|
+
if (cmd === 'run' && !build.entryModule) { warn('thunder run: there is no program to run (no .thunder files in src/).'); return 1; }
|
|
141
|
+
if (cmd === 'test' && !build.results.reduce((n, r) => n + r.info.tests.length, 0)) { say('thunder test: no Test blocks found. Write one: Test "what it checks": then Expect … lines.'); return 0; }
|
|
142
|
+
const r = runTypeScript(build.outDir, cmd === 'run' ? `${build.entryModule}.ts` : 'run_tests.ts', { cwd: build.base, stdio: 'inherit' });
|
|
143
|
+
if (r.error && typeof r.error === 'string') { warn(`thunder ${cmd}: ${r.error}`); return 2; }
|
|
144
|
+
return r.status ?? 1;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** thunder run / test --engine thunder: the program runs on the Thunder engine, with no Python. */
|
|
148
|
+
function runOnEngine(cmd, build, warn, say) {
|
|
149
|
+
if (cmd === 'run') {
|
|
150
|
+
if (!build.entry) { warn('thunder run: there is no program to run (no .thunder files in src/).'); return 1; }
|
|
151
|
+
const r = runWithEngine({ entry: build.entry, cwd: build.base });
|
|
152
|
+
if (r.stdout) process.stdout.write(r.stdout);
|
|
153
|
+
if (r.stderr) process.stderr.write(r.stderr.startsWith('This program uses') ? `thunder run: ${r.stderr}` : r.stderr);
|
|
154
|
+
return r.status;
|
|
155
|
+
}
|
|
156
|
+
const tests = build.results.reduce((n, r) => n + r.info.tests.length, 0);
|
|
157
|
+
if (!tests) { say('thunder test: no Test blocks found. Write one: Test "what it checks": then Expect … lines.'); return 0; }
|
|
158
|
+
const r = testWithEngine({ results: build.results, cwd: build.base });
|
|
159
|
+
if (r.stdout) process.stdout.write(r.stdout);
|
|
160
|
+
if (r.stderr) process.stderr.write(`thunder test: ${r.stderr}`);
|
|
161
|
+
return r.status;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function format(t, a, cwd) {
|
|
165
|
+
let files = [];
|
|
166
|
+
if (t.file) files = [t.file];
|
|
167
|
+
else {
|
|
168
|
+
const b = buildProject({ root: t.root, includeTests: true, write: false });
|
|
169
|
+
files = b.results.map((r) => r.path);
|
|
170
|
+
}
|
|
171
|
+
let changed = 0;
|
|
172
|
+
for (const f of files) {
|
|
173
|
+
const src = readFileSync(f, 'utf8');
|
|
174
|
+
const out = formatCore(src);
|
|
175
|
+
if (out === src) continue;
|
|
176
|
+
changed++;
|
|
177
|
+
if (a.write) { writeFileSync(f, out); say(`formatted ${relative(cwd, f)}`); }
|
|
178
|
+
else if (a.check) warn(`would format ${relative(cwd, f)}`);
|
|
179
|
+
else process.stdout.write(out);
|
|
180
|
+
}
|
|
181
|
+
if (a.check) return changed ? 1 : 0;
|
|
182
|
+
if (a.write && !changed) say('thunder format: nothing to change.');
|
|
183
|
+
return 0;
|
|
184
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// Thunder Core diagnostics: stable codes, plain words, always a line number.
|
|
2
|
+
//
|
|
3
|
+
// Codes never change meaning once released. TC1xx = layout and spelling of a line, TC2xx = names,
|
|
4
|
+
// TC3xx = kinds of values (types), TC4xx = things Thunder Core does not support yet, TC5xx = runtime
|
|
5
|
+
// (reported by the generated program, listed here so the docs and the runtime agree).
|
|
6
|
+
|
|
7
|
+
export const CODES = {
|
|
8
|
+
TC101: 'Tabs are not used for indentation',
|
|
9
|
+
TC102: 'A line is indented but nothing opens a block above it',
|
|
10
|
+
TC103: 'A line ends with ":" but has nothing indented under it',
|
|
11
|
+
TC104: 'Indentation does not line up with an earlier line',
|
|
12
|
+
TC105: 'A line could not be read',
|
|
13
|
+
TC106: 'Text is missing its closing quote',
|
|
14
|
+
TC107: 'Brackets or parentheses do not match',
|
|
15
|
+
TC108: 'Something is missing from this statement',
|
|
16
|
+
TC109: '"Otherwise" without a "When" or "If" before it',
|
|
17
|
+
TC110: '"If this fails:" without a statement before it',
|
|
18
|
+
TC111: '"Receive" is not the first line of a "To" block',
|
|
19
|
+
TC112: 'Something extra at the end of a line',
|
|
20
|
+
TC201: 'Unknown name',
|
|
21
|
+
TC202: 'A name uses a word that belongs to Thunder',
|
|
22
|
+
TC203: 'A name is defined twice',
|
|
23
|
+
TC204: 'A function uses a value from the main program',
|
|
24
|
+
TC205: 'A kept value is changed',
|
|
25
|
+
TC206: 'Unknown field',
|
|
26
|
+
TC207: 'Wrong number of values given to a function',
|
|
27
|
+
TC208: '"Give" outside a "To" block',
|
|
28
|
+
TC209: 'Unknown record',
|
|
29
|
+
TC210: 'A record is missing a field',
|
|
30
|
+
TC211: 'A name is used before it is set',
|
|
31
|
+
TC301: 'Money and a plain number are mixed',
|
|
32
|
+
TC302: 'Values of different kinds are compared',
|
|
33
|
+
TC303: 'A name changes kind',
|
|
34
|
+
TC304: 'A condition is not yes/no',
|
|
35
|
+
TC305: 'This kind of value cannot do that',
|
|
36
|
+
TC306: 'Unknown kind of value',
|
|
37
|
+
TC401: 'Thunder Core does not know this action yet',
|
|
38
|
+
TC402: 'Thunder Core does not support this yet',
|
|
39
|
+
TC403: 'A "Never" rule names more than one changing value',
|
|
40
|
+
TC404: 'A default for an input must be a fixed value, after the inputs without one',
|
|
41
|
+
TC405: 'A Python module ("Use") cannot be used when compiling to TypeScript',
|
|
42
|
+
TC501: 'Divided by zero (runtime)',
|
|
43
|
+
TC502: 'Nothing used where a value was needed (runtime)',
|
|
44
|
+
TC503: 'A field or entry is missing (runtime)',
|
|
45
|
+
TC504: 'A Never rule was broken (runtime)',
|
|
46
|
+
TC505: 'An Expect check failed (runtime)',
|
|
47
|
+
TC506: 'Values of different kinds were mixed (runtime)',
|
|
48
|
+
TC507: 'The program stopped with a message (runtime)',
|
|
49
|
+
TC508: 'Python code used through "Use" failed (runtime)',
|
|
50
|
+
TC509: 'A value could not be converted (runtime)',
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export class CoreError extends Error {
|
|
54
|
+
constructor(diag) { super(formatDiagnostic(diag)); this.diagnostic = diag; }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A diagnostic: { code, line, message, hint?, severity }. `message` already reads as a sentence. */
|
|
58
|
+
export function diag(code, line, message, hint) {
|
|
59
|
+
return { code, severity: 'error', line: line ?? 1, message, ...(hint ? { hint } : {}) };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function formatDiagnostic(d, file) {
|
|
63
|
+
const where = file ? `${file}, line ${d.line}` : `Line ${d.line}`;
|
|
64
|
+
return `${where}: ${d.message}${d.hint ? `\n ${d.hint}` : ''} [${d.code}]`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Small edit distance, for "did you mean".
|
|
68
|
+
export function closest(word, options) {
|
|
69
|
+
let best = null, bestD = Infinity;
|
|
70
|
+
for (const o of options) {
|
|
71
|
+
const d = lev(word, o);
|
|
72
|
+
if (d < bestD) { bestD = d; best = o; }
|
|
73
|
+
}
|
|
74
|
+
return best && bestD <= Math.max(1, Math.floor(word.length / 3)) ? best : null;
|
|
75
|
+
}
|
|
76
|
+
function lev(a, b) {
|
|
77
|
+
const m = a.length, n = b.length;
|
|
78
|
+
const dp = Array.from({ length: m + 1 }, (_, i) => [i, ...Array(n).fill(0)]);
|
|
79
|
+
for (let j = 1; j <= n; j++) dp[0][j] = j;
|
|
80
|
+
for (let i = 1; i <= m; i++) for (let j = 1; j <= n; j++) dp[i][j] = Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
81
|
+
return dp[m][n];
|
|
82
|
+
}
|