@skillstech/thunderlang 1.2.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.
Files changed (47) hide show
  1. package/CHANGELOG.md +131 -0
  2. package/dist/core.cjs +2080 -278
  3. package/dist/index.cjs +12742 -10943
  4. package/dist/learn.cjs +2716 -345
  5. package/package.json +1 -1
  6. package/src/cli.mjs +31 -6
  7. package/src/core/ThunderRuntime.cs +1367 -0
  8. package/src/core/check.mjs +158 -2
  9. package/src/core/cli.mjs +148 -8
  10. package/src/core/csharp-run.mjs +184 -0
  11. package/src/core/diagnostics.mjs +13 -1
  12. package/src/core/engine-core.mjs +285 -2
  13. package/src/core/engine-values.mjs +1 -1
  14. package/src/core/format.mjs +191 -3
  15. package/src/core/go-run.mjs +106 -0
  16. package/src/core/index.mjs +2 -1
  17. package/src/core/java/thunder/T.java +6 -0
  18. package/src/core/java/thunder/ThunderRuntime.java +1374 -0
  19. package/src/core/java-run.mjs +89 -0
  20. package/src/core/parser.mjs +331 -5
  21. package/src/core/project.mjs +31 -6
  22. package/src/core/react-run.mjs +96 -0
  23. package/src/core/runtime-go/thunder_runtime.go +3038 -0
  24. package/src/core/runtime-react.mjs +133 -0
  25. package/src/core/rust/thunder_runtime.rs +1645 -0
  26. package/src/core/rust-run.mjs +192 -0
  27. package/src/core/screen-values.mjs +30 -0
  28. package/src/core/target-csharp.mjs +484 -0
  29. package/src/core/target-go.mjs +555 -0
  30. package/src/core/target-java.mjs +587 -0
  31. package/src/core/target-react.mjs +270 -0
  32. package/src/core/target-rust.mjs +534 -0
  33. package/src/core/target-typescript.mjs +2 -2
  34. package/src/core.d.ts +18 -0
  35. package/src/core.mjs +1 -0
  36. package/src/emit.mjs +1 -1
  37. package/src/faithful.mjs +30 -0
  38. package/src/libraries.mjs +10 -3
  39. package/src/model.mjs +50 -8
  40. package/src/read-level.mjs +48 -2
  41. package/src/read-repo-fs.mjs +3 -1
  42. package/src/read.mjs +28 -5
  43. package/src/replay-cli.mjs +123 -0
  44. package/src/say-less.mjs +152 -11
  45. package/src/screens.mjs +833 -0
  46. package/src/steps.mjs +430 -19
  47. package/src/term.mjs +43 -0
@@ -0,0 +1,106 @@
1
+ // Writing compiled Go to disk, and running it.
2
+ //
3
+ // The output is one Go module: go.mod, thunder/thunder_runtime.go (the runtime, a package of its own so Go's build
4
+ // cache keeps it compiled), one <module>.go per Thunder file (package main), and two entry files picked by a build tag:
5
+ // thunder_main.go runs the program, run_tests.go (-tags thundertests) runs every Test block.
6
+ // Running needs the go toolchain (https://go.dev/dl/). Standard library only: nothing is downloaded.
7
+ import { writeFileSync, mkdirSync, readFileSync, rmSync } from 'node:fs';
8
+ import { join, dirname } from 'node:path';
9
+ import { spawnSync } from 'node:child_process';
10
+ import { fileURLToPath } from 'node:url';
11
+ import { tmpdir } from 'node:os';
12
+ import { toGo, pythonOnlyUses, id, goStr, goNamespaces as namespaces } from './target-go.mjs';
13
+ import { diag } from './diagnostics.mjs';
14
+
15
+ export const GO_RUNTIME = readFileSync(join(dirname(fileURLToPath(import.meta.url)), 'runtime-go', 'thunder_runtime.go'), 'utf8');
16
+
17
+ /** TC405 for every Use in a file: Python modules exist only for the Python target. */
18
+ export function goDiagnostics(result) {
19
+ return pythonOnlyUses(result.info).map((u) => diag('TC405', u.line,
20
+ `"${u.module}" is a Python module, and this program is compiled to Go, which cannot use it.`,
21
+ 'Build for Python (target = "python") to use Python modules. Thunder never swaps in a different library.'));
22
+ }
23
+
24
+
25
+ /** Compile checked results to Go: { [file]: text } for the module files. */
26
+ export function compileGo(results) {
27
+ const ns = namespaces(results);
28
+ const files = {};
29
+ for (const r of results) files[`${r.module}.go`] = toGo(r.program, r.info, { source: r.file, module: r.module, ns: ns.get(r.module) });
30
+ return files;
31
+ }
32
+
33
+ /**
34
+ * Write the Go module for `results` into outDir. `entry` is the module `thunder run` runs (its kept values are worked
35
+ * out first, as Python's import of it does); the tests entry works out every file's kept values, in order.
36
+ */
37
+ export function writeGo(results, outDir, { entry = null } = {}) {
38
+ mkdirSync(join(outDir, 'thunder'), { recursive: true });
39
+ const files = compileGo(results);
40
+ for (const [f, text] of Object.entries(files)) writeFileSync(join(outDir, f), text);
41
+ writeFileSync(join(outDir, 'go.mod'), 'module thunderprogram\n\ngo 1.21\n');
42
+ writeFileSync(join(outDir, 'thunder', 'thunder_runtime.go'), GO_RUNTIME);
43
+ const main = entry ?? results.find((r) => r.kind !== 'test')?.module ?? results[0]?.module;
44
+ writeFileSync(join(outDir, 'thunder_main.go'), [
45
+ '//go:build !thundertests',
46
+ '',
47
+ '// Generated by the Thunder compiler: runs the program. Build it with: go build -o program .',
48
+ 'package main',
49
+ '',
50
+ 'import T "thunderprogram/thunder"',
51
+ '',
52
+ 'func main() {',
53
+ ...(main ? [`\tkeep_${main}()`, `\tT.Run(main_${main})`] : ['\tT.Run(func() {})']),
54
+ '}',
55
+ '',
56
+ ].join('\n'));
57
+ writeFileSync(join(outDir, 'run_tests.go'), [
58
+ '//go:build thundertests',
59
+ '',
60
+ '// Generated by the Thunder compiler: runs every Test block in this project. Run it with: go run -tags thundertests .',
61
+ 'package main',
62
+ '',
63
+ 'import T "thunderprogram/thunder"',
64
+ '',
65
+ 'func main() {',
66
+ ...results.map((r) => `\tkeep_${r.module}()`),
67
+ `\tT.RunTests([]T.Suite{${results.map((r) => `{File: ${goStr(r.file)}, Tests: tests_${r.module}}`).join(', ')}})`,
68
+ '}',
69
+ '',
70
+ ].join('\n'));
71
+ return Object.keys(files);
72
+ }
73
+
74
+ /** The go toolchain here: { cmd, version } or { error }. */
75
+ export function goToolchain() {
76
+ const cmd = process.env.THUNDER_GO || 'go';
77
+ const r = spawnSync(cmd, ['version'], { encoding: 'utf8' });
78
+ if (r.status === 0 && /go version go\d/.test(r.stdout)) return { cmd, version: r.stdout.match(/go(\d+\.\d+(?:\.\d+)?)/)[1] };
79
+ return { error: 'Running Go needs the Go toolchain, version 1.21 or newer (https://go.dev/dl/), or set THUNDER_GO to its path. "thunder build --target go" works without it.' };
80
+ }
81
+
82
+ const goEnv = () => ({ ...process.env, GOTOOLCHAIN: 'local', GOFLAGS: '-mod=mod', GO111MODULE: 'on', CGO_ENABLED: '0' });
83
+
84
+ /**
85
+ * Build the Go module in outDir (the program, or with `tests` its Test blocks) and run it in the folder `cwd`.
86
+ * Returns spawnSync's result ({ status, stdout, stderr }), or { status: 2, stderr } when Go can't run here.
87
+ */
88
+ export function runGo(outDir, { tests = false, cwd = outDir, stdio = 'pipe' } = {}) {
89
+ const go = goToolchain();
90
+ if (go.error) return { status: 2, stdout: '', stderr: `${go.error}\n`, error: go.error };
91
+ const bin = join(outDir, '.run', process.platform === 'win32' ? 'program.exe' : 'program');
92
+ rmSync(dirname(bin), { recursive: true, force: true });
93
+ const args = ['build', '-trimpath', '-o', bin, ...(tests ? ['-tags', 'thundertests'] : []), '.'];
94
+ const b = spawnSync(go.cmd, args, { cwd: outDir, encoding: 'utf8', env: goEnv() });
95
+ if (b.status !== 0) return { status: 2, stdout: '', stderr: `The generated Go did not build (a bug in the Thunder compiler):\n${b.stdout}${b.stderr}`, buildFailed: true };
96
+ return spawnSync(bin, [], { cwd, stdio, encoding: 'utf8' });
97
+ }
98
+
99
+ /** Check that the generated Go builds (go vet off: generated code is not linted). { ok, output } */
100
+ export function buildGo(outDir, { tests = false } = {}) {
101
+ const go = goToolchain();
102
+ if (go.error) return { skipped: go.error };
103
+ const r = spawnSync(go.cmd, ['build', '-trimpath', '-o', join(tmpdir(), `thunder-go-check-${process.pid}`), ...(tests ? ['-tags', 'thundertests'] : []), '.'], { cwd: outDir, encoding: 'utf8', env: goEnv() });
104
+ return { ok: r.status === 0, output: `${r.stdout}${r.stderr}`.trim() };
105
+ }
106
+
@@ -26,7 +26,8 @@ export const CORE_VERSION = '0.1.0';
26
26
  * import them from.
27
27
  */
28
28
  export function compileCore(source, { file = 'main.thunder', imported = [], srcModules = [] } = {}) {
29
- const seed = imported.length ? { known: imported.flatMap((r) => [...r.program.names.known].filter(([, kind]) => kind !== 'var')), fields: imported.flatMap((r) => [...r.program.names.fields]), records: imported.flatMap((r) => [...r.program.names.records]) } : null;
29
+ const seed = imported.length ? { known: imported.flatMap((r) => [...r.program.names.known].filter(([, kind]) => kind !== 'var')), fields: imported.flatMap((r) => [...r.program.names.fields]), records: imported.flatMap((r) => [...r.program.names.records]),
30
+ recordFields: imported.flatMap((r) => [...(r.program.names.recordFields ?? [])]), funcInputs: imported.flatMap((r) => [...(r.program.names.funcInputs ?? [])]) } : null;
30
31
  const program = parseCore(source, { seed });
31
32
  const { diagnostics, info } = checkCore(program, { imported: imported.map((r) => r.info) });
32
33
  const ok = diagnostics.length === 0;
@@ -0,0 +1,6 @@
1
+ // T.java: a short name for ThunderRuntime, so compiled Java reads T.add(a, b). Written by the Thunder compiler.
2
+ package thunder;
3
+
4
+ public final class T extends ThunderRuntime {
5
+ private T() {}
6
+ }