@rulecad/variables 0.3.0 → 0.3.1
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.mjs +55 -36
package/package.json
CHANGED
package/variables.mjs
CHANGED
|
@@ -42,37 +42,60 @@ function tokens(src) {
|
|
|
42
42
|
return out;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
//
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
*
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const t = tokens(String(src));
|
|
45
|
+
// One grammar: an expression parsed once to a tree, which evaluate() and unitsOf() each walk. Nodes: { n } a number,
|
|
46
|
+
// { id } a name, { op: '+' | '-' | '*' | '/', a, b }, { neg: a } (or { pos: a }), { fn, args }.
|
|
47
|
+
// expr = term (('+' | '-') term)* · term = unary (('*' | '/') unary)* · unary = ('-' | '+') unary | atom
|
|
48
|
+
// atom = number | name | name '(' expr (',' expr)* ')' (a function of FNS) | '(' expr ')'
|
|
49
|
+
const trees = new Map(); // (a design reads the same expressions over and over: each is parsed once)
|
|
50
|
+
function parse(src) {
|
|
51
|
+
const key = String(src);
|
|
52
|
+
if (trees.has(key)) return trees.get(key);
|
|
53
|
+
const t = tokens(key);
|
|
55
54
|
let i = 0;
|
|
56
55
|
const peek = () => t[i], take = (op) => (t[i]?.op === op ? (i++, true) : false);
|
|
57
|
-
const expr = () => { let v = term(); for (;;) { if (take('+')) v
|
|
58
|
-
const term = () => { let v = unary(); for (;;) { if (take('*')) v
|
|
59
|
-
const unary = () => (take('-') ?
|
|
56
|
+
const expr = () => { let v = term(); for (;;) { if (take('+')) v = { op: '+', a: v, b: term() }; else if (take('-')) v = { op: '-', a: v, b: term() }; else return v; } };
|
|
57
|
+
const term = () => { let v = unary(); for (;;) { if (take('*')) v = { op: '*', a: v, b: unary() }; else if (take('/')) v = { op: '/', a: v, b: unary() }; else return v; } };
|
|
58
|
+
const unary = () => (take('-') ? { neg: unary() } : take('+') ? { pos: unary() } : atom());
|
|
60
59
|
const atom = () => {
|
|
61
60
|
const k = t[i++];
|
|
62
61
|
if (!k) throw new Error('ends too soon');
|
|
63
|
-
if (k.n != null) return k.n;
|
|
62
|
+
if (k.n != null) return { n: k.n };
|
|
64
63
|
if (k.op === '(') { const v = expr(); if (!take(')')) throw new Error('missing )'); return v; }
|
|
65
64
|
if (k.id && Object.hasOwn(FNS, k.id) && take('(')) {
|
|
66
65
|
const args = [expr()];
|
|
67
66
|
while (take(',')) args.push(expr());
|
|
68
67
|
if (!take(')')) throw new Error('missing )');
|
|
69
|
-
return
|
|
68
|
+
return { fn: k.id, args };
|
|
70
69
|
}
|
|
71
|
-
if (k.id) return
|
|
70
|
+
if (k.id) return { id: k.id };
|
|
72
71
|
throw new Error(`unexpected "${k.op}"`);
|
|
73
72
|
};
|
|
74
|
-
const
|
|
73
|
+
const tree = expr();
|
|
75
74
|
if (peek()) throw new Error(`unexpected "${peek().op ?? peek().id ?? peek().n}"`);
|
|
75
|
+
if (trees.size > 5000) trees.clear();
|
|
76
|
+
trees.set(key, tree);
|
|
77
|
+
return tree;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// evaluate("line * 3", lookup) -> number. lookup(name) gives a name's value.
|
|
81
|
+
/**
|
|
82
|
+
* One expression: + - * / and brackets, min, max, round, sind, cosd, tand, over numbers and names. Never runs code.
|
|
83
|
+
* @param {string | number} src
|
|
84
|
+
* @param {(name: string) => number} lookup the value of a name
|
|
85
|
+
* @returns {number}
|
|
86
|
+
*/
|
|
87
|
+
export function evaluate(src, lookup = (name) => { throw new Error(`no value for "${name}"`); }) {
|
|
88
|
+
if (typeof src === 'number') { if (!Number.isFinite(src)) throw new Error('is not a finite number'); return src; }
|
|
89
|
+
const walk = (x) => {
|
|
90
|
+
if (x.n != null) return x.n;
|
|
91
|
+
if (x.id != null) return lookup(x.id);
|
|
92
|
+
if (x.neg) return -walk(x.neg);
|
|
93
|
+
if (x.pos) return walk(x.pos);
|
|
94
|
+
if (x.fn) return FNS[x.fn](...x.args.map(walk));
|
|
95
|
+
const a = walk(x.a), b = walk(x.b);
|
|
96
|
+
return x.op === '+' ? a + b : x.op === '-' ? a - b : x.op === '*' ? a * b : a / b;
|
|
97
|
+
};
|
|
98
|
+
const v = walk(parse(src));
|
|
76
99
|
if (!Number.isFinite(v)) throw new Error('is not a finite number');
|
|
77
100
|
return v;
|
|
78
101
|
}
|
|
@@ -103,29 +126,25 @@ const combine = (a, b, sign) => { if (a == null) return sign > 0 ? b : b == null
|
|
|
103
126
|
*/
|
|
104
127
|
export function unitsOf(src, unitOf) {
|
|
105
128
|
if (typeof src === 'number') return { unit: null, problems: [] };
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const
|
|
129
|
+
let tree;
|
|
130
|
+
try { tree = parse(src); } catch { return { unit: null, problems: [] }; } // (an expression that doesn't parse: evaluate says so)
|
|
131
|
+
const problems = [];
|
|
109
132
|
const add = (a, b, op) => { if (!sameUnit(a, b)) problems.push(`${op === '+' ? 'adds' : 'takes'} ${showUnit(b)} ${op === '+' ? 'to' : 'from'} ${showUnit(a)}`); return a ?? b; };
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
if (
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
const args = [expr()];
|
|
120
|
-
while (take(',')) args.push(expr());
|
|
121
|
-
take(')');
|
|
122
|
-
if (['sind', 'cosd', 'tand'].includes(k.id)) { if (args[0] && !sameUnit(args[0], { deg: 1 })) problems.push(`takes ${k.id} of ${showUnit(args[0])}, not degrees`); return {}; }
|
|
123
|
-
if (k.id === 'round') return args[0];
|
|
133
|
+
const walk = (x) => {
|
|
134
|
+
if (x.n != null) return null;
|
|
135
|
+
if (x.id != null) return parseUnit(unitOf(x.id));
|
|
136
|
+
if (x.neg) return walk(x.neg);
|
|
137
|
+
if (x.pos) return walk(x.pos);
|
|
138
|
+
if (x.fn) {
|
|
139
|
+
const args = x.args.map(walk);
|
|
140
|
+
if (['sind', 'cosd', 'tand'].includes(x.fn)) { if (args[0] && !sameUnit(args[0], { deg: 1 })) problems.push(`takes ${x.fn} of ${showUnit(args[0])}, not degrees`); return {}; }
|
|
141
|
+
if (x.fn === 'round') return args[0];
|
|
124
142
|
return args.reduce((a, b) => add(a, b, '+'));
|
|
125
143
|
}
|
|
126
|
-
|
|
144
|
+
const a = walk(x.a), b = walk(x.b);
|
|
145
|
+
return x.op === '+' || x.op === '-' ? add(a, b, x.op) : combine(a, b, x.op === '*' ? 1 : -1);
|
|
127
146
|
};
|
|
128
|
-
const v =
|
|
147
|
+
const v = walk(tree);
|
|
129
148
|
return { unit: v == null ? null : showUnit(v), problems, value: v };
|
|
130
149
|
}
|
|
131
150
|
|