BIG deep clean
This commit is contained in:
@@ -166,8 +166,6 @@ PIEvaluatorContent::PIEvaluatorContent() {
|
||||
addFunction("defined", 1);
|
||||
addFunction("round", 1);
|
||||
clearCustomVariables();
|
||||
//addVariable("n", 0.);
|
||||
//addVariable("x1", 123);
|
||||
}
|
||||
|
||||
|
||||
@@ -195,19 +193,12 @@ void PIEvaluatorContent::clearCustomVariables() {
|
||||
|
||||
|
||||
void PIEvaluatorContent::sortVariables() {
|
||||
//Variable tv;
|
||||
for (uint i = 0; i < variables.size(); i++) {
|
||||
for (uint j = variables.size() - 1; j > i; j--) {
|
||||
if (variables[j].name.length() <= variables[i].name.length()) continue;
|
||||
piSwap<Variable>(variables[i], variables[j]);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* qDebug() << "---";
|
||||
* for (int i = 0; i < variables.size(); i++) {
|
||||
* qDebug() << variables[i].name;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
@@ -258,6 +249,7 @@ BaseFunctions PIEvaluatorContent::getBaseFunction(const PIString & name) {
|
||||
return bfUnknown;
|
||||
}
|
||||
|
||||
|
||||
const PIString & PIEvaluator::prepare(const PIString & string) {
|
||||
currentString = string.trimmed();
|
||||
if (currentString.isEmpty()) currentString = "0";
|
||||
@@ -402,18 +394,15 @@ bool PIEvaluator::fillElements() {
|
||||
elements[i].var_num = -666;
|
||||
}
|
||||
currentVariables.clear();
|
||||
//qDebug().nospace() << "search for functions ...";
|
||||
for (int i = 0; i < content.functionsCount(); i++) {
|
||||
curfind = content.function(i).identifier;
|
||||
cfunc = i; //(int)content.function(i).type;
|
||||
cfunc = i;
|
||||
flen = curfind.length();
|
||||
fstart = 0;
|
||||
while (fstart >= 0) {
|
||||
fstart = tmps.find(curfind, fstart);
|
||||
if (fstart < 0) break;
|
||||
if (tmps[fstart + flen] != '(') {
|
||||
//currentString.insert(fstart + flen, "(");
|
||||
//return true;
|
||||
fstart++;
|
||||
continue;
|
||||
}
|
||||
@@ -425,7 +414,6 @@ bool PIEvaluator::fillElements() {
|
||||
}
|
||||
}
|
||||
cnum = 0;
|
||||
//qDebug().nospace() << "search for variables ...";
|
||||
for (int i = 0; i < content.variablesCount(); i++) {
|
||||
curfind = content.variable(i).name;
|
||||
flen = curfind.length();
|
||||
@@ -442,18 +430,11 @@ bool PIEvaluator::fillElements() {
|
||||
}
|
||||
curfind = "";
|
||||
cnum = 1;
|
||||
//qDebug().nospace() << "search for numbers ...";
|
||||
for (int i = 0; i < tmps.length(); i++) {
|
||||
cc = tmps[i];
|
||||
/*if (cc == " " || cc == "(" || cc == ")") {
|
||||
curfind = "";
|
||||
cpart = 0;
|
||||
numFound = false;
|
||||
continue;
|
||||
}*/
|
||||
switch (cpart) {
|
||||
case 0:
|
||||
if ((cc >= '0' && cc <= '9')) {// || cc == '-' || cc == '+') {
|
||||
if ((cc >= '0' && cc <= '9')) {
|
||||
curfind += cc;
|
||||
cpart = 1;
|
||||
continue;
|
||||
@@ -515,7 +496,6 @@ bool PIEvaluator::fillElements() {
|
||||
break;
|
||||
}
|
||||
if (numFound) {
|
||||
//qDebug().nospace() << "add " << cnum << ": " << curfind << " = " << curfind.toDouble();
|
||||
currentVariables.push_back(Variable("tmp" + PIString::fromNumber(cnum), curfind.toDouble()));
|
||||
for (int j = i - curfind.length(); j < i; j++) {
|
||||
elements[j].set(etNumber, cnum, -cnum);
|
||||
@@ -528,7 +508,6 @@ bool PIEvaluator::fillElements() {
|
||||
}
|
||||
}
|
||||
if (cpart > 0) {
|
||||
//qDebug().nospace() << "add " << cnum << ": " << curfind << " = " << curfind.toDouble();
|
||||
currentVariables.push_back(Variable("tmp" + PIString::fromNumber(cnum), curfind.toDouble()));
|
||||
for (int j = tmps.length() - curfind.length(); j < tmps.length(); j++) {
|
||||
elements[j].set(etNumber, cnum, -cnum);
|
||||
@@ -536,7 +515,6 @@ bool PIEvaluator::fillElements() {
|
||||
}
|
||||
}
|
||||
cc = nc = fc;
|
||||
//qDebug().nospace() << "search for signes ...";
|
||||
for (int i = 0; i < tmps.length(); i++) {
|
||||
cc = tmps[i];
|
||||
if (i > 0) pc = tmps[i - 1];
|
||||
@@ -559,7 +537,6 @@ bool PIEvaluator::fillElements() {
|
||||
///cout << "found sign " << cc << " :" << cnum - 1 << endl;
|
||||
if (cc == '-' && currentVariables.size_s() >= cnum)
|
||||
currentVariables[cnum - 1].value = -currentVariables[cnum - 1].value;
|
||||
//i++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -569,9 +546,7 @@ bool PIEvaluator::fillElements() {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
/*
|
||||
qDebug().nospace() << tmps;
|
||||
cout << " ";
|
||||
/*cout << " ";
|
||||
for (int i = 0; i < elements.size(); i++) {
|
||||
switch (elements[i].type) {
|
||||
case etFunction: cout << "f"; break;
|
||||
@@ -580,10 +555,8 @@ bool PIEvaluator::fillElements() {
|
||||
case etVariable: cout << "v"; break;
|
||||
}
|
||||
}
|
||||
cout << endl;
|
||||
*/
|
||||
cout << endl; */
|
||||
return false;
|
||||
//for (int i = 0; i < currentVariables.size(); i++) qDebug() << "var " << i << ": " << currentVariables[i].value.real();
|
||||
}
|
||||
|
||||
|
||||
@@ -598,7 +571,6 @@ bool PIEvaluator::setSignes() {
|
||||
fc = tmps[i].toLower();
|
||||
sc = tmps[ni].toLower();
|
||||
pc = tmps[pi].toLower();
|
||||
//if (elements[i].type == etOperator || elements[ni].type == etVariable) continue;
|
||||
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;
|
||||
@@ -614,32 +586,13 @@ bool PIEvaluator::setSignes() {
|
||||
case 1:
|
||||
currentString.insert(ni + inserted, "*");
|
||||
elements.insert(ni + inserted, Element(etOperator, -1));
|
||||
//inserted++;
|
||||
//i++;
|
||||
return true;
|
||||
/*case 2:
|
||||
currentString.insert(ni + inserted, ")");
|
||||
currentString.insert(ni + inserted, "(");
|
||||
elements.insert(ni + inserted, Element(etOperator, -1));
|
||||
elements.insert(ni + inserted, Element(etOperator, -1));
|
||||
inserted++;
|
||||
i++;
|
||||
return true;*/
|
||||
case 3:
|
||||
currentString.insert(ni + inserted, "1*");
|
||||
elements.insert(ni + inserted, Element(etOperator, -1));
|
||||
//inserted;
|
||||
//i++;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
/*if (elements[tmps.length() - 1].type == etFunction) {
|
||||
currentString.insert(tmps.length() + inserted, ")");
|
||||
currentString.insert(tmps.length() + inserted, "(");
|
||||
elements.insert(tmps.length() + inserted, Element(etOperator, -1));
|
||||
elements.insert(tmps.length() + inserted, Element(etOperator, -1));
|
||||
return true;
|
||||
}*/
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -658,7 +611,6 @@ void PIEvaluator::convert() {
|
||||
}
|
||||
currentString.replace(i, j - i, " ");
|
||||
for (int k = i + 1; k < j; k++) elements.remove(i);
|
||||
//i++;
|
||||
}
|
||||
for (int i = 0; i < currentString.length(); i++) {
|
||||
pe = elements[i];
|
||||
@@ -671,7 +623,6 @@ void PIEvaluator::convert() {
|
||||
}
|
||||
currentString.replace(i, j - i, " ");
|
||||
for (int k = i + 1; k < j; k++) elements.remove(i);
|
||||
//i++;
|
||||
}
|
||||
for (int i = 0; i < currentString.length(); i++) {
|
||||
pe = elements[i];
|
||||
@@ -684,10 +635,8 @@ void PIEvaluator::convert() {
|
||||
}
|
||||
currentString.replace(i, j - i, " ");
|
||||
for (int k = i + 1; k < j; k++) elements.remove(i);
|
||||
//i++;
|
||||
}
|
||||
/*qDebug().nospace() << currentString;
|
||||
cout << " ";
|
||||
/*cout << " ";
|
||||
for (int i = 0; i < elements.size(); i++) {
|
||||
switch (elements[i].type) {
|
||||
case etFunction: cout << "f"; break;
|
||||
@@ -696,7 +645,7 @@ void PIEvaluator::convert() {
|
||||
case etVariable: cout << "v"; break;
|
||||
}
|
||||
}
|
||||
cout << endl;*/
|
||||
cout << endl; */
|
||||
}
|
||||
|
||||
|
||||
@@ -707,7 +656,6 @@ PIString PIEvaluator::preprocess(const PIString & string) {
|
||||
ret = prepare(string);
|
||||
convert();
|
||||
instructions.clear();
|
||||
//qDebug() << preproc->currentString;
|
||||
variables = currentVariables;
|
||||
lind = parse(currentString);
|
||||
if (instructions.size() == 0) {
|
||||
@@ -715,8 +663,7 @@ PIString PIEvaluator::preprocess(const PIString & string) {
|
||||
instructions.push_back(Instruction(oNone, PIVector<short>(1, lind), -variables.size_s()));
|
||||
}
|
||||
kvars = &(content.variables);
|
||||
/*
|
||||
cout << endl << "variables:" << endl;
|
||||
/*cout << endl << "variables:" << endl;
|
||||
for (int i = 0; i < variables.size(); i++)
|
||||
cout << i << " value = " << variables[i].value << endl;
|
||||
|
||||
@@ -729,8 +676,7 @@ PIString PIEvaluator::preprocess(const PIString & string) {
|
||||
cout << instructions[i].operators[j] << "; ";
|
||||
cout << endl << " function " << instructions[i].function << endl;
|
||||
cout << " out " << instructions[i].out << endl;
|
||||
}
|
||||
*/
|
||||
} */
|
||||
makeOutput(ret);
|
||||
return ret;
|
||||
}
|
||||
@@ -745,9 +691,7 @@ int PIEvaluator::parse(const PIString & string, int offset) {
|
||||
PIVector<short> args, atmp;
|
||||
PIVector<Operation> opers;
|
||||
|
||||
///qDebug() << "to parse :" + string;
|
||||
///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];
|
||||
@@ -761,16 +705,11 @@ int PIEvaluator::parse(const PIString & string, int offset) {
|
||||
case etFunction:
|
||||
i++;
|
||||
cfunc = content.function(ce.var_num);
|
||||
//facnt = cfunc.arguments;
|
||||
atmp.clear();
|
||||
bcnt = farg = 1;
|
||||
///qDebug() << "function: " + cfunc.identifier;
|
||||
//for (int k = 0; k < facnt; k++) {
|
||||
carg = "";
|
||||
k = i + 1;
|
||||
//if (string.size_s() <= k || k < 0) return -666;
|
||||
while (bcnt > 0) {
|
||||
//if (k < facnt - 1) fcomma = string.indexOf(',', j);
|
||||
cc = string[k];
|
||||
switch (cc.toAscii()) {
|
||||
case '(': bcnt++; break;
|
||||
@@ -807,10 +746,8 @@ 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;
|
||||
//i = j + 1;
|
||||
continue;
|
||||
case etOperator:
|
||||
//qDebug() << "operator: " << cc;
|
||||
if (cc == '(') {
|
||||
sbrackets = inBrackets(string.right(slen - i));
|
||||
args.push_back(parse(sbrackets, i + offset + 1));
|
||||
@@ -833,12 +770,10 @@ int PIEvaluator::parse(const PIString & string, int offset) {
|
||||
if (cc == '|') {opers.push_back(oOr); continue;}
|
||||
}
|
||||
}
|
||||
/*
|
||||
cout << "stack: " << endl << "args: ";
|
||||
/*cout << "stack: " << endl << "args: ";
|
||||
for (int i = 0; i < args.size_s(); i++) cout << args[i] << ", ";
|
||||
cout << endl << "opers: ";
|
||||
for (int i = 0; i < opers.size_s(); i++) cout << opers[i] << ", ";
|
||||
*/
|
||||
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;
|
||||
@@ -846,7 +781,7 @@ int PIEvaluator::parse(const PIString & string, int offset) {
|
||||
|
||||
int oprior = -1;
|
||||
PIVector<Operation> opv;
|
||||
for (;;) {
|
||||
while(1) {
|
||||
operationsByPriority(++oprior, opv);
|
||||
if (opv.isEmpty()) break;
|
||||
for (int j = 0; j < opers.size_s(); j++) {
|
||||
@@ -875,7 +810,7 @@ int PIEvaluator::parse(const PIString & string, int offset) {
|
||||
}
|
||||
}
|
||||
return instructions.back().out;
|
||||
///cout << endl;
|
||||
//cout << endl;
|
||||
}
|
||||
|
||||
|
||||
@@ -898,7 +833,7 @@ bool PIEvaluator::check() {
|
||||
fac = cf.arguments;
|
||||
gac = ci.operators.size_s();
|
||||
for (int j = 0; j < ci.operators.size_s(); j++) {
|
||||
if (ci.operators[j] == -666) { //(ci.operators[j] < -variables.size_s() || ci.operators[j] >= kvars->size()) {
|
||||
if (ci.operators[j] == -666) {
|
||||
error = true;
|
||||
gac--;
|
||||
}
|
||||
@@ -1013,7 +948,6 @@ inline void PIEvaluator::execFunction(const Instruction & ci) {
|
||||
const Function & cfunc(content.function(ci.function));
|
||||
int oi = -ci.out - 1;
|
||||
complexd tmp, stmp, ttmp;
|
||||
//qDebug() << "function " << (int)cfunc.type;
|
||||
switch (cfunc.type) {
|
||||
case bfSin:
|
||||
tmpvars[oi].value = sin(value(ci.operators[0]));
|
||||
|
||||
Reference in New Issue
Block a user