code format
This commit is contained in:
@@ -1,20 +1,20 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Evaluator designed for stream computing
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
PIP - Platform Independent Primitives
|
||||
Evaluator designed for stream computing
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "pievaluator.h"
|
||||
@@ -122,49 +122,49 @@ using namespace PIEvaluatorTypes;
|
||||
|
||||
|
||||
PIEvaluatorContent::PIEvaluatorContent() {
|
||||
addFunction(PIStringAscii("arcsin" ), 1);
|
||||
addFunction(PIStringAscii("arccos" ), 1);
|
||||
addFunction(PIStringAscii("arctg" ), 1);
|
||||
addFunction(PIStringAscii("arcctg" ), 1);
|
||||
addFunction(PIStringAscii("random" ), 2);
|
||||
addFunction(PIStringAscii("arcsin"), 1);
|
||||
addFunction(PIStringAscii("arccos"), 1);
|
||||
addFunction(PIStringAscii("arctg"), 1);
|
||||
addFunction(PIStringAscii("arcctg"), 1);
|
||||
addFunction(PIStringAscii("random"), 2);
|
||||
addFunction(PIStringAscii("randomn"), 2);
|
||||
addFunction(PIStringAscii("sin" ), 1);
|
||||
addFunction(PIStringAscii("cos" ), 1);
|
||||
addFunction(PIStringAscii("ctg" ), 1);
|
||||
addFunction(PIStringAscii("tg" ), 1);
|
||||
addFunction(PIStringAscii("exp" ), 1);
|
||||
addFunction(PIStringAscii("cth" ), 1);
|
||||
addFunction(PIStringAscii("sh" ), 1);
|
||||
addFunction(PIStringAscii("ch" ), 1);
|
||||
addFunction(PIStringAscii("th" ), 1);
|
||||
addFunction(PIStringAscii("sqrt" ), 1);
|
||||
addFunction(PIStringAscii("sqr" ), 1);
|
||||
addFunction(PIStringAscii("pow" ), 2);
|
||||
addFunction(PIStringAscii("abs" ), 1);
|
||||
addFunction(PIStringAscii("ln" ), 1);
|
||||
addFunction(PIStringAscii("lg" ), 1);
|
||||
addFunction(PIStringAscii("log" ), 2);
|
||||
addFunction(PIStringAscii("im" ), 1);
|
||||
addFunction(PIStringAscii("re" ), 1);
|
||||
addFunction(PIStringAscii("arg" ), 1);
|
||||
addFunction(PIStringAscii("len" ), 1);
|
||||
addFunction(PIStringAscii("conj" ), 1);
|
||||
addFunction(PIStringAscii("sign" ), 1);
|
||||
addFunction(PIStringAscii("rad" ), 1);
|
||||
addFunction(PIStringAscii("deg" ), 1);
|
||||
addFunction(PIStringAscii("j0" ), 1);
|
||||
addFunction(PIStringAscii("j1" ), 1);
|
||||
addFunction(PIStringAscii("jn" ), 2);
|
||||
addFunction(PIStringAscii("y0" ), 1);
|
||||
addFunction(PIStringAscii("y1" ), 1);
|
||||
addFunction(PIStringAscii("yn" ), 2);
|
||||
addFunction(PIStringAscii("min" ), -2); // (x0,x1,...)
|
||||
addFunction(PIStringAscii("max" ), -2); // (x0,x1,...)
|
||||
addFunction(PIStringAscii("clamp" ), 3); // (x,a,b) = x < a ? a : (x > b ? b : x)
|
||||
addFunction(PIStringAscii("step" ), 2); // (x,s) = x >= s ? 1. : 0. (1 if 'x' >= 's', else 0)
|
||||
addFunction(PIStringAscii("mix" ), 3); // (x,a,b) = a*(1.-x) + b*x (interpolate between 'a' and 'b' linear for 'x')
|
||||
addFunction(PIStringAscii("sin"), 1);
|
||||
addFunction(PIStringAscii("cos"), 1);
|
||||
addFunction(PIStringAscii("ctg"), 1);
|
||||
addFunction(PIStringAscii("tg"), 1);
|
||||
addFunction(PIStringAscii("exp"), 1);
|
||||
addFunction(PIStringAscii("cth"), 1);
|
||||
addFunction(PIStringAscii("sh"), 1);
|
||||
addFunction(PIStringAscii("ch"), 1);
|
||||
addFunction(PIStringAscii("th"), 1);
|
||||
addFunction(PIStringAscii("sqrt"), 1);
|
||||
addFunction(PIStringAscii("sqr"), 1);
|
||||
addFunction(PIStringAscii("pow"), 2);
|
||||
addFunction(PIStringAscii("abs"), 1);
|
||||
addFunction(PIStringAscii("ln"), 1);
|
||||
addFunction(PIStringAscii("lg"), 1);
|
||||
addFunction(PIStringAscii("log"), 2);
|
||||
addFunction(PIStringAscii("im"), 1);
|
||||
addFunction(PIStringAscii("re"), 1);
|
||||
addFunction(PIStringAscii("arg"), 1);
|
||||
addFunction(PIStringAscii("len"), 1);
|
||||
addFunction(PIStringAscii("conj"), 1);
|
||||
addFunction(PIStringAscii("sign"), 1);
|
||||
addFunction(PIStringAscii("rad"), 1);
|
||||
addFunction(PIStringAscii("deg"), 1);
|
||||
addFunction(PIStringAscii("j0"), 1);
|
||||
addFunction(PIStringAscii("j1"), 1);
|
||||
addFunction(PIStringAscii("jn"), 2);
|
||||
addFunction(PIStringAscii("y0"), 1);
|
||||
addFunction(PIStringAscii("y1"), 1);
|
||||
addFunction(PIStringAscii("yn"), 2);
|
||||
addFunction(PIStringAscii("min"), -2); // (x0,x1,...)
|
||||
addFunction(PIStringAscii("max"), -2); // (x0,x1,...)
|
||||
addFunction(PIStringAscii("clamp"), 3); // (x,a,b) = x < a ? a : (x > b ? b : x)
|
||||
addFunction(PIStringAscii("step"), 2); // (x,s) = x >= s ? 1. : 0. (1 if 'x' >= 's', else 0)
|
||||
addFunction(PIStringAscii("mix"), 3); // (x,a,b) = a*(1.-x) + b*x (interpolate between 'a' and 'b' linear for 'x')
|
||||
addFunction(PIStringAscii("defined"), 1);
|
||||
addFunction(PIStringAscii("round" ), 1);
|
||||
addFunction(PIStringAscii("round"), 1);
|
||||
clearCustomVariables();
|
||||
}
|
||||
|
||||
@@ -206,94 +206,89 @@ int PIEvaluatorContent::customVariablesCount() const {
|
||||
|
||||
int PIEvaluatorContent::findFunction(const PIString & name) const {
|
||||
for (uint i = 0; i < functions.size(); i++)
|
||||
if (functions[i].identifier == name)
|
||||
return i;
|
||||
if (functions[i].identifier == name) return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int PIEvaluatorContent::findVariable(const PIString & var_name) const {
|
||||
for (uint i = 0; i < variables.size(); i++)
|
||||
if (variables[i].name == var_name)
|
||||
return i;
|
||||
if (variables[i].name == var_name) return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
PIEvaluatorTypes::Function PIEvaluatorContent::function(int index) {
|
||||
if (index < 0 || index >= functions.size_s())
|
||||
return PIEvaluatorTypes::Function();
|
||||
if (index < 0 || index >= functions.size_s()) return PIEvaluatorTypes::Function();
|
||||
return functions[index];
|
||||
}
|
||||
|
||||
|
||||
PIEvaluatorTypes::Variable PIEvaluatorContent::variable(int index) {
|
||||
if (index < 0 || index >= variables.size_s())
|
||||
return PIEvaluatorTypes::Variable();
|
||||
if (index < 0 || index >= variables.size_s()) return PIEvaluatorTypes::Variable();
|
||||
return variables[index];
|
||||
}
|
||||
|
||||
|
||||
PIEvaluatorTypes::Variable PIEvaluatorContent::customVariable(int index) {
|
||||
if (index < 0 || index >= variables.size_s() - cv_count)
|
||||
return PIEvaluatorTypes::Variable();
|
||||
if (index < 0 || index >= variables.size_s() - cv_count) return PIEvaluatorTypes::Variable();
|
||||
return variables[index + cv_count];
|
||||
}
|
||||
|
||||
|
||||
void PIEvaluatorContent::clearCustomVariables() {
|
||||
variables.clear();
|
||||
addVariable(PIStringAscii("i" ), complexd_i);
|
||||
addVariable(PIStringAscii("i"), complexd_i);
|
||||
addVariable(PIStringAscii("pi"), 3.141592653589793238462643383280);
|
||||
addVariable(PIStringAscii("e" ), exp(1.));
|
||||
addVariable(PIStringAscii("e"), exp(1.));
|
||||
cv_count = variables.size_s();
|
||||
}
|
||||
|
||||
|
||||
BaseFunctions PIEvaluatorContent::getBaseFunction(const PIString & name) {
|
||||
if (name == PIStringAscii("sin" )) return bfSin;
|
||||
if (name == PIStringAscii("cos" )) return bfCos;
|
||||
if (name == PIStringAscii("tg" )) return bfTg;
|
||||
if (name == PIStringAscii("ctg" )) return bfCtg;
|
||||
if (name == PIStringAscii("arcsin" )) return bfArcsin;
|
||||
if (name == PIStringAscii("arccos" )) return bfArccos;
|
||||
if (name == PIStringAscii("arctg" )) return bfArctg;
|
||||
if (name == PIStringAscii("arcctg" )) return bfArcctg;
|
||||
if (name == PIStringAscii("exp" )) return bfExp;
|
||||
if (name == PIStringAscii("random" )) return bfRandom;
|
||||
if (name == PIStringAscii("sin")) return bfSin;
|
||||
if (name == PIStringAscii("cos")) return bfCos;
|
||||
if (name == PIStringAscii("tg")) return bfTg;
|
||||
if (name == PIStringAscii("ctg")) return bfCtg;
|
||||
if (name == PIStringAscii("arcsin")) return bfArcsin;
|
||||
if (name == PIStringAscii("arccos")) return bfArccos;
|
||||
if (name == PIStringAscii("arctg")) return bfArctg;
|
||||
if (name == PIStringAscii("arcctg")) return bfArcctg;
|
||||
if (name == PIStringAscii("exp")) return bfExp;
|
||||
if (name == PIStringAscii("random")) return bfRandom;
|
||||
if (name == PIStringAscii("randomn")) return bfRandomn;
|
||||
if (name == PIStringAscii("sh" )) return bfSh;
|
||||
if (name == PIStringAscii("ch" )) return bfCh;
|
||||
if (name == PIStringAscii("th" )) return bfTh;
|
||||
if (name == PIStringAscii("cth" )) return bfCth;
|
||||
if (name == PIStringAscii("sqrt" )) return bfSqrt;
|
||||
if (name == PIStringAscii("sqr" )) return bfSqr;
|
||||
if (name == PIStringAscii("pow" )) return bfPow;
|
||||
if (name == PIStringAscii("abs" )) return bfAbs;
|
||||
if (name == PIStringAscii("ln" )) return bfLn;
|
||||
if (name == PIStringAscii("lg" )) return bfLg;
|
||||
if (name == PIStringAscii("log" )) return bfLog;
|
||||
if (name == PIStringAscii("im" )) return bfIm;
|
||||
if (name == PIStringAscii("re" )) return bfRe;
|
||||
if (name == PIStringAscii("arg" )) return bfArg;
|
||||
if (name == PIStringAscii("len" )) return bfLen;
|
||||
if (name == PIStringAscii("conj" )) return bfConj;
|
||||
if (name == PIStringAscii("sign" )) return bfSign;
|
||||
if (name == PIStringAscii("rad" )) return bfRad;
|
||||
if (name == PIStringAscii("deg" )) return bfDeg;
|
||||
if (name == PIStringAscii("j0" )) return bfJ0;
|
||||
if (name == PIStringAscii("j1" )) return bfJ1;
|
||||
if (name == PIStringAscii("jn" )) return bfJN;
|
||||
if (name == PIStringAscii("y0" )) return bfY0;
|
||||
if (name == PIStringAscii("y1" )) return bfY1;
|
||||
if (name == PIStringAscii("yn" )) return bfYN;
|
||||
if (name == PIStringAscii("min" )) return bfMin;
|
||||
if (name == PIStringAscii("max" )) return bfMax;
|
||||
if (name == PIStringAscii("clamp" )) return bfClamp;
|
||||
if (name == PIStringAscii("step" )) return bfStep;
|
||||
if (name == PIStringAscii("mix" )) return bfMix;
|
||||
if (name == PIStringAscii("sh")) return bfSh;
|
||||
if (name == PIStringAscii("ch")) return bfCh;
|
||||
if (name == PIStringAscii("th")) return bfTh;
|
||||
if (name == PIStringAscii("cth")) return bfCth;
|
||||
if (name == PIStringAscii("sqrt")) return bfSqrt;
|
||||
if (name == PIStringAscii("sqr")) return bfSqr;
|
||||
if (name == PIStringAscii("pow")) return bfPow;
|
||||
if (name == PIStringAscii("abs")) return bfAbs;
|
||||
if (name == PIStringAscii("ln")) return bfLn;
|
||||
if (name == PIStringAscii("lg")) return bfLg;
|
||||
if (name == PIStringAscii("log")) return bfLog;
|
||||
if (name == PIStringAscii("im")) return bfIm;
|
||||
if (name == PIStringAscii("re")) return bfRe;
|
||||
if (name == PIStringAscii("arg")) return bfArg;
|
||||
if (name == PIStringAscii("len")) return bfLen;
|
||||
if (name == PIStringAscii("conj")) return bfConj;
|
||||
if (name == PIStringAscii("sign")) return bfSign;
|
||||
if (name == PIStringAscii("rad")) return bfRad;
|
||||
if (name == PIStringAscii("deg")) return bfDeg;
|
||||
if (name == PIStringAscii("j0")) return bfJ0;
|
||||
if (name == PIStringAscii("j1")) return bfJ1;
|
||||
if (name == PIStringAscii("jn")) return bfJN;
|
||||
if (name == PIStringAscii("y0")) return bfY0;
|
||||
if (name == PIStringAscii("y1")) return bfY1;
|
||||
if (name == PIStringAscii("yn")) return bfYN;
|
||||
if (name == PIStringAscii("min")) return bfMin;
|
||||
if (name == PIStringAscii("max")) return bfMax;
|
||||
if (name == PIStringAscii("clamp")) return bfClamp;
|
||||
if (name == PIStringAscii("step")) return bfStep;
|
||||
if (name == PIStringAscii("mix")) return bfMix;
|
||||
if (name == PIStringAscii("defined")) return bfDefined;
|
||||
if (name == PIStringAscii("round" )) return bfRound;
|
||||
if (name == PIStringAscii("round")) return bfRound;
|
||||
return bfUnknown;
|
||||
}
|
||||
|
||||
@@ -312,8 +307,10 @@ const PIString & PIEvaluator::prepare(const PIString & string) {
|
||||
replaceOperators();
|
||||
removeSpaces();
|
||||
checkBrackets();
|
||||
while (fillElements()) checkBrackets();
|
||||
while (setSignes()) fillElements();
|
||||
while (fillElements())
|
||||
checkBrackets();
|
||||
while (setSignes())
|
||||
fillElements();
|
||||
removeJunk();
|
||||
findUnknownVariables();
|
||||
return currentString;
|
||||
@@ -352,7 +349,8 @@ void PIEvaluator::removeJunk() {
|
||||
elements.pop_back();
|
||||
junk = true;
|
||||
break;
|
||||
} else break;
|
||||
} else
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -388,8 +386,10 @@ void PIEvaluator::findUnknownVariables() {
|
||||
unknownVars.clear();
|
||||
usedVars.clear();
|
||||
for (int i = 0; i < currentString.length(); i++) {
|
||||
if (elements[i].var_num >= 0) usedVars << content.variable(elements[i].var_num).name;
|
||||
else if (elements[i].var_num == -666) cvar += currentString[i];
|
||||
if (elements[i].var_num >= 0)
|
||||
usedVars << content.variable(elements[i].var_num).name;
|
||||
else if (elements[i].var_num == -666)
|
||||
cvar += currentString[i];
|
||||
else {
|
||||
if (cvar.length() == 0) continue;
|
||||
unknownVars << cvar;
|
||||
@@ -412,7 +412,6 @@ bool PIEvaluator::isSign(const PIChar & ch) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
void PIEvaluator::checkBrackets() {
|
||||
PIString tmps = currentString;
|
||||
PIChar fc, sc;
|
||||
@@ -427,7 +426,8 @@ void PIEvaluator::checkBrackets() {
|
||||
if (bcnt == 0) {
|
||||
currentString.insert(bpos + inserted, '(');
|
||||
inserted++;
|
||||
} else bcnt--;
|
||||
} else
|
||||
bcnt--;
|
||||
}
|
||||
}
|
||||
if (bcnt > 0) currentString += PIString(bcnt, ')');
|
||||
@@ -435,9 +435,8 @@ void PIEvaluator::checkBrackets() {
|
||||
for (int i = 0; i < tmps.length() - 1; i++) {
|
||||
fc = tmps[i].toLower();
|
||||
sc = tmps[i + 1].toLower();
|
||||
if ((fc == ')' && sc == '(') ||
|
||||
(fc == ')' && sc >= '0' && sc <= '9') ||
|
||||
(fc == ')' && sc >= 'a' && sc <= 'z') ) tmps.insert(++i, '*');
|
||||
if ((fc == ')' && sc == '(') || (fc == ')' && sc >= '0' && sc <= '9') || (fc == ')' && sc >= 'a' && sc <= 'z'))
|
||||
tmps.insert(++i, '*');
|
||||
}
|
||||
currentString = tmps;
|
||||
}
|
||||
@@ -445,20 +444,20 @@ void PIEvaluator::checkBrackets() {
|
||||
|
||||
bool PIEvaluator::fillElements() {
|
||||
int fstart, flen, cnum = 0, cpart = 0, cfunc;
|
||||
PIChar cc, nc, pc, fc = '!';
|
||||
PIChar cc, nc, pc, fc = '!';
|
||||
bool numFound = false;
|
||||
PIString curfind, tmps = currentString;
|
||||
elements.resize(tmps.length());
|
||||
for (uint i = 0; i < elements.size(); i++) {
|
||||
elements[i].type = etVariable;
|
||||
elements[i].type = etVariable;
|
||||
elements[i].var_num = -666;
|
||||
}
|
||||
currentVariables.clear();
|
||||
for (int i = 0; i < content.functionsCount(); i++) {
|
||||
curfind = content.function(i).identifier;
|
||||
cfunc = i;
|
||||
flen = curfind.length();
|
||||
fstart = 0;
|
||||
cfunc = i;
|
||||
flen = curfind.length();
|
||||
fstart = 0;
|
||||
while (fstart >= 0) {
|
||||
fstart = tmps.find(curfind, fstart);
|
||||
if (fstart < 0) break;
|
||||
@@ -479,12 +478,12 @@ bool PIEvaluator::fillElements() {
|
||||
for (int i = 0; i < content.variables.size_s(); ++i) {
|
||||
var_index << PairII(i, content.variables[i].name.length());
|
||||
}
|
||||
var_index.sort([](const PairII & v1, const PairII & v2){return v1.second > v2.second;});
|
||||
var_index.sort([](const PairII & v1, const PairII & v2) { return v1.second > v2.second; });
|
||||
for (int i = 0; i < var_index.size_s(); i++) {
|
||||
int vind = var_index[i].first;
|
||||
curfind = content.variables[vind].name;
|
||||
flen = curfind.length();
|
||||
fstart = 0;
|
||||
curfind = content.variables[vind].name;
|
||||
flen = curfind.length();
|
||||
fstart = 0;
|
||||
while (fstart >= 0) {
|
||||
fstart = tmps.find(curfind, fstart);
|
||||
if (fstart < 0) break;
|
||||
@@ -570,7 +569,7 @@ bool PIEvaluator::fillElements() {
|
||||
}
|
||||
curfind.clear();
|
||||
cnum++;
|
||||
cpart = 0;
|
||||
cpart = 0;
|
||||
numFound = false;
|
||||
}
|
||||
}
|
||||
@@ -584,24 +583,28 @@ bool PIEvaluator::fillElements() {
|
||||
cc = nc = fc;
|
||||
for (int i = 0; i < tmps.length(); i++) {
|
||||
cc = tmps[i];
|
||||
if (i > 0) pc = tmps[i - 1];
|
||||
else pc = fc;
|
||||
if (i < tmps.length() - 1) nc = tmps[i + 1];
|
||||
else nc = fc;
|
||||
if (i > 0)
|
||||
pc = tmps[i - 1];
|
||||
else
|
||||
pc = fc;
|
||||
if (i < tmps.length() - 1)
|
||||
nc = tmps[i + 1];
|
||||
else
|
||||
nc = fc;
|
||||
if (cc == '(' || cc == ')' || cc == ',') {
|
||||
elements[i].set(etOperator, -1);
|
||||
continue;
|
||||
}
|
||||
if (cc == '-' || cc == '+') {
|
||||
elements[i].set(etOperator, -1);
|
||||
if (i < tmps.length() - 1) if (elements[i + 1].type == etVariable ||
|
||||
elements[i + 1].type == etFunction) continue;
|
||||
if (i < tmps.length() - 1)
|
||||
if (elements[i + 1].type == etVariable || elements[i + 1].type == etFunction) continue;
|
||||
if ((pc == '(' || isSign(pc) || i == 0) && i < tmps.length() - 1) {
|
||||
if (elements[i + 1].type != etOperator) {
|
||||
cnum = elements[i + 1].num;
|
||||
elements[i].set(etNumber, cnum);
|
||||
tmps.replace(i, 1, fc);
|
||||
///cout << "found sign " << cc << " :" << cnum - 1 << endl;
|
||||
/// cout << "found sign " << cc << " :" << cnum - 1 << endl;
|
||||
if (cc == '-' && currentVariables.size_s() >= cnum)
|
||||
currentVariables[cnum - 1].value = -currentVariables[cnum - 1].value;
|
||||
continue;
|
||||
@@ -615,12 +618,12 @@ bool PIEvaluator::fillElements() {
|
||||
}
|
||||
/*cout << " ";
|
||||
for (int i = 0; i < elements.size(); i++) {
|
||||
switch (elements[i].type) {
|
||||
case etFunction: cout << "f"; break;
|
||||
case etNumber: cout << "n"; break;
|
||||
case etOperator: cout << "o"; break;
|
||||
case etVariable: cout << "v"; break;
|
||||
}
|
||||
switch (elements[i].type) {
|
||||
case etFunction: cout << "f"; break;
|
||||
case etNumber: cout << "n"; break;
|
||||
case etOperator: cout << "o"; break;
|
||||
case etVariable: cout << "v"; break;
|
||||
}
|
||||
}
|
||||
cout << endl; */
|
||||
return false;
|
||||
@@ -633,22 +636,26 @@ bool PIEvaluator::setSignes() {
|
||||
PIString tmps = currentString;
|
||||
for (int i = 0; i < tmps.length() - 1; i++) {
|
||||
needInsert = 0;
|
||||
ni = i + 1;
|
||||
ni = i + 1;
|
||||
if (i > 0) pi = i - 1;
|
||||
fc = tmps[i].toLower();
|
||||
sc = tmps[ni].toLower();
|
||||
pc = tmps[pi].toLower();
|
||||
if (fc == ',' || sc == ',') continue;
|
||||
if (elements[i].type == etOperator && elements[ni].type == etOperator) continue;
|
||||
if (fc == ')' && (elements[ni].type == etNumber || elements[ni].type == etVariable || elements[ni].type == etFunction)) needInsert = 1;
|
||||
if (fc == ')' && (elements[ni].type == etNumber || elements[ni].type == etVariable || elements[ni].type == etFunction))
|
||||
needInsert = 1;
|
||||
if (sc == '(' && (elements[i].type == etNumber || elements[i].type == etVariable)) needInsert = 1;
|
||||
if (elements[i].type == etNumber && elements[ni].type == etNumber && elements[i].num != elements[ni].num) needInsert = 1;
|
||||
if (elements[i].type == etVariable && elements[ni].type == etVariable && elements[i].num != elements[ni].num) needInsert = 1;
|
||||
if ((elements[i].type == etNumber && elements[ni].type == etVariable) || (elements[i].type == etVariable && elements[ni].type == etNumber)) needInsert = 1;
|
||||
if ((elements[i].type == etNumber && elements[ni].type == etVariable) ||
|
||||
(elements[i].type == etVariable && elements[ni].type == etNumber))
|
||||
needInsert = 1;
|
||||
if ((elements[i].type == etNumber || elements[i].type == etVariable) && elements[ni].type == etFunction) needInsert = 1;
|
||||
if (elements[i].type == etFunction && elements[ni].type == etFunction && elements[i].num != elements[ni].num) needInsert = 2;
|
||||
if (elements[i].type == etFunction && elements[ni].type != etFunction && sc != '(') needInsert = 2;
|
||||
if (elements[pi].type == etOperator && (elements[ni].type == etFunction || elements[ni].type == etVariable) && fc == '-') needInsert = 3;
|
||||
if (elements[pi].type == etOperator && (elements[ni].type == etFunction || elements[ni].type == etVariable) && fc == '-')
|
||||
needInsert = 3;
|
||||
switch (needInsert) {
|
||||
case 1:
|
||||
currentString.insert(ni + inserted, '*');
|
||||
@@ -677,7 +684,8 @@ void PIEvaluator::convert() {
|
||||
j++;
|
||||
}
|
||||
currentString.replace(i, j - i, ' ');
|
||||
for (int k = i + 1; k < j; k++) elements.remove(i);
|
||||
for (int k = i + 1; k < j; k++)
|
||||
elements.remove(i);
|
||||
}
|
||||
for (int i = 0; i < currentString.length(); i++) {
|
||||
pe = elements[i];
|
||||
@@ -689,7 +697,8 @@ void PIEvaluator::convert() {
|
||||
j++;
|
||||
}
|
||||
currentString.replace(i, j - i, ' ');
|
||||
for (int k = i + 1; k < j; k++) elements.remove(i);
|
||||
for (int k = i + 1; k < j; k++)
|
||||
elements.remove(i);
|
||||
}
|
||||
for (int i = 0; i < currentString.length(); i++) {
|
||||
pe = elements[i];
|
||||
@@ -701,22 +710,22 @@ void PIEvaluator::convert() {
|
||||
j++;
|
||||
}
|
||||
currentString.replace(i, j - i, ' ');
|
||||
for (int k = i + 1; k < j; k++) elements.remove(i);
|
||||
for (int k = i + 1; k < j; k++)
|
||||
elements.remove(i);
|
||||
}
|
||||
/*cout << " ";
|
||||
for (int i = 0; i < elements.size(); i++) {
|
||||
switch (elements[i].type) {
|
||||
case etFunction: cout << "f"; break;
|
||||
case etNumber: cout << "n"; break;
|
||||
case etOperator: cout << "o"; break;
|
||||
case etVariable: cout << "v"; break;
|
||||
}
|
||||
switch (elements[i].type) {
|
||||
case etFunction: cout << "f"; break;
|
||||
case etNumber: cout << "n"; break;
|
||||
case etOperator: cout << "o"; break;
|
||||
case etVariable: cout << "v"; break;
|
||||
}
|
||||
}
|
||||
cout << endl; */
|
||||
}
|
||||
|
||||
|
||||
|
||||
PIString PIEvaluator::preprocess(const PIString & string) {
|
||||
PIString ret;
|
||||
int lind;
|
||||
@@ -724,24 +733,24 @@ PIString PIEvaluator::preprocess(const PIString & string) {
|
||||
convert();
|
||||
instructions.clear();
|
||||
variables = currentVariables;
|
||||
lind = parse(currentString);
|
||||
lind = parse(currentString);
|
||||
if (instructions.size() == 0) {
|
||||
variables.push_back(Variable());
|
||||
instructions.push_back(Instruction(oNone, PIVector<short>(1, lind), -variables.size_s()));
|
||||
}
|
||||
/*cout << endl << "variables:" << endl;
|
||||
for (int i = 0; i < variables.size(); i++)
|
||||
cout << i << " value = " << variables[i].value << endl;
|
||||
cout << i << " value = " << variables[i].value << endl;
|
||||
|
||||
cout << endl << "instructions:" << endl;
|
||||
for (int i = 0; i < instructions.size(); i++) {
|
||||
cout << i << endl;
|
||||
cout << " operation " << instructions[i].operation << endl;
|
||||
cout << " operators: ";
|
||||
for (int j = 0; j < instructions[i].operators.size(); j++)
|
||||
cout << instructions[i].operators[j] << "; ";
|
||||
cout << endl << " function " << instructions[i].function << endl;
|
||||
cout << " out " << instructions[i].out << endl;
|
||||
cout << i << endl;
|
||||
cout << " operation " << instructions[i].operation << endl;
|
||||
cout << " operators: ";
|
||||
for (int j = 0; j < instructions[i].operators.size(); j++)
|
||||
cout << instructions[i].operators[j] << "; ";
|
||||
cout << endl << " function " << instructions[i].function << endl;
|
||||
cout << " out " << instructions[i].out << endl;
|
||||
} */
|
||||
makeOutput(ret);
|
||||
return ret;
|
||||
@@ -757,17 +766,13 @@ int PIEvaluator::parse(const PIString & string, int offset) {
|
||||
PIVector<short> args, atmp;
|
||||
PIVector<Operation> opers;
|
||||
|
||||
//cout << " "; for (int i = 0; i < slen; i++) cout << preproc->elements[i + offset].type; cout << endl;
|
||||
// cout << " "; for (int i = 0; i < slen; i++) cout << preproc->elements[i + offset].type; cout << endl;
|
||||
for (int i = 0; i < slen; i++) {
|
||||
ce = elements[i + offset];
|
||||
cc = string[i];
|
||||
switch (ce.type) {
|
||||
case etNumber:
|
||||
args.push_back(ce.var_num);
|
||||
continue;
|
||||
case etVariable:
|
||||
args.push_back(ce.var_num);
|
||||
continue;
|
||||
case etNumber: args.push_back(ce.var_num); continue;
|
||||
case etVariable: args.push_back(ce.var_num); continue;
|
||||
case etFunction:
|
||||
i++;
|
||||
cfunc = content.function(ce.var_num);
|
||||
@@ -782,21 +787,23 @@ int PIEvaluator::parse(const PIString & string, int offset) {
|
||||
case ')':
|
||||
bcnt--;
|
||||
if (bcnt == 0) {
|
||||
///qDebug() << "arument: " << carg;
|
||||
/// qDebug() << "arument: " << carg;
|
||||
atmp.push_back(parse(carg, k + offset - carg.length()));
|
||||
k++;
|
||||
carg.clear();
|
||||
if (atmp.size_s() > 0) if (atmp.back() < 0 && farg > 0) farg = atmp.back();
|
||||
if (atmp.size_s() > 0)
|
||||
if (atmp.back() < 0 && farg > 0) farg = atmp.back();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
case ',':
|
||||
if (bcnt == 1) {
|
||||
///qDebug() << "arument: " << carg;
|
||||
/// qDebug() << "arument: " << carg;
|
||||
atmp.push_back(parse(carg, k + offset - carg.length()));
|
||||
k++;
|
||||
carg.clear();
|
||||
if (atmp.size_s() > 0) if (atmp.back() < 0 && farg > 0) farg = atmp.back();
|
||||
if (atmp.size_s() > 0)
|
||||
if (atmp.back() < 0 && farg > 0) farg = atmp.back();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
@@ -811,7 +818,7 @@ int PIEvaluator::parse(const PIString & string, int offset) {
|
||||
}
|
||||
instructions.push_back(Instruction(oFunction, atmp, farg, ce.var_num));
|
||||
args.push_back(farg);
|
||||
//for (int i = 0; i < args.size_s(); i++) cout << preproc->currentVariables[-args[i]].value << endl;
|
||||
// for (int i = 0; i < args.size_s(); i++) cout << preproc->currentVariables[-args[i]].value << endl;
|
||||
continue;
|
||||
case etOperator:
|
||||
if (cc == '(') {
|
||||
@@ -820,20 +827,62 @@ int PIEvaluator::parse(const PIString & string, int offset) {
|
||||
i += sbrackets.length() + 1;
|
||||
continue;
|
||||
}
|
||||
if (cc == '+') {opers.push_back(oAdd); continue;}
|
||||
if (cc == '-') {opers.push_back(oSubtract); continue;}
|
||||
if (cc == '*') {opers.push_back(oMultiply); continue;}
|
||||
if (cc == '/') {opers.push_back(oDivide); continue;}
|
||||
if (cc == '%') {opers.push_back(oResidue); continue;}
|
||||
if (cc == '^') {opers.push_back(oPower); continue;}
|
||||
if (cc == '=') {opers.push_back(oEqual); continue;}
|
||||
if (cc == ':') {opers.push_back(oNotEqual); continue;}
|
||||
if (cc == '}') {opers.push_back(oGreaterEqual); continue;}
|
||||
if (cc == '{') {opers.push_back(oSmallerEqual); continue;}
|
||||
if (cc == '>') {opers.push_back(oGreater); continue;}
|
||||
if (cc == '<') {opers.push_back(oSmaller); continue;}
|
||||
if (cc == '&') {opers.push_back(oAnd); continue;}
|
||||
if (cc == '|') {opers.push_back(oOr); continue;}
|
||||
if (cc == '+') {
|
||||
opers.push_back(oAdd);
|
||||
continue;
|
||||
}
|
||||
if (cc == '-') {
|
||||
opers.push_back(oSubtract);
|
||||
continue;
|
||||
}
|
||||
if (cc == '*') {
|
||||
opers.push_back(oMultiply);
|
||||
continue;
|
||||
}
|
||||
if (cc == '/') {
|
||||
opers.push_back(oDivide);
|
||||
continue;
|
||||
}
|
||||
if (cc == '%') {
|
||||
opers.push_back(oResidue);
|
||||
continue;
|
||||
}
|
||||
if (cc == '^') {
|
||||
opers.push_back(oPower);
|
||||
continue;
|
||||
}
|
||||
if (cc == '=') {
|
||||
opers.push_back(oEqual);
|
||||
continue;
|
||||
}
|
||||
if (cc == ':') {
|
||||
opers.push_back(oNotEqual);
|
||||
continue;
|
||||
}
|
||||
if (cc == '}') {
|
||||
opers.push_back(oGreaterEqual);
|
||||
continue;
|
||||
}
|
||||
if (cc == '{') {
|
||||
opers.push_back(oSmallerEqual);
|
||||
continue;
|
||||
}
|
||||
if (cc == '>') {
|
||||
opers.push_back(oGreater);
|
||||
continue;
|
||||
}
|
||||
if (cc == '<') {
|
||||
opers.push_back(oSmaller);
|
||||
continue;
|
||||
}
|
||||
if (cc == '&') {
|
||||
opers.push_back(oAnd);
|
||||
continue;
|
||||
}
|
||||
if (cc == '|') {
|
||||
opers.push_back(oOr);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
/*cout << "stack: " << endl << "args: ";
|
||||
@@ -841,26 +890,34 @@ int PIEvaluator::parse(const PIString & string, int offset) {
|
||||
cout << endl << "opers: ";
|
||||
for (int i = 0; i < opers.size_s(); i++) cout << opers[i] << ", "; */
|
||||
if (opers.size_s() == 0) {
|
||||
if (args.size_s() > 0) return args.back();
|
||||
else return -666;
|
||||
if (args.size_s() > 0)
|
||||
return args.back();
|
||||
else
|
||||
return -666;
|
||||
}
|
||||
|
||||
int oprior = -1;
|
||||
PIVector<Operation> opv;
|
||||
while(1) {
|
||||
while (1) {
|
||||
operationsByPriority(++oprior, opv);
|
||||
if (opv.isEmpty()) break;
|
||||
for (int j = 0; j < opers.size_s(); j++) {
|
||||
if (!opv.contains(opers[j])) continue;
|
||||
atmp.clear();
|
||||
if (j < args.size_s() && j >= 0) atmp.push_back(args[j]);
|
||||
else atmp.push_back(-666);
|
||||
if (j + 1 < args.size_s() && j >= -1) atmp.push_back(args[j + 1]);
|
||||
else atmp.push_back(-666);
|
||||
if (j < args.size_s() && j >= 0)
|
||||
atmp.push_back(args[j]);
|
||||
else
|
||||
atmp.push_back(-666);
|
||||
if (j + 1 < args.size_s() && j >= -1)
|
||||
atmp.push_back(args[j + 1]);
|
||||
else
|
||||
atmp.push_back(-666);
|
||||
farg = 1;
|
||||
if (atmp[0] < 0) farg = atmp[0];
|
||||
if (atmp[0] < 0)
|
||||
farg = atmp[0];
|
||||
else {
|
||||
if (atmp[1] < 0) farg = atmp[1];
|
||||
if (atmp[1] < 0)
|
||||
farg = atmp[1];
|
||||
else {
|
||||
variables.push_back(Variable());
|
||||
farg = -variables.size_s();
|
||||
@@ -876,7 +933,7 @@ int PIEvaluator::parse(const PIString & string, int offset) {
|
||||
}
|
||||
}
|
||||
return instructions.back().out;
|
||||
//cout << endl;
|
||||
// cout << endl;
|
||||
}
|
||||
|
||||
|
||||
@@ -889,13 +946,13 @@ bool PIEvaluator::check() {
|
||||
}
|
||||
for (int i = 0; i < instructions.size_s(); i++) {
|
||||
error = false;
|
||||
ci = instructions[i];
|
||||
ci = instructions[i];
|
||||
Function cf;
|
||||
int fac, gac;
|
||||
switch (ci.operation) {
|
||||
case oNone: break;
|
||||
case oFunction:
|
||||
cf = content.function(ci.function);
|
||||
cf = content.function(ci.function);
|
||||
fac = cf.arguments;
|
||||
gac = ci.operators.size_s();
|
||||
for (int j = 0; j < ci.operators.size_s(); j++) {
|
||||
@@ -906,9 +963,8 @@ bool PIEvaluator::check() {
|
||||
}
|
||||
if (fac > 0) {
|
||||
if (gac != fac) {
|
||||
lastError = PIStringAscii("Invalid arguments count for function \"") + cf.identifier +
|
||||
PIStringAscii("\", expected ") + PIString::fromNumber(fac) + PIStringAscii(" but ") +
|
||||
PIString::fromNumber(gac) + PIStringAscii(" given");
|
||||
lastError = PIStringAscii("Invalid arguments count for function \"") + cf.identifier + PIStringAscii("\", expected ") +
|
||||
PIString::fromNumber(fac) + PIStringAscii(" but ") + PIString::fromNumber(gac) + PIStringAscii(" given");
|
||||
return false;
|
||||
}
|
||||
if (error) {
|
||||
@@ -919,8 +975,8 @@ bool PIEvaluator::check() {
|
||||
if (fac < 0) {
|
||||
if (gac < -fac) {
|
||||
lastError = PIStringAscii("Invalid arguments count for function \"") + cf.identifier +
|
||||
PIStringAscii("\", expected at least ") + PIString::fromNumber(-fac) + PIStringAscii(" but ") +
|
||||
PIString::fromNumber(gac) + PIStringAscii(" given");
|
||||
PIStringAscii("\", expected at least ") + PIString::fromNumber(-fac) + PIStringAscii(" but ") +
|
||||
PIString::fromNumber(gac) + PIStringAscii(" given");
|
||||
return false;
|
||||
}
|
||||
if (error) {
|
||||
@@ -969,20 +1025,20 @@ PIString PIEvaluator::inBrackets(const PIString & string) {
|
||||
|
||||
PIString PIEvaluator::operationChar(const Operation & operation) {
|
||||
switch (operation) {
|
||||
case oAdd : return PIStringAscii("+");
|
||||
case oSubtract : return PIStringAscii("-");
|
||||
case oMultiply : return PIStringAscii("*");
|
||||
case oDivide : return PIStringAscii("/");
|
||||
case oPower : return PIStringAscii("^");
|
||||
case oResidue : return PIStringAscii("%");
|
||||
case oEqual : return PIStringAscii("=");
|
||||
case oNotEqual : return PIString::fromUTF8("≠");
|
||||
case oAdd: return PIStringAscii("+");
|
||||
case oSubtract: return PIStringAscii("-");
|
||||
case oMultiply: return PIStringAscii("*");
|
||||
case oDivide: return PIStringAscii("/");
|
||||
case oPower: return PIStringAscii("^");
|
||||
case oResidue: return PIStringAscii("%");
|
||||
case oEqual: return PIStringAscii("=");
|
||||
case oNotEqual: return PIString::fromUTF8("≠");
|
||||
case oGreaterEqual: return PIString::fromUTF8("≥");
|
||||
case oSmallerEqual: return PIString::fromUTF8("≤");
|
||||
case oGreater : return PIStringAscii(">");
|
||||
case oSmaller : return PIStringAscii("<");
|
||||
case oAnd : return PIString::fromUTF8("⋀");
|
||||
case oOr : return PIString::fromUTF8("⋁");
|
||||
case oGreater: return PIStringAscii(">");
|
||||
case oSmaller: return PIStringAscii("<");
|
||||
case oAnd: return PIString::fromUTF8("⋀");
|
||||
case oOr: return PIString::fromUTF8("⋁");
|
||||
default: break;
|
||||
}
|
||||
return PIStringAscii("???");
|
||||
@@ -1016,119 +1072,63 @@ inline void PIEvaluator::execFunction(const Instruction & ci) {
|
||||
int oi = -ci.out - 1;
|
||||
complexd tmp, stmp, ttmp;
|
||||
switch (cfunc.type) {
|
||||
case bfSin:
|
||||
tmpvars[oi].value = sin(value(ci.operators[0]));
|
||||
break;
|
||||
case bfCos:
|
||||
tmpvars[oi].value = cos(value(ci.operators[0]));
|
||||
break;
|
||||
case bfTg:
|
||||
tmpvars[oi].value = tan(value(ci.operators[0]));
|
||||
break;
|
||||
case bfSin: tmpvars[oi].value = sin(value(ci.operators[0])); break;
|
||||
case bfCos: tmpvars[oi].value = cos(value(ci.operators[0])); break;
|
||||
case bfTg: tmpvars[oi].value = tan(value(ci.operators[0])); break;
|
||||
case bfCtg:
|
||||
tmp = tan(value(ci.operators[0]));
|
||||
if (tmp == complexd_0) tmpvars[oi].value = 0.;
|
||||
else tmpvars[oi].value = complexd_1 / tmp;
|
||||
break;
|
||||
case bfArcsin:
|
||||
tmpvars[oi].value = asinc(value(ci.operators[0]));
|
||||
break;
|
||||
case bfArccos:
|
||||
tmpvars[oi].value = acosc(value(ci.operators[0]));
|
||||
break;
|
||||
case bfArctg:
|
||||
tmpvars[oi].value = atanc(value(ci.operators[0]));
|
||||
break;
|
||||
case bfArcctg:
|
||||
tmpvars[oi].value = atanc(-value(ci.operators[0])) + M_PI_2;
|
||||
break;
|
||||
case bfSh:
|
||||
tmpvars[oi].value = sinh(value(ci.operators[0]));
|
||||
break;
|
||||
case bfCh:
|
||||
tmpvars[oi].value = cosh(value(ci.operators[0]));
|
||||
break;
|
||||
case bfTh:
|
||||
tmpvars[oi].value = tanh(value(ci.operators[0]));
|
||||
if (tmp == complexd_0)
|
||||
tmpvars[oi].value = 0.;
|
||||
else
|
||||
tmpvars[oi].value = complexd_1 / tmp;
|
||||
break;
|
||||
case bfArcsin: tmpvars[oi].value = asinc(value(ci.operators[0])); break;
|
||||
case bfArccos: tmpvars[oi].value = acosc(value(ci.operators[0])); break;
|
||||
case bfArctg: tmpvars[oi].value = atanc(value(ci.operators[0])); break;
|
||||
case bfArcctg: tmpvars[oi].value = atanc(-value(ci.operators[0])) + M_PI_2; break;
|
||||
case bfSh: tmpvars[oi].value = sinh(value(ci.operators[0])); break;
|
||||
case bfCh: tmpvars[oi].value = cosh(value(ci.operators[0])); break;
|
||||
case bfTh: tmpvars[oi].value = tanh(value(ci.operators[0])); break;
|
||||
case bfCth:
|
||||
tmp = tanh(value(ci.operators[0]));
|
||||
if (tmp == complexd_0) tmpvars[oi].value = 0.;
|
||||
else tmpvars[oi].value = complexd_1 / tmp;
|
||||
break;
|
||||
case bfAbs:
|
||||
tmpvars[oi].value = abs(value(ci.operators[0]));
|
||||
break;
|
||||
case bfSqrt:
|
||||
tmpvars[oi].value = sqrt(value(ci.operators[0]));
|
||||
break;
|
||||
case bfSqr:
|
||||
tmpvars[oi].value = value(ci.operators[0]) * value(ci.operators[0]);
|
||||
break;
|
||||
case bfExp:
|
||||
tmpvars[oi].value = exp(value(ci.operators[0]));
|
||||
break;
|
||||
case bfPow:
|
||||
tmpvars[oi].value = pow(value(ci.operators[0]), value(ci.operators[1]));
|
||||
break;
|
||||
case bfLn:
|
||||
tmpvars[oi].value = log(value(ci.operators[0]));
|
||||
break;
|
||||
case bfLg:
|
||||
tmpvars[oi].value = log10(value(ci.operators[0]));
|
||||
if (tmp == complexd_0)
|
||||
tmpvars[oi].value = 0.;
|
||||
else
|
||||
tmpvars[oi].value = complexd_1 / tmp;
|
||||
break;
|
||||
case bfAbs: tmpvars[oi].value = abs(value(ci.operators[0])); break;
|
||||
case bfSqrt: tmpvars[oi].value = sqrt(value(ci.operators[0])); break;
|
||||
case bfSqr: tmpvars[oi].value = value(ci.operators[0]) * value(ci.operators[0]); break;
|
||||
case bfExp: tmpvars[oi].value = exp(value(ci.operators[0])); break;
|
||||
case bfPow: tmpvars[oi].value = pow(value(ci.operators[0]), value(ci.operators[1])); break;
|
||||
case bfLn: tmpvars[oi].value = log(value(ci.operators[0])); break;
|
||||
case bfLg: tmpvars[oi].value = log10(value(ci.operators[0])); break;
|
||||
case bfLog:
|
||||
tmp = log(value(ci.operators[1]));
|
||||
if (tmp == complexd_0) tmpvars[oi].value = 0.;
|
||||
else tmpvars[oi].value = log(value(ci.operators[0])) / tmp;
|
||||
break;
|
||||
case bfRe:
|
||||
tmpvars[oi].value = value(ci.operators[0]).real();
|
||||
break;
|
||||
case bfIm:
|
||||
tmpvars[oi].value = value(ci.operators[0]).imag();
|
||||
break;
|
||||
case bfArg:
|
||||
tmpvars[oi].value = arg(value(ci.operators[0]));
|
||||
break;
|
||||
case bfLen:
|
||||
tmpvars[oi].value = abs(value(ci.operators[0]));
|
||||
break;
|
||||
case bfConj:
|
||||
tmpvars[oi].value = conj(value(ci.operators[0]));
|
||||
break;
|
||||
case bfSign:
|
||||
tmpvars[oi].value = value(ci.operators[0]).real() >= 0. ? complexd_1 : -complexd_1;
|
||||
break;
|
||||
case bfRad:
|
||||
tmpvars[oi].value = value(ci.operators[0]) * complexd(deg2rad, 0.);
|
||||
break;
|
||||
case bfDeg:
|
||||
tmpvars[oi].value = value(ci.operators[0]) * complexd(rad2deg, 0.);
|
||||
break;
|
||||
case bfJ0:
|
||||
tmpvars[oi].value = piJ0(value(ci.operators[0]).real());
|
||||
break;
|
||||
case bfJ1:
|
||||
tmpvars[oi].value = piJ1(value(ci.operators[0]).real());
|
||||
break;
|
||||
case bfJN:
|
||||
tmpvars[oi].value = piJn(piRoundd(value(ci.operators[1]).real()), value(ci.operators[0]).real());
|
||||
break;
|
||||
case bfY0:
|
||||
tmpvars[oi].value = piY0(value(ci.operators[0]).real());
|
||||
break;
|
||||
case bfY1:
|
||||
tmpvars[oi].value = piY1(value(ci.operators[0]).real());
|
||||
break;
|
||||
case bfYN:
|
||||
tmpvars[oi].value = piYn(piRoundd(value(ci.operators[1]).real()), value(ci.operators[0]).real());
|
||||
if (tmp == complexd_0)
|
||||
tmpvars[oi].value = 0.;
|
||||
else
|
||||
tmpvars[oi].value = log(value(ci.operators[0])) / tmp;
|
||||
break;
|
||||
case bfRe: tmpvars[oi].value = value(ci.operators[0]).real(); break;
|
||||
case bfIm: tmpvars[oi].value = value(ci.operators[0]).imag(); break;
|
||||
case bfArg: tmpvars[oi].value = arg(value(ci.operators[0])); break;
|
||||
case bfLen: tmpvars[oi].value = abs(value(ci.operators[0])); break;
|
||||
case bfConj: tmpvars[oi].value = conj(value(ci.operators[0])); break;
|
||||
case bfSign: tmpvars[oi].value = value(ci.operators[0]).real() >= 0. ? complexd_1 : -complexd_1; break;
|
||||
case bfRad: tmpvars[oi].value = value(ci.operators[0]) * complexd(deg2rad, 0.); break;
|
||||
case bfDeg: tmpvars[oi].value = value(ci.operators[0]) * complexd(rad2deg, 0.); break;
|
||||
case bfJ0: tmpvars[oi].value = piJ0(value(ci.operators[0]).real()); break;
|
||||
case bfJ1: tmpvars[oi].value = piJ1(value(ci.operators[0]).real()); break;
|
||||
case bfJN: tmpvars[oi].value = piJn(piRoundd(value(ci.operators[1]).real()), value(ci.operators[0]).real()); break;
|
||||
case bfY0: tmpvars[oi].value = piY0(value(ci.operators[0]).real()); break;
|
||||
case bfY1: tmpvars[oi].value = piY1(value(ci.operators[0]).real()); break;
|
||||
case bfYN: tmpvars[oi].value = piYn(piRoundd(value(ci.operators[1]).real()), value(ci.operators[0]).real()); break;
|
||||
case bfMin:
|
||||
tmp = value(ci.operators[0]);
|
||||
for (int i = 1; i < ci.operators.size_s(); ++i) {
|
||||
stmp = value(ci.operators[i]);
|
||||
tmp = complexd(piMind(tmp.real(), stmp.real()), piMind(tmp.imag(), stmp.imag()));
|
||||
tmp = complexd(piMind(tmp.real(), stmp.real()), piMind(tmp.imag(), stmp.imag()));
|
||||
}
|
||||
tmpvars[oi].value = tmp;
|
||||
break;
|
||||
@@ -1136,37 +1136,29 @@ inline void PIEvaluator::execFunction(const Instruction & ci) {
|
||||
tmp = value(ci.operators[0]);
|
||||
for (int i = 1; i < ci.operators.size_s(); ++i) {
|
||||
stmp = value(ci.operators[i]);
|
||||
tmp = complexd(piMaxd(tmp.real(), stmp.real()), piMaxd(tmp.imag(), stmp.imag()));
|
||||
tmp = complexd(piMaxd(tmp.real(), stmp.real()), piMaxd(tmp.imag(), stmp.imag()));
|
||||
}
|
||||
tmpvars[oi].value = tmp;
|
||||
break;
|
||||
case bfClamp:
|
||||
tmp = value(ci.operators[0]);
|
||||
stmp = value(ci.operators[1]);
|
||||
ttmp = value(ci.operators[2]);
|
||||
tmp = value(ci.operators[0]);
|
||||
stmp = value(ci.operators[1]);
|
||||
ttmp = value(ci.operators[2]);
|
||||
tmpvars[oi].value = complexd(piClampd(tmp.real(), stmp.real(), ttmp.real()), piClampd(tmp.imag(), stmp.imag(), ttmp.imag()));
|
||||
break;
|
||||
case bfStep:
|
||||
tmpvars[oi].value = complexd(value(ci.operators[0]).real() >= value(ci.operators[1]).real() ? complexld_1 : complexld_0);
|
||||
break;
|
||||
case bfMix:
|
||||
tmp = value(ci.operators[0]);
|
||||
stmp = value(ci.operators[1]);
|
||||
ttmp = value(ci.operators[2]);
|
||||
tmp = value(ci.operators[0]);
|
||||
stmp = value(ci.operators[1]);
|
||||
ttmp = value(ci.operators[2]);
|
||||
tmpvars[oi].value = stmp.real() * (1. - tmp.real()) + ttmp.real() * tmp.real();
|
||||
break;
|
||||
case bfDefined:
|
||||
tmpvars[oi].value = value(ci.operators[0]).real() > 0. ? complexd_1 : complexd_0;
|
||||
break;
|
||||
case bfRandom:
|
||||
tmpvars[oi].value = value(ci.operators[0]) + randomd() * value(ci.operators[1]);
|
||||
break;
|
||||
case bfRandomn:
|
||||
tmpvars[oi].value = randomn(value(ci.operators[0]).real(), value(ci.operators[1]).real());
|
||||
break;
|
||||
case bfRound:
|
||||
tmpvars[oi].value = piRoundd(value(ci.operators[0]).real());
|
||||
break;
|
||||
case bfDefined: tmpvars[oi].value = value(ci.operators[0]).real() > 0. ? complexd_1 : complexd_0; break;
|
||||
case bfRandom: tmpvars[oi].value = value(ci.operators[0]) + randomd() * value(ci.operators[1]); break;
|
||||
case bfRandomn: tmpvars[oi].value = randomn(value(ci.operators[0]).real(), value(ci.operators[1]).real()); break;
|
||||
case bfRound: tmpvars[oi].value = piRoundd(value(ci.operators[0]).real()); break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
@@ -1175,74 +1167,45 @@ inline void PIEvaluator::execFunction(const Instruction & ci) {
|
||||
inline bool PIEvaluator::execInstructions() {
|
||||
int oi;
|
||||
tmpvars = variables;
|
||||
//cout << "var count " << tmpvars.size_s() << endl;
|
||||
// cout << "var count " << tmpvars.size_s() << endl;
|
||||
for (int i = 0; i < instructions.size_s(); i++) {
|
||||
const Instruction & ci(instructions[i]);
|
||||
oi = -ci.out - 1;
|
||||
//cout << value(ci.operators[0]) << operationChar(ci.operation) << value(ci.operators[1]) << ", " << oi << endl;
|
||||
// cout << value(ci.operators[0]) << operationChar(ci.operation) << value(ci.operators[1]) << ", " << oi << endl;
|
||||
switch (ci.operation) {
|
||||
case oAdd:
|
||||
tmpvars[oi].value = value(ci.operators[0]) + value(ci.operators[1]);
|
||||
break;
|
||||
case oSubtract:
|
||||
tmpvars[oi].value = value(ci.operators[0]) - value(ci.operators[1]);
|
||||
break;
|
||||
case oMultiply:
|
||||
tmpvars[oi].value = value(ci.operators[0]) * value(ci.operators[1]);
|
||||
break;
|
||||
case oAdd: tmpvars[oi].value = value(ci.operators[0]) + value(ci.operators[1]); break;
|
||||
case oSubtract: tmpvars[oi].value = value(ci.operators[0]) - value(ci.operators[1]); break;
|
||||
case oMultiply: tmpvars[oi].value = value(ci.operators[0]) * value(ci.operators[1]); break;
|
||||
case oDivide: {
|
||||
complexd tmp = value(ci.operators[1]);
|
||||
if (tmp == complexd(0., 0.)) tmpvars[oi].value = 0.;
|
||||
else tmpvars[oi].value = value(ci.operators[0]) / tmp;
|
||||
} break;
|
||||
case oResidue:
|
||||
tmpvars[oi].value = residue(value(ci.operators[0]), value(ci.operators[1]));
|
||||
break;
|
||||
case oPower:
|
||||
tmpvars[oi].value = pow(value(ci.operators[0]), value(ci.operators[1]));
|
||||
break;
|
||||
case oEqual:
|
||||
tmpvars[oi].value = value(ci.operators[0]) == value(ci.operators[1]);
|
||||
break;
|
||||
case oNotEqual:
|
||||
tmpvars[oi].value = value(ci.operators[0]) != value(ci.operators[1]);
|
||||
break;
|
||||
case oGreaterEqual:
|
||||
tmpvars[oi].value = value(ci.operators[0]).real() >= value(ci.operators[1]).real();
|
||||
break;
|
||||
case oSmallerEqual:
|
||||
tmpvars[oi].value = value(ci.operators[0]).real() <= value(ci.operators[1]).real();
|
||||
break;
|
||||
case oGreater:
|
||||
tmpvars[oi].value = value(ci.operators[0]).real() > value(ci.operators[1]).real();
|
||||
break;
|
||||
case oSmaller:
|
||||
tmpvars[oi].value = value(ci.operators[0]).real() < value(ci.operators[1]).real();
|
||||
break;
|
||||
case oAnd:
|
||||
tmpvars[oi].value = value(ci.operators[0]).real() > 0. && value(ci.operators[1]).real() > 0.;
|
||||
break;
|
||||
case oOr:
|
||||
tmpvars[oi].value = value(ci.operators[0]).real() > 0. || value(ci.operators[1]).real() > 0.;
|
||||
break;
|
||||
case oFunction:
|
||||
execFunction(ci);
|
||||
break;
|
||||
case oNone:
|
||||
tmpvars[oi].value = value(ci.operators[0]);
|
||||
break;
|
||||
if (tmp == complexd(0., 0.))
|
||||
tmpvars[oi].value = 0.;
|
||||
else
|
||||
tmpvars[oi].value = value(ci.operators[0]) / tmp;
|
||||
} break;
|
||||
case oResidue: tmpvars[oi].value = residue(value(ci.operators[0]), value(ci.operators[1])); break;
|
||||
case oPower: tmpvars[oi].value = pow(value(ci.operators[0]), value(ci.operators[1])); break;
|
||||
case oEqual: tmpvars[oi].value = value(ci.operators[0]) == value(ci.operators[1]); break;
|
||||
case oNotEqual: tmpvars[oi].value = value(ci.operators[0]) != value(ci.operators[1]); break;
|
||||
case oGreaterEqual: tmpvars[oi].value = value(ci.operators[0]).real() >= value(ci.operators[1]).real(); break;
|
||||
case oSmallerEqual: tmpvars[oi].value = value(ci.operators[0]).real() <= value(ci.operators[1]).real(); break;
|
||||
case oGreater: tmpvars[oi].value = value(ci.operators[0]).real() > value(ci.operators[1]).real(); break;
|
||||
case oSmaller: tmpvars[oi].value = value(ci.operators[0]).real() < value(ci.operators[1]).real(); break;
|
||||
case oAnd: tmpvars[oi].value = value(ci.operators[0]).real() > 0. && value(ci.operators[1]).real() > 0.; break;
|
||||
case oOr: tmpvars[oi].value = value(ci.operators[0]).real() > 0. || value(ci.operators[1]).real() > 0.; break;
|
||||
case oFunction: execFunction(ci); break;
|
||||
case oNone: tmpvars[oi].value = value(ci.operators[0]); break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
if (!instructions.isEmpty())
|
||||
out = value(instructions.back().out);
|
||||
if (!instructions.isEmpty()) out = value(instructions.back().out);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool PIEvaluator::check(const PIString & string) {
|
||||
currentString = preprocess(string);
|
||||
correct = check();
|
||||
correct = check();
|
||||
if (!correct) {
|
||||
instructions.clear();
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user