upgrade token expression evaluator
This commit is contained in:
parent
1b630b6505
commit
a462159d2c
1 changed files with 8 additions and 31 deletions
|
|
@ -1228,43 +1228,20 @@
|
||||||
return ((PI * SQR(noz/2)) / (PI * SQR(fil/2))) * (slice / noz);
|
return ((PI * SQR(noz/2)) / (PI * SQR(fil/2))) * (slice / noz);
|
||||||
}
|
}
|
||||||
|
|
||||||
function subv(consts, v) {
|
|
||||||
let sv = consts[v];
|
|
||||||
if (sv !== undefined) {
|
|
||||||
v = sv;
|
|
||||||
}
|
|
||||||
sv = parseFloat(sv);
|
|
||||||
if (isNaN(sv)) {
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
return sv;
|
|
||||||
}
|
|
||||||
|
|
||||||
function constOp(tok, consts, opch, op) {
|
|
||||||
let pos, v1, v2;
|
|
||||||
if ((pos = tok.indexOf(opch)) > 0) {
|
|
||||||
let lv = tok.substring(0,pos);
|
|
||||||
let rv = tok.substring(pos+1);
|
|
||||||
v1 = subv(consts,lv) || 0;
|
|
||||||
v2 = subv(consts,rv) || 0;
|
|
||||||
return op(v1,v2).round(4);
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function constReplace(str, consts, start, pad, short) {
|
function constReplace(str, consts, start, pad, short) {
|
||||||
let cs = str.indexOf("{", start || 0),
|
let cs = str.indexOf("{", start || 0),
|
||||||
ce = str.indexOf("}", cs),
|
ce = str.indexOf("}", cs),
|
||||||
tok, nutok, nustr;
|
tok, nutok, nustr;
|
||||||
if (cs >=0 && ce > cs) {
|
if (cs >=0 && ce > cs) {
|
||||||
tok = str.substring(cs+1,ce);
|
tok = str.substring(cs+1,ce);
|
||||||
nutok =
|
let eva = [];
|
||||||
constOp(tok, consts, "-", function(v1,v2) { return v1-v2 }) ||
|
for (let [k,v] of Object.entries(consts)) {
|
||||||
constOp(tok, consts, "+", function(v1,v2) { return v1+v2 }) ||
|
eva.push(`let ${k} = "${v}";`);
|
||||||
constOp(tok, consts, "/", function(v1,v2) { return v1/v2 }) ||
|
}
|
||||||
constOp(tok, consts, "*", function(v1,v2) { return v1*v2 }) ||
|
eva.push(`try {( ${tok} )} catch (e) {0}`);
|
||||||
consts[tok] || 0;
|
let scr = eva.join('');
|
||||||
|
let evl = eval(`{ ${scr} }`);
|
||||||
|
nutok = evl;
|
||||||
if (pad) {
|
if (pad) {
|
||||||
nutok = nutok.toString();
|
nutok = nutok.toString();
|
||||||
let oldln = ce-cs+1;
|
let oldln = ce-cs+1;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue