@skillstech/thunderlang 1.1.0 → 1.3.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 +212 -0
- package/README.md +4 -1
- package/dist/core.cjs +2080 -278
- package/dist/index.cjs +12742 -10943
- package/dist/learn.cjs +12976 -3598
- package/package.json +2 -2
- package/src/cli-home.mjs +242 -0
- package/src/cli.mjs +135 -41
- package/src/core/ThunderRuntime.cs +1367 -0
- package/src/core/check.mjs +158 -2
- package/src/core/cli.mjs +208 -17
- package/src/core/csharp-run.mjs +184 -0
- package/src/core/diagnostics.mjs +13 -0
- package/src/core/engine-core.mjs +855 -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 +191 -3
- package/src/core/go-run.mjs +106 -0
- package/src/core/index.mjs +3 -1
- package/src/core/java/thunder/T.java +6 -0
- package/src/core/java/thunder/ThunderRuntime.java +1374 -0
- package/src/core/java-run.mjs +89 -0
- package/src/core/parser.mjs +331 -5
- package/src/core/project.mjs +43 -8
- package/src/core/react-run.mjs +96 -0
- package/src/core/runtime-go/thunder_runtime.go +3038 -0
- package/src/core/runtime-react.mjs +133 -0
- package/src/core/runtime-typescript.mjs +119 -0
- package/src/core/rust/thunder_runtime.rs +1645 -0
- package/src/core/rust-run.mjs +192 -0
- package/src/core/screen-values.mjs +30 -0
- package/src/core/target-csharp.mjs +484 -0
- package/src/core/target-go.mjs +555 -0
- package/src/core/target-java.mjs +587 -0
- package/src/core/target-python.mjs +2 -1
- package/src/core/target-react.mjs +270 -0
- package/src/core/target-rust.mjs +534 -0
- package/src/core/target-typescript.mjs +466 -0
- package/src/core/typescript-run.mjs +114 -0
- package/src/core.d.ts +18 -0
- package/src/core.mjs +1 -0
- package/src/emit.mjs +1 -1
- package/src/faithful.mjs +30 -0
- package/src/learn-lessons.mjs +245 -1
- package/src/learn.d.mts +8 -0
- package/src/learn.mjs +40 -4
- package/src/libraries.mjs +10 -3
- package/src/model.mjs +50 -8
- package/src/read-level.mjs +192 -0
- package/src/read-repo-fs.mjs +3 -1
- package/src/read.mjs +28 -5
- package/src/replay-cli.mjs +123 -0
- package/src/say-less.mjs +152 -11
- package/src/screens.mjs +833 -0
- package/src/steps.mjs +430 -19
- package/src/term.mjs +132 -0
- package/src/try-cli.mjs +225 -0
- package/src/why-cli.mjs +17 -7
- package/src/why.d.mts +17 -7
- package/src/why.mjs +114 -33
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// thunder_runtime.mjs for compiled React, and its declarations (thunder_runtime.d.mts).
|
|
2
|
+
//
|
|
3
|
+
// One meaning of Thunder: the engine's value code (engine-values.mjs) and the engine's screen code (screen-values.mjs),
|
|
4
|
+
// byte for byte, followed by a small browser part: call frames for error lines, a press (copies in, all or nothing),
|
|
5
|
+
// typing into a box, and the message shown when the app cannot be shown. No Node APIs: it runs in a browser.
|
|
6
|
+
import { readFileSync } from 'node:fs';
|
|
7
|
+
import * as VALUES from './engine-values.mjs';
|
|
8
|
+
import * as SCREEN from './screen-values.mjs';
|
|
9
|
+
|
|
10
|
+
const VALUES_SOURCE = readFileSync(new URL('./engine-values.mjs', import.meta.url), 'utf8');
|
|
11
|
+
// (Its import line names what engine-values.mjs already defined above it.)
|
|
12
|
+
const SCREEN_SOURCE = readFileSync(new URL('./screen-values.mjs', import.meta.url), 'utf8').replace(/^import [^\n]*\n/m, '');
|
|
13
|
+
|
|
14
|
+
const BROWSER_PART = `
|
|
15
|
+
// ── Running compiled React: frames (for error lines), presses, typing, and the app's problems ────────────────────
|
|
16
|
+
import { Component, createElement } from 'react';
|
|
17
|
+
|
|
18
|
+
const frames = [];
|
|
19
|
+
const snapshot = () => frames.map((f) => ({ file: f.file, line: f.line }));
|
|
20
|
+
|
|
21
|
+
/** A frame for a screen being shown, a press, or a kept value. */
|
|
22
|
+
export function enterFrame(file, line) {
|
|
23
|
+
if (frames.length >= MAX_FRAMES) throw new PyErr('RecursionError', 'maximum recursion depth exceeded');
|
|
24
|
+
const f = { file, line };
|
|
25
|
+
frames.push(f);
|
|
26
|
+
return f;
|
|
27
|
+
}
|
|
28
|
+
/** A frame for a call; \`first\` is the Thunder line the function starts on (where Python stops when it is too deep). */
|
|
29
|
+
export function enterCall(file, line, first) {
|
|
30
|
+
if (frames.length >= MAX_FRAMES) {
|
|
31
|
+
const err = new PyErr('RecursionError', 'maximum recursion depth exceeded');
|
|
32
|
+
if (first != null) err.where = [...snapshot(), { file, line: first }];
|
|
33
|
+
throw err;
|
|
34
|
+
}
|
|
35
|
+
const f = { file, line };
|
|
36
|
+
frames.push(f);
|
|
37
|
+
return f;
|
|
38
|
+
}
|
|
39
|
+
export function leave() { frames.pop(); }
|
|
40
|
+
export function topFrame() { return frames[frames.length - 1]; }
|
|
41
|
+
export function fail(e) { const err = asPyErr(e); if (!err.where) err.where = snapshot(); return err; }
|
|
42
|
+
function withFrame(frame, fn) {
|
|
43
|
+
if (frames.length >= MAX_FRAMES) throw new PyErr('RecursionError', 'maximum recursion depth exceeded');
|
|
44
|
+
frames.push(frame);
|
|
45
|
+
try { return fn(); } catch (e) { throw fail(e); } finally { frames.pop(); }
|
|
46
|
+
}
|
|
47
|
+
export function keyAt(F, get) { const frame = { file: F.file, line: F.line }; return (item) => withFrame(frame, () => get(item)); }
|
|
48
|
+
|
|
49
|
+
/** Say, from a function a screen calls: a screen shows things instead, so it goes to the browser's console. */
|
|
50
|
+
export function say(text) { console.log(text); }
|
|
51
|
+
export function readText(path) { return readTextVia(null, '.', path); }
|
|
52
|
+
|
|
53
|
+
export function recordKind(py, fields, rules = {}) { return { isRecord: true, py, fields, rules }; }
|
|
54
|
+
export function setAttr(obj, name, value) {
|
|
55
|
+
setAttrWith((rec) => { for (const r of rec.cls.rules[name] ?? []) never(r.cond(value), r.text, r.line, value); }, obj, name, value);
|
|
56
|
+
}
|
|
57
|
+
export function setField(obj, key, value) { setFieldWith(setAttr, obj, key, value); }
|
|
58
|
+
export function newRecord(cls, pairs) { return newRecordWith(setAttr, cls, new Map(pairs)); }
|
|
59
|
+
|
|
60
|
+
let startProblem = null;
|
|
61
|
+
/** Work out a file's kept values; if one fails, the app shows why instead of its screen. */
|
|
62
|
+
export function init(file, fn) {
|
|
63
|
+
if (startProblem) return;
|
|
64
|
+
const F = { file, line: 0 };
|
|
65
|
+
try { withFrame(F, () => fn(F)); } catch (e) { startProblem = describeError(asPyErr(e)); }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* A press: \`steps\` runs on copies of the remembered values (in \`values\`), in its own frame. If every step works, each
|
|
70
|
+
* value is kept (with its setter) and the screen's problem is cleared, then the screen it opened (if any) is shown.
|
|
71
|
+
* If a step fails, nothing is kept and the screen says why.
|
|
72
|
+
*/
|
|
73
|
+
export function press(file, line, setProblem, open, values, setters, steps) {
|
|
74
|
+
let opened = null;
|
|
75
|
+
let next;
|
|
76
|
+
try {
|
|
77
|
+
next = withFrame({ file, line }, () => steps(topFrame(), (screen) => { opened = screen; }, ...values.map(copyValue)));
|
|
78
|
+
} catch (e) {
|
|
79
|
+
setProblem(problemText(e));
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
setters.forEach((set, k) => set(next[k]));
|
|
83
|
+
setProblem(null);
|
|
84
|
+
if (opened !== null) open(opened);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Typing into a text box: the box's value is the text. */
|
|
88
|
+
export function typeText(text, set, setProblem) { set(String(text)); setProblem(null); }
|
|
89
|
+
/** Typing into a number box: the box's value is the number, or the screen says why the text is not one. */
|
|
90
|
+
export function typeNumber(text, kind, set, setProblem) {
|
|
91
|
+
let v;
|
|
92
|
+
try { v = typedNumber(text, kind); } catch (e) { setProblem(problemText(e)); return; }
|
|
93
|
+
set(v);
|
|
94
|
+
setProblem(null);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Around the app: when a screen can't be shown, it says why (the same message thunder run gives). */
|
|
98
|
+
export class ShowsProblems extends Component {
|
|
99
|
+
constructor(props) { super(props); this.state = { failed: null }; }
|
|
100
|
+
static getDerivedStateFromError(error) { return { failed: describeError(asPyErr(error)) }; }
|
|
101
|
+
componentDidCatch() {}
|
|
102
|
+
render() {
|
|
103
|
+
const failed = startProblem ?? this.state.failed;
|
|
104
|
+
if (failed !== null) return createElement('p', { role: 'alert', className: 'thunder-failed' }, failed);
|
|
105
|
+
return this.props.children;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
`;
|
|
109
|
+
|
|
110
|
+
const BROWSER_EXPORTS = ['enterFrame', 'enterCall', 'leave', 'topFrame', 'fail', 'keyAt', 'say', 'readText', 'recordKind', 'setAttr', 'setField', 'newRecord', 'init', 'press', 'typeText', 'typeNumber'];
|
|
111
|
+
|
|
112
|
+
const HEADER = `// thunder_runtime.mjs: written by the Thunder compiler next to compiled React. It needs only React.
|
|
113
|
+
// The values, rules, and screen behavior here are the Thunder engine's own (engine-values.mjs and screen-values.mjs),
|
|
114
|
+
// so the React app and the Thunder engine show the same screen and change it the same way.
|
|
115
|
+
`;
|
|
116
|
+
|
|
117
|
+
export const REACT_RUNTIME = `${HEADER}\n${VALUES_SOURCE.trimEnd()}\n\n// ── Screens (screen-values.mjs) ──\n${SCREEN_SOURCE.trimEnd()}\n${BROWSER_PART}`;
|
|
118
|
+
|
|
119
|
+
const isClass = (v) => typeof v === 'function' && /^class\b/.test(Function.prototype.toString.call(v));
|
|
120
|
+
|
|
121
|
+
function declarations() {
|
|
122
|
+
const L = ['// thunder_runtime.d.mts: types for thunder_runtime.mjs (written by the Thunder compiler).', '// Thunder values are checked when the program runs; to TypeScript they are of any kind.', ''];
|
|
123
|
+
for (const [name, v] of Object.entries({ ...VALUES, ...SCREEN }).sort(([a], [b]) => a.localeCompare(b))) {
|
|
124
|
+
if (isClass(v)) L.push(`export declare class ${name} { constructor(...args: any[]); [key: string]: any; static [key: string]: any; }`);
|
|
125
|
+
else if (typeof v === 'function') L.push(`export declare function ${name}(...args: any[]): any;`);
|
|
126
|
+
else L.push(`export declare const ${name}: any;`);
|
|
127
|
+
}
|
|
128
|
+
for (const f of BROWSER_EXPORTS) L.push(`export declare function ${f}(...args: any[]): any;`);
|
|
129
|
+
L.push('export declare const ShowsProblems: any;');
|
|
130
|
+
return `${L.join('\n')}\n`;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export const REACT_RUNTIME_DTS = declarations();
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// thunder_runtime.mjs, the runtime compiled TypeScript uses, and its declarations (thunder_runtime.d.mts).
|
|
2
|
+
//
|
|
3
|
+
// One meaning of Thunder: the runtime is engine-values.mjs, byte for byte (the same values, rules, and messages the
|
|
4
|
+
// Thunder engine uses), followed by a small Node part: call frames for error lines, output, reading files, and
|
|
5
|
+
// running the program or its tests. compiler/test/core-engines.mjs runs every Core test program on Python, the
|
|
6
|
+
// Thunder engine, and compiled TypeScript, and requires the same output from all three.
|
|
7
|
+
import { readFileSync } from 'node:fs';
|
|
8
|
+
import * as VALUES from './engine-values.mjs';
|
|
9
|
+
|
|
10
|
+
const VALUES_SOURCE = readFileSync(new URL('./engine-values.mjs', import.meta.url), 'utf8');
|
|
11
|
+
|
|
12
|
+
/** The Node part: everything here is about running, not about what a value means. */
|
|
13
|
+
const NODE_PART = `
|
|
14
|
+
// ── Running compiled TypeScript: frames (for error lines), output, files, and the program or its tests ───────────
|
|
15
|
+
import { readFileSync as nodeReadFile, realpathSync as nodeRealpath } from 'node:fs';
|
|
16
|
+
import { resolve as nodeResolve } from 'node:path';
|
|
17
|
+
|
|
18
|
+
const frames = [];
|
|
19
|
+
let aborted = false;
|
|
20
|
+
const snapshot = () => frames.map((f) => ({ file: f.file, line: f.line }));
|
|
21
|
+
|
|
22
|
+
/** A frame for a program, a test, or a kept value (as the engine's withFrame). */
|
|
23
|
+
export function enterFrame(file, line) {
|
|
24
|
+
if (frames.length >= MAX_FRAMES) throw new PyErr('RecursionError', 'maximum recursion depth exceeded');
|
|
25
|
+
const f = { file, line };
|
|
26
|
+
frames.push(f);
|
|
27
|
+
return f;
|
|
28
|
+
}
|
|
29
|
+
/** A frame for a call; \`first\` is the Thunder line the function starts on (where Python stops when it is too deep). */
|
|
30
|
+
export function enterCall(file, line, first) {
|
|
31
|
+
if (frames.length >= MAX_FRAMES) {
|
|
32
|
+
const err = new PyErr('RecursionError', 'maximum recursion depth exceeded');
|
|
33
|
+
if (first != null) err.where = [...snapshot(), { file, line: first }];
|
|
34
|
+
throw err;
|
|
35
|
+
}
|
|
36
|
+
const f = { file, line };
|
|
37
|
+
frames.push(f);
|
|
38
|
+
return f;
|
|
39
|
+
}
|
|
40
|
+
export function leave() { frames.pop(); }
|
|
41
|
+
/** The innermost frame (a record's Never rule runs in its caller's frame). */
|
|
42
|
+
export function topFrame() { return frames[frames.length - 1]; }
|
|
43
|
+
/** An error on its way out of a frame: remember where it happened (the innermost frames first seen). */
|
|
44
|
+
export function fail(e) { const err = asPyErr(e); if (!err.where) err.where = snapshot(); return err; }
|
|
45
|
+
function withFrame(frame, fn) {
|
|
46
|
+
if (frames.length >= MAX_FRAMES) throw new PyErr('RecursionError', 'maximum recursion depth exceeded');
|
|
47
|
+
frames.push(frame);
|
|
48
|
+
try { return fn(); } catch (e) { throw fail(e); } finally { frames.pop(); }
|
|
49
|
+
}
|
|
50
|
+
/** A key for sorting by a field: it runs in a frame at the sorting line, as the generated Python's lambda does. */
|
|
51
|
+
export function keyAt(F, get) { const frame = { file: F.file, line: F.line }; return (item) => withFrame(frame, () => get(item)); }
|
|
52
|
+
|
|
53
|
+
export function say(text) { process.stdout.write(\`\${text}\\n\`); }
|
|
54
|
+
export function readText(path) { return readTextVia((p, c) => nodeReadFile(nodeResolve(c, p), 'utf8'), process.cwd(), path); }
|
|
55
|
+
|
|
56
|
+
/** Records: their kind (name, fields, and Never rules), making one, and setting a field. */
|
|
57
|
+
export function recordKind(py, fields, rules = {}) { return { isRecord: true, py, fields, rules }; }
|
|
58
|
+
export function setAttr(obj, name, value) {
|
|
59
|
+
setAttrWith((rec) => { for (const r of rec.cls.rules[name] ?? []) never(r.cond(value), r.text, r.line, value); }, obj, name, value);
|
|
60
|
+
}
|
|
61
|
+
export function setField(obj, key, value) { setFieldWith(setAttr, obj, key, value); }
|
|
62
|
+
export function newRecord(cls, pairs) { return newRecordWith(setAttr, cls, new Map(pairs)); }
|
|
63
|
+
|
|
64
|
+
function report(e) { process.stderr.write(\`\${describeError(asPyErr(e))}\\n\`); process.exitCode = 1; aborted = true; }
|
|
65
|
+
/** Work out a file's kept values; an error stops the program with a Thunder message. */
|
|
66
|
+
export function init(file, fn) {
|
|
67
|
+
if (aborted) return;
|
|
68
|
+
const F = { file, line: 0 };
|
|
69
|
+
try { withFrame(F, () => fn(F)); } catch (e) { report(e); }
|
|
70
|
+
}
|
|
71
|
+
/** Is this module the one Node was asked to run? */
|
|
72
|
+
export function isEntry(url) {
|
|
73
|
+
const arg = process.argv[1];
|
|
74
|
+
if (!arg) return false;
|
|
75
|
+
const real = (p) => { try { return nodeRealpath(p); } catch { return p; } };
|
|
76
|
+
const strip = (p) => real(p).replace(/\\\\/g, '/').replace(/\\.(ts|mts|js|mjs)$/, '');
|
|
77
|
+
return strip(decodeURIComponent(new URL(url).pathname)) === strip(nodeResolve(arg));
|
|
78
|
+
}
|
|
79
|
+
export function run(main) { if (aborted) return; try { main(); } catch (e) { report(e); } }
|
|
80
|
+
/** Every Test block, the way run_tests.py runs them: one line per test, then the totals. */
|
|
81
|
+
export function runTests(modules) {
|
|
82
|
+
if (aborted) return;
|
|
83
|
+
let passed = 0, failed = 0;
|
|
84
|
+
for (const [file, tests] of modules) {
|
|
85
|
+
for (const [title, fn, line] of tests) {
|
|
86
|
+
try { fn(); passed++; say(\`PASS \${title} (\${file}, line \${line})\`); }
|
|
87
|
+
catch (e) { failed++; say(\`FAIL \${title} (\${file}, line \${line})\`); say(\` \${describeError(asPyErr(e))}\`); }
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const total = passed + failed;
|
|
91
|
+
say(\`\${total} test\${total === 1 ? '' : 's'}: \${passed} passed, \${failed} failed\`);
|
|
92
|
+
if (failed) process.exitCode = 1;
|
|
93
|
+
}
|
|
94
|
+
`;
|
|
95
|
+
|
|
96
|
+
const NODE_EXPORTS = { functions: ['enterFrame', 'enterCall', 'leave', 'topFrame', 'fail', 'keyAt', 'say', 'readText', 'recordKind', 'setAttr', 'setField', 'newRecord', 'init', 'isEntry', 'run', 'runTests'] };
|
|
97
|
+
|
|
98
|
+
const HEADER = `// thunder_runtime.mjs: written by the Thunder compiler next to compiled TypeScript. Standard library only.
|
|
99
|
+
// The values and rules here are the Thunder engine's own (engine-values.mjs), so compiled TypeScript, the Thunder
|
|
100
|
+
// engine, and compiled Python say the same thing for the same program.
|
|
101
|
+
`;
|
|
102
|
+
|
|
103
|
+
export const TS_RUNTIME = `${HEADER}\n${VALUES_SOURCE.trimEnd()}\n${NODE_PART}`;
|
|
104
|
+
|
|
105
|
+
const isClass = (v) => typeof v === 'function' && /^class\b/.test(Function.prototype.toString.call(v));
|
|
106
|
+
|
|
107
|
+
/** Declarations for the runtime, made from its exports: every function takes and gives values of any kind. */
|
|
108
|
+
function declarations() {
|
|
109
|
+
const L = ['// thunder_runtime.d.mts: types for thunder_runtime.mjs (written by the Thunder compiler).', '// Thunder values are checked when the program runs; to TypeScript they are of any kind.', ''];
|
|
110
|
+
for (const [name, v] of Object.entries(VALUES).sort(([a], [b]) => a.localeCompare(b))) {
|
|
111
|
+
if (isClass(v)) L.push(`export declare class ${name} { constructor(...args: any[]); [key: string]: any; static [key: string]: any; }`);
|
|
112
|
+
else if (typeof v === 'function') L.push(`export declare function ${name}(...args: any[]): any;`);
|
|
113
|
+
else L.push(`export declare const ${name}: any;`);
|
|
114
|
+
}
|
|
115
|
+
for (const f of NODE_EXPORTS.functions) L.push(`export declare function ${f}(...args: any[]): any;`);
|
|
116
|
+
return `${L.join('\n')}\n`;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export const TS_RUNTIME_DTS = declarations();
|