@rulecad/variables 0.2.0 → 0.2.3
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/package.json +1 -1
- package/variables.d.mts +1 -1
- package/variables.mjs +27 -14
package/package.json
CHANGED
package/variables.d.mts
CHANGED
|
@@ -5,7 +5,7 @@ export function checkName(name: any, d: any, v: any): string | number;
|
|
|
5
5
|
* @param {(name: string) => number} lookup the value of a name
|
|
6
6
|
* @returns {number}
|
|
7
7
|
*/
|
|
8
|
-
export function evaluate(src: string | number, lookup
|
|
8
|
+
export function evaluate(src: string | number, lookup?: (name: string) => number): number;
|
|
9
9
|
/**
|
|
10
10
|
* The unit an expression comes to, and any mixing of units on the way (mm added to degrees, a sine of millimetres).
|
|
11
11
|
* @param {string | number} src @param {(name: string) => string | undefined} unitOf a name's unit
|
package/variables.mjs
CHANGED
|
@@ -49,8 +49,8 @@ function tokens(src) {
|
|
|
49
49
|
* @param {(name: string) => number} lookup the value of a name
|
|
50
50
|
* @returns {number}
|
|
51
51
|
*/
|
|
52
|
-
export function evaluate(src, lookup) {
|
|
53
|
-
if (typeof src === 'number') return src;
|
|
52
|
+
export function evaluate(src, lookup = (name) => { throw new Error(`no value for "${name}"`); }) {
|
|
53
|
+
if (typeof src === 'number') { if (!Number.isFinite(src)) throw new Error('is not a finite number'); return src; }
|
|
54
54
|
const t = tokens(String(src));
|
|
55
55
|
let i = 0;
|
|
56
56
|
const peek = () => t[i], take = (op) => (t[i]?.op === op ? (i++, true) : false);
|
|
@@ -62,7 +62,7 @@ export function evaluate(src, lookup) {
|
|
|
62
62
|
if (!k) throw new Error('ends too soon');
|
|
63
63
|
if (k.n != null) return k.n;
|
|
64
64
|
if (k.op === '(') { const v = expr(); if (!take(')')) throw new Error('missing )'); return v; }
|
|
65
|
-
if (k.id && FNS
|
|
65
|
+
if (k.id && Object.hasOwn(FNS, k.id) && take('(')) {
|
|
66
66
|
const args = [expr()];
|
|
67
67
|
while (take(',')) args.push(expr());
|
|
68
68
|
if (!take(')')) throw new Error('missing )');
|
|
@@ -115,7 +115,7 @@ export function unitsOf(src, unitOf) {
|
|
|
115
115
|
if (!k) return null;
|
|
116
116
|
if (k.n != null) return null;
|
|
117
117
|
if (k.op === '(') { const v = expr(); take(')'); return v; }
|
|
118
|
-
if (k.id && FNS
|
|
118
|
+
if (k.id && Object.hasOwn(FNS, k.id) && take('(')) {
|
|
119
119
|
const args = [expr()];
|
|
120
120
|
while (take(',')) args.push(expr());
|
|
121
121
|
take(')');
|
|
@@ -153,14 +153,20 @@ export function unitsOf(src, unitOf) {
|
|
|
153
153
|
* @returns {{ values: Record<string, any>, entries: Entry[], deps: Record<string, string[]> }} deps: each name with the names its own expression reads
|
|
154
154
|
*/
|
|
155
155
|
export function resolve(object, { sets = {}, set = {}, display = {}, load } = {}) {
|
|
156
|
+
// Names are looked up as an object's own, never inherited: a variable called constructor is a variable, and a
|
|
157
|
+
// name nobody defined (toString) is unknown, not whatever every object happens to carry
|
|
158
|
+
const own = (o, k) => (o != null && Object.hasOwn(o, k) ? o[k] : undefined);
|
|
156
159
|
const ruleSets = Object.fromEntries((object.uses || []).map((u) => {
|
|
157
|
-
const s = sets
|
|
160
|
+
const s = own(sets, u) || (load ? load(u) : null);
|
|
158
161
|
if (!s) throw fail('rules.missing', `no rule set "${u}" (hand it in as sets.${u}, or give a load function)`, 'uses');
|
|
162
|
+
if (typeof s.then === 'function') throw fail('rules.async', `the load function gave a promise for "${u}": load must return the rule set itself (fetch it first, and hand it in as sets.${u})`, 'uses');
|
|
163
|
+
if (!s.rules || typeof s.rules !== 'object') throw fail('rules.shape', `rule set "${u}" has no "rules"`, 'uses');
|
|
159
164
|
return [s.name || u, s];
|
|
160
165
|
}));
|
|
161
166
|
const vars = object.variables || {};
|
|
167
|
+
for (const name of Object.keys(ruleSets)) if (own(vars, name) || own(vars, name.replace(/-/g, '_'))) throw fail('var.clash', `"${name}" is a rule set in uses and a variable too: rename the variable, so each name means one thing`, `variables.${own(vars, name) ? name : name.replace(/-/g, '_')}`);
|
|
162
168
|
for (const [k, v] of Object.entries(set)) {
|
|
163
|
-
const d = vars
|
|
169
|
+
const d = own(vars, k);
|
|
164
170
|
if (!d) throw fail('set.unknown', `no variable "${k}" to set (variables: ${Object.keys(vars).join(', ')})`, `variables.${k}`);
|
|
165
171
|
if (isName(d)) { checkName(k, d, v); continue; }
|
|
166
172
|
if (!d.tweak) throw fail('set.fixed', `"${k}" is fixed by its rule; only variables with a tweak range can be set`, `variables.${k}.tweak`);
|
|
@@ -178,15 +184,19 @@ export function resolve(object, { sets = {}, set = {}, display = {}, load } = {}
|
|
|
178
184
|
busy.add(name);
|
|
179
185
|
let src, where;
|
|
180
186
|
if (name.includes('.')) {
|
|
181
|
-
let [s, r] = name.split('.');
|
|
182
|
-
if (
|
|
183
|
-
if (!ruleSets
|
|
184
|
-
if (!ruleSets
|
|
187
|
+
let [s, r, more] = name.split('.');
|
|
188
|
+
if (more !== undefined) throw fail('var.unknown-rule', `"${name}": a rule is named <set>.<rule>, with one dot`);
|
|
189
|
+
if (!own(ruleSets, s) && own(ruleSets, s.replace(/_/g, '-'))) s = s.replace(/_/g, '-'); // mx_switch.housing_top reads the set named mx-switch (a - is a minus here)
|
|
190
|
+
if (!own(ruleSets, s)) throw fail('var.unknown-set', `"${name}": rule set "${s}" isn't in uses (${Object.keys(ruleSets).join(', ') || 'none'})`);
|
|
191
|
+
if (!own(ruleSets[s].rules, r)) throw fail('var.unknown-rule', `"${name}": no rule "${r}" in ${s}`);
|
|
185
192
|
src = ruleSets[s].rules[r].value; where = (n) => (n.includes('.') ? n : `${s}.${n}`); // a rule's names are its own set's
|
|
186
193
|
} else {
|
|
187
|
-
|
|
188
|
-
if (
|
|
189
|
-
|
|
194
|
+
const d = own(vars, name);
|
|
195
|
+
if (!d) throw fail('var.unknown', `no variable "${name}"`);
|
|
196
|
+
const shown = Object.hasOwn(display, name), given = Object.hasOwn(set, name);
|
|
197
|
+
if (isName(d)) { busy.delete(name); const c = checkName(name, d, shown ? display[name] : given ? set[name] : d.value); done.set(name, c); return c; }
|
|
198
|
+
src = shown ? display[name] : given ? Number(set[name]) : d.value; where = (n) => n; // display: the screen's settings win
|
|
199
|
+
if (src === undefined) throw fail('var.value', `"${name}" has no value`, `variables.${name}`);
|
|
190
200
|
}
|
|
191
201
|
let v;
|
|
192
202
|
deps.set(name, new Set()); stack.push(name);
|
|
@@ -203,7 +213,7 @@ export function resolve(object, { sets = {}, set = {}, display = {}, load } = {}
|
|
|
203
213
|
const values = {}, entries = [];
|
|
204
214
|
for (const [name, d] of Object.entries(vars)) {
|
|
205
215
|
values[name] = lookup(name);
|
|
206
|
-
entries.push({ name, ...d, value: values[name], expr: typeof d.value === 'string' ? d.value : undefined, set: name
|
|
216
|
+
entries.push({ name, ...d, value: values[name], expr: typeof d.value === 'string' ? d.value : undefined, set: Object.hasOwn(set, name) });
|
|
207
217
|
}
|
|
208
218
|
// Units: each variable's expression, its names' units mixed only where they agree, and coming to the unit it says
|
|
209
219
|
const unitOf = (n) => (n.includes('.') ? ruleSets[n.split('.')[0].replace(/_/g, '-')]?.rules[n.split('.')[1]]?.unit ?? ruleSets[n.split('.')[0]]?.rules[n.split('.')[1]]?.unit : vars[n]?.unit);
|
|
@@ -222,6 +232,9 @@ export function resolve(object, { sets = {}, set = {}, display = {}, load } = {}
|
|
|
222
232
|
entries.push({ name: `${s}.${r}`, ...d, value: values[s][r], expr: typeof d.value === 'string' ? d.value : undefined, used: used.has(`${s}.${r}`), ...(rs.private ? { private: true } : {}) });
|
|
223
233
|
}
|
|
224
234
|
if (rs.footprints) values[s].footprints = rs.footprints; // pad data for boards (v.esp32c6.footprints.module), as the set gives it
|
|
235
|
+
if (rs.circuit) values[s].circuit = rs.circuit;
|
|
236
|
+
const pinMaps = Object.entries(rs.rules).filter(([, d]) => d.pins); // rules' pin maps (package pin: name), as v.rp2040.pinMaps.gpio
|
|
237
|
+
if (pinMaps.length) values[s].pinMaps = Object.fromEntries(pinMaps.map(([r, d]) => [r, d.pins])); // (camel case: no rule is named so) // a whole board's circuit, from cad clone (v.sea_picro.circuit), for @rulecad/pcb's clone()
|
|
225
238
|
}
|
|
226
239
|
return { values, entries, deps: Object.fromEntries([...deps].map(([k, v]) => [k, [...v]])), units };
|
|
227
240
|
}
|