@skillstech/thunderlang 1.0.3 → 1.1.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 +207 -0
- package/dist/core.cjs +13779 -10043
- package/dist/index.cjs +28500 -24392
- package/dist/learn.cjs +12853 -9579
- package/intent-graph.schema.json +11 -0
- package/package.json +6 -2
- package/src/cli.mjs +141 -58
- package/src/codegen.mjs +145 -55
- package/src/conformance.mjs +15 -6
- package/src/core/check.mjs +725 -0
- package/src/core/cli.mjs +133 -0
- package/src/core/diagnostics.mjs +81 -0
- package/src/core/format.mjs +20 -0
- package/src/core/from-python.mjs +645 -0
- package/src/core/index.mjs +38 -0
- package/src/core/lexer.mjs +115 -0
- package/src/core/parser.mjs +742 -0
- package/src/core/project.mjs +167 -0
- package/src/core/round-trip.mjs +150 -0
- package/src/core/runtime-python.mjs +801 -0
- package/src/core/target-python.mjs +486 -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/libraries.mjs +2 -2
- package/src/mcp.mjs +23 -0
- package/src/meaning.mjs +47 -7
- package/src/model.d.mts +7 -3
- package/src/model.mjs +30 -9
- package/src/money.d.mts +18 -0
- package/src/money.mjs +124 -0
- package/src/plain-contract.mjs +18 -2
- package/src/powershell.mjs +24 -5
- package/src/read-repo.mjs +1 -0
- package/src/read.d.mts +11 -0
- package/src/read.mjs +121 -13
- package/src/review-comment.d.mts +16 -0
- package/src/review-comment.mjs +145 -0
- package/src/rlang.mjs +10 -3
- package/src/say-less.mjs +1432 -0
- package/src/shell.mjs +45 -8
- package/src/sql-proc.mjs +1198 -0
- package/src/sql.mjs +87 -119
- package/src/steps.mjs +188 -37
- 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/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/why-cli.mjs +31 -0
- package/src/why.d.mts +28 -0
- package/src/why.mjs +532 -0
- package/src/write-steps.mjs +22 -5
package/src/core/cli.mjs
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
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 } from './project.mjs';
|
|
14
|
+
import { formatDiagnostic } from './diagnostics.mjs';
|
|
15
|
+
import { formatCore } from './format.mjs';
|
|
16
|
+
|
|
17
|
+
const CORE_COMMANDS = new Set(['new', 'run', 'build', 'test', 'check', 'format', 'fmt']);
|
|
18
|
+
|
|
19
|
+
function parse(argv) {
|
|
20
|
+
const a = { _: [], core: false, json: false, write: false, check: false, force: false, mission: false };
|
|
21
|
+
for (const x of argv) {
|
|
22
|
+
if (x === '--core') a.core = true;
|
|
23
|
+
else if (x === '--json') a.json = true;
|
|
24
|
+
else if (x === '--write' || x === '-w') a.write = true;
|
|
25
|
+
else if (x === '--check') a.check = true;
|
|
26
|
+
else if (x === '--force') a.force = true;
|
|
27
|
+
else if (x === '--mission') a.mission = true;
|
|
28
|
+
else if (x.startsWith('--')) a.other = true;
|
|
29
|
+
else a._.push(x);
|
|
30
|
+
}
|
|
31
|
+
return a;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Decide whether this invocation is a Thunder Core one: { root, file } or null. */
|
|
35
|
+
export function coreTarget(cmd, argv, cwd = process.cwd()) {
|
|
36
|
+
if (!CORE_COMMANDS.has(cmd)) return null;
|
|
37
|
+
const a = parse(argv);
|
|
38
|
+
if (cmd === 'new') return a.mission || a._.length === 0 || a.other ? null : { new: a._[0] };
|
|
39
|
+
const file = a._[0];
|
|
40
|
+
if (!file) { const root = findProjectRoot(cwd); return root && !a.other ? { root, file: null } : null; }
|
|
41
|
+
const p = resolve(cwd, file);
|
|
42
|
+
if (existsSync(p) && statSync(p).isDirectory()) { const root = existsSync(join(p, 'thunder.toml')) ? p : null; return root ? { root, file: null } : null; }
|
|
43
|
+
if (!file.endsWith('.thunder')) return null;
|
|
44
|
+
const root = findProjectRoot(p);
|
|
45
|
+
if (root || a.core) return { root, file: p };
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const say = (s = '') => process.stdout.write(`${s}\n`);
|
|
50
|
+
const warn = (s = '') => process.stderr.write(`${s}\n`);
|
|
51
|
+
|
|
52
|
+
function report(build, cwd) {
|
|
53
|
+
let n = 0;
|
|
54
|
+
for (const r of build.results) for (const d of r.diagnostics) { warn(formatDiagnostic(d, relative(cwd, r.path) || r.rel)); n++; }
|
|
55
|
+
return n;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Run a Thunder Core command. Returns the process exit code, or null when it is not a Core invocation. */
|
|
59
|
+
export function coreMain(cmd, argv, cwd = process.cwd()) {
|
|
60
|
+
const t = coreTarget(cmd, argv, cwd);
|
|
61
|
+
if (!t) return null;
|
|
62
|
+
const a = parse(argv);
|
|
63
|
+
try {
|
|
64
|
+
if (cmd === 'new') {
|
|
65
|
+
const root = newProject(resolve(cwd, t.new), { force: a.force });
|
|
66
|
+
const rel = relative(cwd, root) || '.';
|
|
67
|
+
say(`thunder new: created a Thunder project in ${rel}`);
|
|
68
|
+
say(` ${rel}/thunder.toml the project: name, and build target = "python"`);
|
|
69
|
+
say(` ${rel}/src/main.thunder your program`);
|
|
70
|
+
say(` ${rel}/tests/greet.thunder a test`);
|
|
71
|
+
say(` next: cd ${rel} then thunder run (and thunder test)`);
|
|
72
|
+
say(` (wanted a mission file instead? thunder init ${t.new})`);
|
|
73
|
+
return 0;
|
|
74
|
+
}
|
|
75
|
+
if (cmd === 'format' || cmd === 'fmt') return format(t, a, cwd);
|
|
76
|
+
const build = buildProject({ root: t.root, single: t.file, includeTests: cmd === 'test' || cmd === 'check', write: cmd !== 'check' });
|
|
77
|
+
if (cmd === 'check') {
|
|
78
|
+
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; }
|
|
79
|
+
const n = report(build, cwd);
|
|
80
|
+
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; }
|
|
81
|
+
say(`thunder check: no problems in ${build.results.length} Thunder Core file${build.results.length === 1 ? '' : 's'}.`);
|
|
82
|
+
return 0;
|
|
83
|
+
}
|
|
84
|
+
if (!build.ok) { const n = report(build, cwd); warn(`thunder ${cmd}: ${n} problem${n === 1 ? '' : 's'}; nothing was ${cmd === 'build' ? 'built' : 'run'}.`); return 1; }
|
|
85
|
+
const out = relative(cwd, build.outDir) || '.';
|
|
86
|
+
if (cmd === 'build') {
|
|
87
|
+
say(`thunder build: ${build.results.length} file${build.results.length === 1 ? '' : 's'} -> ${out}/ (Python 3.10+)`);
|
|
88
|
+
for (const r of build.results) say(` ${relative(cwd, r.path)} -> ${out}/${r.module}.py`);
|
|
89
|
+
say(` ${out}/thunder_runtime.py (the small runtime the generated code uses)`);
|
|
90
|
+
say(` run it without Thunder: cd ${build.base === cwd ? '.' : relative(cwd, build.base)} && python3 ${relative(build.base, build.outDir)}/${build.entryModule}.py`);
|
|
91
|
+
return 0;
|
|
92
|
+
}
|
|
93
|
+
const python = findPython();
|
|
94
|
+
if (python.error) { warn(`thunder ${cmd}: ${python.error}`); return 2; }
|
|
95
|
+
if (cmd === 'run') {
|
|
96
|
+
if (!build.entryModule) { warn('thunder run: there is no program to run (no .thunder files in src/).'); return 1; }
|
|
97
|
+
const r = runPython(python, build.outDir, `${build.entryModule}.py`, build.base);
|
|
98
|
+
return r.status ?? 1;
|
|
99
|
+
}
|
|
100
|
+
if (cmd === 'test') {
|
|
101
|
+
const tests = build.results.reduce((n, r) => n + r.info.tests.length, 0);
|
|
102
|
+
if (!tests) { say('thunder test: no Test blocks found. Write one: Test "what it checks": then Expect … lines.'); return 0; }
|
|
103
|
+
const r = runPython(python, build.outDir, 'run_tests.py', build.base);
|
|
104
|
+
return r.status ?? 1;
|
|
105
|
+
}
|
|
106
|
+
} catch (e) {
|
|
107
|
+
warn(`thunder ${cmd}: ${e.message}`);
|
|
108
|
+
return 2;
|
|
109
|
+
}
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function format(t, a, cwd) {
|
|
114
|
+
let files = [];
|
|
115
|
+
if (t.file) files = [t.file];
|
|
116
|
+
else {
|
|
117
|
+
const b = buildProject({ root: t.root, includeTests: true, write: false });
|
|
118
|
+
files = b.results.map((r) => r.path);
|
|
119
|
+
}
|
|
120
|
+
let changed = 0;
|
|
121
|
+
for (const f of files) {
|
|
122
|
+
const src = readFileSync(f, 'utf8');
|
|
123
|
+
const out = formatCore(src);
|
|
124
|
+
if (out === src) continue;
|
|
125
|
+
changed++;
|
|
126
|
+
if (a.write) { writeFileSync(f, out); say(`formatted ${relative(cwd, f)}`); }
|
|
127
|
+
else if (a.check) warn(`would format ${relative(cwd, f)}`);
|
|
128
|
+
else process.stdout.write(out);
|
|
129
|
+
}
|
|
130
|
+
if (a.check) return changed ? 1 : 0;
|
|
131
|
+
if (a.write && !changed) say('thunder format: nothing to change.');
|
|
132
|
+
return 0;
|
|
133
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
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
|
+
TC501: 'Divided by zero (runtime)',
|
|
42
|
+
TC502: 'Nothing used where a value was needed (runtime)',
|
|
43
|
+
TC503: 'A field or entry is missing (runtime)',
|
|
44
|
+
TC504: 'A Never rule was broken (runtime)',
|
|
45
|
+
TC505: 'An Expect check failed (runtime)',
|
|
46
|
+
TC506: 'Values of different kinds were mixed (runtime)',
|
|
47
|
+
TC507: 'The program stopped with a message (runtime)',
|
|
48
|
+
TC508: 'Python code used through "Use" failed (runtime)',
|
|
49
|
+
TC509: 'A value could not be converted (runtime)',
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export class CoreError extends Error {
|
|
53
|
+
constructor(diag) { super(formatDiagnostic(diag)); this.diagnostic = diag; }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** A diagnostic: { code, line, message, hint?, severity }. `message` already reads as a sentence. */
|
|
57
|
+
export function diag(code, line, message, hint) {
|
|
58
|
+
return { code, severity: 'error', line: line ?? 1, message, ...(hint ? { hint } : {}) };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function formatDiagnostic(d, file) {
|
|
62
|
+
const where = file ? `${file}, line ${d.line}` : `Line ${d.line}`;
|
|
63
|
+
return `${where}: ${d.message}${d.hint ? `\n ${d.hint}` : ''} [${d.code}]`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Small edit distance, for "did you mean".
|
|
67
|
+
export function closest(word, options) {
|
|
68
|
+
let best = null, bestD = Infinity;
|
|
69
|
+
for (const o of options) {
|
|
70
|
+
const d = lev(word, o);
|
|
71
|
+
if (d < bestD) { bestD = d; best = o; }
|
|
72
|
+
}
|
|
73
|
+
return best && bestD <= Math.max(1, Math.floor(word.length / 3)) ? best : null;
|
|
74
|
+
}
|
|
75
|
+
function lev(a, b) {
|
|
76
|
+
const m = a.length, n = b.length;
|
|
77
|
+
const dp = Array.from({ length: m + 1 }, (_, i) => [i, ...Array(n).fill(0)]);
|
|
78
|
+
for (let j = 1; j <= n; j++) dp[0][j] = j;
|
|
79
|
+
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));
|
|
80
|
+
return dp[m][n];
|
|
81
|
+
}
|
|
@@ -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
|
+
}
|