@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,270 @@
|
|
|
1
|
+
// Thunder Core screens -> React (TSX, function components with useState).
|
|
2
|
+
//
|
|
3
|
+
// The same checked program the Thunder engine shows (compileCore -> program + info), and the same meaning: a screen
|
|
4
|
+
// is shown the way the engine's screen tree shows it (engine-core.mjs, ScreenApp), and a press or typing changes it the
|
|
5
|
+
// same way, through the same code (screen-values.mjs, copied into thunder_runtime.mjs with the engine's value code).
|
|
6
|
+
// compiler/test/core-react.test.mjs renders every screen program with react-dom/server, reads the HTML back into a
|
|
7
|
+
// tree, and requires the engine's tree; then it presses and types the same things in both and compares again.
|
|
8
|
+
//
|
|
9
|
+
// Records, functions, and kept values are written exactly as the TypeScript target writes them (it is the same
|
|
10
|
+
// generator). Each screen is a component: its inputs are props, each Remember is a useState, and each "When pressed"
|
|
11
|
+
// is a function that runs the steps on copies of the remembered values and keeps them only if every step works.
|
|
12
|
+
// "// thunder:N" (or {/* thunder:N */} inside the markup) marks the Thunder line each line came from.
|
|
13
|
+
|
|
14
|
+
import { Gen, id } from './target-typescript.mjs';
|
|
15
|
+
|
|
16
|
+
const js = (s) => JSON.stringify(s);
|
|
17
|
+
const pascal = (py) => py.split('_').filter(Boolean).map((w) => w[0].toUpperCase() + w.slice(1)).join('') || 'Value';
|
|
18
|
+
|
|
19
|
+
/** Programs with screens: the screens a React app is built from. */
|
|
20
|
+
export const hasScreens = (info) => (info.screens ?? []).some((s) => !info.imported?.has(s));
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Compile one checked file with screens to <module>.tsx. Its first screen is where the app starts; ThunderApp shows
|
|
24
|
+
* it, and any screen a button opens.
|
|
25
|
+
*/
|
|
26
|
+
export function toReact(program, info, { source = 'main.thunder' } = {}) {
|
|
27
|
+
return new ReactGen(program, info, source).app();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
class ReactGen extends Gen {
|
|
31
|
+
/**
|
|
32
|
+
* A line of markup, with its Thunder line as a JSX comment (a // comment is not allowed there). The comment touches
|
|
33
|
+
* the tag: spaces between them would be text on the page.
|
|
34
|
+
*/
|
|
35
|
+
tag(text, line) { this.out.push(`${' '.repeat(this.ind)}${text}${line ? `{/* thunder:${line} */}` : ''}`); }
|
|
36
|
+
|
|
37
|
+
app() {
|
|
38
|
+
const own = (s) => !this.info.imported?.has(s);
|
|
39
|
+
const screens = this.info.screens.filter(own);
|
|
40
|
+
const head = [
|
|
41
|
+
`// Generated by the Thunder compiler from ${this.source}.`,
|
|
42
|
+
`// A build artifact: change ${this.source.split('/').pop()} and build again, or keep this file and edit it as React.`,
|
|
43
|
+
'// It needs React 18 and thunder_runtime.mjs (written next to it). "thunder:N" marks the Thunder line.',
|
|
44
|
+
'',
|
|
45
|
+
"import * as React from 'react';",
|
|
46
|
+
"import * as T from './thunder_runtime.mjs';",
|
|
47
|
+
'',
|
|
48
|
+
`const FILE = ${js(this.source)};`,
|
|
49
|
+
'',
|
|
50
|
+
'/** Opens another screen of the app (by its name). */',
|
|
51
|
+
'type Opens = (screen: string) => void;',
|
|
52
|
+
];
|
|
53
|
+
for (const r of this.info.records.filter(own)) { this.blank(); this.record(r); }
|
|
54
|
+
for (const f of this.info.funcs.filter(own)) { this.blank(); this.func(f); }
|
|
55
|
+
const consts = this.info.consts.filter(own);
|
|
56
|
+
if (consts.length) {
|
|
57
|
+
this.blank();
|
|
58
|
+
for (const k of consts) this.emit(`export let ${id(k.py)}: any;`, k.line);
|
|
59
|
+
this.emit('T.init(FILE, (F: any) => {');
|
|
60
|
+
this.ind++;
|
|
61
|
+
for (const k of consts) this.emit(`F.line = ${k.line}; ${id(k.py)} = ${this.x(k.e)};`, k.line);
|
|
62
|
+
this.ind--;
|
|
63
|
+
this.emit('});');
|
|
64
|
+
}
|
|
65
|
+
for (const s of screens) { this.blank(); this.screen(s); }
|
|
66
|
+
// The app: the first screen, and the screens a button opens.
|
|
67
|
+
const opened = new Set();
|
|
68
|
+
const visit = (els) => {
|
|
69
|
+
for (const el of els ?? []) {
|
|
70
|
+
if (el.k === 'button') { if (el.opens?.py) opened.add(el.opens.py); walkSteps(el.press?.body, (s) => { if (s.k === 'open' && s.py) opened.add(s.py); }); }
|
|
71
|
+
for (const b of el.branches ?? []) visit(b.body);
|
|
72
|
+
visit(el.otherwise); visit(el.body);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
for (const s of screens) visit(s.body);
|
|
76
|
+
const first = screens[0];
|
|
77
|
+
const others = screens.filter((s) => s !== first && opened.has(s.py));
|
|
78
|
+
this.blank();
|
|
79
|
+
this.emit(`/** The app: it starts on ${first.text}${others.length ? `, and shows ${others.map((s) => s.text).join(', ')} when a button opens ${others.length === 1 ? 'it' : 'them'}` : ''}. */`);
|
|
80
|
+
this.emit('export function ThunderApp() {');
|
|
81
|
+
this.ind++;
|
|
82
|
+
this.emit(`const [screen, setScreen] = React.useState<string>(${js(first.py)});`);
|
|
83
|
+
this.emit('return (');
|
|
84
|
+
this.ind++;
|
|
85
|
+
this.tag('<T.ShowsProblems key={screen}>');
|
|
86
|
+
this.ind++;
|
|
87
|
+
const chain = others.map((s) => `screen === ${js(s.py)} ? <${s.py} open={setScreen} /> : `).join('');
|
|
88
|
+
this.tag(chain ? `{${chain}<${first.py} open={setScreen} />}` : `<${first.py} open={setScreen} />`);
|
|
89
|
+
this.ind--;
|
|
90
|
+
this.tag('</T.ShowsProblems>');
|
|
91
|
+
this.ind--;
|
|
92
|
+
this.emit(');');
|
|
93
|
+
this.ind--;
|
|
94
|
+
this.emit('}');
|
|
95
|
+
return `${head.join('\n')}\n${this.out.join('\n').replace(/\n{3,}/g, '\n\n')}\n`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
screen(s) {
|
|
99
|
+
const props = ['open: Opens', ...s.params.map((p) => `${id(p.py)}${p.dflt ? '?' : ''}: any`)];
|
|
100
|
+
this.emit(`/** Screen ${s.text}. */`);
|
|
101
|
+
this.emit(`export function ${s.py}(props: { ${props.join('; ')} }) {`, s.line);
|
|
102
|
+
this.ind++;
|
|
103
|
+
this.emit(`const F = T.enterFrame(FILE, ${s.line});`);
|
|
104
|
+
this.emit('try {');
|
|
105
|
+
this.ind++;
|
|
106
|
+
for (const p of s.params) {
|
|
107
|
+
const v = id(p.py);
|
|
108
|
+
if (p.dflt) this.emit(`const ${v} = props.${v} === undefined ? (F.line = ${p.line}, ${this.x(p.dflt)}) : props.${v};`, p.line);
|
|
109
|
+
else this.emit(`const ${v} = props.${v};`, p.line);
|
|
110
|
+
}
|
|
111
|
+
this.state = s.remember.map((r) => ({ name: id(r.py), set: `set${pascal(r.py)}` }));
|
|
112
|
+
for (const r of s.remember) this.emit(`const [${id(r.py)}, set${pascal(r.py)}] = React.useState<any>(() => (F.line = ${r.line}, ${this.x(r.e)}));`, r.line);
|
|
113
|
+
this.emit('const [screenProblem, setScreenProblem] = React.useState<string | null>(null);');
|
|
114
|
+
// The buttons' steps, one function each.
|
|
115
|
+
this.handlers = new Map();
|
|
116
|
+
const used = new Set();
|
|
117
|
+
const buttons = [];
|
|
118
|
+
// A button inside a list is pressed for one item: the item (and any outer list's item) is given to its steps.
|
|
119
|
+
const visit = (els, items) => {
|
|
120
|
+
for (const el of els) {
|
|
121
|
+
if (el.k === 'button' && el.press) { el.press.items = items; buttons.push(el); }
|
|
122
|
+
for (const b of el.branches ?? []) visit(b.body, items);
|
|
123
|
+
visit(el.otherwise ?? [], items);
|
|
124
|
+
visit(el.body ?? [], el.k === 'list' ? [...items, id(el.v.py)] : items);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
visit(s.body, []);
|
|
128
|
+
for (const el of buttons) {
|
|
129
|
+
const words = el.label.k === 'str' && el.label.parts.every((p) => typeof p === 'string') ? el.label.parts.join('') : '';
|
|
130
|
+
let name = `press${pascal(words.toLowerCase().replace(/[^a-z0-9]+/g, '_')) || 'Button'}`;
|
|
131
|
+
if (used.has(name) || name === 'pressValue') name = `press${pascal(words.toLowerCase().replace(/[^a-z0-9]+/g, '_')) || 'Button'}${el.line}`;
|
|
132
|
+
used.add(name);
|
|
133
|
+
this.handlers.set(el, name);
|
|
134
|
+
this.pressHandler(el, name, s);
|
|
135
|
+
}
|
|
136
|
+
this.emit('return (');
|
|
137
|
+
this.ind++;
|
|
138
|
+
this.tag(`<section className="thunder-screen" aria-label=${js(s.text)}>`, s.line);
|
|
139
|
+
this.ind++;
|
|
140
|
+
this.elements(s.body);
|
|
141
|
+
this.tag('{screenProblem !== null && <p role="alert" className="thunder-problem">{screenProblem}</p>}');
|
|
142
|
+
this.ind--;
|
|
143
|
+
this.tag('</section>');
|
|
144
|
+
this.ind--;
|
|
145
|
+
this.emit(');');
|
|
146
|
+
this.ind--;
|
|
147
|
+
this.emit('} catch (e) { throw T.fail(e); } finally { T.leave(); }');
|
|
148
|
+
this.ind--;
|
|
149
|
+
this.emit('}');
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** When "<label>" is pressed: the steps run on copies of the remembered values, which are kept only if all work. */
|
|
153
|
+
pressHandler(el, name, s) {
|
|
154
|
+
const names = this.state.map((x) => x.name);
|
|
155
|
+
const outer = new Set([...names, ...s.params.map((p) => id(p.py)), ...(el.press.items ?? [])]);
|
|
156
|
+
this.emit(`// When ${el.label.k === 'str' ? js(el.label.parts.filter((p) => typeof p === 'string').join('')) : 'the button'} is pressed: the steps change copies, kept only if every step works.`, el.press.line);
|
|
157
|
+
const items = el.press.items ?? [];
|
|
158
|
+
this.emit(`const ${name} = (${items.map((v) => `${v}: any`).join(', ')}) => T.press(FILE, ${el.press.line}, setScreenProblem, props.open, [${names.join(', ')}], [${this.state.map((x) => x.set).join(', ')}],`);
|
|
159
|
+
this.ind++;
|
|
160
|
+
this.emit(`(F: any, openScreen: Opens${names.map((n) => `, ${n}: any`).join('')}) => {`);
|
|
161
|
+
this.ind++;
|
|
162
|
+
const locals = this.localsOf(el.press.body).map(id).filter((n) => !outer.has(n));
|
|
163
|
+
if (locals.length) this.emit(`let ${locals.map((n) => `${n}: any`).join(', ')};`);
|
|
164
|
+
this.block(el.press.body, true);
|
|
165
|
+
if (el.opens?.py) this.line(el.line, `openScreen(${js(el.opens.py)});`);
|
|
166
|
+
this.emit(`return [${names.join(', ')}];`);
|
|
167
|
+
this.ind--;
|
|
168
|
+
this.emit('});');
|
|
169
|
+
this.ind--;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
stmt(s, isVoid) {
|
|
173
|
+
if (s.k === 'open') { this.line(s.line, `openScreen(${js(s.py)});`); return; }
|
|
174
|
+
super.stmt(s, isVoid);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** A value shown on the screen: text and whole numbers as they are, anything else the Thunder way. */
|
|
178
|
+
shown(e) {
|
|
179
|
+
if (e.k === 'str' && e.parts.every((p) => typeof p === 'string')) return js(e.parts.join(''));
|
|
180
|
+
const k = e.type?.k;
|
|
181
|
+
return `(F.line = ${e.line}, ${k === 'text' || k === 'whole' ? `T.pyStr(${this.x(e)})` : `T.show(${this.x(e)})`})`;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
elements(els) {
|
|
185
|
+
for (const el of els) {
|
|
186
|
+
const L = el.line;
|
|
187
|
+
switch (el.k) {
|
|
188
|
+
case 'heading': this.tag(`<h1>{${this.shown(el.e)}}</h1>`, L); break;
|
|
189
|
+
case 'text': this.tag(`<p>{${this.shown(el.e)}}</p>`, L); break;
|
|
190
|
+
case 'row':
|
|
191
|
+
this.tag('<div className="thunder-row">', L);
|
|
192
|
+
this.ind++; this.elements(el.body); this.ind--;
|
|
193
|
+
this.tag('</div>');
|
|
194
|
+
break;
|
|
195
|
+
case 'list': {
|
|
196
|
+
const v = id(el.v.py);
|
|
197
|
+
this.tag('<ul>', L);
|
|
198
|
+
this.ind++;
|
|
199
|
+
this.tag(`{T.pyIter((F.line = ${L}, ${this.x(el.iter)})).map((${v}: any, itemIndex: number) => (`);
|
|
200
|
+
this.ind++;
|
|
201
|
+
this.tag('<li key={itemIndex}>');
|
|
202
|
+
this.ind++; this.elements(el.body); this.ind--;
|
|
203
|
+
this.tag('</li>');
|
|
204
|
+
this.ind--;
|
|
205
|
+
this.tag('))}');
|
|
206
|
+
this.ind--;
|
|
207
|
+
this.tag('</ul>');
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
case 'when': {
|
|
211
|
+
el.branches.forEach((b, k) => {
|
|
212
|
+
this.tag(`${k === 0 ? '{' : ') : '}(F.line = ${b.line}, ${this.yn(b.cond)}) ? (`);
|
|
213
|
+
this.ind++;
|
|
214
|
+
this.tag(`<React.Fragment key="${k}">`, b.line);
|
|
215
|
+
this.ind++; this.elements(b.body); this.ind--;
|
|
216
|
+
this.tag('</React.Fragment>');
|
|
217
|
+
this.ind--;
|
|
218
|
+
});
|
|
219
|
+
if (el.otherwise) {
|
|
220
|
+
this.tag(') : (');
|
|
221
|
+
this.ind++;
|
|
222
|
+
this.tag('<React.Fragment key="else">');
|
|
223
|
+
this.ind++; this.elements(el.otherwise); this.ind--;
|
|
224
|
+
this.tag('</React.Fragment>');
|
|
225
|
+
this.ind--;
|
|
226
|
+
this.tag(')}');
|
|
227
|
+
} else this.tag(') : null}');
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
case 'textbox': case 'numberbox': {
|
|
231
|
+
const st = this.state.find((x) => x.name === id(el.target.ref.py));
|
|
232
|
+
const change = el.k === 'textbox'
|
|
233
|
+
? `(event) => T.typeText(event.target.value, ${st.set}, setScreenProblem)`
|
|
234
|
+
: `(event) => T.typeNumber(event.target.value, ${js(el.boxKind)}, ${st.set}, setScreenProblem)`;
|
|
235
|
+
this.tag('<label className="thunder-box">', L);
|
|
236
|
+
this.ind++;
|
|
237
|
+
this.tag(`{${this.shown(el.label)}}`);
|
|
238
|
+
this.tag(`<input type="${el.k === 'textbox' ? 'text' : 'number'}" value={${el.k === 'textbox' ? `T.pyStr(${st.name})` : `T.show(${st.name})`}} onChange={${change}} />`);
|
|
239
|
+
this.ind--;
|
|
240
|
+
this.tag('</label>');
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
case 'button': {
|
|
244
|
+
const h = this.handlers.get(el);
|
|
245
|
+
const onClick = h ? (el.press.items?.length ? `() => ${h}(${el.press.items.join(', ')})` : h) : `() => props.open(${js(el.opens.py)})`;
|
|
246
|
+
const off = el.off ? ` disabled={(F.line = ${L}, ${this.yn(el.off)})}` : '';
|
|
247
|
+
this.tag(`<button type="button"${off} onClick={${onClick}}>{${this.shown(el.label)}}</button>`, L);
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
case 'child': {
|
|
251
|
+
const given = el.given.map((g) => ` ${id(g.py)}={(F.line = ${g.line}, ${this.x(g.e)})}`).join('');
|
|
252
|
+
this.tag(`<${el.screen.py} open={props.open}${given} />`, L);
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
255
|
+
default: this.tag(`{/* not generated: ${el.k} */}`, L);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** Every step under a "When pressed", nested ones too. */
|
|
262
|
+
function walkSteps(body, fn) {
|
|
263
|
+
for (const s of body ?? []) {
|
|
264
|
+
fn(s);
|
|
265
|
+
walkSteps(s.body, fn); walkSteps(s.otherwise, fn); walkSteps(s.handler, fn);
|
|
266
|
+
for (const b of s.branches ?? []) walkSteps(b.body, fn);
|
|
267
|
+
if (s.stmt) walkSteps([s.stmt], fn);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|