@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.
- package/CHANGELOG.md +131 -0
- package/dist/core.cjs +2080 -278
- package/dist/index.cjs +12742 -10943
- package/dist/learn.cjs +2716 -345
- package/package.json +1 -1
- package/src/cli.mjs +31 -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 +18 -0
- package/src/core.mjs +1 -0
- package/src/emit.mjs +1 -1
- package/src/faithful.mjs +30 -0
- package/src/libraries.mjs +10 -3
- package/src/model.mjs +50 -8
- package/src/read-level.mjs +48 -2
- 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 +43 -0
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
// Thunder Core -> C# (.NET): readable static classes, one per Thunder file, and ThunderRuntime.cs next to them.
|
|
2
|
+
//
|
|
3
|
+
// The same checked program the other targets use (compileCore -> program + info), and the same meaning: every
|
|
4
|
+
// statement does what the Thunder engine does for it, and every operation calls ThunderRuntime.cs, a line-by-line port
|
|
5
|
+
// of the engine's own value code (engine-values.mjs). compiler/test/core-engines.mjs runs every Core test program on
|
|
6
|
+
// Python, the Thunder engine, compiled TypeScript, and compiled C#, and requires the same output from all of them.
|
|
7
|
+
//
|
|
8
|
+
// Every generated statement sets the frame's line (F.line = N) and ends with `// thunder:N`, the Thunder line it came
|
|
9
|
+
// from, so an error says which Thunder line failed, exactly as the other targets do.
|
|
10
|
+
|
|
11
|
+
import { percentDecimal } from './target-python.mjs';
|
|
12
|
+
import { firstLine } from './target-typescript.mjs';
|
|
13
|
+
|
|
14
|
+
/** A C# string literal for any text (JSON's escapes are C#'s, except the line breaks C# does not allow raw). */
|
|
15
|
+
export const cs = (s) => JSON.stringify(s).replace(/[\u0085\u2028\u2029]/g, (c) => `\\u${c.charCodeAt(0).toString(16).padStart(4, '0')}`);
|
|
16
|
+
|
|
17
|
+
const KEYWORDS = new Set(('abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach goto if implicit in int interface internal is lock long namespace new null object operator out override params private protected public readonly ref return sbyte sealed short sizeof stackalloc static string struct switch this throw true try typeof uint ulong unchecked unsafe ushort using virtual void volatile while').split(' '));
|
|
18
|
+
const OWNED = new Set(['T', 'F', 'FILE', 'main', 'Init', 'THUNDER_TESTS', 'Thunder', 'System', 'Program']);
|
|
19
|
+
/** A C# name for a Thunder (Python) name: the same, unless C# or the generated code owns it. */
|
|
20
|
+
export const id = (py) => (OWNED.has(py) ? `${py}_` : KEYWORDS.has(py) ? `@${py}` : py);
|
|
21
|
+
/** The static class a Thunder module becomes. */
|
|
22
|
+
export const classOf = (module) => `M_${module}`;
|
|
23
|
+
|
|
24
|
+
const KNOWN_BOOL = new Set(['cmp', 'contains', 'between', 'adj', 'isNothing', 'not', 'bool']);
|
|
25
|
+
|
|
26
|
+
/** Thunder modules a program uses that only Python has (Use): compiled C# cannot call them. */
|
|
27
|
+
export function pythonOnlyUses(info) {
|
|
28
|
+
return (info.modules ?? []).filter((m) => !info.imported?.has(m)).map((m) => ({ module: m.module, line: m.line }));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Compile one checked file to C#: a static class M_<module> in `namespace`. `imports`: the src modules a test file
|
|
33
|
+
* sees (their classes are brought in with `using static`).
|
|
34
|
+
*/
|
|
35
|
+
export function toCSharp(program, info, { source = 'main.thunder', module = 'main', namespace = 'ThunderProgram', imports = [] } = {}) {
|
|
36
|
+
return new Gen(program, info, source).module(module, namespace, imports);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
class Gen {
|
|
40
|
+
constructor(program, info, source) {
|
|
41
|
+
this.p = program; this.info = info; this.source = source;
|
|
42
|
+
this.out = []; this.ind = 2; this.tmp = 0;
|
|
43
|
+
}
|
|
44
|
+
emit(text, line) { this.out.push(`${' '.repeat(this.ind)}${text}${line ? ` // thunder:${line}` : ''}`); }
|
|
45
|
+
blank() { if (this.out.length && this.out[this.out.length - 1] !== '') this.out.push(''); }
|
|
46
|
+
fresh(base) { return `__t_${base}${++this.tmp}`; }
|
|
47
|
+
|
|
48
|
+
module(module, namespace, imports) {
|
|
49
|
+
const own = (s) => !this.info.imported?.has(s);
|
|
50
|
+
for (const r of this.info.records.filter(own)) { this.blank(); this.record(r); }
|
|
51
|
+
for (const f of this.info.funcs.filter(own)) { this.blank(); this.func(f); }
|
|
52
|
+
const consts = this.info.consts.filter(own);
|
|
53
|
+
this.blank();
|
|
54
|
+
for (const k of consts) this.emit(`public static object ${id(k.py)};`, k.line);
|
|
55
|
+
this.emit('public static void Init()');
|
|
56
|
+
this.emit('{');
|
|
57
|
+
this.ind++;
|
|
58
|
+
if (consts.length) {
|
|
59
|
+
this.emit('T.init(FILE, F =>');
|
|
60
|
+
this.emit('{');
|
|
61
|
+
this.ind++;
|
|
62
|
+
for (const k of consts) this.emit(`F.line = ${k.line}; ${id(k.py)} = ${this.x(k.e)};`, k.line);
|
|
63
|
+
this.ind--;
|
|
64
|
+
this.emit('});');
|
|
65
|
+
}
|
|
66
|
+
this.ind--;
|
|
67
|
+
this.emit('}');
|
|
68
|
+
const tests = this.info.tests;
|
|
69
|
+
for (const t of tests) {
|
|
70
|
+
this.blank();
|
|
71
|
+
this.emit(`static void ${id(t.py)}()`, t.line);
|
|
72
|
+
this.emit('{');
|
|
73
|
+
this.ind++;
|
|
74
|
+
this.frameBody(`T.enterFrame(FILE, ${t.line})`, t.body, [], true);
|
|
75
|
+
this.ind--;
|
|
76
|
+
this.emit('}');
|
|
77
|
+
}
|
|
78
|
+
this.blank();
|
|
79
|
+
this.emit('public static readonly (string, System.Action, int)[] THUNDER_TESTS = new (string, System.Action, int)[]');
|
|
80
|
+
this.emit('{');
|
|
81
|
+
for (const t of tests) this.emit(` (${cs(t.title)}, new System.Action(${id(t.py)}), ${t.line}),`);
|
|
82
|
+
this.emit('};');
|
|
83
|
+
const main = this.p.main ?? [];
|
|
84
|
+
this.blank();
|
|
85
|
+
this.emit('public static void main()');
|
|
86
|
+
this.emit('{');
|
|
87
|
+
if (main.length) {
|
|
88
|
+
this.ind++;
|
|
89
|
+
this.frameBody('T.enterFrame(FILE, 0)', main, [], true);
|
|
90
|
+
this.ind--;
|
|
91
|
+
}
|
|
92
|
+
this.emit('}');
|
|
93
|
+
const head = [
|
|
94
|
+
`// Generated by the Thunder compiler from ${this.source}.`,
|
|
95
|
+
`// A build artifact: change ${this.source.split('/').pop()} and build again, or keep this file and edit it as C#.`,
|
|
96
|
+
'// It needs only .NET and ThunderRuntime.cs (written next to it). "// thunder:N" marks the Thunder line.',
|
|
97
|
+
'#nullable disable',
|
|
98
|
+
'#pragma warning disable CS0162, CS0168, CS0219, CS0472, CS1717, CS8321',
|
|
99
|
+
`namespace ${namespace}`,
|
|
100
|
+
'{',
|
|
101
|
+
' using Thunder;',
|
|
102
|
+
...imports.map((m) => ` using static ${namespace}.${classOf(m)};`),
|
|
103
|
+
'',
|
|
104
|
+
` public static class ${classOf(module)}`,
|
|
105
|
+
' {',
|
|
106
|
+
` const string FILE = ${cs(this.source)};`,
|
|
107
|
+
'',
|
|
108
|
+
];
|
|
109
|
+
return `${head.join('\n')}\n${this.out.join('\n').replace(/\n{3,}/g, '\n\n').replace(/^\n+/, '')}\n }\n}\n`;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
record(r) {
|
|
113
|
+
const fields = r.fields.map((f) => `T.field(${cs(f.py)}, ${cs(f.type?.k ?? 'any')})`).join(', ');
|
|
114
|
+
const rules = r.rulesToCheck ?? [];
|
|
115
|
+
if (!rules.length) { this.emit(`public static readonly RecordKind ${id(r.py)} = T.recordKind(${cs(r.py)}, new FieldDef[] { ${fields} });`, r.line); return; }
|
|
116
|
+
this.emit(`public static readonly RecordKind ${id(r.py)} = T.recordKind(${cs(r.py)}, new FieldDef[] { ${fields} }, new System.Collections.Generic.Dictionary<string, Rule[]>`, r.line);
|
|
117
|
+
this.emit('{');
|
|
118
|
+
this.ind++;
|
|
119
|
+
const bySubject = new Map();
|
|
120
|
+
for (const rule of rules) { const f = r.fieldMap.get(rule.subject); (bySubject.get(f.py) ?? bySubject.set(f.py, []).get(f.py)).push(rule); }
|
|
121
|
+
for (const [field, rs] of bySubject) {
|
|
122
|
+
this.emit(`[${cs(field)}] = new Rule[]`);
|
|
123
|
+
this.emit('{');
|
|
124
|
+
this.ind++;
|
|
125
|
+
for (const rule of rs) {
|
|
126
|
+
const v = this.fresh('value');
|
|
127
|
+
this.emit(`new Rule(${v} => { var F = T.topFrame(); return ${this.x(rule.rule.cond, { subject: v })}; }, ${cs(rule.text)}, ${rule.line}),`, rule.line);
|
|
128
|
+
}
|
|
129
|
+
this.ind--;
|
|
130
|
+
this.emit('},');
|
|
131
|
+
}
|
|
132
|
+
this.ind--;
|
|
133
|
+
this.emit('});');
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
func(f) {
|
|
137
|
+
const params = f.params.map((p) => `object ${id(p.py)}`);
|
|
138
|
+
// Inputs with a default: a call may leave them out (one overload per number of inputs given); a left-out input
|
|
139
|
+
// arrives as T.NOT_GIVEN and is filled in here: a fixed value at once, a kept value inside the call's frame.
|
|
140
|
+
const required = f.params.filter((p) => !p.dflt).length;
|
|
141
|
+
for (let k = required; k < f.params.length; k++) {
|
|
142
|
+
const given = f.params.slice(0, k).map((p) => id(p.py));
|
|
143
|
+
const rest = f.params.slice(k).map(() => 'T.NOT_GIVEN');
|
|
144
|
+
this.emit(`public static object ${id(f.py)}(${params.slice(0, k).join(', ')}) => ${id(f.py)}(${[...given, ...rest].join(', ')});`, f.line);
|
|
145
|
+
}
|
|
146
|
+
this.emit(`public static object ${id(f.py)}(${params.join(', ')})`, f.line);
|
|
147
|
+
this.emit('{');
|
|
148
|
+
this.ind++;
|
|
149
|
+
for (const p of f.params) if (p.dflt && p.dflt.k !== 'name') this.emit(`if (ReferenceEquals(${id(p.py)}, T.NOT_GIVEN)) ${id(p.py)} = ${this.x(p.dflt)};`);
|
|
150
|
+
const pre = [];
|
|
151
|
+
for (const p of f.params) if (p.dflt?.k === 'name') pre.push([`if (ReferenceEquals(${id(p.py)}, T.NOT_GIVEN)) { F.line = ${p.line}; ${id(p.py)} = ${this.x(p.dflt)}; }`, p.line]);
|
|
152
|
+
for (const p of f.params) for (const rule of p.rules ?? []) pre.push([`F.line = ${p.line}; T.never(${this.x(rule.rule.cond, { subject: id(p.py) })}, ${cs(rule.text)}, ${rule.line}, ${id(p.py)});`, p.line]);
|
|
153
|
+
const first = firstLine(f);
|
|
154
|
+
this.frameBody(`T.enterCall(FILE, ${f.line}, ${first ?? -1})`, f.body, pre, false, f.params.map((p) => p.py));
|
|
155
|
+
this.ind--;
|
|
156
|
+
this.emit('}');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** A body that runs in its own frame: errors leave with the Thunder line they happened on. */
|
|
160
|
+
frameBody(enter, body, pre, isVoid, params = []) {
|
|
161
|
+
this.emit(`var F = ${enter};`);
|
|
162
|
+
this.emit('try');
|
|
163
|
+
this.emit('{');
|
|
164
|
+
this.ind++;
|
|
165
|
+
const locals = [...this.localsOf(body)].filter((n) => !params.includes(n));
|
|
166
|
+
if (locals.length) this.emit(`object ${locals.map((n) => `${id(n)} = null`).join(', ')};`);
|
|
167
|
+
for (const [code, line] of pre) this.emit(code, line);
|
|
168
|
+
this.block(body, isVoid);
|
|
169
|
+
if (!isVoid) this.emit('return null;');
|
|
170
|
+
this.ind--;
|
|
171
|
+
this.emit('}');
|
|
172
|
+
const e = this.fresh('e');
|
|
173
|
+
this.emit(`catch (System.Exception ${e}) { throw T.fail(${e}); }`);
|
|
174
|
+
this.emit('finally { T.leave(); }');
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** The names a body assigns (Python's locals): declared once at the top, as a Python function's are. */
|
|
178
|
+
localsOf(body) {
|
|
179
|
+
const names = new Set();
|
|
180
|
+
const visit = (ss) => {
|
|
181
|
+
for (const s of ss ?? []) {
|
|
182
|
+
if (s.k === 'set' && s.target.k === 'name' && s.target.ref?.py) names.add(s.target.ref.py);
|
|
183
|
+
for (const v of s.vars ?? []) if (v?.py) names.add(v.py);
|
|
184
|
+
if (s.k === 'range' && s.v?.py) names.add(s.v.py);
|
|
185
|
+
if (s.errPy) names.add(s.errPy);
|
|
186
|
+
visit(s.body); visit(s.handler); visit(s.otherwise);
|
|
187
|
+
for (const b of s.branches ?? []) visit(b.body);
|
|
188
|
+
if (s.stmt) visit([s.stmt]);
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
visit(body);
|
|
192
|
+
const globals = new Set([...this.info.consts, ...this.info.funcs, ...this.info.records].map((x) => x.py));
|
|
193
|
+
return [...names].filter((n) => !globals.has(n));
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
block(body, isVoid) { for (const s of body) this.stmt(s, isVoid); }
|
|
197
|
+
|
|
198
|
+
/** F.line = N; <code> // thunder:N */
|
|
199
|
+
line(L, code) { this.emit(`F.line = ${L}; ${code}`, L); }
|
|
200
|
+
|
|
201
|
+
/** Check a watched name's Never rules on its new value, then store it. */
|
|
202
|
+
assign(s, name, valueCode) {
|
|
203
|
+
if (!s.rules?.length) { this.line(s.line, `${id(name)} = ${valueCode};`); return; }
|
|
204
|
+
const v = this.fresh('new');
|
|
205
|
+
this.line(s.line, `{ object ${v} = ${valueCode};`);
|
|
206
|
+
this.ind++;
|
|
207
|
+
for (const rule of s.rules) this.emit(`T.never(${this.x(rule.rule.cond, { subject: v })}, ${cs(rule.text)}, ${rule.line}, ${v});`, s.line);
|
|
208
|
+
this.emit(`${id(name)} = ${v}; }`, s.line);
|
|
209
|
+
this.ind--;
|
|
210
|
+
}
|
|
211
|
+
rulesAfter(s, name) {
|
|
212
|
+
for (const rule of s.rules ?? []) this.emit(`T.never(${this.x(rule.rule.cond, { subject: id(name) })}, ${cs(rule.text)}, ${rule.line}, ${id(name)});`, s.line);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Code that stores `v` (a name of generated code) into a name, a field, or an entry. */
|
|
216
|
+
storeCode(t, v) {
|
|
217
|
+
if (t.k === 'name') return `${id(t.ref?.py ?? 'unknown')} = ${v};`;
|
|
218
|
+
if (t.k === 'field') return t.static ? `T.setAttr(${this.x(t.obj)}, ${cs(t.py)}, ${v});` : `T.setField(${this.x(t.obj)}, ${cs(t.text)}, ${v});`;
|
|
219
|
+
if (t.k === 'at') {
|
|
220
|
+
const fn = t.objKind === 'list' ? 'T.setItem' : t.objKind === 'map' ? 'T.mapSet' : 'T.setEntry';
|
|
221
|
+
return `${fn}(${this.x(t.obj)}, ${this.x(t.key)}, ${v});`;
|
|
222
|
+
}
|
|
223
|
+
return '';
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
stmt(s, isVoid) {
|
|
227
|
+
const L = s.line;
|
|
228
|
+
switch (s.k) {
|
|
229
|
+
case 'say': this.line(L, `T.say(${s.e.type?.k === 'text' ? `T.pyStr(${this.x(s.e)})` : `T.show(${this.x(s.e)})`});`); return;
|
|
230
|
+
case 'set': {
|
|
231
|
+
const t = s.target;
|
|
232
|
+
if (t.k === 'name') { this.assign(s, t.ref.py, this.x(s.e)); return; }
|
|
233
|
+
const v = this.fresh('v');
|
|
234
|
+
this.line(L, `{ object ${v} = ${this.x(s.e)}; ${this.storeCode(t, v)} }`);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
case 'give': this.line(L, isVoid ? 'return;' : `return ${s.e.k === 'nothing' ? 'null' : this.x(s.e)};`); return;
|
|
238
|
+
case 'stop': { const v = this.x(s.e); this.line(L, `throw T.Stop(${s.e.type?.k === 'text' ? `T.pyStr(${v})` : `T.show(${v})`});`); return; }
|
|
239
|
+
case 'if': {
|
|
240
|
+
this.emit(`F.line = ${L};`, L);
|
|
241
|
+
s.branches.forEach((b, k) => {
|
|
242
|
+
this.emit(`${k === 0 ? 'if' : '} else if'} (T.at(F, ${b.line}) && ${this.yn(b.cond)}) {`, b.line);
|
|
243
|
+
this.ind++; this.block(b.body, isVoid); this.ind--;
|
|
244
|
+
});
|
|
245
|
+
if (s.otherwise) { this.emit('} else {'); this.ind++; this.block(s.otherwise, isVoid); this.ind--; }
|
|
246
|
+
this.emit('}');
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
case 'each': {
|
|
250
|
+
const [a, b] = s.vars;
|
|
251
|
+
const it = this.x(s.iter);
|
|
252
|
+
const seq = b ? (s.iterKind === 'map' ? `T.mapItems(${it})` : s.iterKind === 'pairs' ? `T.pyIter(${it})` : `T.pairsOf(${it})`) : `T.pyIter(${it})`;
|
|
253
|
+
const el = this.fresh('el');
|
|
254
|
+
this.line(L, `foreach (var ${el} in ${seq}) {`);
|
|
255
|
+
this.ind++;
|
|
256
|
+
if (b) { const pr = this.fresh('pair'); this.emit(`F.line = ${L}; { var ${pr} = T.unpack2(${el}); ${id(a.py)} = ${pr}[0]; ${id(b.py)} = ${pr}[1]; }`, L); }
|
|
257
|
+
else this.emit(`F.line = ${L}; ${id(a.py)} = ${el};`, L);
|
|
258
|
+
this.block(s.body, isVoid);
|
|
259
|
+
this.ind--;
|
|
260
|
+
this.emit('}');
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
case 'range': {
|
|
264
|
+
const lo = this.fresh('from'), hi = this.fresh('to'), n = this.fresh('n');
|
|
265
|
+
const to = s.to.k === 'num' && s.to.whole ? `T.I(${cs(String(BigInt(s.to.text.replace(/_/g, '')) + 1n))})` : `T.add(${this.x(s.to)}, T.I("1"))`;
|
|
266
|
+
const a = this.fresh('a'), b = this.fresh('b');
|
|
267
|
+
this.line(L, `{ object ${a} = ${this.x(s.from)}, ${b} = ${to}; System.Numerics.BigInteger ${lo} = T.rangeInt(${a}), ${hi} = T.rangeInt(${b});`);
|
|
268
|
+
this.ind++;
|
|
269
|
+
this.emit(`for (System.Numerics.BigInteger ${n} = ${lo}; ${n} < ${hi}; ${n}++) {`);
|
|
270
|
+
this.ind++;
|
|
271
|
+
this.emit(`F.line = ${L}; ${id(s.v.py)} = ${n};`, L);
|
|
272
|
+
this.block(s.body, isVoid);
|
|
273
|
+
this.ind--;
|
|
274
|
+
this.emit('}');
|
|
275
|
+
this.ind--;
|
|
276
|
+
this.emit('}');
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
case 'while':
|
|
280
|
+
this.emit(`while (T.at(F, ${L}) && ${s.until ? `!(${this.yn(s.cond)})` : this.yn(s.cond)}) {`, L);
|
|
281
|
+
this.ind++; this.block(s.body, isVoid); this.ind--;
|
|
282
|
+
this.emit('}');
|
|
283
|
+
return;
|
|
284
|
+
case 'times': {
|
|
285
|
+
const count = this.fresh('count'), k = this.fresh('k');
|
|
286
|
+
this.line(L, `{ System.Numerics.BigInteger ${count} = T.rangeInt(${this.x(s.count)});`);
|
|
287
|
+
this.ind++;
|
|
288
|
+
this.emit(`for (System.Numerics.BigInteger ${k} = 0; ${k} < ${count}; ${k}++) {`);
|
|
289
|
+
this.ind++;
|
|
290
|
+
this.emit(`F.line = ${L};`, L);
|
|
291
|
+
this.block(s.body, isVoid);
|
|
292
|
+
this.ind--;
|
|
293
|
+
this.emit('}');
|
|
294
|
+
this.ind--;
|
|
295
|
+
this.emit('}');
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
case 'expect': this.expect(s); return;
|
|
299
|
+
case 'add': case 'remove': this.addRemove(s); return;
|
|
300
|
+
case 'increase': case 'decrease': {
|
|
301
|
+
const op = s.k === 'increase' ? 'T.add' : 'T.sub';
|
|
302
|
+
if (s.target.k === 'name') { const py = s.target.ref?.py ?? 'unknown'; this.assign(s, py, `${op}(${id(py)}, ${this.x(s.e)})`); return; }
|
|
303
|
+
const v = this.fresh('v');
|
|
304
|
+
this.line(L, `{ object ${v} = ${op}(${this.x(s.target)}, ${this.x(s.e)}); ${this.storeCode(s.target, v)} }`);
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
case 'sort': {
|
|
308
|
+
const it = this.fresh('item');
|
|
309
|
+
const key = s.field ? `T.keyAt(F, ${it} => ${this.fieldGet(it, s.field)})` : 'null';
|
|
310
|
+
this.line(L, `T.sortInPlace(${this.x(s.target)}, ${key}, ${s.desc ? 'true' : 'false'});`);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
case 'reverse': this.line(L, `T.reverseInPlace(${this.x(s.target)});`); return;
|
|
314
|
+
case 'do': this.line(L, `T.discard(${this.x(s.e)});`); return;
|
|
315
|
+
case 'tryFail': {
|
|
316
|
+
this.emit('try {', L);
|
|
317
|
+
this.ind++; this.stmt(s.stmt, isVoid); this.ind--;
|
|
318
|
+
const e = this.fresh('failure');
|
|
319
|
+
this.emit(`} catch (System.Exception ${e}) {`, L);
|
|
320
|
+
this.ind++;
|
|
321
|
+
this.line(L, `${id(s.errPy)} = T.explain(T.asPyErr(${e}));`);
|
|
322
|
+
this.block(s.handler, isVoid);
|
|
323
|
+
this.ind--;
|
|
324
|
+
this.emit('}');
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
default: this.emit(`// not generated: ${s.k}`, L);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
addRemove(s) {
|
|
332
|
+
const L = s.line;
|
|
333
|
+
const isName = s.target.k === 'name';
|
|
334
|
+
const py = isName ? s.target.ref?.py ?? 'unknown' : null;
|
|
335
|
+
const v = this.fresh('v'), t = this.fresh('t');
|
|
336
|
+
const head = `object ${v} = ${this.x(s.e)}, ${t} = ${this.x(s.target)};`;
|
|
337
|
+
let body;
|
|
338
|
+
if (s.targetKind === 'list') body = s.k === 'add' ? `T.appendTo(${t}, ${v});` : `T.removeFrom(${t}, ${v});`;
|
|
339
|
+
else if (s.targetKind === 'map') body = `T.removeFrom(${t}, ${v});`;
|
|
340
|
+
else if (s.targetKind === 'number') {
|
|
341
|
+
const nv = `${s.k === 'add' ? 'T.add' : 'T.sub'}(${t}, ${v})`;
|
|
342
|
+
if (isName && s.rules?.length) {
|
|
343
|
+
const n = this.fresh('new');
|
|
344
|
+
this.line(L, `{ ${head} object ${n} = ${nv};`);
|
|
345
|
+
this.ind++;
|
|
346
|
+
for (const rule of s.rules) this.emit(`T.never(${this.x(rule.rule.cond, { subject: n })}, ${cs(rule.text)}, ${rule.line}, ${n});`, L);
|
|
347
|
+
this.emit(`${id(py)} = ${n}; }`, L);
|
|
348
|
+
this.ind--;
|
|
349
|
+
this.rulesAfter(s, py);
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
body = isName ? `${id(py)} = ${nv};` : this.storeCode(s.target, nv);
|
|
353
|
+
} else if (s.k === 'add') body = isName ? `${id(py)} = T.addTo(${t}, ${v});` : this.storeCode(s.target, `T.addTo(${t}, ${v})`);
|
|
354
|
+
else body = `T.removeFrom(${t}, ${v});`;
|
|
355
|
+
this.line(L, `{ ${head} ${body} }`);
|
|
356
|
+
if (isName) this.rulesAfter(s, py);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
expect(s) {
|
|
360
|
+
const L = s.line;
|
|
361
|
+
const text = cs((s.raw ?? 'Expect').trim());
|
|
362
|
+
if (s.mode === 'true') { this.line(L, `T.expectTrue(${this.x(s.e)}, ${text});`); return; }
|
|
363
|
+
if (s.mode === 'fail') {
|
|
364
|
+
const m = this.fresh('msg');
|
|
365
|
+
this.line(L, `{ object ${m} = ${s.msg ? this.x(s.msg) : 'null'}; T.expectFailure(() => { T.discard(${this.x(s.e)}); }, ${text}, ${m}); }`);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
if (s.mode === 'cmp') { this.line(L, `T.expectValue(${this.x(s.e)}, "contains", "contains", ${this.x(s.want)}, ${text});`); return; }
|
|
369
|
+
let c = s.check, negate = false;
|
|
370
|
+
if (c.k === 'not') { negate = true; c = c.e; }
|
|
371
|
+
const OPS = { '==': 'is', '!=': 'is not', '>=': 'at least', '<=': 'at most', '>': 'more than', '<': 'less than' };
|
|
372
|
+
let op, want = 'null';
|
|
373
|
+
if (c.k === 'cmp') { op = OPS[c.op]; want = this.x(c.r); }
|
|
374
|
+
else if (c.k === 'adj') op = c.adj;
|
|
375
|
+
else if (c.k === 'isNothing') op = c.neg ? 'not nothing' : 'nothing';
|
|
376
|
+
else if (c.k === 'between') { op = 'between'; want = `T.list(${this.x(c.lo)}, ${this.x(c.hi)})`; }
|
|
377
|
+
else if (c.k === 'contains') { op = 'in'; want = this.x(c.l); }
|
|
378
|
+
else op = 'is';
|
|
379
|
+
this.line(L, `T.expectValue(${this.x(s.e)}, ${cs(negate ? `not ${op}` : op)}, ${cs(op)}, ${want}, ${text});`);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
fieldGet(v, field) { return field.dynamic ? `T.getField(${v}, ${cs(field.key)})` : `T.getAttr(${v}, ${cs(field.py)})`; }
|
|
383
|
+
|
|
384
|
+
/** A value used as yes/no: checked when the program runs unless it is known to be yes/no. */
|
|
385
|
+
yn(e, ctx = {}) {
|
|
386
|
+
const c = this.x(e, ctx);
|
|
387
|
+
if (KNOWN_BOOL.has(e.k) || (e.k === 'bin' && (e.op === 'and' || e.op === 'or'))) return c;
|
|
388
|
+
if (e.type?.k === 'bool') return `T.B(${c})`;
|
|
389
|
+
return `T.yesNo(${c})`;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
x(e, ctx = {}) {
|
|
393
|
+
const sub = (k) => this.x(k, ctx);
|
|
394
|
+
switch (e.k) {
|
|
395
|
+
case 'num': return e.whole ? `T.I(${cs(String(BigInt(e.text.replace(/_/g, ''))))})` : `T.dec(${cs(e.text)})`;
|
|
396
|
+
case 'money': return `T.money(${cs(e.text)})`;
|
|
397
|
+
case 'percent': return `T.dec(${cs(percentDecimal(e.text))})`;
|
|
398
|
+
case 'bool': return e.v ? 'true' : 'false';
|
|
399
|
+
case 'nothing': case 'hole': return 'null';
|
|
400
|
+
case 'paren': return sub(e.e);
|
|
401
|
+
case 'today': return 'T.today()';
|
|
402
|
+
case 'date': return `T.date(${cs(e.text)})`;
|
|
403
|
+
case 'str': return this.str(e, ctx);
|
|
404
|
+
case 'list': return e.items ? `T.list(${e.items.map(sub).join(', ')})` : 'T.newMap()';
|
|
405
|
+
case 'map': return 'T.newMap()';
|
|
406
|
+
case 'name': {
|
|
407
|
+
const r = e.ref ?? {};
|
|
408
|
+
if (r.kind === 'subject') return ctx.subject ?? 'null';
|
|
409
|
+
if (r.kind === 'elemField') return `T.getAttr(${ctx.item}, ${cs(r.py)})`;
|
|
410
|
+
if (r.kind === 'elemKey') return `T.getField(${ctx.item}, ${cs(r.key)})`;
|
|
411
|
+
if (r.kind === 'elem') return ctx.item;
|
|
412
|
+
return id(r.py ?? e.name.replace(/ /g, '_'));
|
|
413
|
+
}
|
|
414
|
+
case 'call': return `${id(e.py)}(${e.args.map(sub).join(', ')})`;
|
|
415
|
+
case 'new': return `T.newRecord(${id(e.py)}${e.fields.map((f) => `, ${cs(f.py)}, ${sub(f.e)}`).join('')})`;
|
|
416
|
+
case 'field': return e.static ? `T.getAttr(${sub(e.obj)}, ${cs(e.py)})` : `T.getField(${sub(e.obj)}, ${cs(e.text)})`;
|
|
417
|
+
case 'at': {
|
|
418
|
+
const fn = e.objKind === 'map' ? 'T.mapGet' : e.objKind === 'list' ? 'T.itemAt' : 'T.entry';
|
|
419
|
+
return `${fn}(${sub(e.obj)}, ${sub(e.key)}${e.dflt ? `, ${sub(e.dflt)}` : ''})`;
|
|
420
|
+
}
|
|
421
|
+
case 'conv': {
|
|
422
|
+
const fn = { list: 'T.pyList', number: 'T.toNumber', whole: 'T.toWhole', money: 'T.toMoney', text: 'T.show', bool: 'T.toYesNo', date: 'T.toDate' }[e.to];
|
|
423
|
+
return fn ? `${fn}(${sub(e.e)})` : sub(e.e);
|
|
424
|
+
}
|
|
425
|
+
case 'where': {
|
|
426
|
+
const v = this.fresh('item');
|
|
427
|
+
return `T.filter(T.pyIter(${sub(e.list)}), ${v} => T.truthy(${this.x(e.cond, { ...ctx, item: v })}))`;
|
|
428
|
+
}
|
|
429
|
+
case 'from': {
|
|
430
|
+
const inner = e.list.k === 'where' ? e.list : null;
|
|
431
|
+
const v = this.fresh('item');
|
|
432
|
+
const list = `T.pyIter(${sub(inner ? inner.list : e.list)})`;
|
|
433
|
+
const filtered = inner ? `T.filter(${list}, ${v} => T.truthy(${this.x(inner.cond, { ...ctx, item: v })}))` : list;
|
|
434
|
+
const w = this.fresh('item');
|
|
435
|
+
return `T.map(${filtered}, ${w} => ${this.fieldGet(w, e.field)})`;
|
|
436
|
+
}
|
|
437
|
+
case 'sorted': {
|
|
438
|
+
const v = this.fresh(e.mapSort ? 'entry' : 'item');
|
|
439
|
+
if (e.mapSort) return `T.pySort(T.mapItems(${sub(e.list)}), T.keyAt(F, ${v} => T.tupleAt(${v}, ${e.by === 'key' ? 0 : 1})), ${e.desc ? 'true' : 'false'})`;
|
|
440
|
+
return `T.pySort(T.pyList(${sub(e.list)}), T.keyAt(F, ${v} => ${this.fieldGet(v, e.field)}), ${e.desc ? 'true' : 'false'})`;
|
|
441
|
+
}
|
|
442
|
+
case 'op': return `T.applyOp(${cs(e.op)}, ${sub(e.arg)})`;
|
|
443
|
+
case 'round': return `T.roundTo(${sub(e.e)}${e.places ? `, ${sub(e.places)}` : ''})`;
|
|
444
|
+
case 'readFile': return `T.readText(${sub(e.path)})`;
|
|
445
|
+
case 'neg': return `T.neg(${sub(e.e)})`;
|
|
446
|
+
case 'not': return `!${this.yn(e.e, ctx)}`;
|
|
447
|
+
case 'bin': {
|
|
448
|
+
if (e.op === 'and') return `(${this.yn(e.l, ctx)} && ${this.yn(e.r, ctx)})`;
|
|
449
|
+
if (e.op === 'or') return `(${this.yn(e.l, ctx)} || ${this.yn(e.r, ctx)})`;
|
|
450
|
+
const fn = { '+': 'T.add', '-': 'T.sub', '*': 'T.mul', '/': 'T.divide' }[e.op]
|
|
451
|
+
?? (e.op === 'mod' ? (e.l.type?.k === 'whole' && e.r.type?.k === 'whole' ? 'T.intMod' : 'T.modulo') : null);
|
|
452
|
+
return fn ? `${fn}(${sub(e.l)}, ${sub(e.r)})` : 'null';
|
|
453
|
+
}
|
|
454
|
+
case 'cmp': {
|
|
455
|
+
if (e.r.k === 'bool' && (e.op === '==' || e.op === '!=')) return `(${this.yn(e.l, ctx)} ${e.op === '==' ? '==' : '!='} ${sub(e.r)})`;
|
|
456
|
+
return `T.compare(${cs(e.op)}, ${sub(e.l)}, ${sub(e.r)})`;
|
|
457
|
+
}
|
|
458
|
+
case 'between': return `T.between(${sub(e.lo)}, ${sub(e.e)}, () => ${sub(e.hi)})`;
|
|
459
|
+
case 'contains': return `T.isIn(${sub(e.r)}, ${sub(e.l)})`;
|
|
460
|
+
case 'affix': return `T.affix(${sub(e.l)}, ${sub(e.r)}, ${e.end ? 'true' : 'false'})`;
|
|
461
|
+
case 'sortedValues': return `T.pySort(T.pyList(${sub(e.list)}), null, ${e.desc ? 'true' : 'false'})`;
|
|
462
|
+
case 'joined': return `T.joined(${sub(e.list)}, ${sub(e.sep)})`;
|
|
463
|
+
case 'splitAt': return `T.splitAt(${sub(e.text)}, ${sub(e.sep)})`;
|
|
464
|
+
case 'replacing': return `T.replacing(${sub(e.text)}, ${sub(e.from)}, ${sub(e.to)})`;
|
|
465
|
+
case 'adj': {
|
|
466
|
+
if (e.adj === 'empty') return `T.eq(T.pyLen(${sub(e.e)}), T.I("0"))`;
|
|
467
|
+
return `T.compare(${cs({ positive: '>', negative: '<', zero: '==' }[e.adj])}, ${sub(e.e)}, T.I("0"))`;
|
|
468
|
+
}
|
|
469
|
+
case 'isNothing': return `(${sub(e.e)} ${e.neg ? '!=' : '=='} null)`;
|
|
470
|
+
default: return `null /* not generated: ${e.k} */`;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/** Text with {holes}: text and whole numbers as they are, anything else shown the Thunder way. */
|
|
475
|
+
str(e, ctx) {
|
|
476
|
+
const parts = e.parts.filter((p) => p !== '').map((p) => {
|
|
477
|
+
if (typeof p === 'string') return cs(p);
|
|
478
|
+
const t = p.e.type?.k;
|
|
479
|
+
return t === 'text' || t === 'whole' ? `T.pyStr(${this.x(p.e, ctx)})` : `T.show(${this.x(p.e, ctx)})`;
|
|
480
|
+
});
|
|
481
|
+
if (!parts.length) return '""';
|
|
482
|
+
return parts.length === 1 ? parts[0] : `(${parts.join(' + ')})`;
|
|
483
|
+
}
|
|
484
|
+
}
|