diff options
| author | 2018-04-10 20:43:24 +0200 | |
|---|---|---|
| committer | 2018-04-11 00:18:05 +0200 | |
| commit | d205f806d0bcfc119e2ecd3ea063288877064dde (patch) | |
| tree | e1bd4b4741143972aabdeeef2cb2146eac41265b | |
| parent | 826f21f33721f0c232e1440ab8c4d04a822aff8c (diff) | |
| download | OneRoll-d205f806d0bcfc119e2ecd3ea063288877064dde.tar.gz OneRoll-d205f806d0bcfc119e2ecd3ea063288877064dde.zip | |
-Add support for subcommand on reroll operator.
| -rw-r--r-- | diceparser.cpp | 33 | ||||
| -rw-r--r-- | diceparser.h | 2 | ||||
| -rw-r--r-- | node/rerolldicenode.cpp | 32 | ||||
| -rw-r--r-- | node/rerolldicenode.h | 8 |
4 files changed, 68 insertions, 7 deletions
diff --git a/diceparser.cpp b/diceparser.cpp index d76c474..f23cad4 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -955,7 +955,8 @@ bool DiceParser::readOption(QString& str,ExecutionNode* previous)//, { str=str.remove(0,tmp.size()); - switch(m_OptionOp->value(tmp)) + auto operatorName = m_OptionOp->value(tmp); + switch(operatorName) { case Keep: { @@ -1055,8 +1056,17 @@ bool DiceParser::readOption(QString& str,ExecutionNode* previous)//, Validator* validator = m_parsingToolbox->readCompositeValidator(str); if(nullptr!=validator) { - m_parsingToolbox->isValidValidator(previous,validator); + if(!m_parsingToolbox->isValidValidator(previous,validator)) + { + m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Validator is missing after the %1 operator. Please, change it") + .arg(operatorName == Reroll ? "r" : "a" )); + } RerollDiceNode* rerollNode = new RerollDiceNode(); + ExecutionNode* nodeParam = nullptr; + if(readParameterNode(str,nodeParam)) + { + rerollNode->setInstruction(nodeParam); + } if(m_OptionOp->value(tmp)==RerollAndAdd) { rerollNode->setAddingMode(true); @@ -1065,6 +1075,7 @@ bool DiceParser::readOption(QString& str,ExecutionNode* previous)//, previous->setNextNode(rerollNode); node = rerollNode; found = true; + } else { @@ -1182,6 +1193,24 @@ bool DiceParser::readIfInstruction(QString& str,ExecutionNode*& trueNode,Executi } return false; } + +bool DiceParser::readParameterNode(QString& str, ExecutionNode* & node) +{ + if(str.startsWith("(")) + { + str=str.remove(0,1); + if(readExpression(str,node)) + { + if(str.startsWith(")")) + { + str=str.remove(0,1); + return true; + } + } + } + return false; +} + bool DiceParser::readBlocInstruction(QString& str,ExecutionNode*& resultnode) { if(str.startsWith('{')) diff --git a/diceparser.h b/diceparser.h index 6c59bae..c3b5be2 100644 --- a/diceparser.h +++ b/diceparser.h @@ -219,6 +219,8 @@ public: bool readOptionFromNull(QString &str, ExecutionNode *&node); bool readInstructionList(QString &str); +protected: + bool readParameterNode(QString &str, ExecutionNode *&node); private: /** diff --git a/node/rerolldicenode.cpp b/node/rerolldicenode.cpp index 2ead761..f51eb9e 100644 --- a/node/rerolldicenode.cpp +++ b/node/rerolldicenode.cpp @@ -1,5 +1,5 @@ #include "rerolldicenode.h" - +#include "parsingtoolbox.h" RerollDiceNode::RerollDiceNode() : m_diceResult(new DiceResult()),m_adding(false),m_validator(nullptr) @@ -32,13 +32,29 @@ void RerollDiceNode::run(ExecutionNode* previous) } //m_diceResult->setResultList(list); - QList<Die*> list = m_diceResult->getResultList(); + QList<Die*>& list = m_diceResult->getResultList(); for(Die* die: list) { if(m_validator->hasValid(die,false)) { - die->roll(m_adding); + if(m_instruction != nullptr) + { + m_instruction->run(this); + auto lastNode = ParsingToolBox::getLatestNode(m_instruction); + if(lastNode != nullptr) + { + auto lastResult = dynamic_cast<DiceResult*>(lastNode->getResult()); + if(lastResult != nullptr) + { + list.append(lastResult->getResultList()); + } + } + } + else + { + die->roll(m_adding); + } } } @@ -96,3 +112,13 @@ ExecutionNode* RerollDiceNode::getCopy() const } return node; } + +ExecutionNode *RerollDiceNode::getInstruction() const +{ + return m_instruction; +} + +void RerollDiceNode::setInstruction(ExecutionNode *instruction) +{ + m_instruction = instruction; +} diff --git a/node/rerolldicenode.h b/node/rerolldicenode.h index f566d77..c999dda 100644 --- a/node/rerolldicenode.h +++ b/node/rerolldicenode.h @@ -58,10 +58,14 @@ public: */ virtual ExecutionNode* getCopy() const; + ExecutionNode *getInstruction() const; + void setInstruction(ExecutionNode *instruction); + private: - DiceResult* m_diceResult; + DiceResult* m_diceResult = nullptr; bool m_adding; - Validator* m_validator; + Validator* m_validator = nullptr; + ExecutionNode* m_instruction = nullptr; }; #endif // REROLLDICENODE_H |