diff options
38 files changed, 678 insertions, 795 deletions
diff --git a/src/libparser/node/allsamenode.cpp b/src/libparser/node/allsamenode.cpp index 3b2f10f..2303b78 100644 --- a/src/libparser/node/allsamenode.cpp +++ b/src/libparser/node/allsamenode.cpp @@ -8,53 +8,47 @@ AllSameNode::AllSameNode() : m_diceResult(new DiceResult()) void AllSameNode::run(ExecutionNode* previous) { m_previousNode= previous; - if(nullptr != previous) - { - DiceResult* previous_result= dynamic_cast<DiceResult*>(previous->getResult()); - if(nullptr != previous_result) - { - m_result->setPrevious(previous_result); - bool allSame= true; - int i= 0; - qint64 previousValue= 0; - if(previous_result->getResultList().size() < 2) - { - m_errors.insert(Dice::ERROR_CODE::ENDLESS_LOOP_ERROR, - QStringLiteral("T operator must operate on more than 1 die")); - return; - } - for(auto& die : previous_result->getResultList()) - { - if(i == 0) - previousValue= die->getValue(); - Die* tmpdie= new Die(*die); - m_diceResult->insertResult(tmpdie); - die->displayed(); - if(previousValue != die->getValue()) - allSame= false; - ++i; - } + if(isValid(!m_previousNode, Dice::ERROR_CODE::NO_PREVIOUS_ERROR, tr("No valid previous node"))) + return; - while(allSame) - { - QList<Die*> list= m_diceResult->getResultList(); - qint64 pValue= 0; - int i= 0; - for(auto& die : list) - { - die->roll(true); - if(i == 0) - pValue= die->getValue(); - if(pValue != die->getValue()) - allSame= false; - ++i; - } - } - } + DiceResult* previous_result= dynamic_cast<DiceResult*>(previous->getResult()); + if(isValid(!previous_result, Dice::ERROR_CODE::NO_VALID_RESULT, tr("No dice result"))) + return; + + m_result->setPrevious(previous_result); + bool allSame= true; + int i= 0; + qint64 previousValue= 0; + if(isValid(previous_result->getResultList().size() < 2, Dice::ERROR_CODE::ENDLESS_LOOP_ERROR, + QStringLiteral("T operator must operate on more than 1 die"))) + return; + + for(auto& die : previous_result->getResultList()) + { + if(i == 0) + previousValue= die->getValue(); + Die* tmpdie= new Die(*die); + m_diceResult->insertResult(tmpdie); + die->displayed(); + if(previousValue != die->getValue()) + allSame= false; + ++i; } - if(nullptr != m_nextNode) + + while(allSame) { - m_nextNode->run(this); + QList<Die*> list= m_diceResult->getResultList(); + qint64 pValue= 0; + int i= 0; + for(auto& die : list) + { + die->roll(true); + if(i == 0) + pValue= die->getValue(); + if(pValue != die->getValue()) + allSame= false; + ++i; + } } } diff --git a/src/libparser/node/bind.cpp b/src/libparser/node/bind.cpp index 490071f..41449d8 100644 --- a/src/libparser/node/bind.cpp +++ b/src/libparser/node/bind.cpp @@ -31,6 +31,9 @@ void BindNode::run(ExecutionNode* previous) if(nullptr == m_previousNode) return; + if(!m_startList) + return; + m_result->setPrevious(previous->getResult()); for(auto start : *m_startList) { @@ -58,11 +61,6 @@ void BindNode::run(ExecutionNode* previous) } } } - - if(nullptr != m_nextNode) - { - m_nextNode->run(this); - } } ExecutionNode* BindNode::getLatestNode(ExecutionNode* node) { diff --git a/src/libparser/node/bind.h b/src/libparser/node/bind.h index 71bbfb0..b13c2bd 100644 --- a/src/libparser/node/bind.h +++ b/src/libparser/node/bind.h @@ -44,7 +44,7 @@ private: private: DiceResult* m_diceResult; - std::vector<ExecutionNode*>* m_startList; + std::vector<ExecutionNode*>* m_startList{nullptr}; }; #endif // NUMBERNODE_H diff --git a/src/libparser/node/countexecutenode.cpp b/src/libparser/node/countexecutenode.cpp index b45fe37..5a0c60b 100644 --- a/src/libparser/node/countexecutenode.cpp +++ b/src/libparser/node/countexecutenode.cpp @@ -23,7 +23,8 @@ void CountExecuteNode::run(ExecutionNode* previous) m_previousNode= previous; if(nullptr == previous) { - m_errors.insert(Dice::ERROR_CODE::NO_PREVIOUS_ERROR, QStringLiteral("No scalar result before Swith/Case operator")); + m_errors.insert(Dice::ERROR_CODE::NO_PREVIOUS_ERROR, + QStringLiteral("No scalar result before Swith/Case operator")); return; } DiceResult* previousResult= dynamic_cast<DiceResult*>(previous->getResult()); @@ -34,10 +35,11 @@ void CountExecuteNode::run(ExecutionNode* previous) std::function<void(Die*, qint64)> f= [&sum](const Die*, qint64 score) { sum+= score; }; m_validatorList->validResult(previousResult, true, true, f); m_scalarResult->setValue(sum); - if(nullptr != m_nextNode) + // TODO nextNode to null? + /*if(nullptr != m_nextNode) { m_nextNode->run(this); - } + }*/ } } QString CountExecuteNode::toString(bool withlabel) const diff --git a/src/libparser/node/dicerollernode.cpp b/src/libparser/node/dicerollernode.cpp index 2b00c0a..8ef0164 100644 --- a/src/libparser/node/dicerollernode.cpp +++ b/src/libparser/node/dicerollernode.cpp @@ -14,50 +14,47 @@ DiceRollerNode::DiceRollerNode(qint64 max, qint64 min) void DiceRollerNode::run(ExecutionNode* previous) { m_previousNode= previous; - if(nullptr != previous) - { - Result* result= previous->getResult(); - if(nullptr != result) - { - auto num= result->getResult(Dice::RESULT_TYPE::SCALAR).toReal(); - if(num <= 0) - { - m_errors.insert(Dice::ERROR_CODE::NO_DICE_TO_ROLL, QObject::tr("No dice to roll")); - } - m_diceCount= num > 0 ? static_cast<quint64>(num) : 0; - m_result->setPrevious(result); + if(isValid(!previous, Dice::ERROR_CODE::NO_PREVIOUS_ERROR, QObject::tr("No Previous node"))) + return; - auto possibleValue= static_cast<quint64>(std::abs((m_max - m_min) + 1)); - if(possibleValue < m_diceCount && m_unique) - { - m_errors.insert(Dice::ERROR_CODE::TOO_MANY_DICE, - QObject::tr("More unique values asked than possible values (D operator)")); - return; - } + Result* result= previous->getResult(); - for(quint64 i= 0; i < m_diceCount; ++i) - { - Die* die= new Die(); - die->setOp(m_operator); - die->setBase(m_min); - die->setMaxValue(m_max); - die->roll(); - if(m_unique) - { - const auto& equal= [](const Die* a, const Die* b) { return a->getValue() == b->getValue(); }; - while(m_diceResult->contains(die, equal)) - { - die->roll(false); - } - } - m_diceResult->insertResult(die); - } - if(nullptr != m_nextNode) + if(isValid(!result, Dice::ERROR_CODE::NO_VALID_RESULT, QObject::tr("No result from previous node"))) + return; + + auto num= result->getResult(Dice::RESULT_TYPE::SCALAR).toReal(); + + isValid(num <= 0, Dice::ERROR_CODE::NO_DICE_TO_ROLL, QObject::tr("No dice to roll")); + + m_diceCount= num > 0 ? static_cast<quint64>(num) : 0; + m_result->setPrevious(result); + + auto possibleValue= static_cast<quint64>(std::abs((m_max - m_min) + 1)); + if(isValid(possibleValue < m_diceCount && m_unique, Dice::ERROR_CODE::TOO_MANY_DICE, + QObject::tr("More unique values asked than possible values (D operator)"))) + return; + + for(quint64 i= 0; i < m_diceCount; ++i) + { + Die* die= new Die(); + die->setOp(m_operator); + die->setBase(m_min); + die->setMaxValue(m_max); + die->roll(); + if(m_unique) + { + const auto& equal= [](const Die* a, const Die* b) { return a->getValue() == b->getValue(); }; + while(m_diceResult->contains(die, equal)) { - m_nextNode->run(this); + die->roll(false); } } + m_diceResult->insertResult(die); } + /*if(nullptr != m_nextNode) + {TODO nextNode to null? + m_nextNode->run(this); + }*/ } quint64 DiceRollerNode::getFaces() const diff --git a/src/libparser/node/executionnode.cpp b/src/libparser/node/executionnode.cpp index 4545934..096c8fe 100644 --- a/src/libparser/node/executionnode.cpp +++ b/src/libparser/node/executionnode.cpp @@ -40,6 +40,17 @@ ExecutionNode* ExecutionNode::getNextNode() { return m_nextNode; } + +void ExecutionNode::execute(ExecutionNode* previous) +{ + auto errorCount= m_errors.count(); + + run(previous); + + if(m_nextNode && errorCount == m_errors.count()) + m_nextNode->execute(this); +} + QMap<Dice::ERROR_CODE, QString> ExecutionNode::getExecutionErrorMap() { if(nullptr != m_nextNode) @@ -99,3 +110,11 @@ qint64 ExecutionNode::getScalarResult() return 0; return m_result->getResult(Dice::RESULT_TYPE::SCALAR).toInt(); } + +bool ExecutionNode::isValid(bool condition, Dice::ERROR_CODE code, const QString& errorTxt, bool error) +{ + if(condition) + error ? m_errors.insert(code, errorTxt) : m_warnings.insert(code, errorTxt); + + return condition; +} diff --git a/src/libparser/node/executionnode.h b/src/libparser/node/executionnode.h index 64cc2da..342ae36 100644 --- a/src/libparser/node/executionnode.h +++ b/src/libparser/node/executionnode.h @@ -21,6 +21,8 @@ public: * @brief ~ExecutionNode */ virtual ~ExecutionNode(); + + virtual void execute(ExecutionNode* previous= nullptr); /** * @brief run * @param previous @@ -81,6 +83,8 @@ public: virtual qint64 getScalarResult(); + bool isValid(bool condition, Dice::ERROR_CODE code , const QString& errorTxt, bool error = true); + protected: /** * @brief m_nextNode @@ -99,6 +103,8 @@ protected: */ QMap<Dice::ERROR_CODE, QString> m_errors; + QMap<Dice::ERROR_CODE, QString> m_warnings; + QString m_id; }; diff --git a/src/libparser/node/explodedicenode.cpp b/src/libparser/node/explodedicenode.cpp index 5f51f95..136611a 100644 --- a/src/libparser/node/explodedicenode.cpp +++ b/src/libparser/node/explodedicenode.cpp @@ -31,7 +31,7 @@ void ExplodeDiceNode::run(ExecutionNode* previous) qint64 limit= -1; if(m_limit) { - m_limit->run(this); + m_limit->execute(this); auto limitNode= ParsingToolBox::getLeafNode(m_limit); auto result= limitNode->getResult(); if(result->hasResultOfType(Dice::RESULT_TYPE::SCALAR)) @@ -42,16 +42,15 @@ void ExplodeDiceNode::run(ExecutionNode* previous) std::function<void(Die*, qint64)> f= [&hasExploded, this, limit](Die* die, qint64) { static QHash<Die*, qint64> explodePerDice; - if(Dice::CONDITION_STATE::ALWAYSTRUE - == m_validatorList->isValidRangeSize(std::make_pair<qint64, qint64>(die->getBase(), die->getMaxValue()))) - { - m_errors.insert(Dice::ERROR_CODE::ENDLESS_LOOP_ERROR, - QObject::tr("Condition (%1) cause an endless loop with this dice: %2") - .arg(toString(true)) - .arg(QStringLiteral("d[%1,%2]") - .arg(static_cast<int>(die->getBase())) - .arg(static_cast<int>(die->getMaxValue())))); - } + auto validity + = m_validatorList->isValidRangeSize(std::make_pair<qint64, qint64>(die->getBase(), die->getMaxValue())); + isValid(Dice::CONDITION_STATE::ALWAYSTRUE == validity, Dice::ERROR_CODE::ENDLESS_LOOP_ERROR, + QObject::tr("Condition (%1) cause an endless loop with this dice: %2") + .arg(toString(true)) + .arg(QStringLiteral("d[%1,%2]") + .arg(static_cast<int>(die->getBase())) + .arg(static_cast<int>(die->getMaxValue())))); + hasExploded= true; if(limit >= 0) { @@ -70,11 +69,6 @@ void ExplodeDiceNode::run(ExecutionNode* previous) hasExploded= false; m_validatorList->validResult(m_diceResult, false, false, f); } while(hasExploded); - - if(nullptr != m_nextNode) - { - m_nextNode->run(this); - } } ExplodeDiceNode::~ExplodeDiceNode() { diff --git a/src/libparser/node/filternode.cpp b/src/libparser/node/filternode.cpp index b72b6e1..af3d166 100644 --- a/src/libparser/node/filternode.cpp +++ b/src/libparser/node/filternode.cpp @@ -19,43 +19,39 @@ void FilterNode::setValidatorList(ValidatorList* validatorlist) } void FilterNode::run(ExecutionNode* previous) { - m_previousNode= previous; - if(nullptr == previous) - { + if(isValid(!previous, Dice::ERROR_CODE::NO_PREVIOUS_ERROR, tr("No Previous node"))) return; - } + m_previousNode= previous; + DiceResult* previousDiceResult= dynamic_cast<DiceResult*>(previous->getResult()); m_result->setPrevious(previousDiceResult); - if(nullptr != previousDiceResult) - { - QList<Die*> diceList2; - std::function<void(Die*, qint64)> f= [&diceList2](Die* die, qint64) { - if(die == nullptr) - return; - Die* tmpdie= new Die(*die); - diceList2.append(tmpdie); - die->displayed(); - }; - m_validatorList->validResult(previousDiceResult, true, true, f); + if(isValid(!previousDiceResult, Dice::ERROR_CODE::NO_VALID_RESULT, tr("No Valid result"))) + return; - QList<Die*> diceList= previousDiceResult->getResultList(); + QList<Die*> diceList2; + std::function<void(Die*, qint64)> f= [&diceList2](Die* die, qint64) + { + if(die == nullptr) + return; + Die* tmpdie= new Die(*die); + diceList2.append(tmpdie); + die->displayed(); + }; + m_validatorList->validResult(previousDiceResult, true, true, f); - diceList.erase(std::remove_if(diceList.begin(), diceList.end(), - [&diceList2](Die* die) { return diceList2.contains(die); }), - diceList.end()); - for(Die* tmp : diceList) - { - tmp->setHighlighted(false); - tmp->setDisplayed(true); - } + QList<Die*> diceList= previousDiceResult->getResultList(); - m_diceResult->setResultList(diceList2); - if(nullptr != m_nextNode) - { - m_nextNode->run(this); - } + diceList.erase( + std::remove_if(diceList.begin(), diceList.end(), [&diceList2](Die* die) { return diceList2.contains(die); }), + diceList.end()); + for(Die* tmp : diceList) + { + tmp->setHighlighted(false); + tmp->setDisplayed(true); } + + m_diceResult->setResultList(diceList2); } QString FilterNode::toString(bool wl) const diff --git a/src/libparser/node/groupnode.cpp b/src/libparser/node/groupnode.cpp index 860d758..8dab236 100644 --- a/src/libparser/node/groupnode.cpp +++ b/src/libparser/node/groupnode.cpp @@ -40,6 +40,11 @@ void DieGroup::removeValue(DieGroup i) } } +void DieGroup::sort() +{ + std::sort(std::begin(*this), std::end(*this), std::greater<qint64>()); +} + int DieGroup::getLost() const { return getSum() - m_exceptedValue; @@ -69,63 +74,60 @@ void GroupNode::run(ExecutionNode* previous) m_result= m_scalarResult; m_previousNode= previous; - if(nullptr != previous) + if(isValid(!m_previousNode, Dice::ERROR_CODE::NO_PREVIOUS_ERROR, tr("No Previous node"))) + return; + + m_result->setPrevious(previous->getResult()); + Result* tmpResult= previous->getResult(); + + if(isValid(!tmpResult, Dice::ERROR_CODE::NO_VALID_RESULT, tr("No Valid result"))) + return; + + DiceResult* dice= dynamic_cast<DiceResult*>(tmpResult); + + if(isValid(!dice, Dice::ERROR_CODE::NO_VALID_RESULT, tr("No Valid Dice result"))) + return; + + auto allDicelist= dice->getResultList(); + DieGroup allResult; + for(auto& die : allDicelist) { - m_result->setPrevious(previous->getResult()); - Result* tmpResult= previous->getResult(); - if(nullptr != tmpResult) - { - DiceResult* dice= dynamic_cast<DiceResult*>(tmpResult); - if(nullptr != dice) - { - auto list= dice->getResultList(); - DieGroup allResult; - for(auto& die : list) - { - allResult << die->getListValue(); - } - std::sort(allResult.begin(), allResult.end(), std::greater<qint64>()); - if(allResult.getSum() > m_groupValue) - { - auto copy= allResult; - auto const die= getGroup(allResult); + allResult << die->getListValue(); + } + std::sort(allResult.begin(), allResult.end(), std::greater<qint64>()); + if(allResult.getSum() <= m_groupValue) + { + m_scalarResult->setValue(0); + return; + } - for(auto list : die) - { - for(auto val : list) - { - copy.removeOne(val); - } - } - m_scalarResult->setValue(die.size()); - QStringList list; - for(auto group : die) - { - QStringList values; - std::transform(group.begin(), group.end(), std::back_inserter(values), - [](qint64 val) { return QString::number(val); }); - list << QStringLiteral("{%1}").arg(values.join(",")); - } - QStringList unused; - std::transform(copy.begin(), copy.end(), std::back_inserter(unused), - [](qint64 val) { return QString::number(val); }); - if(!unused.isEmpty()) - m_stringResult->addText( - QStringLiteral("%1 (%2 - [%3])").arg(die.size()).arg(list.join(",")).arg(unused.join(","))); - else - m_stringResult->addText(QStringLiteral("%1 (%2)").arg(die.size()).arg(list.join(","))); - } - else - { - m_scalarResult->setValue(0); - } - } + auto copy= allResult; + auto const die= getGroup(allResult); + + for(auto& list : die) + { + for(auto& val : list) + { + copy.removeOne(val); } } - if(nullptr != m_nextNode) + m_scalarResult->setValue(die.size()); + QStringList list; + for(auto group : die) { - m_nextNode->run(this); + QStringList values; + std::transform(group.begin(), group.end(), std::back_inserter(values), + [](qint64 val) { return QString::number(val); }); + list << QStringLiteral("{%1}").arg(values.join(",")); } + QStringList unused; + std::transform(copy.begin(), copy.end(), std::back_inserter(unused), + [](qint64 val) { return QString::number(val); }); + if(!unused.isEmpty()) + m_stringResult->addText( + QStringLiteral("%1 (%2 - [%3])").arg(die.size()).arg(list.join(",")).arg(unused.join(","))); + else + m_stringResult->addText(QStringLiteral("%1 (%2)").arg(die.size()).arg(list.join(","))); } QString GroupNode::toString(bool withLabel) const diff --git a/src/libparser/node/groupnode.h b/src/libparser/node/groupnode.h index d037080..72e795c 100644 --- a/src/libparser/node/groupnode.h +++ b/src/libparser/node/groupnode.h @@ -38,6 +38,7 @@ public: qint64 getExceptedValue() const; void setExceptedValue(qint64 exceptedValue); + void sort(); private: qint64 m_exceptedValue= 0; }; @@ -62,6 +63,10 @@ protected: bool composeWithPrevious(DieGroup previous, qint64 first, qint64 current, DieGroup& addValue); private: + DieGroup findPerfect(DieGroup values, int count, int currentValue) const; + DieGroup findCombo(DieGroup values, int count, int currentValue) const; + +private: ScalarResult* m_scalarResult; StringResult* m_stringResult; qint64 m_groupValue; diff --git a/src/libparser/node/helpnode.cpp b/src/libparser/node/helpnode.cpp index 9cac27e..e8edc9c 100644 --- a/src/libparser/node/helpnode.cpp +++ b/src/libparser/node/helpnode.cpp @@ -21,38 +21,33 @@ HelpNode::HelpNode() : m_path("https://invent.kde.org/rolisteam/rolisteam-diceparser/-/blob/master/HelpMe.md") { - m_result= new StringResult(); + m_stringResult= new StringResult(); + m_result= m_stringResult; + m_stringResult->setPrevious(nullptr); } void HelpNode::run(ExecutionNode* previous) { m_previousNode= previous; - StringResult* txtResult= dynamic_cast<StringResult*>(m_result); - txtResult->setHighLight(false); + m_stringResult->setHighLight(false); - if((nullptr == previous) && (txtResult != nullptr)) + if(!previous) { - txtResult->addText(QObject::tr("Rolisteam Dice Parser:\n" - "\n" - "Example (with ! as prefix):\n" - "!2d6\n" - "!1d20\n" - "!6d10e10k3 (L5R)\n" - "\n" - "Full documentation at: %1") - .arg(m_path)); - m_result->setPrevious(nullptr); + m_stringResult->addText(QObject::tr("Rolisteam Dice Parser:\n" + "\n" + "Example (with ! as prefix):\n" + "!2d6\n" + "!1d20\n" + "!6d10e10k3 (L5R)\n" + "\n" + "Full documentation at: %1") + .arg(m_path)); } - else if(nullptr != previous) + else { - txtResult->addText(previous->getHelp()); + m_stringResult->addText(previous->getHelp()); m_result->setPrevious(previous->getResult()); } - txtResult->finished(); - - if(nullptr != m_nextNode) - { - m_nextNode->run(this); - } + m_stringResult->finished(); } QString HelpNode::toString(bool wl) const { diff --git a/src/libparser/node/helpnode.h b/src/libparser/node/helpnode.h index a333c6d..7e6da5b 100644 --- a/src/libparser/node/helpnode.h +++ b/src/libparser/node/helpnode.h @@ -64,6 +64,7 @@ public: private: QString m_path; + StringResult* m_stringResult; }; #endif // HELPNODE_H diff --git a/src/libparser/node/ifnode.cpp b/src/libparser/node/ifnode.cpp index 8de3cd5..62821ae 100644 --- a/src/libparser/node/ifnode.cpp +++ b/src/libparser/node/ifnode.cpp @@ -39,10 +39,10 @@ void PartialDiceRollNode::run(ExecutionNode* previous) { m_result->setPrevious(presult); } - if(nullptr != m_nextNode) + /*if(nullptr != m_nextNode) { m_nextNode->run(this); - } + }*/ } ExecutionNode* PartialDiceRollNode::getCopy() const { @@ -94,141 +94,140 @@ IfNode::~IfNode() void IfNode::run(ExecutionNode* previous) { m_previousNode= previous; - if(nullptr == previous) - { + if(isValid(!previous, Dice::ERROR_CODE::NO_PREVIOUS_ERROR, tr("No Previous node"))) return; - } + ExecutionNode* previousLoop= previous; ExecutionNode* nextNode= nullptr; - bool runNext= (nullptr == m_nextNode) ? false : true; Result* previousResult= previous->getResult(); + if(isValid(!previousResult, Dice::ERROR_CODE::NO_VALID_RESULT, tr("No Valid result"))) + return; + m_result= previousResult->getCopy(); - if(nullptr != m_result) + if(isValid(!m_result, Dice::ERROR_CODE::NO_VALID_RESULT, tr("No Valid copy of result"))) + return; + + qreal value= previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(); + if(isValid(!m_validatorList, Dice::ERROR_CODE::NO_VALIDATOR_LIST, tr("No validator list"))) + return; + + DiceResult* previousDiceResult= getFirstDiceResult(previousResult); + if(nullptr != previousDiceResult) { - qreal value= previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(); + QList<Die*> diceList= previousDiceResult->getResultList(); - if(nullptr != m_validatorList) + if(m_conditionType == Dice::OnEach) { - DiceResult* previousDiceResult= getFirstDiceResult(previousResult); - if(nullptr != previousDiceResult) + for(Die* dice : diceList) { - QList<Die*> diceList= previousDiceResult->getResultList(); - - if(m_conditionType == Dice::OnEach) + auto diceNode= new PartialDiceRollNode(); + diceNode->insertDie(new Die(*dice)); + if(m_validatorList->hasValid(dice, true, true)) { - for(Die* dice : diceList) - { - auto diceNode= new PartialDiceRollNode(); - diceNode->insertDie(new Die(*dice)); - if(m_validatorList->hasValid(dice, true, true)) - { - nextNode= (nullptr == m_true) ? nullptr : m_true->getCopy(); - } - else - { - nextNode= (nullptr == m_false) ? nullptr : m_false->getCopy(); - } - - if(nullptr != nextNode) - { - if(nullptr == previousLoop->getNextNode()) - { - previousLoop->setNextNode(nextNode); - } - if(nullptr == m_nextNode) - { - m_nextNode= nextNode; - } - diceNode->setNextNode(nextNode); - diceNode->run(previousLoop); - previousLoop= getLeafNode(nextNode); - } - } + nextNode= (nullptr == m_true) ? nullptr : m_true->getCopy(); } - else if((m_conditionType == Dice::OneOfThem) || (m_conditionType == Dice::AllOfThem)) + else { - bool trueForAll= true; - bool falseForAll= true; - - bool oneIsTrue= false; - bool oneIsFalse= false; - - for(Die* dice : diceList) - { - bool result= m_validatorList->hasValid(dice, true, true); - trueForAll= trueForAll ? result : false; - falseForAll= falseForAll ? result : false; + nextNode= (nullptr == m_false) ? nullptr : m_false->getCopy(); + } - oneIsTrue|= result; - oneIsFalse= !result ? true : oneIsFalse; - } - if(m_conditionType == Dice::OneOfThem) - { - if(oneIsTrue) - { - nextNode= (nullptr == m_true) ? nullptr : m_true->getCopy(); - } - else // if(oneIsFalse) - { - nextNode= (nullptr == m_false) ? nullptr : m_false->getCopy(); - } - } - else if(m_conditionType == Dice::AllOfThem) + if(nullptr != nextNode) + { + if(nullptr == previousLoop->getNextNode()) { - if(trueForAll) - { - nextNode= (nullptr == m_true) ? nullptr : m_true->getCopy(); - } - else // if(falseForAll) - { - nextNode= (nullptr == m_false) ? nullptr : m_false->getCopy(); - } + previousLoop->setNextNode(nextNode); } - - if(nullptr != nextNode) + if(nullptr == m_nextNode) { - if(nullptr == m_nextNode) - { - m_nextNode= nextNode; - } - nextNode->run(previousLoop); - previousLoop= getLeafNode(nextNode); + m_nextNode= nextNode; } + diceNode->setNextNode(nextNode); + diceNode->execute(previousLoop); + previousLoop= getLeafNode(nextNode); + } + else + { + delete diceNode; } } + } + else if((m_conditionType == Dice::OneOfThem) || (m_conditionType == Dice::AllOfThem)) + { + bool trueForAll= true; + bool falseForAll= true; + + bool oneIsTrue= false; + bool oneIsFalse= false; + + for(Die* dice : diceList) + { + bool result= m_validatorList->hasValid(dice, true, true); + trueForAll= trueForAll ? result : false; + falseForAll= falseForAll ? result : false; - if(m_conditionType == Dice::OnScalar) + oneIsTrue|= result; + oneIsFalse= !result ? true : oneIsFalse; + } + if(m_conditionType == Dice::OneOfThem) { - Die dice; - auto val= static_cast<qint64>(value); - dice.setValue(val); - dice.insertRollValue(val); - dice.setMaxValue(val); - if(m_validatorList->hasValid(&dice, true, true)) + if(oneIsTrue) { - nextNode= m_true; + nextNode= (nullptr == m_true) ? nullptr : m_true->getCopy(); } - else + else // if(oneIsFalse) { - nextNode= m_false; + nextNode= (nullptr == m_false) ? nullptr : m_false->getCopy(); } - if(nullptr != nextNode) + } + else if(m_conditionType == Dice::AllOfThem) + { + if(trueForAll) { - if(nullptr == m_nextNode) - { - m_nextNode= nextNode; - } - nextNode->run(previousLoop); - previousLoop= getLeafNode(nextNode); + nextNode= (nullptr == m_true) ? nullptr : m_true->getCopy(); } + else // if(falseForAll) + { + nextNode= (nullptr == m_false) ? nullptr : m_false->getCopy(); + } + } + + if(nullptr != nextNode) + { + if(nullptr == m_nextNode) + { + m_nextNode= nextNode; + } + nextNode->execute(previousLoop); + previousLoop= getLeafNode(nextNode); } } } - if((nullptr != m_nextNode) && (runNext)) + if(m_conditionType == Dice::OnScalar) { - m_nextNode->run(previousLoop); + Die dice; + auto val= static_cast<qint64>(value); + dice.setValue(val); + dice.insertRollValue(val); + dice.setMaxValue(val); + if(m_validatorList->hasValid(&dice, true, true)) + { + nextNode= m_true; + } + else + { + nextNode= m_false; + } + if(nullptr != nextNode) + { + if(nullptr == m_nextNode) + { + m_nextNode= nextNode; + } + nextNode->execute(previousLoop); + previousLoop= getLeafNode(nextNode); + } } } diff --git a/src/libparser/node/jumpbackwardnode.cpp b/src/libparser/node/jumpbackwardnode.cpp index 9fa0f45..e4ae59a 100644 --- a/src/libparser/node/jumpbackwardnode.cpp +++ b/src/libparser/node/jumpbackwardnode.cpp @@ -116,43 +116,37 @@ void JumpBackwardNode::run(ExecutionNode* previous) parent= parent->getPreviousNode(); } } - if(nullptr == result) - { - m_errors.insert( - Dice::ERROR_CODE::DIE_RESULT_EXPECTED, - QObject::tr(" The @ operator expects dice result. Please check the documentation to fix your command.")); - } - else + if(isValid(!result, Dice::ERROR_CODE::DIE_RESULT_EXPECTED, + tr(" The @ operator expects dice result. Please check the documentation to fix your command."))) + return; + + DiceResult* diceResult= dynamic_cast<DiceResult*>(result); + if(nullptr != diceResult) { - DiceResult* diceResult= dynamic_cast<DiceResult*>(result); - if(nullptr != diceResult) + for(auto& die : diceResult->getResultList()) { - for(auto& die : diceResult->getResultList()) - { - Die* tmpdie= new Die(*die); - //*tmpdie= *die; - m_diceResult->insertResult(tmpdie); - die->displayed(); - } + Die* tmpdie= new Die(*die); + //*tmpdie= *die; + m_diceResult->insertResult(tmpdie); + die->displayed(); } + } - m_result->setPrevious(previous->getResult()); + m_result->setPrevious(previous->getResult()); - if(nullptr != m_nextNode) - { - m_nextNode->run(this); - } - if(nullptr != diceResult) + if(m_nextNode) + m_nextNode->execute(this); + + if(!diceResult) + return; + + for(int i= 0; i < diceResult->getResultList().size(); ++i) + { + Die* tmp= diceResult->getResultList().at(i); + Die* tmp2= m_diceResult->getResultList().at(i); + if(tmp->isHighlighted()) { - for(int i= 0; i < diceResult->getResultList().size(); ++i) - { - Die* tmp= diceResult->getResultList().at(i); - Die* tmp2= m_diceResult->getResultList().at(i); - if(tmp->isHighlighted()) - { - tmp2->setHighlighted(true); - } - } + tmp2->setHighlighted(true); } } } @@ -166,3 +160,8 @@ ExecutionNode* JumpBackwardNode::getCopy() const } return node; } + +void JumpBackwardNode::execute(ExecutionNode* previous) +{ + run(previous); +} diff --git a/src/libparser/node/jumpbackwardnode.h b/src/libparser/node/jumpbackwardnode.h index 598a540..5f20291 100644 --- a/src/libparser/node/jumpbackwardnode.h +++ b/src/libparser/node/jumpbackwardnode.h @@ -37,29 +37,29 @@ public: * @brief run - performs the actions * @param previous */ - virtual void run(ExecutionNode* previous= nullptr); + virtual void run(ExecutionNode* previous= nullptr) override; /** * @brief toString * @return */ - virtual QString toString(bool) const; + virtual QString toString(bool) const override; /** * @brief getPriority * @return */ - virtual qint64 getPriority() const; + virtual qint64 getPriority() const override; /** * @brief generateDotTree * @param s */ - virtual void generateDotTree(QString& s); + virtual void generateDotTree(QString& s) override; /** * @brief getCopy * @return */ - virtual ExecutionNode* getCopy() const; - + virtual ExecutionNode* getCopy() const override; + virtual void execute(ExecutionNode* previous= nullptr) override; private: DiceResult* m_diceResult; ExecutionNode* m_backwardNode; diff --git a/src/libparser/node/keepdiceexecnode.cpp b/src/libparser/node/keepdiceexecnode.cpp index b197822..b4547c3 100644 --- a/src/libparser/node/keepdiceexecnode.cpp +++ b/src/libparser/node/keepdiceexecnode.cpp @@ -31,11 +31,10 @@ KeepDiceExecNode::~KeepDiceExecNode() {} void KeepDiceExecNode::run(ExecutionNode* previous) { m_previousNode= previous; - if(nullptr == previous || nullptr == m_numberOfDiceNode) - { + if(isValid(!previous || !m_numberOfDiceNode, Dice::ERROR_CODE::NO_VALID_RESULT, tr("No Valid result"))) return; - } - m_numberOfDiceNode->run(previous); + + m_numberOfDiceNode->execute(previous); auto lastnode= ParsingToolBox::getLeafNode(m_numberOfDiceNode); if(nullptr == lastnode) return; @@ -49,45 +48,41 @@ void KeepDiceExecNode::run(ExecutionNode* previous) DiceResult* previousDiceResult= dynamic_cast<DiceResult*>(previous->getResult()); m_result->setPrevious(previousDiceResult); - if(nullptr != previousDiceResult) - { - QList<Die*> diceList= previousDiceResult->getResultList(); - if(numberOfDice < 0) - { - numberOfDice= diceList.size() + numberOfDice; - } + if(isValid(!previousDiceResult, Dice::ERROR_CODE::NO_VALID_RESULT, tr("No Valid result"))) + return; + + QList<Die*> diceList= previousDiceResult->getResultList(); - QList<Die*> diceList3= diceList.mid(0, static_cast<int>(numberOfDice)); - QList<Die*> diceList2; + if(numberOfDice < 0) + { + numberOfDice= diceList.size() + numberOfDice; + } - for(Die* die : qAsConst(diceList3)) - { - Die* tmpdie= new Die(*die); - diceList2.append(tmpdie); - die->displayed(); - die->setSelected(false); - } + QList<Die*> diceList3= diceList.mid(0, static_cast<int>(numberOfDice)); + QList<Die*> diceList2; - if(numberOfDice > static_cast<qint64>(diceList.size())) - { - m_errors.insert(Dice::ERROR_CODE::TOO_MANY_DICE, - QObject::tr(" You ask to keep %1 dice but the result only has %2") - .arg(numberOfDice) - .arg(diceList.size())); - } + for(Die* die : std::as_const(diceList3)) + { + Die* tmpdie= new Die(*die); + diceList2.append(tmpdie); + die->displayed(); + die->setSelected(false); + } - for(auto& tmp : diceList.mid(static_cast<int>(numberOfDice), -1)) - { - tmp->setHighlighted(false); - } + if(numberOfDice > static_cast<qint64>(diceList.size())) + { + m_errors.insert( + Dice::ERROR_CODE::TOO_MANY_DICE, + QObject::tr(" You ask to keep %1 dice but the result only has %2").arg(numberOfDice).arg(diceList.size())); + } - m_diceResult->setResultList(diceList2); - if(nullptr != m_nextNode) - { - m_nextNode->run(this); - } + for(auto& tmp : diceList.mid(static_cast<int>(numberOfDice), -1)) + { + tmp->setHighlighted(false); } + + m_diceResult->setResultList(diceList2); } void KeepDiceExecNode::setDiceKeepNumber(ExecutionNode* n) diff --git a/src/libparser/node/listaliasnode.cpp b/src/libparser/node/listaliasnode.cpp index c3f6d31..8b840ee 100644 --- a/src/libparser/node/listaliasnode.cpp +++ b/src/libparser/node/listaliasnode.cpp @@ -32,16 +32,9 @@ void ListAliasNode::run(ExecutionNode* previous) txtResult->addText(buildList()); txtResult->finished(); - if(nullptr != previous) - { - // txtResult->setText(previous->getHelp()); - m_result->setPrevious(previous->getResult()); - } - if(nullptr != m_nextNode) - { - m_nextNode->run(this); - } + if(previous) + m_result->setPrevious(previous->getResult()); } QString ListAliasNode::buildList() const { diff --git a/src/libparser/node/listsetrollnode.cpp b/src/libparser/node/listsetrollnode.cpp index 4ea5b18..adb02ec 100644 --- a/src/libparser/node/listsetrollnode.cpp +++ b/src/libparser/node/listsetrollnode.cpp @@ -59,41 +59,34 @@ qint64 ListSetRollNode::getPriority() const void ListSetRollNode::run(ExecutionNode* previous) { m_previousNode= previous; - if(nullptr != previous) + if(isValid(!previous, Dice::ERROR_CODE::NO_PREVIOUS_ERROR, tr("No Previous node"))) + return; + + Result* result= previous->getResult(); + if(isValid(!result, Dice::ERROR_CODE::NO_VALID_RESULT, tr("No Valid result"))) + return; + + quint64 diceCount= result->getResult(Dice::RESULT_TYPE::SCALAR).toReal(); + + if(isValid(diceCount > static_cast<quint64>(m_values.size()) && m_unique, Dice::ERROR_CODE::TOO_MANY_DICE, + tr("More unique values asked than possible values (L operator)"))) + return; + + m_result->setPrevious(result); + for(quint64 i= 0; i < diceCount; ++i) { - Result* result= previous->getResult(); - if(nullptr != result) + QStringList rollResult; + Die* die= new Die(); + computeFacesNumber(die); + die->roll(); + m_diceResult->insertResult(die); + getValueFromDie(die, rollResult); + for(auto const& str : std::as_const(rollResult)) { - quint64 diceCount= result->getResult(Dice::RESULT_TYPE::SCALAR).toReal(); - if(diceCount > static_cast<quint64>(m_values.size()) && m_unique) - { - m_errors.insert(Dice::ERROR_CODE::TOO_MANY_DICE, - tr("More unique values asked than possible values (L operator)")); - } - else - { - m_result->setPrevious(result); - for(quint64 i= 0; i < diceCount; ++i) - { - QStringList rollResult; - Die* die= new Die(); - computeFacesNumber(die); - die->roll(); - m_diceResult->insertResult(die); - getValueFromDie(die, rollResult); - for(auto const& str : qAsConst(rollResult)) - { - m_stringResult->addText(str); - } - } - m_stringResult->finished(); - } - if(nullptr != m_nextNode) - { - m_nextNode->run(this); - } + m_stringResult->addText(str); } } + m_stringResult->finished(); } void ListSetRollNode::setListValue(QStringList lirs) { diff --git a/src/libparser/node/listsetrollnode.h b/src/libparser/node/listsetrollnode.h index a6c5e1a..bdf6022 100644 --- a/src/libparser/node/listsetrollnode.h +++ b/src/libparser/node/listsetrollnode.h @@ -45,6 +45,7 @@ public: void setNoComma(bool); void setRangeList(QList<Range>&); virtual ExecutionNode* getCopy() const; + std::pair<qint64, qint64> getRange() const; private: void getValueFromDie(Die* die, QStringList& rollResult); diff --git a/src/libparser/node/mergenode.cpp b/src/libparser/node/mergenode.cpp index 096bb8c..4c19bd7 100644 --- a/src/libparser/node/mergenode.cpp +++ b/src/libparser/node/mergenode.cpp @@ -29,16 +29,16 @@ MergeNode::MergeNode() : m_diceResult(new DiceResult()) } void MergeNode::run(ExecutionNode* previous) { - if(nullptr == previous) - { - m_errors.insert(Dice::ERROR_CODE::NO_PREVIOUS_ERROR, QObject::tr("No previous node before Merge operator")); + if(isValid(!previous, Dice::ERROR_CODE::NO_PREVIOUS_ERROR, tr("No previous node before Merge operator"))) return; - } m_previousNode= previous; m_result->setPrevious(previous->getResult()); ExecutionNode* previousLast= nullptr; std::vector<Result*> pastResult; + if(!m_startList) + return; + for(auto start : *m_startList) { ExecutionNode* last= getLatestNode(start); @@ -92,11 +92,6 @@ void MergeNode::run(ExecutionNode* previous) auto first= m_startList->front(); m_startList->clear(); m_startList->push_back(first); - - if(nullptr != m_nextNode) - { - m_nextNode->run(this); - } } #include <QDebug> ExecutionNode* MergeNode::getLatestNode(ExecutionNode* node) diff --git a/src/libparser/node/numbernode.cpp b/src/libparser/node/numbernode.cpp index e50656e..499a0a2 100644 --- a/src/libparser/node/numbernode.cpp +++ b/src/libparser/node/numbernode.cpp @@ -36,15 +36,11 @@ NumberNode::~NumberNode() void NumberNode::run(ExecutionNode* previous) { + if(!previous) + return; + m_previousNode= previous; - if(nullptr != previous) - { - m_result->setPrevious(previous->getResult()); - } - if(nullptr != m_nextNode) - { - m_nextNode->run(this); - } + m_result->setPrevious(previous->getResult()); } void NumberNode::setNumber(qint64 a) diff --git a/src/libparser/node/occurencecountnode.cpp b/src/libparser/node/occurencecountnode.cpp index fc5c2f2..780ca08 100644 --- a/src/libparser/node/occurencecountnode.cpp +++ b/src/libparser/node/occurencecountnode.cpp @@ -27,13 +27,14 @@ OccurenceCountNode::OccurenceCountNode() : ExecutionNode() {} void OccurenceCountNode::run(ExecutionNode* previous) { + if(isValid(!previous, Dice::ERROR_CODE::NO_PREVIOUS_ERROR, tr("No Previous node"))) + return; + m_previousNode= previous; std::map<qint64, qint64> mapOccurence; - if(nullptr == m_previousNode) - return; DiceResult* previousDiceResult= dynamic_cast<DiceResult*>(m_previousNode->getResult()); - if(nullptr == previousDiceResult) + if(isValid(!previousDiceResult, Dice::ERROR_CODE::NO_VALID_RESULT, tr("No Valid result"))) return; auto const& diceList= previousDiceResult->getResultList(); @@ -52,14 +53,10 @@ void OccurenceCountNode::run(ExecutionNode* previous) } std::sort(vec.begin(), vec.end()); - if(nullptr == m_nextNode) - { + if(!m_nextNode) runForStringResult(mapOccurence, vec); - } else - { runForDiceResult(mapOccurence); - } } QString OccurenceCountNode::toString(bool label) const { @@ -177,9 +174,4 @@ void OccurenceCountNode::runForDiceResult(const std::map<qint64, qint64>& mapOcc m_diceResult->insertResult(die); } } - - if(nullptr != m_nextNode) - { - m_nextNode->run(this); - } } diff --git a/src/libparser/node/paintnode.cpp b/src/libparser/node/paintnode.cpp index 22c020e..0289622 100644 --- a/src/libparser/node/paintnode.cpp +++ b/src/libparser/node/paintnode.cpp @@ -57,39 +57,33 @@ PainterNode::~PainterNode() void PainterNode::run(ExecutionNode* previous) { - m_previousNode= previous; - if(nullptr == previous) - { - m_errors.insert(Dice::ERROR_CODE::NO_PREVIOUS_ERROR, QObject::tr("No previous node before Paint operator")); + if(isValid(!previous, Dice::ERROR_CODE::NO_PREVIOUS_ERROR, tr("No Previous node"))) return; - } + m_previousNode= previous; + Result* previousResult= previous->getResult(); - if(nullptr == previousResult) + if(isValid(!previousResult, Dice::ERROR_CODE::NO_VALID_RESULT, tr("No Valid result"))) return; m_diceResult= dynamic_cast<DiceResult*>(previousResult->getCopy()); - if(nullptr != m_diceResult) + if(isValid(!m_diceResult, Dice::ERROR_CODE::NO_VALID_RESULT, tr("No Valid Dice result"))) + return; + + QList<Die*> diceList= m_diceResult->getResultList(); + int pastDice= 0; + for(ColorItem& item : m_colors) { - QList<Die*> diceList= m_diceResult->getResultList(); - int pastDice= 0; - for(ColorItem& item : m_colors) + int current= item.colorNumber(); + QList<Die*>::iterator it; + for(it= diceList.begin() + pastDice; it != diceList.end() && current > 0; ++it) { - int current= item.colorNumber(); - QList<Die*>::iterator it; - for(it= diceList.begin() + pastDice; it != diceList.end() && current > 0; ++it) - { - (*it)->setColor(item.color()); - --current; - ++pastDice; - } + (*it)->setColor(item.color()); + --current; + ++pastDice; } - m_diceResult->setPrevious(previousResult); - m_result= m_diceResult; - } - if(nullptr != m_nextNode) - { - m_nextNode->run(this); } + m_diceResult->setPrevious(previousResult); + m_result= m_diceResult; } QString PainterNode::toString(bool wl) const diff --git a/src/libparser/node/parenthesesnode.cpp b/src/libparser/node/parenthesesnode.cpp index 9557536..d122fe2 100644 --- a/src/libparser/node/parenthesesnode.cpp +++ b/src/libparser/node/parenthesesnode.cpp @@ -29,21 +29,16 @@ void ParenthesesNode::setInternelNode(ExecutionNode* node) void ParenthesesNode::run(ExecutionNode* previous) { m_previousNode= previous; - if(nullptr != m_internalNode) - { - m_internalNode->run(this); - ExecutionNode* temp= m_internalNode; - while(nullptr != temp->getNextNode()) - { - temp= temp->getNextNode(); - } - m_result= temp->getResult(); - } + if(!m_internalNode) + return; - if(nullptr != m_nextNode) + m_internalNode->execute(this); + ExecutionNode* temp= m_internalNode; + while(nullptr != temp->getNextNode()) { - m_nextNode->run(this); + temp= temp->getNextNode(); } + m_result= temp->getResult(); } QString ParenthesesNode::toString(bool b) const { diff --git a/src/libparser/node/repeaternode.cpp b/src/libparser/node/repeaternode.cpp index f93a9fe..320e0d4 100644 --- a/src/libparser/node/repeaternode.cpp +++ b/src/libparser/node/repeaternode.cpp @@ -70,7 +70,7 @@ void RepeaterNode::run(ExecutionNode* previousNode) if(nullptr == m_times || m_cmd.empty()) return; - m_times->run(this); + m_times->execute(this); m_times= ParsingToolBox::getLeafNode(m_times); auto times= m_times->getResult(); if(!times) @@ -86,7 +86,7 @@ void RepeaterNode::run(ExecutionNode* previousNode) std::for_each(cmd.begin(), cmd.end(), [this, &resultVec](ExecutionNode* node) { - node->run(this); + node->execute(this); auto end= ParsingToolBox::getLeafNode(node); auto leafResult= end->getResult(); @@ -127,8 +127,8 @@ void RepeaterNode::run(ExecutionNode* previousNode) // qDebug().noquote() << listOfStrResult.join('\n'); } - if(nullptr != m_nextNode) - m_nextNode->run(this); + /*if(nullptr != m_nextNode) + m_nextNode->run(this);*/ } QString RepeaterNode::toString(bool withLabel) const diff --git a/src/libparser/node/replacevaluenode.cpp b/src/libparser/node/replacevaluenode.cpp index c313fb0..0ab5cfd 100644 --- a/src/libparser/node/replacevaluenode.cpp +++ b/src/libparser/node/replacevaluenode.cpp @@ -35,24 +35,18 @@ void ReplaceValueNode::setStopAtFirt(bool b) void ReplaceValueNode::run(ExecutionNode* previous) { - m_previousNode= previous; - if(nullptr == previous) - { - m_errors.insert(Dice::ERROR_CODE::NO_PREVIOUS_ERROR, - QStringLiteral("No previous node before Switch/Case operator")); + if(isValid(!previous, Dice::ERROR_CODE::NO_PREVIOUS_ERROR, tr("No previous node before Switch/Case operator"))) return; - } + m_previousNode= previous; + auto previousResult= previous->getResult(); m_result->setPrevious(previousResult); - if(nullptr == previousResult - || (!previousResult->hasResultOfType(Dice::RESULT_TYPE::SCALAR) - && !previousResult->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST))) - { - m_errors.insert(Dice::ERROR_CODE::NO_VALID_RESULT, - QStringLiteral("No scalar or dice result before Switch/Case operator")); + if(isValid(!previousResult + || (!previousResult->hasResultOfType(Dice::RESULT_TYPE::SCALAR) + && !previousResult->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST)), + Dice::ERROR_CODE::NO_VALID_RESULT, tr("No scalar or dice result before Switch/Case operator"))) return; - } QList<Die*> dieList; if(previousResult->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST)) @@ -65,7 +59,7 @@ void ReplaceValueNode::run(ExecutionNode* previous) for(auto die : dieList) { QStringList resultList; - for(auto const& info : qAsConst(m_branchList)) + for(auto const& info : std::as_const(m_branchList)) { if(info->validatorList) { @@ -83,11 +77,6 @@ void ReplaceValueNode::run(ExecutionNode* previous) } m_diceResult->insertResult(die); } - - if(nullptr != m_nextNode) - { - m_nextNode->run(this); - } } QString ReplaceValueNode::toString(bool withLabel) const @@ -115,7 +104,7 @@ qint64 ReplaceValueNode::getPriority() const ExecutionNode* ReplaceValueNode::getCopy() const { ReplaceValueNode* node= new ReplaceValueNode(); - for(auto const& info : qAsConst(m_branchList)) + for(auto const& info : std::as_const(m_branchList)) { node->insertCase(info->node, info->validatorList); } diff --git a/src/libparser/node/rerolldicenode.cpp b/src/libparser/node/rerolldicenode.cpp index fd8c258..af075ce 100644 --- a/src/libparser/node/rerolldicenode.cpp +++ b/src/libparser/node/rerolldicenode.cpp @@ -19,85 +19,80 @@ RerollDiceNode::~RerollDiceNode() } void RerollDiceNode::run(ExecutionNode* previous) { + if(isValid(!previous, Dice::ERROR_CODE::NO_PREVIOUS_ERROR, tr("No Previous node"))) + return; + m_previousNode= previous; - if((nullptr != previous) && (nullptr != previous->getResult())) + + auto previousDiceResult= previous->getResult(); + if(isValid(!previousDiceResult, Dice::ERROR_CODE::NO_VALID_RESULT, tr("No Valid result"))) + return; + + DiceResult* previous_result= dynamic_cast<DiceResult*>(previousDiceResult); + if(isValid(!previous_result, Dice::ERROR_CODE::DIE_RESULT_EXPECTED, + tr(" The a operator expects dice result. Please check the documentation and fix your command."))) + return; + + m_result->setPrevious(previous_result); + + for(auto& die : previous_result->getResultList()) { - DiceResult* previous_result= dynamic_cast<DiceResult*>(previous->getResult()); - m_result->setPrevious(previous_result); - if(nullptr != previous_result) - { - for(auto& die : previous_result->getResultList()) - { - Die* tmpdie= new Die(*die); - m_diceResult->insertResult(tmpdie); - die->displayed(); - } - // m_diceResult->setResultList(list); + Die* tmpdie= new Die(*die); + m_diceResult->insertResult(tmpdie); + die->displayed(); + } + // m_diceResult->setResultList(list); - QList<Die*>& list= m_diceResult->getResultList(); - QList<Die*> toRemove; + QList<Die*>& list= m_diceResult->getResultList(); + QList<Die*> toRemove; - for(auto& die : list) + for(auto& die : list) + { + bool finished= false; + auto state + = m_validatorList->isValidRangeSize(std::make_pair<qint64, qint64>(die->getBase(), die->getMaxValue())); + if((Dice::CONDITION_STATE::ALWAYSTRUE == state && m_adding) + || (!m_reroll && !m_adding && state == Dice::CONDITION_STATE::UNREACHABLE)) + { + m_errors.insert(Dice::ERROR_CODE::ENDLESS_LOOP_ERROR, + QObject::tr("Condition (%1) cause an endless loop with this dice: %2") + .arg(toString(true)) + .arg(QStringLiteral("d[%1,%2]") + .arg(static_cast<int>(die->getBase())) + .arg(static_cast<int>(die->getMaxValue())))); + continue; + } + while(m_validatorList->hasValid(die, false) && !finished) + { + if(m_instruction != nullptr) { - bool finished= false; - auto state= m_validatorList->isValidRangeSize( - std::make_pair<qint64, qint64>(die->getBase(), die->getMaxValue())); - if((Dice::CONDITION_STATE::ALWAYSTRUE == state && m_adding) - || (!m_reroll && !m_adding && state == Dice::CONDITION_STATE::UNREACHABLE)) + m_instruction->execute(this); + auto lastNode= ParsingToolBox::getLeafNode(m_instruction); + if(lastNode != nullptr) { - m_errors.insert(Dice::ERROR_CODE::ENDLESS_LOOP_ERROR, - QObject::tr("Condition (%1) cause an endless loop with this dice: %2") - .arg(toString(true)) - .arg(QStringLiteral("d[%1,%2]") - .arg(static_cast<int>(die->getBase())) - .arg(static_cast<int>(die->getMaxValue())))); - continue; - } - while(m_validatorList->hasValid(die, false) && !finished) - { - if(m_instruction != nullptr) - { - m_instruction->run(this); - auto lastNode= ParsingToolBox::getLeafNode(m_instruction); - if(lastNode != nullptr) - { - auto lastResult= dynamic_cast<DiceResult*>(lastNode->getResult()); - if(lastResult != nullptr) - { - toRemove.append(die); - list.append(lastResult->getResultList()); - lastResult->clear(); - } - } - } - else + auto lastResult= dynamic_cast<DiceResult*>(lastNode->getResult()); + if(lastResult != nullptr) { - die->roll(m_adding); - } - if(m_reroll) - { - finished= true; + toRemove.append(die); + list.append(lastResult->getResultList()); + lastResult->clear(); } } } - - for(auto die : toRemove) + else { - list.removeOne(die); + die->roll(m_adding); } - - if(nullptr != m_nextNode) + if(m_reroll) { - m_nextNode->run(this); + finished= true; } } - else - { - m_errors.insert( - Dice::ERROR_CODE::DIE_RESULT_EXPECTED, - QObject::tr( - " The a operator expects dice result. Please check the documentation and fix your command.")); - } + } + + for(auto die : toRemove) + { + list.removeOne(die); } } void RerollDiceNode::setValidatorList(ValidatorList* val) diff --git a/src/libparser/node/scalaroperatornode.cpp b/src/libparser/node/scalaroperatornode.cpp index c1c4dc5..40f725d 100644 --- a/src/libparser/node/scalaroperatornode.cpp +++ b/src/libparser/node/scalaroperatornode.cpp @@ -40,74 +40,68 @@ ScalarOperatorNode::~ScalarOperatorNode() void ScalarOperatorNode::run(ExecutionNode* previous) { + if(isValid(!previous, Dice::ERROR_CODE::NO_PREVIOUS_ERROR, tr("No Previous node"))) + return; + m_previousNode= previous; - if(nullptr != m_internalNode) - { - m_internalNode->run(this); - } - if(nullptr != previous) - { - auto previousResult= previous->getResult(); - if(nullptr != previousResult) - { - ExecutionNode* internal= m_internalNode; - if(nullptr != internal) - { - while(nullptr != internal->getNextNode()) - { - internal= internal->getNextNode(); - } + if(isValid(!previous, Dice::ERROR_CODE::NO_PREVIOUS_ERROR, tr("No Previous node"))) + return; - Result* internalResult= internal->getResult(); - m_result->setPrevious(internalResult); - if(nullptr != m_internalNode->getResult()) - { - m_internalNode->getResult()->setPrevious(previousResult); - } + if(isValid(!m_internalNode, Dice::ERROR_CODE::NO_INTERNAL_INSTRUCTION, tr("No internal node to run"))) + return; - if(internalResult == nullptr) - { - m_errors.insert(Dice::ERROR_CODE::NO_VALID_RESULT, - QObject::tr("No Valid result in arithmetic operation: %1").arg(toString(true))); - return; - } + m_internalNode->execute(this); - switch(m_arithmeticOperator) - { - case Dice::ArithmeticOperator::PLUS: - m_scalarResult->setValue(add(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), - internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal())); - break; - case Dice::ArithmeticOperator::MINUS: - m_scalarResult->setValue(substract(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), - internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal())); - break; - case Dice::ArithmeticOperator::MULTIPLICATION: - m_scalarResult->setValue(multiple(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), - internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal())); - break; - case Dice::ArithmeticOperator::DIVIDE: - m_scalarResult->setValue(divide(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), - internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal())); - break; - case Dice::ArithmeticOperator::INTEGER_DIVIDE: - m_scalarResult->setValue( - static_cast<int>(divide(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), - internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal()))); - break; - case Dice::ArithmeticOperator::POW: - m_scalarResult->setValue(pow(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), - internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal())); - break; - } - } + auto previousResult= previous->getResult(); + if(isValid(!previousResult, Dice::ERROR_CODE::NO_VALID_RESULT, tr("No valid result"))) + return; - if(nullptr != m_nextNode) - { - m_nextNode->run(this); - } - } + ExecutionNode* internal= m_internalNode; + + while(nullptr != internal->getNextNode()) + { + internal= internal->getNextNode(); + } + + Result* internalResult= internal->getResult(); + m_result->setPrevious(internalResult); + if(nullptr != m_internalNode->getResult()) + { + m_internalNode->getResult()->setPrevious(previousResult); + } + + if(isValid(!internalResult, Dice::ERROR_CODE::NO_VALID_RESULT, + tr("No Valid result in arithmetic operation: %1").arg(toString(true)))) + return; + + switch(m_arithmeticOperator) + { + case Dice::ArithmeticOperator::PLUS: + m_scalarResult->setValue(add(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), + internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal())); + break; + case Dice::ArithmeticOperator::MINUS: + m_scalarResult->setValue(substract(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), + internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal())); + break; + case Dice::ArithmeticOperator::MULTIPLICATION: + m_scalarResult->setValue(multiple(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), + internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal())); + break; + case Dice::ArithmeticOperator::DIVIDE: + m_scalarResult->setValue(divide(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), + internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal())); + break; + case Dice::ArithmeticOperator::INTEGER_DIVIDE: + m_scalarResult->setValue( + static_cast<int>(divide(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), + internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal()))); + break; + case Dice::ArithmeticOperator::POW: + m_scalarResult->setValue(pow(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), + internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal())); + break; } } /*bool ScalarOperatorNode::setOperatorChar(QChar c) diff --git a/src/libparser/node/sortresult.cpp b/src/libparser/node/sortresult.cpp index 5d514cf..2039589 100644 --- a/src/libparser/node/sortresult.cpp +++ b/src/libparser/node/sortresult.cpp @@ -30,41 +30,23 @@ SortResultNode::SortResultNode() : m_diceResult(new DiceResult) } void SortResultNode::run(ExecutionNode* previous) { - m_previousNode= previous; - if(nullptr == previous) - { + if(isValid(!previous, Dice::ERROR_CODE::NO_PREVIOUS_ERROR, tr("No Previous node"))) return; - } + m_previousNode= previous; + DiceResult* previousDiceResult= dynamic_cast<DiceResult*>(previous->getResult()); m_diceResult->setPrevious(previousDiceResult); - if(nullptr == previousDiceResult) + if(isValid(!previousDiceResult, Dice::ERROR_CODE::NO_VALID_RESULT, tr("No Valid dice result"))) return; auto const& diceList= previousDiceResult->getResultList(); QList<Die*> diceList2= m_diceResult->getResultList(); - /* const auto& asce = [](const Die* a,const Die* b){ - return a->getValue() < b->getValue(); - }; - const auto& desc = [](const Die* a,const Die* b){ - return a->getValue() > b->getValue(); - }; - - for(auto const dice : diceList) - { - Die* tmp1 = new Die(*dice); - diceList2.append(tmp1); - } - if(m_ascending) - std::sort(diceList2.begin(), diceList2.end(), asce); - else - std::sort(diceList2.begin(), diceList2.end(), desc);*/ - // half-interval search sorting for(int i= 0; i < diceList.size(); ++i) { Die* tmp1= new Die(*diceList[i]); - //qDebug() << tmp1->getColor() << diceList[i]->getColor(); + // qDebug() << tmp1->getColor() << diceList[i]->getColor(); //*tmp1=*diceList[i]; diceList[i]->displayed(); @@ -106,10 +88,6 @@ void SortResultNode::run(ExecutionNode* previous) } } m_diceResult->setResultList(diceList2); - if(nullptr != m_nextNode) - { - m_nextNode->run(this); - } } void SortResultNode::setSortAscending(bool asc) { diff --git a/src/libparser/node/splitnode.cpp b/src/libparser/node/splitnode.cpp index 8faa0a5..ff5fc40 100644 --- a/src/libparser/node/splitnode.cpp +++ b/src/libparser/node/splitnode.cpp @@ -27,39 +27,35 @@ SplitNode::SplitNode() : m_diceResult(new DiceResult()) } void SplitNode::run(ExecutionNode* previous) { + if(isValid(!previous, Dice::ERROR_CODE::NO_PREVIOUS_ERROR, tr("No Previous node"))) + return; m_previousNode= previous; - if(nullptr != previous) - { - m_result->setPrevious(previous->getResult()); - Result* tmpResult= previous->getResult(); - if(nullptr != tmpResult) + m_result->setPrevious(previous->getResult()); + + Result* tmpResult= previous->getResult(); + if(isValid(!tmpResult, Dice::ERROR_CODE::NO_VALID_RESULT, tr("No Valid result"))) + return; + + DiceResult* dice= dynamic_cast<DiceResult*>(tmpResult); + if(isValid(!dice, Dice::ERROR_CODE::NO_VALID_RESULT, tr("No Valid dice result"))) + return; + + for(auto& oldDie : dice->getResultList()) + { + oldDie->displayed(); + m_diceResult->setOperator(oldDie->getOp()); + for(qint64& value : oldDie->getListValue()) { - DiceResult* dice= dynamic_cast<DiceResult*>(tmpResult); - if(nullptr != dice) - { - for(auto& oldDie : dice->getResultList()) - { - oldDie->displayed(); - m_diceResult->setOperator(oldDie->getOp()); - for(qint64& value : oldDie->getListValue()) - { - Die* tmpdie= new Die(); - tmpdie->insertRollValue(value); - tmpdie->setBase(oldDie->getBase()); - tmpdie->setMaxValue(oldDie->getMaxValue()); - tmpdie->setValue(value); - tmpdie->setOp(oldDie->getOp()); - m_diceResult->insertResult(tmpdie); - } - } - } + Die* tmpdie= new Die(); + tmpdie->insertRollValue(value); + tmpdie->setBase(oldDie->getBase()); + tmpdie->setMaxValue(oldDie->getMaxValue()); + tmpdie->setValue(value); + tmpdie->setOp(oldDie->getOp()); + m_diceResult->insertResult(tmpdie); } } - if(nullptr != m_nextNode) - { - m_nextNode->run(this); - } } QString SplitNode::toString(bool withLabel) const diff --git a/src/libparser/node/startingnode.cpp b/src/libparser/node/startingnode.cpp index 97248c6..ef2c2ca 100644 --- a/src/libparser/node/startingnode.cpp +++ b/src/libparser/node/startingnode.cpp @@ -24,10 +24,10 @@ StartingNode::StartingNode() {} void StartingNode::run(ExecutionNode*) { m_previousNode= nullptr; - if(nullptr != m_nextNode) + /*if(nullptr != m_nextNode) { m_nextNode->run(this); - } + }*/ } QString StartingNode::toString(bool withlabel) const { diff --git a/src/libparser/node/stringnode.cpp b/src/libparser/node/stringnode.cpp index e908463..6f41956 100644 --- a/src/libparser/node/stringnode.cpp +++ b/src/libparser/node/stringnode.cpp @@ -8,14 +8,8 @@ StringNode::StringNode() : m_stringResult(new StringResult()) void StringNode::run(ExecutionNode* previous) { m_previousNode= previous; - if(nullptr != previous) - { + if(previous) m_result->setPrevious(previous->getResult()); - } - if(nullptr != m_nextNode) - { - m_nextNode->run(this); - } } void StringNode::setString(QString str) diff --git a/src/libparser/node/switchcasenode.cpp b/src/libparser/node/switchcasenode.cpp index c48d81f..61942ca 100644 --- a/src/libparser/node/switchcasenode.cpp +++ b/src/libparser/node/switchcasenode.cpp @@ -36,24 +36,19 @@ void SwitchCaseNode::setStopAtFirt(bool b) void SwitchCaseNode::run(ExecutionNode* previous) { - m_previousNode= previous; - if(nullptr == previous) - { - m_errors.insert(Dice::ERROR_CODE::NO_PREVIOUS_ERROR, - QStringLiteral("No previous node before Switch/Case operator")); + if(isValid(!previous, Dice::ERROR_CODE::NO_PREVIOUS_ERROR, tr("No previous node before Switch/Case operator"))) return; - } + + m_previousNode= previous; + auto previousResult= previous->getResult(); m_result->setPrevious(previousResult); - if(nullptr == previousResult - || (!previousResult->hasResultOfType(Dice::RESULT_TYPE::SCALAR) - && !previousResult->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST))) - { - m_errors.insert(Dice::ERROR_CODE::NO_VALID_RESULT, - QStringLiteral("No scalar or dice result before Switch/Case operator")); + if(isValid(!previousResult + || (!previousResult->hasResultOfType(Dice::RESULT_TYPE::SCALAR) + && !previousResult->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST)), + Dice::ERROR_CODE::NO_VALID_RESULT, tr("No scalar or dice result before Switch/Case operator"))) return; - } auto diceResult= dynamic_cast<DiceResult*>(previousResult); @@ -63,7 +58,7 @@ void SwitchCaseNode::run(ExecutionNode* previous) for(auto die : diceResult->getResultList()) { QStringList resultList; - for(auto const& info : qAsConst(m_branchList)) + for(auto const& info : std::as_const(m_branchList)) { if(m_stopAtFirst && !resultList.isEmpty()) break; @@ -80,7 +75,7 @@ void SwitchCaseNode::run(ExecutionNode* previous) } else if(resultList.isEmpty()) { - info->node->run(m_previousNode); + info->node->execute(m_previousNode); auto lastNode= ParsingToolBox::getLeafNode(info->node); if(lastNode && lastNode->getResult()) { @@ -96,7 +91,7 @@ void SwitchCaseNode::run(ExecutionNode* previous) else { auto scalar= previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(); - for(auto const& info : qAsConst(m_branchList)) + for(auto const& info : std::as_const(m_branchList)) { if(m_stopAtFirst && !finalResultList.isEmpty()) break; @@ -116,7 +111,7 @@ void SwitchCaseNode::run(ExecutionNode* previous) } else if(finalResultList.isEmpty()) { - info->node->run(m_previousNode); + info->node->execute(m_previousNode); auto lastNode= ParsingToolBox::getLeafNode(info->node); if(lastNode && lastNode->getResult()) { @@ -128,16 +123,12 @@ void SwitchCaseNode::run(ExecutionNode* previous) } } - for(auto const& str : qAsConst(finalResultList)) + for(auto const& str : std::as_const(finalResultList)) m_stringResult->addText(str); - if(m_stringResult->getText().isEmpty()) - m_errors.insert(Dice::ERROR_CODE::NO_VALID_RESULT, QStringLiteral("No value fits the Switch/Case operator")); - - if(nullptr != m_nextNode) - { - m_nextNode->run(this); - } + if(isValid(m_stringResult->getText().isEmpty(), Dice::ERROR_CODE::NO_VALID_RESULT, + tr("No value fits the Switch/Case operator"))) + return; } QString SwitchCaseNode::toString(bool withLabel) const @@ -165,7 +156,7 @@ qint64 SwitchCaseNode::getPriority() const ExecutionNode* SwitchCaseNode::getCopy() const { SwitchCaseNode* node= new SwitchCaseNode(); - for(auto const& info : qAsConst(m_branchList)) + for(auto const& info : std::as_const(m_branchList)) { node->insertCase(info->node, info->validatorList); } diff --git a/src/libparser/node/uniquenode.cpp b/src/libparser/node/uniquenode.cpp index c4668be..e5ede7b 100644 --- a/src/libparser/node/uniquenode.cpp +++ b/src/libparser/node/uniquenode.cpp @@ -27,38 +27,34 @@ UniqueNode::UniqueNode() : m_diceResult(new DiceResult()) } void UniqueNode::run(ExecutionNode* previous) { + if(isValid(!previous, Dice::ERROR_CODE::NO_PREVIOUS_ERROR, tr("No Previous node"))) + return; m_previousNode= previous; - if(nullptr != previous) + + m_result->setPrevious(previous->getResult()); + Result* tmpResult= previous->getResult(); + if(isValid(!tmpResult, Dice::ERROR_CODE::NO_VALID_RESULT, tr("No Valid result"))) + return; + + DiceResult* dice= dynamic_cast<DiceResult*>(tmpResult); + if(isValid(!dice, Dice::ERROR_CODE::NO_VALID_RESULT, tr("No Valid dice result"))) + return; + + auto const& resultList= dice->getResultList(); + std::vector<qint64> formerValues; + formerValues.reserve(resultList.size()); + for(auto& oldDie : resultList) { - m_result->setPrevious(previous->getResult()); - Result* tmpResult= previous->getResult(); - if(nullptr != tmpResult) - { - DiceResult* dice= dynamic_cast<DiceResult*>(tmpResult); - if(nullptr != dice) - { - auto const& resultList= dice->getResultList(); - std::vector<qint64> formerValues; - formerValues.reserve(resultList.size()); - for(auto& oldDie : resultList) - { - auto value= oldDie->getValue(); - auto it= std::find(formerValues.begin(), formerValues.end(), value); + auto value= oldDie->getValue(); + auto it= std::find(formerValues.begin(), formerValues.end(), value); - if(it == formerValues.end()) - { - auto die= new Die(*oldDie); - m_diceResult->insertResult(die); - formerValues.push_back(value); - } - oldDie->displayed(); - } - } + if(it == formerValues.end()) + { + auto die= new Die(*oldDie); + m_diceResult->insertResult(die); + formerValues.push_back(value); } - } - if(nullptr != m_nextNode) - { - m_nextNode->run(this); + oldDie->displayed(); } } diff --git a/src/libparser/node/valueslistnode.cpp b/src/libparser/node/valueslistnode.cpp index 33a347d..588dae2 100644 --- a/src/libparser/node/valueslistnode.cpp +++ b/src/libparser/node/valueslistnode.cpp @@ -12,7 +12,7 @@ void ValuesListNode::run(ExecutionNode* previous) m_previousNode= previous; for(auto node : m_data) { - node->run(this); + node->execute(this); auto result= node->getResult(); if(!result) continue; @@ -24,11 +24,6 @@ void ValuesListNode::run(ExecutionNode* previous) die->insertRollValue(val); m_diceResult->insertResult(die); } - - if(nullptr != m_nextNode) - { - m_nextNode->run(this); - } } void ValuesListNode::insertValue(ExecutionNode* value) diff --git a/src/libparser/node/variablenode.cpp b/src/libparser/node/variablenode.cpp index 709ab46..9de6759 100644 --- a/src/libparser/node/variablenode.cpp +++ b/src/libparser/node/variablenode.cpp @@ -8,35 +8,27 @@ VariableNode::VariableNode() {} void VariableNode::run(ExecutionNode* previous) { m_previousNode= previous; - if((nullptr != m_data) && (m_data->size() > m_index)) - { - auto value= (*m_data)[m_index]; - value= ParsingToolBox::getLeafNode(value); - if(nullptr == value) - return; - - auto result= value->getResult(); - if(!result) - return; - - m_result= result->getCopy(); - auto diceResult= dynamic_cast<DiceResult*>(result); - if(nullptr != diceResult) - { - for(auto& die : diceResult->getResultList()) - { - die->setDisplayed(false); - } - } - - if(nullptr != m_nextNode) - { - m_nextNode->run(this); - } - } - else + if(isValid(!m_data || (m_data->size() <= m_index), Dice::ERROR_CODE::NO_VARIBALE, + tr("No variable at index:%1").arg(m_index + 1))) + return; + + auto value= (*m_data)[m_index]; + value= ParsingToolBox::getLeafNode(value); + if(nullptr == value) + return; + + auto result= value->getResult(); + if(!result) + return; + + m_result= result->getCopy(); + auto diceResult= dynamic_cast<DiceResult*>(result); + if(isValid(!diceResult, Dice::ERROR_CODE::NO_VALID_RESULT, tr("No Valid dice result"), false)) + return; + + for(auto& die : diceResult->getResultList()) { - m_errors.insert(Dice::ERROR_CODE::NO_VARIBALE, QObject::tr("No variable at index:%1").arg(m_index + 1)); + die->setDisplayed(false); } } diff --git a/src/libparser/operationcondition.cpp b/src/libparser/operationcondition.cpp index 70ebab4..1e0f145 100644 --- a/src/libparser/operationcondition.cpp +++ b/src/libparser/operationcondition.cpp @@ -146,7 +146,9 @@ qint64 OperationCondition::valueToScalar() const m_value->run(nullptr); auto result= m_value->getResult(); - return result->getResult(Dice::RESULT_TYPE::SCALAR).toInt(); + if(result) + return result->getResult(Dice::RESULT_TYPE::SCALAR).toInt(); + return 0; } const std::set<qint64>& OperationCondition::getPossibleValues(const std::pair<qint64, qint64>& range) |