@skillstech/thunderlang 1.0.4 → 1.2.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 +221 -0
- package/README.md +4 -1
- package/dist/core.cjs +2568 -462
- package/dist/index.cjs +7562 -5084
- package/dist/learn.cjs +16994 -8339
- package/intent-graph.schema.json +11 -0
- package/package.json +6 -2
- package/src/cli-home.mjs +242 -0
- package/src/cli.mjs +241 -89
- package/src/codegen.mjs +145 -55
- package/src/conformance.mjs +15 -6
- package/src/core/check.mjs +725 -0
- package/src/core/cli.mjs +184 -0
- package/src/core/diagnostics.mjs +82 -0
- package/src/core/engine-core.mjs +572 -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 +20 -0
- package/src/core/from-python.mjs +645 -0
- package/src/core/index.mjs +39 -0
- package/src/core/lexer.mjs +115 -0
- package/src/core/parser.mjs +742 -0
- package/src/core/project.mjs +177 -0
- package/src/core/round-trip.mjs +150 -0
- package/src/core/runtime-python.mjs +801 -0
- package/src/core/runtime-typescript.mjs +119 -0
- package/src/core/target-python.mjs +487 -0
- package/src/core/target-typescript.mjs +466 -0
- package/src/core/typescript-run.mjs +114 -0
- package/src/core.d.ts +5 -2
- package/src/core.mjs +5 -2
- package/src/data-schema.mjs +8 -1
- package/src/emit.mjs +1 -1
- package/src/evidence.mjs +1 -1
- package/src/expr.mjs +81 -4
- package/src/form-words.mjs +2 -0
- package/src/index.d.ts +8 -1
- package/src/index.mjs +5 -1
- package/src/intent-schema.mjs +1 -0
- package/src/learn-lessons.mjs +245 -1
- package/src/learn.d.mts +8 -0
- package/src/learn.mjs +40 -4
- package/src/mcp.mjs +23 -0
- package/src/meaning.mjs +45 -6
- package/src/model.d.mts +7 -3
- package/src/model.mjs +19 -6
- package/src/money.d.mts +18 -0
- package/src/money.mjs +124 -0
- package/src/plain-contract.mjs +18 -2
- package/src/read-level.mjs +146 -0
- package/src/read.d.mts +11 -0
- package/src/read.mjs +40 -6
- package/src/review-comment.d.mts +16 -0
- package/src/review-comment.mjs +145 -0
- package/src/say-less.mjs +1432 -0
- package/src/system.mjs +153 -44
- package/src/target-cs.mjs +39 -34
- package/src/target-elixir.mjs +40 -30
- package/src/target-go.mjs +41 -36
- package/src/target-java.mjs +34 -27
- package/src/target-kotlin.mjs +43 -35
- package/src/target-py.mjs +57 -55
- package/src/target-result.d.mts +48 -0
- package/src/target-result.mjs +112 -0
- package/src/target-rust.mjs +42 -38
- package/src/target-scala.mjs +45 -35
- package/src/target-ts.mjs +37 -45
- package/src/target-types.mjs +41 -5
- package/src/target-util.mjs +55 -2
- package/src/term.mjs +89 -0
- package/src/trace-cli.mjs +58 -0
- package/src/trace-fs.mjs +76 -0
- package/src/trace.mjs +261 -0
- package/src/translate.d.mts +3 -0
- package/src/translate.mjs +62 -32
- package/src/try-cli.mjs +225 -0
- package/src/why-cli.mjs +41 -0
- package/src/why.d.mts +38 -0
- package/src/why.mjs +613 -0
- package/src/write-steps.mjs +22 -5
|
@@ -0,0 +1,725 @@
|
|
|
1
|
+
// Thunder Core checker: resolves every name, works out the kind (type) of every value where it can be known,
|
|
2
|
+
// and reports what is wrong in plain words with a stable code. It annotates the syntax tree in place:
|
|
3
|
+
// expression.type = { k: 'whole' | 'number' | 'money' | 'text' | 'bool' | 'date' | 'nothing' | 'unknown' |
|
|
4
|
+
// 'list' (of) | 'map' (key, of) | 'record' (name) | 'optional' (of) | 'module' }
|
|
5
|
+
// name.ref = { kind: 'var' | 'const' | 'param' | 'module' | 'elemField' | 'elemKey', py, ... }
|
|
6
|
+
// Unknown types are allowed (values from Python modules, inputs without a declared kind); the runtime then
|
|
7
|
+
// checks them (for example, Money refuses to be added to a plain number).
|
|
8
|
+
|
|
9
|
+
import { diag, closest } from './diagnostics.mjs';
|
|
10
|
+
import { RESERVED, LIST_ACTIONS, STATEMENT_WORDS } from './parser.mjs';
|
|
11
|
+
|
|
12
|
+
const T = (k, extra = {}) => ({ k, ...extra });
|
|
13
|
+
const UNKNOWN = T('unknown');
|
|
14
|
+
const numeric = (t) => t.k === 'whole' || t.k === 'number';
|
|
15
|
+
const known = (t) => t && t.k !== 'unknown';
|
|
16
|
+
export const typeWords = (t) => {
|
|
17
|
+
if (!t) return 'a value';
|
|
18
|
+
switch (t.k) {
|
|
19
|
+
case 'whole': return 'a whole number';
|
|
20
|
+
case 'number': return 'a number';
|
|
21
|
+
case 'money': return 'money';
|
|
22
|
+
case 'text': return 'text';
|
|
23
|
+
case 'bool': return 'yes/no';
|
|
24
|
+
case 'date': return 'a date';
|
|
25
|
+
case 'nothing': return 'nothing';
|
|
26
|
+
case 'list': return known(t.of) ? `a list of ${typeWords(t.of).replace(/^an? /, '')}` : 'a list';
|
|
27
|
+
case 'map': return 'a map';
|
|
28
|
+
case 'record': return `a ${t.text ?? t.name}`;
|
|
29
|
+
case 'optional': return `${typeWords(t.of)} or nothing`;
|
|
30
|
+
case 'module': return 'a Python module';
|
|
31
|
+
default: return 'a value';
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// Python words a Thunder name must not become.
|
|
36
|
+
const PY_TAKEN = new Set(`False None True and as assert async await break class continue def del elif else except finally for from global if import in is lambda nonlocal not or pass raise return try while with yield match case
|
|
37
|
+
print len sorted range list dict str int float bool object super isinstance getattr setattr type id input open sum min max any all map filter iter next round abs format repr hash vars dir set tuple
|
|
38
|
+
Decimal date dataclass field main THUNDER_SOURCE THUNDER_TESTS failure
|
|
39
|
+
Money Stop RuleBroken ExpectationFailed ThunderError show divide modulo to_number to_whole to_money to_text to_yes_no to_date total_of average_of smallest_of largest_of first_of last_of item_at set_item entry get_field set_field add_to remove_from round_to read_file never expect_value expect_true expect_failure explain run run_tests pairs_of`.split(/\s+/));
|
|
40
|
+
|
|
41
|
+
export function pyName(key, style = 'snake') {
|
|
42
|
+
const words = key.split(' ').map((w) => w.replace(/[^\p{L}\p{N}_]/gu, '_'));
|
|
43
|
+
let n = style === 'upper' ? words.join('_').toUpperCase() : style === 'pascal' ? words.map((w) => w[0].toUpperCase() + w.slice(1)).join('') : words.join('_').toLowerCase();
|
|
44
|
+
if (/^[0-9]/.test(n)) n = `_${n}`;
|
|
45
|
+
while (PY_TAKEN.has(n)) n += '_';
|
|
46
|
+
return n;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
class Scope {
|
|
50
|
+
constructor(parent, kind) { this.parent = parent; this.kind = kind; this.vars = new Map(); }
|
|
51
|
+
get(key) { return this.vars.get(key) ?? this.parent?.get(key); }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function checkCore(program, { imported = [] } = {}) {
|
|
55
|
+
const c = new Checker(program);
|
|
56
|
+
c.imported = imported;
|
|
57
|
+
c.run();
|
|
58
|
+
return { program, diagnostics: [...program.diagnostics, ...c.d].sort((a, b) => a.line - b.line), info: c.info };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
class Checker {
|
|
62
|
+
constructor(program) {
|
|
63
|
+
this.p = program;
|
|
64
|
+
this.d = [];
|
|
65
|
+
this.global = new Scope(null, 'global');
|
|
66
|
+
this.records = new Map(); // key -> record stmt
|
|
67
|
+
this.funcs = new Map();
|
|
68
|
+
this.rules = []; // top-level and record Never rules
|
|
69
|
+
this.mainNames = new Map(); // key -> first line it is set in the main program
|
|
70
|
+
this.elem = []; // stack of { type } for where / from / sorted
|
|
71
|
+
this.info = { records: [], funcs: [], consts: [], modules: [], tests: [], rules: [], usesDecimal: false };
|
|
72
|
+
}
|
|
73
|
+
err(code, line, msg, hint) { this.d.push(diag(code, line, msg, hint)); }
|
|
74
|
+
|
|
75
|
+
run() {
|
|
76
|
+
const top = this.p.body;
|
|
77
|
+
// 0. Declarations from other files of the project (tests see everything in src/).
|
|
78
|
+
this.info.imported = new Set();
|
|
79
|
+
for (const inf of this.imported ?? []) {
|
|
80
|
+
for (const r of inf.records) { this.records.set(r.name, r); this.info.imported.add(r); }
|
|
81
|
+
for (const f of inf.funcs) { this.global.vars.set(f.name, { kind: 'func', line: f.line, name: f.name, text: f.text, py: f.py, stmt: f, ret: f.ret ?? UNKNOWN }); this.funcs.set(f.name, f); this.info.imported.add(f); }
|
|
82
|
+
for (const k of inf.consts) { this.global.vars.set(k.name, { kind: 'const', line: k.line, name: k.name, text: k.text, py: k.py, type: k.e.type ?? UNKNOWN }); this.info.imported.add(k); }
|
|
83
|
+
for (const m of inf.modules) { this.global.vars.set(m.name, { kind: 'module', line: m.line, name: m.name, text: m.text, py: m.py, type: T('module') }); this.info.imported.add(m); }
|
|
84
|
+
}
|
|
85
|
+
// 1. Global declarations, hoisted: records, functions, modules, kept values.
|
|
86
|
+
for (const s of top) {
|
|
87
|
+
if (s.k === 'record') this.declare(s, 'record', s.line, { py: pyName(s.name, 'pascal'), stmt: s });
|
|
88
|
+
else if (s.k === 'func') this.declare(s, 'func', s.line, { py: pyName(s.name), stmt: s, ret: UNKNOWN });
|
|
89
|
+
else if (s.k === 'use') this.declare(s, 'module', s.line, { py: pyName(s.name), module: s.module, type: T('module') });
|
|
90
|
+
}
|
|
91
|
+
for (const s of top) if (s.k === 'record') this.checkRecordShape(s);
|
|
92
|
+
collectMainNames(top, this.mainNames);
|
|
93
|
+
// 2. Kept values, in order (they may use each other and functions).
|
|
94
|
+
for (const s of top) {
|
|
95
|
+
if (s.k !== 'keep') continue;
|
|
96
|
+
const t = this.ex(s.e, this.global, { keep: true });
|
|
97
|
+
this.declare(s, 'const', s.line, { py: pyName(s.name, 'upper'), type: t });
|
|
98
|
+
this.info.consts.push(s);
|
|
99
|
+
}
|
|
100
|
+
// 3. Never rules written at the top level or inside records.
|
|
101
|
+
for (const s of top) if (s.k === 'never') this.addRule(s, null);
|
|
102
|
+
for (const r of this.records.values()) for (const rule of r.rules) this.addRule(rule, r);
|
|
103
|
+
// 4. Functions, then tests, then the main program.
|
|
104
|
+
for (const s of top) if (s.k === 'func') this.checkFunc(s);
|
|
105
|
+
for (const s of top) if (s.k === 'test') { s.py = `test_${pyName(s.title.toLowerCase().replace(/[^\p{L}\p{N}]+/gu, ' ').trim() || 'unnamed')}`.replace(/_+$/, ''); this.uniqueTestName(s); const sc = new Scope(this.global, 'test'); this.block(s.body, sc, { inTest: true }); this.info.tests.push(s); }
|
|
106
|
+
const main = new Scope(this.global, 'main');
|
|
107
|
+
this.mainScope = main;
|
|
108
|
+
const mainBody = top.filter((s) => !['record', 'func', 'use', 'keep', 'never', 'test'].includes(s.k));
|
|
109
|
+
this.block(mainBody, main, {});
|
|
110
|
+
this.p.main = mainBody;
|
|
111
|
+
this.info.records = [...this.records.values()];
|
|
112
|
+
this.info.funcs = [...this.funcs.values()];
|
|
113
|
+
this.info.modules = top.filter((s) => s.k === 'use');
|
|
114
|
+
this.info.rules = this.rules;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
uniqueTestName(s) {
|
|
118
|
+
this.testNames ??= new Set();
|
|
119
|
+
let n = s.py, k = 2;
|
|
120
|
+
while (this.testNames.has(n)) n = `${s.py}_${k++}`;
|
|
121
|
+
s.py = n; this.testNames.add(n);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
declare(s, kind, line, extra) {
|
|
125
|
+
const prev = kind === 'record' ? this.records.get(s.name) : this.global.vars.get(s.name);
|
|
126
|
+
if (prev) { this.err('TC203', line, `"${s.text}" is already defined on line ${prev.line}.`, 'Give one of them a different name.'); return; }
|
|
127
|
+
const entry = { kind, line, name: s.name, text: s.text, ...extra };
|
|
128
|
+
if (kind === 'record') { this.records.set(s.name, s); s.py = extra.py; return; }
|
|
129
|
+
this.global.vars.set(s.name, entry);
|
|
130
|
+
if (kind === 'func') this.funcs.set(s.name, s);
|
|
131
|
+
s.py = extra.py;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
checkRecordShape(r) {
|
|
135
|
+
const seen = new Set();
|
|
136
|
+
r.fieldMap = new Map();
|
|
137
|
+
for (const f of r.fields) {
|
|
138
|
+
if (seen.has(f.name)) this.err('TC203', f.line, `${r.text} already has a field "${f.text}".`);
|
|
139
|
+
seen.add(f.name);
|
|
140
|
+
f.py = pyName(f.name);
|
|
141
|
+
this.resolveType(f.type, f.line);
|
|
142
|
+
r.fieldMap.set(f.name, f);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
resolveType(t, line) {
|
|
146
|
+
if (!t) return;
|
|
147
|
+
if (t.k === 'record') {
|
|
148
|
+
const r = this.records.get(t.name);
|
|
149
|
+
if (!r) this.err('TC209', line, `I don't know a record called "${t.name}".`); else { t.text = r.text; t.py = r.py; }
|
|
150
|
+
}
|
|
151
|
+
if (t.of) this.resolveType(t.of, line);
|
|
152
|
+
if (t.key) this.resolveType(t.key, line);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
addRule(rule, record) {
|
|
156
|
+
// A rule is about exactly one value that changes (a name or a field); kept values may also appear.
|
|
157
|
+
const names = [];
|
|
158
|
+
walkExpr(rule.cond, (e) => { if (e.k === 'name') names.push(e); });
|
|
159
|
+
const changing = [...new Set(names.filter((n) => this.global.vars.get(n.name)?.kind !== 'const').map((n) => n.name))];
|
|
160
|
+
if (changing.length !== 1) {
|
|
161
|
+
this.err('TC403', rule.line, changing.length ? `"${rule.text}" names ${changing.length} changing values (${changing.join(', ')}). In Thunder Core a Never rule is about one value.` : `"${rule.text}" does not name a value to watch.`,
|
|
162
|
+
'Split it into one rule per value, or check the combination with "When … : Stop with …".');
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const subject = changing[0];
|
|
166
|
+
const fieldOf = record ? (record.fieldMap.has(subject) ? [record] : []) : [...this.records.values()].filter((r) => r.fieldMap.has(subject));
|
|
167
|
+
if (record && !fieldOf.length) { this.err('TC206', rule.line, `${record.text} has no field "${subject}" for this rule to watch.`); return; }
|
|
168
|
+
const isVar = !record && (this.mainNames.has(subject) || this.anyParamOrLocal(subject));
|
|
169
|
+
if (!fieldOf.length && !isVar) { this.err('TC201', rule.line, `"${rule.text}" watches "${subject}", but nothing in this program is called that.`); return; }
|
|
170
|
+
// Type the condition with the subject as an unknown value (it is checked against each place it is set).
|
|
171
|
+
const sc = new Scope(this.global, 'rule');
|
|
172
|
+
const fieldType = fieldOf[0]?.fieldMap.get(subject).type ?? UNKNOWN;
|
|
173
|
+
sc.vars.set(subject, { kind: 'subject', py: 'value', type: fieldType });
|
|
174
|
+
this.ex(rule.cond, sc, {});
|
|
175
|
+
const entry = { rule, subject, records: fieldOf, watchesVar: isVar, line: rule.line, text: rule.text };
|
|
176
|
+
this.rules.push(entry);
|
|
177
|
+
for (const r of fieldOf) (r.rulesToCheck ??= []).push(entry);
|
|
178
|
+
}
|
|
179
|
+
anyParamOrLocal(key) {
|
|
180
|
+
let hit = false;
|
|
181
|
+
const visit = (body) => { for (const s of body) { if (s.k === 'set' && s.target.k === 'name' && s.target.name === key) hit = true; for (const b of childBlocks(s)) visit(b); } };
|
|
182
|
+
for (const f of this.funcs.values()) { if (f.params.some((p) => p.name === key)) hit = true; visit(f.body); }
|
|
183
|
+
for (const s of this.p.body) if (s.k === 'test') visit(s.body);
|
|
184
|
+
return hit;
|
|
185
|
+
}
|
|
186
|
+
rulesFor(key) { return this.rules.filter((r) => r.watchesVar && r.subject === key); }
|
|
187
|
+
|
|
188
|
+
checkFunc(f) {
|
|
189
|
+
const sc = new Scope(this.global, 'func');
|
|
190
|
+
sc.func = f;
|
|
191
|
+
f.gives = [];
|
|
192
|
+
let sawDefault = false;
|
|
193
|
+
for (const p of f.params) {
|
|
194
|
+
if (p.dflt) {
|
|
195
|
+
sawDefault = true;
|
|
196
|
+
// A default is a fixed value (Python would create a list or map default once and share it between calls).
|
|
197
|
+
const fixed = (e) => ['num', 'money', 'percent', 'bool', 'nothing'].includes(e.k) || (e.k === 'str' && e.parts.every((x) => typeof x === 'string'))
|
|
198
|
+
|| (e.k === 'neg' && fixed(e.e)) || (e.k === 'name' && this.global.vars.get(e.key ?? e.name)?.kind === 'const');
|
|
199
|
+
const dt = this.ex(p.dflt, this.global, {});
|
|
200
|
+
if (!fixed(p.dflt)) this.err('TC404', p.line, `The default for "${p.text}" must be a fixed value: a number, money, a percent, text, yes/no, nothing, or a kept value.`, p.dflt.k === 'list' || p.dflt.k === 'map' ? 'A list or a map as a default would be shared between calls. Give nothing, and Set it inside the function.' : null);
|
|
201
|
+
else if (p.type && known(p.type) && known(dt) && !compatible(p.type, dt) && dt.k !== 'nothing') this.err('TC303', p.line, `"${p.text}" is ${typeWords(p.type)}, but its default is ${typeWords(dt)}.`);
|
|
202
|
+
} else if (sawDefault) this.err('TC404', p.line, `"${p.text}" has no default, so it must come before the inputs that have one.`);
|
|
203
|
+
if (sc.vars.has(p.name)) this.err('TC203', p.line, `"${p.text}" is received twice.`);
|
|
204
|
+
if (this.global.vars.has(p.name)) this.err('TC203', p.line, `"${p.text}" is already the name of ${this.global.vars.get(p.name).kind === 'func' ? 'a function' : 'something else'} (line ${this.global.vars.get(p.name).line}).`, 'Receive it under a different name.');
|
|
205
|
+
this.resolveType(p.type, p.line);
|
|
206
|
+
p.py = pyName(p.name);
|
|
207
|
+
p.rules = this.rulesFor(p.name);
|
|
208
|
+
sc.vars.set(p.name, { kind: 'param', py: p.py, type: p.type ?? UNKNOWN, line: p.line });
|
|
209
|
+
}
|
|
210
|
+
this.block(f.body, sc, { inFunc: true });
|
|
211
|
+
// The result's kind is known only when every Give gives the same known kind.
|
|
212
|
+
const ts = f.gives;
|
|
213
|
+
const entry = this.global.vars.get(f.name);
|
|
214
|
+
if (entry) entry.ret = ts.length && ts.every(known) && ts.every((t) => sameKind(t, ts[0])) ? (ts.some((t) => t.k === 'number') ? T('number') : ts[0]) : UNKNOWN;
|
|
215
|
+
f.ret = entry?.ret;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
block(body, sc, ctx) { for (const s of body) this.stmt(s, sc, ctx); }
|
|
219
|
+
|
|
220
|
+
stmt(s, sc, ctx) {
|
|
221
|
+
const line = s.line;
|
|
222
|
+
switch (s.k) {
|
|
223
|
+
case 'say': this.ex(s.e, sc); return;
|
|
224
|
+
case 'set': return this.checkSet(s, sc, ctx);
|
|
225
|
+
case 'keep': this.err('TC402', line, '"Keep" is written at the left edge of the file, not inside a block.', 'Keep values are fixed for the whole program.'); return;
|
|
226
|
+
case 'give': {
|
|
227
|
+
if (!ctx.inFunc || ctx.inTest) { this.err('TC208', line, '"Give" can only be used inside a "To" block, to give back a result.'); return; }
|
|
228
|
+
const t = this.ex(s.e, sc);
|
|
229
|
+
sc.func ? sc.func.gives.push(t) : (findFunc(sc)?.gives.push(t));
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
case 'stop': this.ex(s.e, sc); return;
|
|
233
|
+
case 'if':
|
|
234
|
+
for (const b of s.branches) { this.cond(b.cond, sc); this.block(b.body, sc, ctx); }
|
|
235
|
+
if (s.otherwise) this.block(s.otherwise, sc, ctx);
|
|
236
|
+
return;
|
|
237
|
+
case 'each': {
|
|
238
|
+
const it = this.ex(s.iter, sc);
|
|
239
|
+
const inner = new Scope(sc, 'loop');
|
|
240
|
+
const [a, b] = s.vars;
|
|
241
|
+
let ta = UNKNOWN, tb = UNKNOWN;
|
|
242
|
+
if (it.k === 'list') { ta = it.of ?? UNKNOWN; if (b && it.of?.k === 'pair') { ta = it.of.a; tb = it.of.b; } }
|
|
243
|
+
else if (it.k === 'map') { ta = it.key ?? UNKNOWN; tb = it.of ?? UNKNOWN; }
|
|
244
|
+
else if (it.k === 'text') ta = T('text');
|
|
245
|
+
else if (known(it) && it.k !== 'optional') this.err('TC305', line, `"For each" goes through a list, a map, or text, but this is ${typeWords(it)}.`);
|
|
246
|
+
if (b && it.k === 'list' && it.of?.k !== 'pair' && known(it.of)) this.err('TC305', line, `"For each ${a.text} and ${b.text}" needs a map (or a sorted map), but this is ${typeWords(it)}.`);
|
|
247
|
+
s.iterKind = it.k === 'map' ? 'map' : it.k === 'list' && it.of?.k === 'pair' ? 'pairs' : it.k === 'list' || it.k === 'text' ? 'list' : 'unknown';
|
|
248
|
+
for (const [v, t] of [[a, ta], [b, tb]]) if (v) { v.py = this.localPy(v.name, sc); inner.vars.set(v.name, { kind: 'var', py: v.py, type: t, line }); }
|
|
249
|
+
this.block(s.body, inner, ctx);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
case 'range': {
|
|
253
|
+
for (const e of [s.from, s.to]) { const t = this.ex(e, sc); if (known(t) && t.k !== 'whole') this.err('TC305', line, `"For each … from … to" counts in whole numbers, but this is ${typeWords(t)}.`); }
|
|
254
|
+
const inner = new Scope(sc, 'loop');
|
|
255
|
+
s.v.py = this.localPy(s.v.name, sc);
|
|
256
|
+
inner.vars.set(s.v.name, { kind: 'var', py: s.v.py, type: T('whole'), line });
|
|
257
|
+
this.block(s.body, inner, ctx);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
case 'while': this.cond(s.cond, sc); this.block(s.body, sc, ctx); return;
|
|
261
|
+
case 'times': { const t = this.ex(s.count, sc); if (known(t) && t.k !== 'whole') this.err('TC305', line, `"Repeat … times" needs a whole number, but this is ${typeWords(t)}.`); this.block(s.body, sc, ctx); return; }
|
|
262
|
+
case 'func': case 'record': case 'use': case 'test': this.err('TC402', line, 'This must be written at the left edge of the file.'); return;
|
|
263
|
+
case 'never': this.err('TC402', line, 'In Thunder Core, "Never allow" is written at the left edge of the file or inside a record ("A … has:").', 'It then watches the value everywhere it is set.'); return;
|
|
264
|
+
case 'expect': return this.checkExpect(s, sc);
|
|
265
|
+
case 'add': case 'remove': {
|
|
266
|
+
const tt = this.ex(s.target, sc);
|
|
267
|
+
const te = this.ex(s.e, sc);
|
|
268
|
+
this.lvalue(s.target, sc, line);
|
|
269
|
+
s.targetKind = tt.k === 'list' ? 'list' : (numeric(tt) || tt.k === 'money') ? 'number' : tt.k === 'map' && s.k === 'remove' ? 'map' : 'unknown';
|
|
270
|
+
if (known(tt) && s.targetKind === 'unknown') this.err('TC305', line, `I can only ${s.k} ${s.k === 'add' ? 'to' : 'from'} a list or a number, but this is ${typeWords(tt)}.`);
|
|
271
|
+
if (s.targetKind === 'number') this.arith(s.k === 'add' ? '+' : '-', tt, te, line);
|
|
272
|
+
if (s.targetKind === 'list' && known(tt.of) && known(te) && !compatible(tt.of, te)) this.err('TC303', line, `This list holds ${typeWords(tt.of)}, so ${typeWords(te)} can't be added to it.`);
|
|
273
|
+
// An empty list learns what it holds from the first thing added to it.
|
|
274
|
+
if (s.k === 'add' && s.targetKind === 'list' && !known(tt.of) && known(te) && s.target.k === 'name' && s.target.ref?.type?.k === 'list') s.target.ref.type = T('list', { of: te });
|
|
275
|
+
this.afterAssign(s, s.target, sc);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
case 'increase': case 'decrease': {
|
|
279
|
+
const tt = this.ex(s.target, sc);
|
|
280
|
+
const te = this.ex(s.e, sc);
|
|
281
|
+
this.lvalue(s.target, sc, line);
|
|
282
|
+
if (known(tt) && !numeric(tt) && tt.k !== 'money') this.err('TC305', line, `Only a number or money can be ${s.k}d, but this is ${typeWords(tt)}.`);
|
|
283
|
+
this.arith(s.k === 'increase' ? '+' : '-', tt, te, line);
|
|
284
|
+
this.afterAssign(s, s.target, sc);
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
case 'sort': case 'reverse': {
|
|
288
|
+
const tt = this.ex(s.target, sc);
|
|
289
|
+
if (known(tt) && tt.k !== 'list') this.err('TC305', line, `Only a list can be ${s.k === 'sort' ? 'sorted' : 'reversed'}, but this is ${typeWords(tt)}.`);
|
|
290
|
+
if (s.k === 'sort' && s.by) s.field = this.fieldOfElem(tt.of ?? UNKNOWN, s.by, s.text, line);
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
case 'unknownVerb': return this.unknownVerb(s, sc);
|
|
294
|
+
case 'do': this.ex(s.e, sc); return;
|
|
295
|
+
case 'tryFail': {
|
|
296
|
+
this.stmt(s.stmt, sc, ctx);
|
|
297
|
+
const inner = new Scope(sc, 'handler');
|
|
298
|
+
s.errPy = this.localPy('error', sc);
|
|
299
|
+
inner.vars.set('error', { kind: 'var', py: s.errPy, type: T('text'), line });
|
|
300
|
+
this.block(s.handler, inner, ctx);
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
default: this.err('TC402', line, `Thunder Core does not support "${s.k}" here yet.`);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
localPy(key, sc) { return pyName(key); }
|
|
308
|
+
|
|
309
|
+
checkSet(s, sc, ctx) {
|
|
310
|
+
const line = s.line;
|
|
311
|
+
const tv = this.ex(s.e, sc);
|
|
312
|
+
const tgt = s.target;
|
|
313
|
+
if (tgt.k === 'name') {
|
|
314
|
+
const g = this.global.vars.get(tgt.name);
|
|
315
|
+
if (g && g.kind === 'const') { this.err('TC205', line, `"${tgt.text}" is kept at a fixed value (line ${g.line}), so it can't be set.`, 'Use a different name for a value that changes, or change the Keep line.'); return; }
|
|
316
|
+
if (g && g.kind !== 'const') { this.err('TC203', line, `"${tgt.text}" is already the name of ${g.kind === 'func' ? 'a function' : g.kind === 'record' ? 'a record' : 'a module'} (line ${g.line}).`, 'Give this value a different name.'); return; }
|
|
317
|
+
const cur = sc.get(tgt.name);
|
|
318
|
+
if (cur && cur.kind !== 'subject') {
|
|
319
|
+
if (cur.kind === 'param' && cur.type && known(cur.type) && known(tv) && !compatible(cur.type, tv)) this.err('TC303', line, `"${tgt.text}" is ${typeWords(cur.type)}, so it can't be set to ${typeWords(tv)}.`);
|
|
320
|
+
else if (known(cur.type) && known(tv) && !compatible(cur.type, tv)) this.err('TC303', line, `"${tgt.text}" was ${typeWords(cur.type)} (line ${cur.line}); now it is set to ${typeWords(tv)}. A name keeps one kind of value.`, 'Use a new name for the new kind of value.');
|
|
321
|
+
else if (!known(cur.type) && known(tv)) cur.type = tv;
|
|
322
|
+
else if (cur.type?.k === 'whole' && tv.k === 'number') cur.type = tv;
|
|
323
|
+
tgt.ref = cur; tgt.type = cur.type;
|
|
324
|
+
} else {
|
|
325
|
+
// A new name lives in the nearest function, test, or main program (not only in a loop or a branch).
|
|
326
|
+
let home = sc;
|
|
327
|
+
while (home.kind === 'loop' || home.kind === 'handler') home = home.parent;
|
|
328
|
+
const entry = { kind: 'var', py: pyName(tgt.name), type: tv, line };
|
|
329
|
+
home.vars.set(tgt.name, entry);
|
|
330
|
+
tgt.ref = entry; tgt.type = tv;
|
|
331
|
+
}
|
|
332
|
+
} else {
|
|
333
|
+
const tt = this.ex(tgt, sc);
|
|
334
|
+
this.lvalue(tgt, sc, line);
|
|
335
|
+
if (known(tt) && known(tv) && !compatible(tt, tv)) this.err('TC303', line, `${tgt.k === 'field' ? `The field "${tgt.text}"` : 'This entry'} holds ${typeWords(tt)}, so it can't be set to ${typeWords(tv)}.`);
|
|
336
|
+
}
|
|
337
|
+
this.afterAssign(s, tgt, sc);
|
|
338
|
+
}
|
|
339
|
+
lvalue(e, sc, line) {
|
|
340
|
+
if (e.k === 'name') {
|
|
341
|
+
if (e.ref?.kind === 'const') this.err('TC205', line, `"${e.text}" is kept at a fixed value, so it can't be changed.`);
|
|
342
|
+
} else if (e.k === 'field' || e.k === 'at') {
|
|
343
|
+
if (e.obj.k === 'name' && e.obj.ref?.kind === 'const') this.err('TC205', line, `"${e.obj.text}" is kept at a fixed value, so it can't be changed.`);
|
|
344
|
+
} else this.err('TC305', line, 'Only a name, a field, or an entry can be changed here.');
|
|
345
|
+
}
|
|
346
|
+
// Never rules on a name are checked right after it is set (the check names the line where it was set).
|
|
347
|
+
afterAssign(s, tgt, sc) {
|
|
348
|
+
if (tgt.k === 'name') s.rules = this.rulesFor(tgt.name);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
checkExpect(s, sc) {
|
|
352
|
+
const line = s.line;
|
|
353
|
+
if (s.mode === 'true') { this.cond(s.e, sc); return; }
|
|
354
|
+
if (s.mode === 'fail') { this.ex(s.e, sc); if (s.msg) this.ex(s.msg, sc); return; }
|
|
355
|
+
const ta = this.ex(s.e, sc);
|
|
356
|
+
if (s.mode === 'cmp') { this.ex(s.want, sc); return; }
|
|
357
|
+
const c = s.check;
|
|
358
|
+
const hole = (e) => { if (e && e.k === 'hole') e.type = ta; };
|
|
359
|
+
hole(c.l); hole(c.e);
|
|
360
|
+
const inner = c.k === 'not' ? c.e : c;
|
|
361
|
+
hole(inner.l); hole(inner.e);
|
|
362
|
+
walkExpr(c, (e) => { if (e.k === 'hole') e.type = ta; });
|
|
363
|
+
this.ex(c, sc);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
unknownVerb(s, sc) {
|
|
367
|
+
const verb = s.verb.toLowerCase();
|
|
368
|
+
const t = s.object ? this.exQuiet(s.object, sc) : UNKNOWN;
|
|
369
|
+
const funcs = [...this.funcs.values()].map((f) => f.name);
|
|
370
|
+
const guess = closest(verb, [...funcs, ...STATEMENT_WORDS]);
|
|
371
|
+
if (t.k === 'list') this.err('TC401', s.line, `I don't know how to '${verb}' a list yet. Supported: ${LIST_ACTIONS.join(', ')}.`, guess ? `Did you mean "${guess}"?` : 'You can also write a "To" block that does it.');
|
|
372
|
+
else this.err('TC401', s.line, `I don't know how to '${verb}' yet.`, guess ? `Did you mean "${guess}"?` : `A line starts with one of: ${STATEMENT_WORDS.join(', ')}, or the name of a "To" block.`);
|
|
373
|
+
}
|
|
374
|
+
exQuiet(e, sc) { const n = this.d.length; try { return this.ex(e, sc); } finally { this.d.length = n; } }
|
|
375
|
+
|
|
376
|
+
cond(e, sc) {
|
|
377
|
+
const t = this.ex(e, sc);
|
|
378
|
+
if (known(t) && t.k !== 'bool') this.err('TC304', e.line, `A condition must be yes or no, but this is ${typeWords(t)}.`, t.k === 'list' || t.k === 'text' ? 'Did you mean "is empty" or "is not empty"?' : 'Compare it with something, for example: is at least 10.');
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// ------------------------------------------------------------ expressions
|
|
382
|
+
ex(e, sc, o = {}) { const t = this.ex1(e, sc, o) ?? UNKNOWN; e.type = t; return t; }
|
|
383
|
+
|
|
384
|
+
ex1(e, sc, o) {
|
|
385
|
+
const line = e.line;
|
|
386
|
+
switch (e.k) {
|
|
387
|
+
case 'num': if (!e.whole) this.info.usesDecimal = true; return T(e.whole ? 'whole' : 'number');
|
|
388
|
+
case 'money': return T('money');
|
|
389
|
+
case 'percent': this.info.usesDecimal = true; return T('number');
|
|
390
|
+
case 'str': for (const p of e.parts) if (typeof p !== 'string') this.ex(p.e, sc, o); return T('text');
|
|
391
|
+
case 'bool': return T('bool');
|
|
392
|
+
case 'nothing': return T('nothing');
|
|
393
|
+
case 'hole': return e.type ?? UNKNOWN;
|
|
394
|
+
case 'today': case 'date': return T('date');
|
|
395
|
+
case 'paren': return this.ex(e.e, sc, o);
|
|
396
|
+
case 'list': {
|
|
397
|
+
if (!e.items) return T('map', { key: UNKNOWN, of: UNKNOWN });
|
|
398
|
+
const ts = e.items.map((x) => this.ex(x, sc, o));
|
|
399
|
+
if (e.of) { this.resolveType(e.of, line); return T('list', { of: e.of }); }
|
|
400
|
+
const kt = ts.find(known);
|
|
401
|
+
if (kt && ts.some((x) => known(x) && !compatible(x, kt))) this.err('TC303', line, 'All items in a list should be the same kind of value.', `This list mixes ${[...new Set(ts.filter(known).map(typeWords))].join(' and ')}.`);
|
|
402
|
+
return T('list', { of: kt ?? UNKNOWN });
|
|
403
|
+
}
|
|
404
|
+
case 'map': return T('map', { key: UNKNOWN, of: UNKNOWN });
|
|
405
|
+
case 'name': return this.name(e, sc, o);
|
|
406
|
+
case 'call': return this.call(e, sc, o);
|
|
407
|
+
case 'new': return this.newRecord(e, sc, o);
|
|
408
|
+
case 'field': {
|
|
409
|
+
const ot = this.ex(e.obj, sc, o);
|
|
410
|
+
const r = ot.k === 'record' ? this.records.get(ot.name) : null;
|
|
411
|
+
if (r) {
|
|
412
|
+
const f = r.fieldMap.get(e.name);
|
|
413
|
+
if (!f) { const g = closest(e.name, [...r.fieldMap.keys()]); this.err('TC206', line, `A ${r.text} has no field "${e.text}".`, g ? `Did you mean "${g}"? Fields: ${[...r.fieldMap.keys()].join(', ')}.` : `Fields: ${[...r.fieldMap.keys()].join(', ')}.`); return UNKNOWN; }
|
|
414
|
+
e.py = f.py; e.static = true;
|
|
415
|
+
return f.type;
|
|
416
|
+
}
|
|
417
|
+
if (known(ot) && ot.k !== 'map' && ot.k !== 'optional') { this.err('TC305', line, `"'s ${e.text}" reads a field of a record, but this is ${typeWords(ot)}.`); return UNKNOWN; }
|
|
418
|
+
return UNKNOWN;
|
|
419
|
+
}
|
|
420
|
+
case 'at': {
|
|
421
|
+
const ot = this.ex(e.obj, sc, o);
|
|
422
|
+
const kt = this.ex(e.key, sc, o);
|
|
423
|
+
if (e.dflt) this.ex(e.dflt, sc, o);
|
|
424
|
+
e.objKind = ot.k === 'map' ? 'map' : ot.k === 'list' ? 'list' : 'unknown';
|
|
425
|
+
if (ot.k === 'list') { if (known(kt) && kt.k !== 'whole') this.err('TC305', line, `A position in a list is a whole number (1 is the first item), but this is ${typeWords(kt)}.`); return ot.of ?? UNKNOWN; }
|
|
426
|
+
if (ot.k === 'map') return ot.of ?? UNKNOWN;
|
|
427
|
+
if (known(ot)) this.err('TC305', line, `"at" looks up an entry in a map or a position in a list, but this is ${typeWords(ot)}.`);
|
|
428
|
+
return UNKNOWN;
|
|
429
|
+
}
|
|
430
|
+
case 'conv': {
|
|
431
|
+
const from = this.ex(e.e, sc, o);
|
|
432
|
+
this.resolveType(e.type, line);
|
|
433
|
+
const to = e.type;
|
|
434
|
+
if (!['number', 'whole', 'money', 'text', 'bool', 'date', 'list'].includes(to.k)) { this.err('TC402', line, `Converting to ${typeWords(to)} is not supported yet. Supported: number, whole number, money, text, yes/no, date, list.`); return UNKNOWN; }
|
|
435
|
+
if (to.k === 'list') { e.to = 'list'; return T('list', { of: to.of ?? UNKNOWN }); }
|
|
436
|
+
if (to.k === 'number' || to.k === 'money') this.info.usesDecimal = true;
|
|
437
|
+
e.to = to.k; e.from = from;
|
|
438
|
+
return T(to.k);
|
|
439
|
+
}
|
|
440
|
+
case 'where': {
|
|
441
|
+
const lt = this.ex(e.list, sc, o);
|
|
442
|
+
if (known(lt) && lt.k !== 'list') this.err('TC305', line, `"where" picks items from a list, but this is ${typeWords(lt)}.`);
|
|
443
|
+
this.elem.push({ type: lt.of ?? UNKNOWN });
|
|
444
|
+
this.cond(e.cond, sc);
|
|
445
|
+
this.elem.pop();
|
|
446
|
+
return lt.k === 'list' ? lt : T('list', { of: UNKNOWN });
|
|
447
|
+
}
|
|
448
|
+
case 'from': {
|
|
449
|
+
const lt = this.ex(e.list, sc, o);
|
|
450
|
+
if (known(lt) && lt.k !== 'list') { this.err('TC305', line, `"${e.text} from …" takes a field from each item of a list, but this is ${typeWords(lt)}.`); return UNKNOWN; }
|
|
451
|
+
const f = this.fieldOfElem(lt.of ?? UNKNOWN, e.name, e.text, line, true);
|
|
452
|
+
e.field = f;
|
|
453
|
+
return T('list', { of: f?.type ?? UNKNOWN });
|
|
454
|
+
}
|
|
455
|
+
case 'sorted': {
|
|
456
|
+
const lt = this.ex(e.list, sc, o);
|
|
457
|
+
if (lt.k === 'map') {
|
|
458
|
+
if (e.by !== 'key' && e.by !== 'value') { this.err('TC305', line, 'A map is sorted by "key" or by "value".'); return UNKNOWN; }
|
|
459
|
+
e.mapSort = true;
|
|
460
|
+
return T('list', { of: T('pair', { a: lt.key ?? UNKNOWN, b: lt.of ?? UNKNOWN }) });
|
|
461
|
+
}
|
|
462
|
+
if (known(lt) && lt.k !== 'list') { this.err('TC305', line, `Only a list or a map can be sorted, but this is ${typeWords(lt)}.`); return UNKNOWN; }
|
|
463
|
+
e.field = this.fieldOfElem(lt.of ?? UNKNOWN, e.by, e.text, line, true);
|
|
464
|
+
return lt.k === 'list' ? lt : T('list', { of: UNKNOWN });
|
|
465
|
+
}
|
|
466
|
+
case 'dot': {
|
|
467
|
+
const ot = this.ex(e.obj, sc, o);
|
|
468
|
+
if (ot.k !== 'module') this.err('TC402', line, `"${e.attr}" with a dot reaches into a Python module brought in with "Use". ${e.obj.k === 'name' ? `"${e.obj.text}"` : 'This'} is not one.`, 'Write "Use <module> as <name>" at the top of the file first.');
|
|
469
|
+
for (const a of e.args ?? []) this.ex(a, sc, o);
|
|
470
|
+
return UNKNOWN;
|
|
471
|
+
}
|
|
472
|
+
case 'op': return this.op(e, sc, o);
|
|
473
|
+
case 'sortedValues': {
|
|
474
|
+
const lt = this.ex(e.list, sc, o);
|
|
475
|
+
if (known(lt) && lt.k !== 'list') this.err('TC305', line, `Only a list can be sorted this way, but this is ${typeWords(lt)}.`, lt.k === 'map' ? 'A map is sorted by "key" or by "value".' : null);
|
|
476
|
+
return lt.k === 'list' ? lt : T('list', { of: UNKNOWN });
|
|
477
|
+
}
|
|
478
|
+
case 'joined': {
|
|
479
|
+
const lt = this.ex(e.list, sc, o), st = this.ex(e.sep, sc, o);
|
|
480
|
+
if (known(lt) && lt.k !== 'list') this.err('TC305', line, `"joined with" joins the items of a list, but this is ${typeWords(lt)}.`);
|
|
481
|
+
if (lt.k === 'list' && known(lt.of) && lt.of.k !== 'text') this.err('TC305', line, `"joined with" joins text, but this list holds ${typeWords(lt.of)}.`, 'Turn each item into text first: (item as text).');
|
|
482
|
+
if (known(st) && st.k !== 'text') this.err('TC305', line, 'What goes between the items is text, for example: joined with ", ".');
|
|
483
|
+
return T('text');
|
|
484
|
+
}
|
|
485
|
+
case 'splitAt': {
|
|
486
|
+
const tt = this.ex(e.text, sc, o), st = this.ex(e.sep, sc, o);
|
|
487
|
+
for (const [x, what] of [[tt, 'what is split'], [st, 'what it is split at']]) if (known(x) && x.k !== 'text') this.err('TC305', line, `"split at" works on text: ${what} is ${typeWords(x)}.`);
|
|
488
|
+
return T('list', { of: T('text') });
|
|
489
|
+
}
|
|
490
|
+
case 'replacing': {
|
|
491
|
+
for (const [x, what] of [[e.text, 'the text'], [e.from, 'what is replaced'], [e.to, 'what replaces it']]) { const xt = this.ex(x, sc, o); if (known(xt) && xt.k !== 'text') this.err('TC305', line, `"replacing … with" works on text: ${what} is ${typeWords(xt)}.`); }
|
|
492
|
+
return T('text');
|
|
493
|
+
}
|
|
494
|
+
case 'affix': {
|
|
495
|
+
for (const x of [e.l, e.r]) { const xt = this.ex(x, sc, o); if (known(xt) && xt.k !== 'text') this.err('TC305', line, `"${e.end ? 'ends' : 'starts'} with" compares text, but this is ${typeWords(xt)}.`); }
|
|
496
|
+
return T('bool');
|
|
497
|
+
}
|
|
498
|
+
case 'round': {
|
|
499
|
+
const t = this.ex(e.e, sc, o);
|
|
500
|
+
if (e.places) { const pt = this.ex(e.places, sc, o); if (known(pt) && pt.k !== 'whole') this.err('TC305', line, 'The number of places to round to is a whole number.'); }
|
|
501
|
+
if (known(t) && !numeric(t) && t.k !== 'money') this.err('TC305', line, `Only a number or money can be rounded, but this is ${typeWords(t)}.`);
|
|
502
|
+
return t.k === 'money' ? t : !e.places && numeric(t) ? T('whole') : known(t) ? T('number') : UNKNOWN;
|
|
503
|
+
}
|
|
504
|
+
case 'readFile': { const t = this.ex(e.path, sc, o); if (known(t) && t.k !== 'text') this.err('TC305', line, 'A file is named with text, for example: read file "orders.csv".'); return T('text'); }
|
|
505
|
+
case 'neg': { const t = this.ex(e.e, sc, o); if (known(t) && !numeric(t) && t.k !== 'money') this.err('TC305', line, `Only numbers and money can be negative, but this is ${typeWords(t)}.`); return t; }
|
|
506
|
+
case 'not': this.cond(e.e, sc); return T('bool');
|
|
507
|
+
case 'bin': {
|
|
508
|
+
if (e.op === 'and' || e.op === 'or') { this.cond(e.l, sc); this.cond(e.r, sc); return T('bool'); }
|
|
509
|
+
const l = this.ex(e.l, sc, o), r = this.ex(e.r, sc, o);
|
|
510
|
+
return this.arith(e.op, l, r, line);
|
|
511
|
+
}
|
|
512
|
+
case 'cmp': {
|
|
513
|
+
const l = this.ex(e.l, sc, o), r = this.ex(e.r, sc, o);
|
|
514
|
+
this.comparable(l, r, line, e.op);
|
|
515
|
+
return T('bool');
|
|
516
|
+
}
|
|
517
|
+
case 'between': { const t = this.ex(e.e, sc, o); this.comparable(t, this.ex(e.lo, sc, o), line, '<='); this.comparable(t, this.ex(e.hi, sc, o), line, '<='); return T('bool'); }
|
|
518
|
+
case 'contains': {
|
|
519
|
+
const l = this.ex(e.l, sc, o), r = this.ex(e.r, sc, o);
|
|
520
|
+
if (known(l) && !['list', 'text', 'map'].includes(l.k)) this.err('TC305', line, `"contains" looks inside a list, a map, or text, but this is ${typeWords(l)}.`);
|
|
521
|
+
if (l.k === 'text' && known(r) && r.k !== 'text') this.err('TC302', line, `Text can only contain text, but this is ${typeWords(r)}.`);
|
|
522
|
+
return T('bool');
|
|
523
|
+
}
|
|
524
|
+
case 'adj': {
|
|
525
|
+
const t = this.ex(e.e, sc, o);
|
|
526
|
+
if (e.adj === 'empty') { if (known(t) && !['list', 'text', 'map'].includes(t.k)) this.err('TC305', line, `Only a list, a map, or text can be empty, but this is ${typeWords(t)}.`); }
|
|
527
|
+
else if (known(t) && !numeric(t) && t.k !== 'money') this.err('TC305', line, `Only numbers and money can be ${e.adj}, but this is ${typeWords(t)}.`);
|
|
528
|
+
return T('bool');
|
|
529
|
+
}
|
|
530
|
+
case 'isNothing': this.ex(e.e, sc, o); return T('bool');
|
|
531
|
+
default: this.err('TC402', line, `Thunder Core can't use "${e.k}" here yet.`); return UNKNOWN;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
name(e, sc, o) {
|
|
536
|
+
const line = e.line;
|
|
537
|
+
// Inside "where" / "sorted by": a field of each item comes first.
|
|
538
|
+
const el = this.elem[this.elem.length - 1];
|
|
539
|
+
if (el) {
|
|
540
|
+
const r = el.type.k === 'record' ? this.records.get(el.type.name) : null;
|
|
541
|
+
if (r && r.fieldMap.has(e.name)) { const f = r.fieldMap.get(e.name); e.ref = { kind: 'elemField', py: f.py }; return f.type; }
|
|
542
|
+
if (!r && !sc.get(e.name) && !el.type.k.match(/^(whole|number|money|text|bool|date)$/)) { e.ref = { kind: 'elemKey', key: e.text }; return UNKNOWN; }
|
|
543
|
+
if (!r && !sc.get(e.name) && e.name === 'it') { e.ref = { kind: 'elem' }; return el.type; }
|
|
544
|
+
}
|
|
545
|
+
if (e.name === 'it' && el) { e.ref = { kind: 'elem' }; return el.type; }
|
|
546
|
+
const v = sc.get(e.name);
|
|
547
|
+
if (v) {
|
|
548
|
+
if (o.keep && v.kind !== 'const' && v.kind !== 'func' && v.kind !== 'module') this.err('TC204', line, `A kept value can only use numbers, text, and other kept values, but "${e.text}" changes.`);
|
|
549
|
+
if (v.kind === 'record') { this.err('TC305', line, `"${e.text}" is a record. Make one with: a ${v.text} with …`); return UNKNOWN; }
|
|
550
|
+
e.ref = v;
|
|
551
|
+
return v.type ?? UNKNOWN;
|
|
552
|
+
}
|
|
553
|
+
// Not found: explain why, as precisely as possible.
|
|
554
|
+
const inFunc = findScope(sc, 'func') ?? findScope(sc, 'test');
|
|
555
|
+
if (inFunc && this.mainNames.has(e.name)) {
|
|
556
|
+
this.err('TC204', line, `${inFunc.func ? `"${inFunc.func.text}"` : 'A test'} can't use "${e.text}", because it is set in the main program (line ${this.mainNames.get(e.name)}).`, inFunc.func ? 'Give it to the function with "Receive", or make it fixed with "Keep".' : 'Set it inside the test, or make it fixed with "Keep".');
|
|
557
|
+
return UNKNOWN;
|
|
558
|
+
}
|
|
559
|
+
if (!inFunc && this.mainNames.has(e.name) && this.mainNames.get(e.name) >= line) {
|
|
560
|
+
this.err('TC211', line, `"${e.text}" is used before it is set (it is first set on line ${this.mainNames.get(e.name)}).`);
|
|
561
|
+
return UNKNOWN;
|
|
562
|
+
}
|
|
563
|
+
const all = [...allNames(sc)];
|
|
564
|
+
const g = closest(e.name, all);
|
|
565
|
+
const fieldHint = [...this.records.values()].find((r) => r.fieldMap.has(e.name));
|
|
566
|
+
this.err('TC201', line, `I don't know what "${e.text}" is.`, g ? `Did you mean "${g}"?` : fieldHint ? `"${e.text}" is a field of ${fieldHint.text}. Write it with an owner: customer's ${e.text}.` : 'Set it first (Set … to …), receive it, or check the spelling.');
|
|
567
|
+
return UNKNOWN;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
call(e, sc, o) {
|
|
571
|
+
const f = this.funcs.get(e.name);
|
|
572
|
+
const entry = this.global.vars.get(e.name);
|
|
573
|
+
if (!f) { this.err('TC201', e.line, `I don't know a function called "${e.text}".`); return UNKNOWN; }
|
|
574
|
+
e.py = f.py;
|
|
575
|
+
const ts = e.args.map((a) => this.ex(a, sc, o));
|
|
576
|
+
const required = f.params.filter((p) => !p.dflt).length;
|
|
577
|
+
if (ts.length < required || ts.length > f.params.length) {
|
|
578
|
+
const range = required === f.params.length ? `${f.params.length} value${f.params.length === 1 ? '' : 's'}` : `${required} to ${f.params.length} values`;
|
|
579
|
+
this.err('TC207', e.line, `"${f.text}" receives ${range} (${f.params.map((p) => (p.dflt ? `${p.text}, if given` : p.text)).join(', ') || 'none'}), but ${ts.length} ${ts.length === 1 ? 'was' : 'were'} given.`,
|
|
580
|
+
f.params.length > 1 && ts.length === 1 ? `Separate the values with commas or "and": ${f.text} with ${f.params.map((p) => p.text).join(' and ')}. To give a yes/no made with "and", put it in parentheses.` : null);
|
|
581
|
+
} else f.params.slice(0, ts.length).forEach((p, k) => { if (p.type && known(p.type) && known(ts[k]) && !compatible(p.type, ts[k])) this.err('TC303', e.line, `"${f.text}" receives ${p.text} as ${typeWords(p.type)}, but ${typeWords(ts[k])} was given.`); });
|
|
582
|
+
return entry?.ret ?? UNKNOWN;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
newRecord(e, sc, o) {
|
|
586
|
+
const r = this.records.get(e.record);
|
|
587
|
+
if (!r) { this.err('TC209', e.line, `I don't know a record called "${e.record}".`); return UNKNOWN; }
|
|
588
|
+
e.py = r.py;
|
|
589
|
+
const given = new Set();
|
|
590
|
+
for (const f of e.fields) {
|
|
591
|
+
const def = r.fieldMap.get(f.name);
|
|
592
|
+
const t = this.ex(f.e, sc, o);
|
|
593
|
+
if (!def) { const g = closest(f.name, [...r.fieldMap.keys()]); this.err('TC206', e.line, `A ${r.text} has no field "${f.text}".`, g ? `Did you mean "${g}"?` : `Fields: ${[...r.fieldMap.keys()].join(', ')}.`); continue; }
|
|
594
|
+
f.py = def.py;
|
|
595
|
+
if (given.has(f.name)) this.err('TC203', e.line, `The field "${f.text}" is given twice.`);
|
|
596
|
+
given.add(f.name);
|
|
597
|
+
if (known(def.type) && known(t) && !compatible(def.type, t)) this.err('TC303', e.line, `A ${r.text}'s ${def.text} is ${typeWords(def.type)}, but ${typeWords(t)} was given.`);
|
|
598
|
+
}
|
|
599
|
+
const missing = r.fields.filter((f) => !given.has(f.name) && f.type.k !== 'optional' && f.type.k !== 'list' && f.type.k !== 'map');
|
|
600
|
+
if (missing.length) this.err('TC210', e.line, `A ${r.text} needs ${missing.map((f) => f.text).join(', ')}.`, `Write: a ${r.text} with ${r.fields.map((f) => `${f.text} …`).join(' and ')}.`);
|
|
601
|
+
return T('record', { name: r.name, text: r.text });
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
op(e, sc, o) {
|
|
605
|
+
const t = this.ex(e.arg, sc, o);
|
|
606
|
+
const line = e.line;
|
|
607
|
+
const need = (kinds, what) => { if (known(t) && !kinds.includes(t.k)) this.err('TC305', line, `"the ${e.word} of" needs ${what}, but this is ${typeWords(t)}.`); };
|
|
608
|
+
const el = t.k === 'list' ? (t.of ?? UNKNOWN) : UNKNOWN;
|
|
609
|
+
switch (e.op) {
|
|
610
|
+
case 'sum': need(['list'], 'a list of numbers or money'); if (known(el) && !numeric(el) && el.k !== 'money') this.err('TC305', line, `Only numbers or money can be added up, but this list holds ${typeWords(el)}.`); return el;
|
|
611
|
+
case 'average': need(['list'], 'a list of numbers or money'); this.info.usesDecimal = true; return el.k === 'money' ? el : known(el) ? T('number') : UNKNOWN;
|
|
612
|
+
case 'count': need(['list', 'map', 'text'], 'a list, a map, or text'); return T('whole');
|
|
613
|
+
case 'length': need(['list', 'map', 'text'], 'text or a list'); return T('whole');
|
|
614
|
+
case 'min': case 'max': case 'first': case 'last': need(['list'], 'a list'); return el;
|
|
615
|
+
case 'words': case 'lines': need(['text'], 'text'); return T('list', { of: T('text') });
|
|
616
|
+
case 'lowercase': case 'uppercase': case 'trimmed': need(['text'], 'text'); return T('text');
|
|
617
|
+
case 'parts': need(['text'], 'text'); return T('list', { of: T('text') });
|
|
618
|
+
case 'abs': if (known(t) && !numeric(t) && t.k !== 'money') this.err('TC305', line, `"the absolute value of" needs a number or money, but this is ${typeWords(t)}.`); return known(t) ? t : UNKNOWN;
|
|
619
|
+
case 'jsonText': return T('text');
|
|
620
|
+
case 'jsonData': need(['text'], 'text'); return UNKNOWN;
|
|
621
|
+
case 'keys': need(['map'], 'a map'); return T('list', { of: t.key ?? UNKNOWN });
|
|
622
|
+
case 'values': need(['map'], 'a map'); return T('list', { of: t.of ?? UNKNOWN });
|
|
623
|
+
default: return UNKNOWN;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// A field of each item of a list (for "from", "sorted by", "Sort … by"). Plural words name a field only when
|
|
628
|
+
// removing a final "s" or "es" gives exactly a field's name (prices -> price); otherwise the words are used as is.
|
|
629
|
+
fieldOfElem(et, key, text, line, plural = false) {
|
|
630
|
+
const r = et.k === 'record' ? this.records.get(et.name) : null;
|
|
631
|
+
if (!r) return { dynamic: true, key: text, type: UNKNOWN };
|
|
632
|
+
const options = [key, ...(plural ? [key.replace(/es$/, ''), key.replace(/s$/, '')] : [])];
|
|
633
|
+
for (const k of options) if (r.fieldMap.has(k)) { const f = r.fieldMap.get(k); return { py: f.py, type: f.type }; }
|
|
634
|
+
const g = closest(key, [...r.fieldMap.keys()]);
|
|
635
|
+
this.err('TC206', line, `A ${r.text} has no field "${text}".`, g ? `Did you mean "${g}"?` : `Fields: ${[...r.fieldMap.keys()].join(', ')}.`);
|
|
636
|
+
return { dynamic: true, key: text, type: UNKNOWN };
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
arith(op, l, r, line) {
|
|
640
|
+
const sym = { '+': 'add', '-': 'subtract', '*': 'multiply', '/': 'divide', mod: 'take the remainder of' }[op];
|
|
641
|
+
if (!known(l) || !known(r)) {
|
|
642
|
+
if ((l.k === 'text' || r.k === 'text') && op !== '+') this.err('TC305', line, `Text can't be used to ${sym}.`);
|
|
643
|
+
return op === '/' ? (l.k === 'money' || r.k === 'money' ? UNKNOWN : UNKNOWN) : (l.k === 'text' || r.k === 'text') && op === '+' ? T('text') : UNKNOWN;
|
|
644
|
+
}
|
|
645
|
+
if (op === '+' && l.k === 'text' && r.k === 'text') return T('text');
|
|
646
|
+
if (op === '+' && l.k === 'list' && r.k === 'list') return l;
|
|
647
|
+
if (l.k === 'text' || r.k === 'text') { this.err('TC305', line, `Text can't be used to ${sym}${op === '+' ? ' with other kinds of values' : ''}.`, op === '+' ? 'To put a value into text, write it in braces: "Total: {total}".' : null); return UNKNOWN; }
|
|
648
|
+
if (numeric(l) && numeric(r)) return op === '/' ? (this.info.usesDecimal = true, T('number')) : l.k === 'whole' && r.k === 'whole' ? T('whole') : T('number');
|
|
649
|
+
if (l.k === 'money' && r.k === 'money') {
|
|
650
|
+
if (op === '*') { this.err('TC305', line, 'Money can\'t be multiplied by money.', 'Multiply money by a plain number: price * 3.'); return UNKNOWN; }
|
|
651
|
+
return op === '/' ? T('number') : T('money');
|
|
652
|
+
}
|
|
653
|
+
if ((l.k === 'money' && numeric(r)) || (numeric(l) && r.k === 'money')) {
|
|
654
|
+
if (op === '*' || (op === '/' && l.k === 'money')) return T('money');
|
|
655
|
+
if (op === '/') { this.err('TC305', line, 'A plain number can\'t be divided by money.'); return UNKNOWN; }
|
|
656
|
+
this.err('TC301', line, `I can't ${sym} money and a plain number.`, `Write the number as money (for example $${'5'}), or turn it into money with "as money".`);
|
|
657
|
+
return T('money');
|
|
658
|
+
}
|
|
659
|
+
this.err('TC305', line, `I can't ${sym} ${typeWords(l)} and ${typeWords(r)}.`);
|
|
660
|
+
return UNKNOWN;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
comparable(l, r, line, op) {
|
|
664
|
+
if (!known(l) || !known(r) || l.k === 'nothing' || r.k === 'nothing') return;
|
|
665
|
+
const order = ['<', '<=', '>', '>='].includes(op);
|
|
666
|
+
if (numeric(l) && numeric(r)) return;
|
|
667
|
+
if (l.k === 'money' && r.k === 'money') return;
|
|
668
|
+
if ((l.k === 'money' && numeric(r)) || (numeric(l) && r.k === 'money')) {
|
|
669
|
+
this.err('TC302', line, 'Money is compared with money, not with a plain number.', `Write the amount as money, for example $100.`);
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
if (l.k !== r.k && !(l.k === 'optional' || r.k === 'optional')) { this.err('TC302', line, `I can't compare ${typeWords(l)} with ${typeWords(r)}.`); return; }
|
|
673
|
+
if (order && (l.k === 'bool' || l.k === 'list' || l.k === 'map' || l.k === 'record')) this.err('TC302', line, `${typeWords(l)} can't be more or less than another; only equal or not.`);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
function sameKind(a, b) { return a.k === b.k || (numeric(a) && numeric(b)); }
|
|
678
|
+
export function compatible(a, b) {
|
|
679
|
+
if (!known(a) || !known(b)) return true;
|
|
680
|
+
if (a.k === 'nothing' || b.k === 'nothing') return a.k === 'optional' || b.k === 'optional' || a.k === b.k || true;
|
|
681
|
+
if (a.k === 'optional') return compatible(a.of, b.k === 'optional' ? b.of : b);
|
|
682
|
+
if (b.k === 'optional') return compatible(a, b.of);
|
|
683
|
+
if (numeric(a) && numeric(b)) return true;
|
|
684
|
+
if (a.k !== b.k) return false;
|
|
685
|
+
if (a.k === 'record') return a.name === b.name;
|
|
686
|
+
if (a.k === 'list') return compatible(a.of ?? UNKNOWN, b.of ?? UNKNOWN);
|
|
687
|
+
return true;
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
function findScope(sc, kind) { for (let s = sc; s; s = s.parent) if (s.kind === kind) return s; return null; }
|
|
691
|
+
function findFunc(sc) { for (let s = sc; s; s = s.parent) if (s.func) return s.func; return null; }
|
|
692
|
+
function allNames(sc) { const out = new Set(); for (let s = sc; s; s = s.parent) for (const k of s.vars.keys()) out.add(k); return out; }
|
|
693
|
+
|
|
694
|
+
export function childBlocks(s) {
|
|
695
|
+
switch (s.k) {
|
|
696
|
+
case 'if': return [...s.branches.map((b) => b.body), ...(s.otherwise ? [s.otherwise] : [])];
|
|
697
|
+
case 'each': case 'range': case 'while': case 'times': case 'func': case 'test': return [s.body];
|
|
698
|
+
case 'tryFail': return [[s.stmt], s.handler];
|
|
699
|
+
default: return [];
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
function collectMainNames(top, out) {
|
|
703
|
+
const visit = (body) => {
|
|
704
|
+
for (const s of body) {
|
|
705
|
+
if (s.k === 'func' || s.k === 'test' || s.k === 'record') continue;
|
|
706
|
+
if (s.k === 'set' && s.target.k === 'name' && !out.has(s.target.name)) out.set(s.target.name, s.line);
|
|
707
|
+
if (s.k === 'each') for (const v of s.vars) if (!out.has(v.name)) out.set(v.name, s.line);
|
|
708
|
+
if (s.k === 'range' && !out.has(s.v.name)) out.set(s.v.name, s.line);
|
|
709
|
+
for (const b of childBlocks(s)) visit(b);
|
|
710
|
+
}
|
|
711
|
+
};
|
|
712
|
+
visit(top);
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
export function walkExpr(e, fn) {
|
|
716
|
+
if (!e || typeof e !== 'object') return;
|
|
717
|
+
fn(e);
|
|
718
|
+
for (const k of ['e', 'l', 'r', 'obj', 'key', 'dflt', 'list', 'cond', 'arg', 'places', 'path', 'lo', 'hi']) if (e[k]) walkExpr(e[k], fn);
|
|
719
|
+
for (const a of e.args ?? []) walkExpr(a, fn);
|
|
720
|
+
for (const a of e.items ?? []) walkExpr(a, fn);
|
|
721
|
+
for (const f of e.fields ?? []) walkExpr(f.e, fn);
|
|
722
|
+
if (e.k === 'str') for (const p of e.parts) if (typeof p !== 'string') walkExpr(p.e, fn);
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
export { RESERVED };
|