@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.
Files changed (61) hide show
  1. package/CHANGELOG.md +212 -0
  2. package/README.md +4 -1
  3. package/dist/core.cjs +2080 -278
  4. package/dist/index.cjs +12742 -10943
  5. package/dist/learn.cjs +12976 -3598
  6. package/package.json +2 -2
  7. package/src/cli-home.mjs +242 -0
  8. package/src/cli.mjs +135 -41
  9. package/src/core/ThunderRuntime.cs +1367 -0
  10. package/src/core/check.mjs +158 -2
  11. package/src/core/cli.mjs +208 -17
  12. package/src/core/csharp-run.mjs +184 -0
  13. package/src/core/diagnostics.mjs +13 -0
  14. package/src/core/engine-core.mjs +855 -0
  15. package/src/core/engine-values.mjs +1054 -0
  16. package/src/core/engine-worker.mjs +15 -0
  17. package/src/core/engine.mjs +63 -0
  18. package/src/core/format.mjs +191 -3
  19. package/src/core/go-run.mjs +106 -0
  20. package/src/core/index.mjs +3 -1
  21. package/src/core/java/thunder/T.java +6 -0
  22. package/src/core/java/thunder/ThunderRuntime.java +1374 -0
  23. package/src/core/java-run.mjs +89 -0
  24. package/src/core/parser.mjs +331 -5
  25. package/src/core/project.mjs +43 -8
  26. package/src/core/react-run.mjs +96 -0
  27. package/src/core/runtime-go/thunder_runtime.go +3038 -0
  28. package/src/core/runtime-react.mjs +133 -0
  29. package/src/core/runtime-typescript.mjs +119 -0
  30. package/src/core/rust/thunder_runtime.rs +1645 -0
  31. package/src/core/rust-run.mjs +192 -0
  32. package/src/core/screen-values.mjs +30 -0
  33. package/src/core/target-csharp.mjs +484 -0
  34. package/src/core/target-go.mjs +555 -0
  35. package/src/core/target-java.mjs +587 -0
  36. package/src/core/target-python.mjs +2 -1
  37. package/src/core/target-react.mjs +270 -0
  38. package/src/core/target-rust.mjs +534 -0
  39. package/src/core/target-typescript.mjs +466 -0
  40. package/src/core/typescript-run.mjs +114 -0
  41. package/src/core.d.ts +18 -0
  42. package/src/core.mjs +1 -0
  43. package/src/emit.mjs +1 -1
  44. package/src/faithful.mjs +30 -0
  45. package/src/learn-lessons.mjs +245 -1
  46. package/src/learn.d.mts +8 -0
  47. package/src/learn.mjs +40 -4
  48. package/src/libraries.mjs +10 -3
  49. package/src/model.mjs +50 -8
  50. package/src/read-level.mjs +192 -0
  51. package/src/read-repo-fs.mjs +3 -1
  52. package/src/read.mjs +28 -5
  53. package/src/replay-cli.mjs +123 -0
  54. package/src/say-less.mjs +152 -11
  55. package/src/screens.mjs +833 -0
  56. package/src/steps.mjs +430 -19
  57. package/src/term.mjs +132 -0
  58. package/src/try-cli.mjs +225 -0
  59. package/src/why-cli.mjs +17 -7
  60. package/src/why.d.mts +17 -7
  61. package/src/why.mjs +114 -33
@@ -0,0 +1,184 @@
1
+ // Writing compiled C# to disk, building it with the .NET SDK, and running it.
2
+ //
3
+ // A build is an ordinary console project: one <module>.cs per Thunder file (a static class), ThunderRuntime.cs, and a
4
+ // Program.cs that runs the program (`dotnet ThunderProgram.dll`) or its tests (`dotnet ThunderProgram.dll test`).
5
+ // Several programs can share one build (one namespace each, picked by number), which is how the conformance tests
6
+ // keep .NET's build time down.
7
+ import { writeFileSync, mkdirSync, readFileSync, existsSync } from 'node:fs';
8
+ import { join } from 'node:path';
9
+ import { spawnSync } from 'node:child_process';
10
+ import { toCSharp, pythonOnlyUses, classOf, cs } from './target-csharp.mjs';
11
+ import { diag } from './diagnostics.mjs';
12
+
13
+ export const CS_RUNTIME = readFileSync(new URL('./ThunderRuntime.cs', import.meta.url), 'utf8');
14
+ export const ASSEMBLY = 'ThunderProgram';
15
+
16
+ /** TC405 for every Use in a file: Python modules exist only for the Python target. */
17
+ export function csharpDiagnostics(result) {
18
+ return pythonOnlyUses(result.info).map((u) => diag('TC405', u.line,
19
+ `"${u.module}" is a Python module, and this program is compiled to C#, which cannot use it.`,
20
+ 'Build for Python (target = "python") to use Python modules. Thunder never swaps in a different library.'));
21
+ }
22
+
23
+ /** The .NET SDK here: { cmd, version, major } or { error }. */
24
+ export function findDotnet() {
25
+ const cmd = process.env.THUNDER_DOTNET || 'dotnet';
26
+ const r = spawnSync(cmd, ['--version'], { encoding: 'utf8' });
27
+ if (r.status !== 0 || !r.stdout) return { error: 'Building C# needs the .NET SDK (dotnet), version 8 or newer, and I could not find it. Install it from https://dotnet.microsoft.com/download or set THUNDER_DOTNET to its path. "thunder build --target csharp" still writes the C# without it.' };
28
+ const version = r.stdout.trim();
29
+ const major = Number(version.split('.')[0]);
30
+ if (!(major >= 8)) return { error: `Building C# needs the .NET SDK version 8 or newer, but "${cmd}" is ${version}. Install a newer one from https://dotnet.microsoft.com/download.` };
31
+ return { cmd, version, major };
32
+ }
33
+
34
+ const csproj = (major) => `<Project Sdk="Microsoft.NET.Sdk">
35
+ <!-- Generated by the Thunder compiler. Build: dotnet build; run: dotnet bin/${ASSEMBLY}.dll (add "test" to run the tests). -->
36
+ <PropertyGroup>
37
+ <OutputType>Exe</OutputType>
38
+ <TargetFramework>net${major}.0</TargetFramework>
39
+ <AssemblyName>${ASSEMBLY}</AssemblyName>
40
+ <RootNamespace>${ASSEMBLY}</RootNamespace>
41
+ <Nullable>disable</Nullable>
42
+ <ImplicitUsings>disable</ImplicitUsings>
43
+ <InvariantGlobalization>true</InvariantGlobalization>
44
+ <SatelliteResourceLanguages>en</SatelliteResourceLanguages>
45
+ <TreatWarningsAsErrors>false</TreatWarningsAsErrors>
46
+ <NoWarn>CS0162;CS0168;CS0219;CS0472;CS1717;CS8321;CS0414</NoWarn>
47
+ <EnableDefaultCompileItems>true</EnableDefaultCompileItems>
48
+ </PropertyGroup>
49
+ <ItemGroup>
50
+ <Compile Remove="bin/**;obj/**" />
51
+ </ItemGroup>
52
+ </Project>
53
+ `;
54
+
55
+ /** The C# for one program's results (src files, then test files) in `namespace`: { [module]: text }. */
56
+ export function compileCSharp(results, namespace = ASSEMBLY) {
57
+ const src = results.filter((r) => r.kind !== 'test');
58
+ const out = {};
59
+ for (const r of results) {
60
+ const imports = r.kind === 'test' ? src.map((s) => s.module) : [];
61
+ out[r.module] = toCSharp(r.program, r.info, { source: r.file, module: r.module, namespace, imports });
62
+ }
63
+ return out;
64
+ }
65
+
66
+ /** The statements that run one program (`entry`) or its tests, the way the other targets load and run them. */
67
+ function runCode(results, entry, namespace, indent) {
68
+ const pad = ' '.repeat(indent);
69
+ const q = (m) => `${namespace}.${classOf(m)}`;
70
+ const tests = [
71
+ ...results.map((r) => `${q(r.module)}.Init();`),
72
+ `T.runTests(new (string, (string, System.Action, int)[])[] { ${results.map((r) => `(${cs(r.file)}, ${q(r.module)}.THUNDER_TESTS)`).join(', ')} });`,
73
+ ];
74
+ const run = entry ? [`${q(entry.module)}.Init();`, `T.run(${q(entry.module)}.main);`] : [];
75
+ return { tests: tests.map((l) => pad + l).join('\n'), run: run.map((l) => pad + l).join('\n') };
76
+ }
77
+
78
+ /**
79
+ * Write a project for `results` into `outDir`: <module>.cs, ThunderRuntime.cs, Program.cs, ThunderProgram.csproj.
80
+ * `entry` is the result `thunder run` runs.
81
+ */
82
+ export function writeCSharp(results, outDir, { entry = results.find((r) => r.kind !== 'test') ?? null, major = findDotnet().major ?? 8 } = {}) {
83
+ mkdirSync(outDir, { recursive: true });
84
+ const files = compileCSharp(results);
85
+ for (const [m, text] of Object.entries(files)) writeFileSync(join(outDir, `${m}.cs`), text);
86
+ writeFileSync(join(outDir, 'ThunderRuntime.cs'), CS_RUNTIME);
87
+ const code = runCode(results, entry, ASSEMBLY, 16);
88
+ writeFileSync(join(outDir, 'Program.cs'), `// Generated by the Thunder compiler: runs the program, or every Test block with "test".
89
+ using Thunder;
90
+
91
+ namespace ${ASSEMBLY}
92
+ {
93
+ public static class Program
94
+ {
95
+ public static int Main(string[] args)
96
+ {
97
+ bool tests = args.Length > 0 && args[0] == "test";
98
+ return T.Start(() =>
99
+ {
100
+ if (tests)
101
+ {
102
+ ${code.tests}
103
+ }
104
+ else
105
+ {
106
+ ${code.run}
107
+ }
108
+ });
109
+ }
110
+ }
111
+ }
112
+ `);
113
+ writeFileSync(join(outDir, `${ASSEMBLY}.csproj`), csproj(major));
114
+ return Object.keys(files);
115
+ }
116
+
117
+ /**
118
+ * Write one build holding many programs, each in its own namespace (P0, P1, …), chosen when it runs:
119
+ * `dotnet ThunderProgram.dll <n> [test]`. `programs`: [{ results, entry }].
120
+ */
121
+ export function writeCSharpBatch(programs, outDir, { major = findDotnet().major ?? 8 } = {}) {
122
+ mkdirSync(outDir, { recursive: true });
123
+ const cases = [];
124
+ programs.forEach((p, i) => {
125
+ const ns = `P${i}`;
126
+ for (const [m, text] of Object.entries(compileCSharp(p.results, ns))) writeFileSync(join(outDir, `${ns}_${m}.cs`), text);
127
+ const code = runCode(p.results, p.entry, ns, 24);
128
+ cases.push(` case ${i}:\n if (tests)\n {\n${code.tests}\n }\n else\n {\n${code.run}\n }\n break;`);
129
+ });
130
+ writeFileSync(join(outDir, 'ThunderRuntime.cs'), CS_RUNTIME);
131
+ writeFileSync(join(outDir, 'Program.cs'), `// Generated by the Thunder compiler: many programs in one build, chosen by number.
132
+ using Thunder;
133
+
134
+ namespace ${ASSEMBLY}
135
+ {
136
+ public static class Program
137
+ {
138
+ public static int Main(string[] args)
139
+ {
140
+ int which = int.Parse(args[0]);
141
+ bool tests = args.Length > 1 && args[1] == "test";
142
+ return T.Start(() =>
143
+ {
144
+ switch (which)
145
+ {
146
+ ${cases.join('\n')}
147
+ }
148
+ });
149
+ }
150
+ }
151
+ }
152
+ `);
153
+ writeFileSync(join(outDir, `${ASSEMBLY}.csproj`), csproj(major));
154
+ }
155
+
156
+ /** dotnet build the project in `outDir` into outDir/bin. { ok, output } or { error } when there is no .NET SDK. */
157
+ export function buildCSharp(outDir, { dotnet = findDotnet() } = {}) {
158
+ if (dotnet.error) return { ok: false, error: dotnet.error };
159
+ const r = spawnSync(dotnet.cmd, ['build', join(outDir, `${ASSEMBLY}.csproj`), '-nologo', '-v', 'q', '-clp:NoSummary;ErrorsOnly', '-o', join(outDir, 'bin')], {
160
+ encoding: 'utf8', env: { ...process.env, DOTNET_CLI_TELEMETRY_OPTOUT: '1', DOTNET_NOLOGO: '1', DOTNET_SKIP_FIRST_TIME_EXPERIENCE: '1' }, maxBuffer: 64 * 1024 * 1024,
161
+ });
162
+ return { ok: r.status === 0, output: `${r.stdout ?? ''}${r.stderr ?? ''}`.trim() };
163
+ }
164
+
165
+ /** Run a built project: `dotnet bin/ThunderProgram.dll …args` in the folder `cwd`. spawnSync's result. */
166
+ export function runBuiltCSharp(outDir, args = [], { cwd = outDir, stdio = 'pipe', dotnet = findDotnet() } = {}) {
167
+ return spawnSync(dotnet.cmd, [join(outDir, 'bin', `${ASSEMBLY}.dll`), ...args], {
168
+ cwd, stdio, encoding: 'utf8', env: { ...process.env, DOTNET_CLI_TELEMETRY_OPTOUT: '1', DOTNET_NOLOGO: '1' }, maxBuffer: 64 * 1024 * 1024,
169
+ });
170
+ }
171
+
172
+ /**
173
+ * Build (if needed) and run the project in `outDir`: the program, or its tests when `tests`. Returns spawnSync's
174
+ * result, or { status: 2, stderr, error } when C# cannot be built or run here.
175
+ */
176
+ export function runCSharp(outDir, { tests = false, cwd = outDir, stdio = 'pipe' } = {}) {
177
+ const dotnet = findDotnet();
178
+ if (dotnet.error) return { status: 2, stdout: '', stderr: `${dotnet.error}\n`, error: dotnet.error };
179
+ const b = buildCSharp(outDir, { dotnet });
180
+ if (!b.ok) return { status: 2, stdout: '', stderr: `The C# did not build:\n${b.output}\n`, error: `The C# did not build:\n${b.output}` };
181
+ return runBuiltCSharp(outDir, tests ? ['test'] : [], { cwd, stdio, dotnet });
182
+ }
183
+
184
+ export const csharpBuilt = (outDir) => existsSync(join(outDir, 'bin', `${ASSEMBLY}.dll`));
@@ -17,6 +17,13 @@ export const CODES = {
17
17
  TC110: '"If this fails:" without a statement before it',
18
18
  TC111: '"Receive" is not the first line of a "To" block',
19
19
  TC112: 'Something extra at the end of a line',
20
+ TC113: 'A line under a record or call written as a block is not "name: value"',
21
+ TC114: 'A field or an input is given twice',
22
+ TC115: 'A function called with names has no input of that name (or its inputs are not known)',
23
+ TC116: 'A function called with names is missing an input it needs',
24
+ TC117: 'Lines like "name: value" with no record or function call to give them to',
25
+ TC118: 'A line inside a Screen is not something a screen can show',
26
+ TC119: '"Receive" or "Remember" is not at the top of a Screen',
20
27
  TC201: 'Unknown name',
21
28
  TC202: 'A name uses a word that belongs to Thunder',
22
29
  TC203: 'A name is defined twice',
@@ -28,6 +35,9 @@ export const CODES = {
28
35
  TC209: 'Unknown record',
29
36
  TC210: 'A record is missing a field',
30
37
  TC211: 'A name is used before it is set',
38
+ TC212: 'Unknown screen',
39
+ TC213: 'An input of a screen is changed',
40
+ TC214: 'A screen is given an input it does not have, or is missing one it needs',
31
41
  TC301: 'Money and a plain number are mixed',
32
42
  TC302: 'Values of different kinds are compared',
33
43
  TC303: 'A name changes kind',
@@ -38,6 +48,9 @@ export const CODES = {
38
48
  TC402: 'Thunder Core does not support this yet',
39
49
  TC403: 'A "Never" rule names more than one changing value',
40
50
  TC404: 'A default for an input must be a fixed value, after the inputs without one',
51
+ TC405: 'A Python module ("Use") can be used only when compiling to Python',
52
+ TC406: 'A Screen is built only for React (--target react)',
53
+ TC407: 'A step that cannot run when a button is pressed',
41
54
  TC501: 'Divided by zero (runtime)',
42
55
  TC502: 'Nothing used where a value was needed (runtime)',
43
56
  TC503: 'A field or entry is missing (runtime)',