From 146863a533ad4e09043bd7a9831ed1efe2313c48 Mon Sep 17 00:00:00 2001 From: Ivan Pelipenko Date: Mon, 7 Sep 2020 00:52:51 +0300 Subject: [PATCH] EvalSpinBox evaluator operators priority backported from PIP --- libs/widgets/qpievaluator_p.cpp | 29 ++++++++++++++++++++++------- libs/widgets/qpievaluator_p.h | 1 + 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/libs/widgets/qpievaluator_p.cpp b/libs/widgets/qpievaluator_p.cpp index 41ae73f..7267c9c 100644 --- a/libs/widgets/qpievaluator_p.cpp +++ b/libs/widgets/qpievaluator_p.cpp @@ -715,14 +715,13 @@ int QPIEvaluator::parse(const QString & string, int offset) { if (args.size() > 0) return args.back(); else return -666; } - for (int i = 0; i < QPIEvaluatorTypes::operationCount; i++) { - coper = operationInOrder(i); + int oprior = -1; + QVector opv; + while(1) { + operationsByPriority(++oprior, opv); + if (opv.isEmpty()) break; for (int j = 0; j < opers.size(); j++) { - if (coper == QPIEvaluatorTypes::oDivide || coper == QPIEvaluatorTypes::oMultiply) { - if (opers[j] != QPIEvaluatorTypes::oDivide && opers[j] != QPIEvaluatorTypes::oMultiply) continue; - } else { - if (opers[j] != coper) continue; - } + if (!opv.contains(opers[j])) continue; atmp.clear(); if (j < args.size() && j >= 0) atmp.push_back(args[j]); else atmp.push_back(-666); @@ -834,6 +833,22 @@ QString QPIEvaluator::operationChar(const QPIEvaluatorTypes::Operation & operati } +void QPIEvaluator::operationsByPriority(int p, QVector & ret) { + ret.clear(); + switch (p) { + case 0: ret << QPIEvaluatorTypes::oPower; break; + case 1: ret << QPIEvaluatorTypes::oMultiply << QPIEvaluatorTypes::oDivide << QPIEvaluatorTypes::oResidue; break; + case 2: ret << QPIEvaluatorTypes::oAdd << QPIEvaluatorTypes::oSubtract; break; + case 3: ret << QPIEvaluatorTypes::oEqual << QPIEvaluatorTypes::oNotEqual + << QPIEvaluatorTypes::oGreaterEqual << QPIEvaluatorTypes::oSmallerEqual + << QPIEvaluatorTypes::oGreater << QPIEvaluatorTypes::oSmaller; break; + case 4: ret << QPIEvaluatorTypes::oAnd; break; + case 5: ret << QPIEvaluatorTypes::oOr; break; + default: break; + } +} + + inline complexd QPIEvaluator::residue(const complexd & f, const complexd & s) { complexd ret; if (s.real() != 0.) ret = complexd(f.real() - ((int)(f.real() / s.real())) * s.real(), 0.); diff --git a/libs/widgets/qpievaluator_p.h b/libs/widgets/qpievaluator_p.h index 03fafca..91f3adb 100644 --- a/libs/widgets/qpievaluator_p.h +++ b/libs/widgets/qpievaluator_p.h @@ -186,6 +186,7 @@ private: bool check(); bool execInstructions(); QString operationChar(const QPIEvaluatorTypes::Operation & operation); + void operationsByPriority(int p, QVector & ret); QPIEvaluatorTypes::Operation operationInOrder(const int & index); complexd value(const int & index) {if (index < 0) return tmpvars[-index - 1].value; else return kvars->at(index).value;} inline complexd residue(const complexd & f, const complexd & s);