@skillstech/thunderlang 1.2.0 → 1.4.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 +190 -0
- package/dist/core.cjs +6718 -1232
- package/dist/index.cjs +17490 -12204
- package/dist/learn.cjs +5866 -497
- package/package.json +6 -2
- package/src/cli-home.mjs +1 -1
- package/src/cli.mjs +54 -6
- package/src/core/ThunderRuntime.cs +1367 -0
- package/src/core/check.mjs +158 -2
- package/src/core/cli.mjs +148 -8
- package/src/core/csharp-run.mjs +184 -0
- package/src/core/diagnostics.mjs +13 -1
- package/src/core/engine-core.mjs +285 -2
- package/src/core/engine-values.mjs +1 -1
- package/src/core/format.mjs +191 -3
- package/src/core/go-run.mjs +106 -0
- package/src/core/index.mjs +2 -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 +31 -6
- 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/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-react.mjs +270 -0
- package/src/core/target-rust.mjs +534 -0
- package/src/core/target-typescript.mjs +2 -2
- package/src/core.d.ts +59 -0
- package/src/core.mjs +14 -0
- package/src/emit.mjs +1 -1
- package/src/faithful.mjs +34 -1
- package/src/flow/access.mjs +1451 -0
- package/src/flow/ts-parse.mjs +772 -0
- package/src/index.d.ts +3 -0
- package/src/index.mjs +3 -0
- package/src/libraries.mjs +32 -4
- package/src/model.mjs +72 -9
- package/src/passport-cli.mjs +101 -0
- package/src/passport-fs.d.mts +9 -0
- package/src/passport-fs.mjs +180 -0
- package/src/passport.d.mts +46 -0
- package/src/passport.mjs +255 -0
- package/src/program.d.mts +31 -0
- package/src/program.mjs +14 -0
- package/src/read-level.mjs +48 -2
- package/src/read-repo-fs.mjs +3 -1
- package/src/read.mjs +64 -5
- package/src/replay-cli.mjs +123 -0
- package/src/review.mjs +31 -3
- package/src/say-less.mjs +168 -13
- package/src/screens.mjs +833 -0
- package/src/steps.mjs +443 -20
- package/src/term.mjs +43 -0
- package/src/things-langs.mjs +20 -3
- package/src/write-across-cli.mjs +143 -0
- package/src/write-across.mjs +168 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
// Writing compiled Rust to disk, building it, and running it.
|
|
2
|
+
//
|
|
3
|
+
// The output is a small Cargo project: main.rs (which runs the program or its tests), one <module>.rs per Thunder
|
|
4
|
+
// file, and thunder_runtime.rs (a library crate, standard library only). It builds offline with no dependencies:
|
|
5
|
+
// `cargo build` (or plain rustc). Running needs the Rust toolchain; without it, Thunder says plainly what to install.
|
|
6
|
+
import { writeFileSync, mkdirSync, readFileSync, existsSync, copyFileSync, rmSync, readdirSync, mkdtempSync, renameSync } from 'node:fs';
|
|
7
|
+
import { join, dirname } from 'node:path';
|
|
8
|
+
import { spawnSync } from 'node:child_process';
|
|
9
|
+
import { createHash } from 'node:crypto';
|
|
10
|
+
import { tmpdir } from 'node:os';
|
|
11
|
+
import { fileURLToPath } from 'node:url';
|
|
12
|
+
import { toRust, pythonOnlyUses, exportedRustNames, rs } from './target-rust.mjs';
|
|
13
|
+
import { diag } from './diagnostics.mjs';
|
|
14
|
+
|
|
15
|
+
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
16
|
+
/** The runtime the compiled Rust uses: thunder_runtime.rs, a line-by-line port of the engine's value code. */
|
|
17
|
+
export const RUST_RUNTIME = readFileSync(join(HERE, 'rust', 'thunder_runtime.rs'), 'utf8');
|
|
18
|
+
|
|
19
|
+
/** The Rust module for a Thunder module (always a valid Rust name, never a keyword or the runtime's). */
|
|
20
|
+
export const rustModule = (m) => `m_${m}`;
|
|
21
|
+
|
|
22
|
+
/** TC405 for every Use in a file: Python modules exist only for the Python target. */
|
|
23
|
+
export function rustDiagnostics(result) {
|
|
24
|
+
return pythonOnlyUses(result.info).map((u) => diag('TC405', u.line,
|
|
25
|
+
`"${u.module}" is a Python module, and this program is compiled to Rust, which cannot use it.`,
|
|
26
|
+
'Build for Python (target = "python") to use Python modules. Thunder never swaps in a different library.'));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Compile checked results to Rust: { [rustModule]: text }. Test modules see every src module's names. */
|
|
30
|
+
export function compileRust(results) {
|
|
31
|
+
const src = results.filter((r) => r.kind !== 'test');
|
|
32
|
+
const out = {};
|
|
33
|
+
for (const r of results) {
|
|
34
|
+
const imports = r.kind === 'test' ? src.map((s) => ({ module: rustModule(s.module), names: exportedRustNames(s.info) })) : [];
|
|
35
|
+
out[rustModule(r.module)] = toRust(r.program, r.info, { source: r.file, imports });
|
|
36
|
+
}
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** main.rs: kept values first (in order), then the entry's program, or every module's tests with --tests. */
|
|
41
|
+
export function mainRs(results, entryModule) {
|
|
42
|
+
const mods = results.map((r) => rustModule(r.module));
|
|
43
|
+
const entry = rustModule(entryModule ?? results[0]?.module ?? 'main');
|
|
44
|
+
return [
|
|
45
|
+
'// Generated by the Thunder compiler: runs the program, or every Test block with --tests.',
|
|
46
|
+
'#![allow(unused_imports)]',
|
|
47
|
+
...mods.map((m) => `mod ${m};`),
|
|
48
|
+
'',
|
|
49
|
+
'use thunder_runtime as T;',
|
|
50
|
+
'',
|
|
51
|
+
'fn main() { T::start(run); }',
|
|
52
|
+
'',
|
|
53
|
+
'fn run(tests: bool) {',
|
|
54
|
+
' if tests {',
|
|
55
|
+
...mods.map((m) => ` ${m}::init();`),
|
|
56
|
+
` T::run_tests(vec![${results.map((r) => `(${rs(r.file)}, ${rustModule(r.module)}::thunder_tests())`).join(', ')}]);`,
|
|
57
|
+
' } else {',
|
|
58
|
+
` ${entry}::init();`,
|
|
59
|
+
` T::run(${entry}::main);`,
|
|
60
|
+
' }',
|
|
61
|
+
'}',
|
|
62
|
+
'',
|
|
63
|
+
].join('\n');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export const CARGO_TOML = `# Generated by the Thunder compiler. Builds offline: no dependencies.
|
|
67
|
+
[package]
|
|
68
|
+
name = "thunder_program"
|
|
69
|
+
version = "0.1.0"
|
|
70
|
+
edition = "2021"
|
|
71
|
+
|
|
72
|
+
[lib]
|
|
73
|
+
name = "thunder_runtime"
|
|
74
|
+
path = "thunder_runtime.rs"
|
|
75
|
+
|
|
76
|
+
[[bin]]
|
|
77
|
+
name = "program"
|
|
78
|
+
path = "main.rs"
|
|
79
|
+
|
|
80
|
+
[profile.dev]
|
|
81
|
+
opt-level = 1
|
|
82
|
+
`;
|
|
83
|
+
|
|
84
|
+
/** Write the Cargo project: Cargo.toml, main.rs, <module>.rs for every result, and thunder_runtime.rs. */
|
|
85
|
+
export function writeRust(results, outDir, { entryModule = null } = {}) {
|
|
86
|
+
mkdirSync(outDir, { recursive: true });
|
|
87
|
+
const files = compileRust(results);
|
|
88
|
+
for (const [m, text] of Object.entries(files)) writeFileSync(join(outDir, `${m}.rs`), text);
|
|
89
|
+
writeFileSync(join(outDir, 'main.rs'), mainRs(results, entryModule));
|
|
90
|
+
writeFileSync(join(outDir, 'thunder_runtime.rs'), RUST_RUNTIME);
|
|
91
|
+
writeFileSync(join(outDir, 'Cargo.toml'), CARGO_TOML);
|
|
92
|
+
writeFileSync(join(outDir, '.gitignore'), 'target/\nCargo.lock\n');
|
|
93
|
+
return Object.keys(files);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const MISSING = 'Running Rust needs the Rust toolchain (rustc and cargo): install it from https://rustup.rs. "thunder build --target rust" works without it.';
|
|
97
|
+
|
|
98
|
+
/** Is the Rust toolchain here? { rustc, cargo, version } or { error }. */
|
|
99
|
+
export function rustToolchain() {
|
|
100
|
+
const v = spawnSync('rustc', ['--version'], { encoding: 'utf8' });
|
|
101
|
+
if (v.error || v.status !== 0) return { error: MISSING };
|
|
102
|
+
const c = spawnSync('cargo', ['--version'], { encoding: 'utf8' });
|
|
103
|
+
return { rustc: 'rustc', cargo: c.error || c.status !== 0 ? null : 'cargo', version: v.stdout.trim() };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Build the Cargo project in `outDir` (offline). { ok, binary, output }. */
|
|
107
|
+
export function buildRust(outDir) {
|
|
108
|
+
const tc = rustToolchain();
|
|
109
|
+
if (tc.error) return { ok: false, output: tc.error, missing: true };
|
|
110
|
+
if (!tc.cargo) return buildWithRustc(outDir, join(outDir, 'target', 'program'));
|
|
111
|
+
const r = spawnSync(tc.cargo, ['build', '--offline', '--quiet', '--manifest-path', join(outDir, 'Cargo.toml'), '--target-dir', join(outDir, 'target')], { encoding: 'utf8' });
|
|
112
|
+
if (r.status !== 0) return { ok: false, output: `${r.stdout}${r.stderr}`.trim() };
|
|
113
|
+
return { ok: true, binary: join(outDir, 'target', 'debug', process.platform === 'win32' ? 'program.exe' : 'program') };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Run the built program (or, with tests, its Test blocks) in the folder `cwd`. Returns spawnSync's result, or
|
|
118
|
+
* { status: 2, stderr } when Rust is missing or the generated code does not build.
|
|
119
|
+
*/
|
|
120
|
+
export function runRust(outDir, { tests = false, cwd = outDir, stdio = 'pipe' } = {}) {
|
|
121
|
+
const b = buildRust(outDir);
|
|
122
|
+
if (!b.ok) return { status: 2, stdout: '', stderr: b.missing ? `${b.output}\n` : `The generated Rust did not build:\n${b.output}\n`, error: b.output };
|
|
123
|
+
return spawnSync(b.binary, tests ? ['--tests'] : [], { cwd, stdio, encoding: 'utf8' });
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// ── Building with rustc directly, with the runtime compiled once and every program cached by its content ───────
|
|
127
|
+
// (What the conformance tests use: hundreds of small programs would each spend seconds in cargo.)
|
|
128
|
+
|
|
129
|
+
const CACHE = join(tmpdir(), 'thunder-rust-cache');
|
|
130
|
+
const sha = (s) => createHash('sha256').update(s).digest('hex').slice(0, 24);
|
|
131
|
+
let runtimeLib = null;
|
|
132
|
+
|
|
133
|
+
/** The runtime compiled once (optimized) as an rlib, cached by its text and the rustc version. */
|
|
134
|
+
export function runtimeRlib() {
|
|
135
|
+
if (runtimeLib) return runtimeLib;
|
|
136
|
+
const tc = rustToolchain();
|
|
137
|
+
if (tc.error) return { error: tc.error };
|
|
138
|
+
const dir = join(CACHE, `runtime-${sha(RUST_RUNTIME + tc.version)}`);
|
|
139
|
+
const lib = join(dir, 'libthunder_runtime.rlib');
|
|
140
|
+
if (!existsSync(lib)) {
|
|
141
|
+
// Built in a folder of its own (rustc writes its temporary object files next to the output, under the same names
|
|
142
|
+
// in every process), then moved into the cache in one step, so builds running side by side never collide.
|
|
143
|
+
mkdirSync(dir, { recursive: true });
|
|
144
|
+
const work = mkdtempSync(join(CACHE, 'build-'));
|
|
145
|
+
try {
|
|
146
|
+
const srcPath = join(work, 'thunder_runtime.rs');
|
|
147
|
+
writeFileSync(srcPath, RUST_RUNTIME);
|
|
148
|
+
const tmpLib = join(work, 'libthunder_runtime.rlib');
|
|
149
|
+
const r = spawnSync(tc.rustc, ['--edition', '2021', '--crate-type', 'rlib', '--crate-name', 'thunder_runtime', '-O', srcPath, '-o', tmpLib], { encoding: 'utf8' });
|
|
150
|
+
if (r.status !== 0) return { error: `The Rust runtime did not build:\n${r.stderr}` };
|
|
151
|
+
if (!existsSync(join(dir, 'thunder_runtime.rs'))) writeFileSync(join(dir, 'thunder_runtime.rs'), RUST_RUNTIME);
|
|
152
|
+
if (!existsSync(lib)) renameSync(tmpLib, lib);
|
|
153
|
+
} finally { rmSync(work, { recursive: true, force: true }); }
|
|
154
|
+
}
|
|
155
|
+
runtimeLib = { lib, dir, version: tc.version };
|
|
156
|
+
return runtimeLib;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Build outDir/main.rs with rustc against the cached runtime; the binary is cached by the sources' content. */
|
|
160
|
+
export function buildWithRustc(outDir, binaryOut = null) {
|
|
161
|
+
const rt = runtimeRlib();
|
|
162
|
+
if (rt.error) return { ok: false, output: rt.error, missing: /install it/.test(rt.error) };
|
|
163
|
+
const sources = Object.entries(Object.fromEntries(['main.rs', ...listRs(outDir)].map((f) => [f, readFileSync(join(outDir, f), 'utf8')]))).sort();
|
|
164
|
+
const key = sha(JSON.stringify(sources) + rt.lib);
|
|
165
|
+
const cached = join(CACHE, 'bin', key, process.platform === 'win32' ? 'program.exe' : 'program');
|
|
166
|
+
if (!existsSync(cached)) {
|
|
167
|
+
mkdirSync(dirname(cached), { recursive: true });
|
|
168
|
+
const work = mkdtempSync(join(CACHE, 'build-'));
|
|
169
|
+
try {
|
|
170
|
+
const tmpBin = join(work, process.platform === 'win32' ? 'program.exe' : 'program');
|
|
171
|
+
const r = spawnSync('rustc', ['--edition', '2021', '-C', 'opt-level=0', '-C', 'debuginfo=0', join(outDir, 'main.rs'), '--extern', `thunder_runtime=${rt.lib}`, '-L', rt.dir, '-o', tmpBin], { encoding: 'utf8' });
|
|
172
|
+
if (r.status !== 0) return { ok: false, output: `${r.stdout}${r.stderr}`.trim() };
|
|
173
|
+
if (!existsSync(cached)) renameSync(tmpBin, cached);
|
|
174
|
+
} finally { rmSync(work, { recursive: true, force: true }); }
|
|
175
|
+
}
|
|
176
|
+
if (binaryOut) { mkdirSync(dirname(binaryOut), { recursive: true }); copyFileSync(cached, binaryOut); return { ok: true, binary: binaryOut }; }
|
|
177
|
+
return { ok: true, binary: cached };
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** The generated module files in a folder (not main.rs or the runtime). */
|
|
181
|
+
function listRs(dir) {
|
|
182
|
+
let all = [];
|
|
183
|
+
try { all = readdirSync(dir); } catch { /* no folder */ }
|
|
184
|
+
return all.filter((f) => f.endsWith('.rs') && f !== 'main.rs' && f !== 'thunder_runtime.rs');
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Run compiled Rust built with rustc (the fast path the tests use). */
|
|
188
|
+
export function runRustFast(outDir, { tests = false, cwd = outDir } = {}) {
|
|
189
|
+
const b = buildWithRustc(outDir);
|
|
190
|
+
if (!b.ok) return { status: 2, stdout: '', stderr: b.missing ? `${b.output}\n` : `The generated Rust did not build:\n${b.output}\n`, error: b.output };
|
|
191
|
+
return spawnSync(b.binary, tests ? ['--tests'] : [], { cwd, encoding: 'utf8' });
|
|
192
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// What a Thunder screen means, shared by the Thunder engine (engine-core.mjs) and compiled React (the runtime
|
|
2
|
+
// target-react writes is engine-values.mjs, then this file, then a small browser part). One meaning of a screen:
|
|
3
|
+
// how a value is shown, what typing into a box does, what a press copies, and what a failed press says.
|
|
4
|
+
//
|
|
5
|
+
// Browser-safe: no Node APIs. The import line below is dropped when this file is copied into thunder_runtime.mjs,
|
|
6
|
+
// where these names are already defined above it.
|
|
7
|
+
import { PyDict, RecordVal, show, pyStr, explain, asPyErr, toNumber, toWhole, ConversionFailed } from './engine-values.mjs';
|
|
8
|
+
|
|
9
|
+
/** A value as a screen shows it: text and whole numbers as they are, anything else the Thunder way (as in "{…}"). */
|
|
10
|
+
export function shownText(v, kind) { return kind === 'text' || kind === 'whole' ? pyStr(v) : show(v); }
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A copy for a press to change: lists, maps, and records are copied all the way down, so a press that fails part
|
|
14
|
+
* way leaves the screen exactly as it was (a press changes everything it changes, or nothing).
|
|
15
|
+
*/
|
|
16
|
+
export function copyValue(v) {
|
|
17
|
+
if (Array.isArray(v)) return v.map(copyValue);
|
|
18
|
+
if (v instanceof PyDict) { const d = new PyDict(); for (const [id, [k, x]] of v.m) d.m.set(id, [k, copyValue(x)]); return d; }
|
|
19
|
+
if (v instanceof RecordVal) { const r = new RecordVal(v.cls); for (const [k, x] of v.f) r.f.set(k, copyValue(x)); return r; }
|
|
20
|
+
return v;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The value a number box holds after typing `text` into it (a whole number box keeps whole numbers). */
|
|
24
|
+
export function typedNumber(text, kind) {
|
|
25
|
+
if (String(text).trim() === '') throw ConversionFailed('The box is empty, and it needs a number.');
|
|
26
|
+
return kind === 'whole' ? toWhole(String(text)) : toNumber(String(text));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** What the screen says when a press (or typing) fails: the words "If this fails" gives as "error", as a sentence. */
|
|
30
|
+
export function problemText(e) { const t = explain(asPyErr(e)); return t.charAt(0).toUpperCase() + t.slice(1); }
|