diff options
| author | 2018-12-12 14:07:05 +0100 | |
|---|---|---|
| committer | 2018-12-12 14:07:05 +0100 | |
| commit | 1a904315abb8bf75de4c833b2dfdd1358452ca25 (patch) | |
| tree | 30a7f7ecc229e873203984f5f7eda93da93d66c3 /diceparser.cpp | |
| parent | 0bc3bcb30a50a81e818109b949c1c5a500c89fe7 (diff) | |
| download | OneRoll-1a904315abb8bf75de4c833b2dfdd1358452ca25.tar.gz OneRoll-1a904315abb8bf75de4c833b2dfdd1358452ca25.zip | |
add new way to display result in dot file
Diffstat (limited to 'diceparser.cpp')
| -rw-r--r-- | diceparser.cpp | 2141 |
1 files changed, 1072 insertions, 1069 deletions
diff --git a/diceparser.cpp b/diceparser.cpp index e7b2612..1ad8847 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -1,1399 +1,1402 @@ -/*************************************************************************** -* Copyright (C) 2014 by Renaud Guezennec * -* http://www.rolisteam.org/contact * -* * -* This file is part of DiceParser * -* * -* DiceParser is free software; you can redistribute it and/or modify * -* it under the terms of the GNU General Public License as published by * -* the Free Software Foundation; either version 2 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 General Public License for more details. * -* * -* You should have received a copy of the GNU General Public License * -* along with this program; if not, write to the * -* Free Software Foundation, Inc., * -* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * -***************************************************************************/ -#include "diceparser.h" -#include <QDebug> -#include <QStringList> -#include <QObject> -#include <QFile> -#include <functional> - -#include "node/startingnode.h" -#include "node/scalaroperatornode.h" -#include "node/filternode.h" -#include "node/numbernode.h" -#include "node/keepdiceexecnode.h" -#include "node/sortresult.h" -#include "node/countexecutenode.h" -#include "node/rerolldicenode.h" -#include "node/explodedicenode.h" -#include "node/parenthesesnode.h" -#include "node/helpnode.h" -#include "node/jumpbackwardnode.h" -#include "node/listsetrollnode.h" -#include "node/listaliasnode.h" -#include "node/mergenode.h" -#include "node/ifnode.h" -#include "node/paintnode.h" -#include "node/stringnode.h" -#include "node/splitnode.h" -#include "node/groupnode.h" -#include "node/variablenode.h" -#include "node/bind.h" - -#define DEFAULT_FACES_NUMBER 10 - -DiceParser::DiceParser() - //: //m_start(nullptr),m_current(nullptr) -{ - m_currentTreeHasSeparator =false; - m_parsingToolbox = new ParsingToolBox(); - ParsingToolBox::setStartNodes(&m_startNodes); - - - m_mapDiceOp = new QMap<QString,DiceOperator>(); - m_mapDiceOp->insert(QStringLiteral("D"),D); - m_mapDiceOp->insert(QStringLiteral("L"),L); - - m_OptionOp = new QMap<QString,OptionOperator>(); - m_OptionOp->insert(QStringLiteral("k"),Keep); - m_OptionOp->insert(QStringLiteral("K"),KeepAndExplode); - m_OptionOp->insert(QStringLiteral("s"),Sort); - m_OptionOp->insert(QStringLiteral("c"),Count); - m_OptionOp->insert(QStringLiteral("r"),Reroll); - m_OptionOp->insert(QStringLiteral("e"),Explode); - m_OptionOp->insert(QStringLiteral("R"),RerollUntil); - m_OptionOp->insert(QStringLiteral("a"),RerollAndAdd); - m_OptionOp->insert(QStringLiteral("m"),Merge); - m_OptionOp->insert(QStringLiteral("i"),ifOperator); - m_OptionOp->insert(QStringLiteral("p"),Painter); - m_OptionOp->insert(QStringLiteral("f"),Filter); - m_OptionOp->insert(QStringLiteral("u"),Split); - m_OptionOp->insert(QStringLiteral("g"),Group); - m_OptionOp->insert(QStringLiteral("b"),Bind); - - m_aliasList = new QList<DiceAlias*>(); - - m_nodeActionMap = new QMap<QString,NodeAction>(); - m_nodeActionMap->insert(QStringLiteral("@"),JumpBackward); - - m_commandList = new QStringList(); - m_commandList->append(QStringLiteral("help")); - m_commandList->append(QStringLiteral("la")); - -} -DiceParser::~DiceParser() -{ - if(nullptr!=m_commandList) + /*************************************************************************** + * Copyright (C) 2014 by Renaud Guezennec * + * http://www.rolisteam.org/contact * + * * + * This file is part of DiceParser * + * * + * DiceParser is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + #include "diceparser.h" + #include <QDebug> + #include <QStringList> + #include <QObject> + #include <QFile> + #include <functional> + + #include "node/startingnode.h" + #include "node/scalaroperatornode.h" + #include "node/filternode.h" + #include "node/numbernode.h" + #include "node/keepdiceexecnode.h" + #include "node/sortresult.h" + #include "node/countexecutenode.h" + #include "node/rerolldicenode.h" + #include "node/explodedicenode.h" + #include "node/parenthesesnode.h" + #include "node/helpnode.h" + #include "node/jumpbackwardnode.h" + #include "node/listsetrollnode.h" + #include "node/listaliasnode.h" + #include "node/mergenode.h" + #include "node/ifnode.h" + #include "node/paintnode.h" + #include "node/stringnode.h" + #include "node/splitnode.h" + #include "node/groupnode.h" + #include "node/variablenode.h" + #include "node/bind.h" + + #define DEFAULT_FACES_NUMBER 10 + + DiceParser::DiceParser() + //: //m_start(nullptr),m_current(nullptr) { - delete m_commandList; - m_commandList = nullptr; - } - if(nullptr!=m_nodeActionMap) - { - delete m_nodeActionMap; - m_nodeActionMap = nullptr; - } - if(nullptr!=m_OptionOp) - { - delete m_OptionOp; - m_OptionOp = nullptr; + m_currentTreeHasSeparator =false; + m_parsingToolbox = new ParsingToolBox(); + ParsingToolBox::setStartNodes(&m_startNodes); + + + m_mapDiceOp = new QMap<QString,DiceOperator>(); + m_mapDiceOp->insert(QStringLiteral("D"),D); + m_mapDiceOp->insert(QStringLiteral("L"),L); + + m_OptionOp = new QMap<QString,OptionOperator>(); + m_OptionOp->insert(QStringLiteral("k"),Keep); + m_OptionOp->insert(QStringLiteral("K"),KeepAndExplode); + m_OptionOp->insert(QStringLiteral("s"),Sort); + m_OptionOp->insert(QStringLiteral("c"),Count); + m_OptionOp->insert(QStringLiteral("r"),Reroll); + m_OptionOp->insert(QStringLiteral("e"),Explode); + m_OptionOp->insert(QStringLiteral("R"),RerollUntil); + m_OptionOp->insert(QStringLiteral("a"),RerollAndAdd); + m_OptionOp->insert(QStringLiteral("m"),Merge); + m_OptionOp->insert(QStringLiteral("i"),ifOperator); + m_OptionOp->insert(QStringLiteral("p"),Painter); + m_OptionOp->insert(QStringLiteral("f"),Filter); + m_OptionOp->insert(QStringLiteral("u"),Split); + m_OptionOp->insert(QStringLiteral("g"),Group); + m_OptionOp->insert(QStringLiteral("b"),Bind); + + m_aliasList = new QList<DiceAlias*>(); + + m_nodeActionMap = new QMap<QString,NodeAction>(); + m_nodeActionMap->insert(QStringLiteral("@"),JumpBackward); + + m_commandList = new QStringList(); + m_commandList->append(QStringLiteral("help")); + m_commandList->append(QStringLiteral("la")); + } - if(nullptr!=m_mapDiceOp) + DiceParser::~DiceParser() { - delete m_mapDiceOp; - m_mapDiceOp = nullptr; + if(nullptr!=m_commandList) + { + delete m_commandList; + m_commandList = nullptr; + } + if(nullptr!=m_nodeActionMap) + { + delete m_nodeActionMap; + m_nodeActionMap = nullptr; + } + if(nullptr!=m_OptionOp) + { + delete m_OptionOp; + m_OptionOp = nullptr; + } + if(nullptr!=m_mapDiceOp) + { + delete m_mapDiceOp; + m_mapDiceOp = nullptr; + } + if(nullptr!=m_parsingToolbox) + { + delete m_parsingToolbox; + m_parsingToolbox = nullptr; + } + if(nullptr!=m_aliasList) + { + delete m_aliasList; + m_aliasList = nullptr; + } + if(nullptr!=m_start) + { + delete m_start; + m_start = nullptr; + } } - if(nullptr!=m_parsingToolbox) + + QString DiceParser::convertAlias(QString str) { - delete m_parsingToolbox; - m_parsingToolbox = nullptr; + for(DiceAlias* cmd : *m_aliasList) + { + if(cmd->isEnable()) + { + cmd->resolved(str); + } + } + return str; } - if(nullptr!=m_aliasList) + QList<DiceAlias*>* DiceParser::getAliases() { - delete m_aliasList; - m_aliasList = nullptr; + return m_aliasList; } - if(nullptr!=m_start) + void DiceParser::insertAlias(DiceAlias* dice, int i) { - delete m_start; - m_start = nullptr; + if(i>=m_aliasList->size()) + { + m_aliasList->insert(i, dice); + } } -} -QString DiceParser::convertAlias(QString str) -{ - for(DiceAlias* cmd : *m_aliasList) + bool DiceParser::parseLine(QString str, bool allowAlias) { - if(cmd->isEnable()) + m_errorMap.clear(); + m_comment = QStringLiteral(""); + if(!m_startNodes.empty()) { - cmd->resolved(str); + qDeleteAll(m_startNodes); + m_startNodes.clear(); } - } - return str; -} -QList<DiceAlias*>* DiceParser::getAliases() -{ - return m_aliasList; -} -void DiceParser::insertAlias(DiceAlias* dice, int i) -{ - if(i>=m_aliasList->size()) - { - m_aliasList->insert(i, dice); - } -} + m_currentTreeHasSeparator=false; + if(allowAlias) + { + str = convertAlias(str); + } + m_command = str; + bool hasInstruction = readInstructionList(str); -bool DiceParser::parseLine(QString str, bool allowAlias) -{ - m_errorMap.clear(); - m_comment = QStringLiteral(""); - if(!m_startNodes.empty()) - { - qDeleteAll(m_startNodes); - m_startNodes.clear(); - } - m_currentTreeHasSeparator=false; - if(allowAlias) - { - str = convertAlias(str); - } - m_command = str; - bool hasInstruction = readInstructionList(str); + bool value = hasInstruction; + if(!hasInstruction) + { + m_errorMap.insert(ExecutionNode::NOTHING_UNDERSTOOD,QObject::tr("Nothing was understood. To roll dice: !1d6 - full documation: " + "<a href=\"https://github.com/Rolisteam/DiceParser/blob/master/HelpMe.md\">https://github.com/Rolisteam/DiceParser/blob/master/HelpMe.md</a>")); + } + else if(hasInstruction && !str.isEmpty()) + { + auto i = m_command.size()-str.size(); + m_warningMap.insert(ExecutionNode::UNEXPECTED_CHARACTER,QObject::tr("Unexpected character at %1 - end of command was ignored \"%2\"").arg(i).arg(str)); + } - bool value = hasInstruction; - if(!hasInstruction) - { - m_errorMap.insert(ExecutionNode::NOTHING_UNDERSTOOD,QObject::tr("Nothing was understood. To roll dice: !1d6 - full documation: " - "<a href=\"https://github.com/Rolisteam/DiceParser/blob/master/HelpMe.md\">https://github.com/Rolisteam/DiceParser/blob/master/HelpMe.md</a>")); - } - else if(hasInstruction && !str.isEmpty()) - { - auto i = m_command.size()-str.size(); - m_warningMap.insert(ExecutionNode::UNEXPECTED_CHARACTER,QObject::tr("Unexpected character at %1 - end of command was ignored \"%2\"").arg(i).arg(str)); + return value; } - return value; -} - -bool DiceParser::readExpression(QString& str,ExecutionNode* & node) -{ - ExecutionNode* operandNode=nullptr; - if(m_parsingToolbox->readOpenParentheses(str)) + bool DiceParser::readExpression(QString& str,ExecutionNode* & node) { - ExecutionNode* internalNode=nullptr; - if(readExpression(str,internalNode)) + ExecutionNode* operandNode=nullptr; + if(m_parsingToolbox->readOpenParentheses(str)) { - ParenthesesNode* parentheseNode = new ParenthesesNode(); - parentheseNode->setInternelNode(internalNode); - node = parentheseNode; - if(m_parsingToolbox->readCloseParentheses(str)) + ExecutionNode* internalNode=nullptr; + if(readExpression(str,internalNode)) { - ExecutionNode* diceNode=nullptr; - ExecutionNode* operatorNode=nullptr; - if(readDice(str, diceNode)) + ParenthesesNode* parentheseNode = new ParenthesesNode(); + parentheseNode->setInternelNode(internalNode); + node = parentheseNode; + if(m_parsingToolbox->readCloseParentheses(str)) { - parentheseNode->setNextNode(diceNode); + ExecutionNode* diceNode=nullptr; + ExecutionNode* operatorNode=nullptr; + if(readDice(str, diceNode)) + { + parentheseNode->setNextNode(diceNode); + } + else if(readExpression(str,operatorNode)) + { + parentheseNode->setNextNode(operatorNode); + } + return true; } - else if(readExpression(str,operatorNode)) + else { - parentheseNode->setNextNode(operatorNode); + m_warningMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Expected closing parenthesis - can't validate the inside.")); } - return true; } - else + } + else if(m_parsingToolbox->readOperand(str,operandNode)) + { + ExecutionNode* diceNode=nullptr; + if(readDice(str,diceNode)) { - m_warningMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Expected closing parenthesis - can't validate the inside.")); + operandNode->setNextNode(diceNode); } + node = operandNode; + + operandNode= ParsingToolBox::getLatestNode(operandNode); + ExecutionNode* operatorNode=nullptr; + while(readOperator(str,operatorNode)) + { + operandNode->setNextNode(operatorNode); + operandNode= ParsingToolBox::getLatestNode(operandNode); + }; + return true; } - } - else if(m_parsingToolbox->readOperand(str,operandNode)) - { - ExecutionNode* diceNode=nullptr; - if(readDice(str,diceNode)) + else if(readCommand(str,operandNode)) { - operandNode->setNextNode(diceNode); + node = operandNode; + return true; } - node = operandNode; - - operandNode= ParsingToolBox::getLatestNode(operandNode); - ExecutionNode* operatorNode=nullptr; - while(readOperator(str,operatorNode)) + else if(readNode(str,operandNode)) { - operandNode->setNextNode(operatorNode); - operandNode= ParsingToolBox::getLatestNode(operandNode); - }; - return true; - } - else if(readCommand(str,operandNode)) - { - node = operandNode; - return true; - } - else if(readNode(str,operandNode)) - { - node = operandNode; - return true; - } - else if(readOptionFromNull(str,operandNode)) - { - node = operandNode; - return true; - } - else if(readOperator(str,operandNode)) - { - node = operandNode; - return true; - } - else - { - ExecutionNode* diceNode=nullptr; - if(readDice(str,diceNode)) + node = operandNode; + return true; + } + else if(readOptionFromNull(str,operandNode)) + { + node = operandNode; + return true; + } + else if(readOperator(str,operandNode)) { - NumberNode* numberNode=new NumberNode(); - numberNode->setNumber(1); - numberNode->setNextNode(diceNode); - node = numberNode; + node = operandNode; return true; } else { - return false; + ExecutionNode* diceNode=nullptr; + if(readDice(str,diceNode)) + { + NumberNode* numberNode=new NumberNode(); + numberNode->setNumber(1); + numberNode->setNextNode(diceNode); + node = numberNode; + return true; + } + else + { + return false; + } } + return false; } - return false; -} -bool DiceParser::readOptionFromNull(QString& str,ExecutionNode* & node) -{ - StartingNode nodePrevious; - if(readOption(str,&nodePrevious)) - { - auto nodeNext = nodePrevious.getNextNode(); - nodePrevious.setNextNode(nullptr); - node = nodeNext; - return true; - } - return false; -} -bool DiceParser::readNode(QString& str,ExecutionNode* & node) -{ - QString key= str.left(1); - if(m_nodeActionMap->contains(key)) + bool DiceParser::readOptionFromNull(QString& str,ExecutionNode* & node) { - JumpBackwardNode* jumpNode = new JumpBackwardNode(); - node = jumpNode; - str=str.remove(0,1); - readOption(str,jumpNode); - return true; + StartingNode nodePrevious; + if(readOption(str,&nodePrevious)) + { + auto nodeNext = nodePrevious.getNextNode(); + nodePrevious.setNextNode(nullptr); + node = nodeNext; + return true; + } + return false; } - return false; -} - -void DiceParser::start() -{ - for(auto start : m_startNodes) + bool DiceParser::readNode(QString& str,ExecutionNode* & node) { - start->run(); + QString key= str.left(1); + if(m_nodeActionMap->contains(key)) + { + JumpBackwardNode* jumpNode = new JumpBackwardNode(); + node = jumpNode; + str=str.remove(0,1); + readOption(str,jumpNode); + return true; + } + return false; } -} -QString DiceParser::displayResult() -{ - QStringList resultList; - for(auto start : m_startNodes) + void DiceParser::start() { - ExecutionNode* next = start; - int nodeCount=0; - int resulCount=0; - while(nullptr != next->getNextNode() ) + for(auto start : m_startNodes) { - next = next->getNextNode(); - ++nodeCount; + start->run(); } - ////////////////////////////////// - // - // Display - // - ////////////////////////////////// - - QString str; - QTextStream stream(&str); - Result* result=next->getResult(); - - QString totalValue("you got %1 ;"); - QString dieValue("D%1 : {%2} "); + } - bool scalarDone=false; - while(nullptr!=result) + QString DiceParser::displayResult() + { + QStringList resultList; + for(auto start : m_startNodes) { - ++resulCount; - if((result->hasResultOfType(Result::SCALAR))&&(!scalarDone)) + ExecutionNode* next = start; + int nodeCount=0; + int resulCount=0; + while(nullptr != next->getNextNode() ) { - stream << totalValue.arg(result->getResult(Result::SCALAR).toReal()) << endl; //.arg(m_command) - scalarDone=true; + next = next->getNextNode(); + ++nodeCount; } - else if(result->hasResultOfType(Result::DICE_LIST)) - { + ////////////////////////////////// + // + // Display + // + ////////////////////////////////// - DiceResult* myDiceResult = dynamic_cast<DiceResult*>(result); - if(nullptr!=myDiceResult) + QString str; + QTextStream stream(&str); + Result* result=next->getResult(); + + QString totalValue("you got %1 ;"); + QString dieValue("D%1 : {%2} "); + + bool scalarDone=false; + while(nullptr!=result) + { + ++resulCount; + if((result->hasResultOfType(Result::SCALAR))&&(!scalarDone)) + { + stream << totalValue.arg(result->getResult(Result::SCALAR).toReal()) << endl; //.arg(m_command) + scalarDone=true; + } + else if(result->hasResultOfType(Result::DICE_LIST)) { - QString resulStr; - quint64 face=0; - for(Die* die : myDiceResult->getResultList()) + DiceResult* myDiceResult = dynamic_cast<DiceResult*>(result); + if(nullptr!=myDiceResult) { - if(!die->hasBeenDisplayed()) + + QString resulStr; + quint64 face=0; + for(Die* die : myDiceResult->getResultList()) { - resulStr+=QStringLiteral("%1").arg(die->getValue()); - die->displayed(); - face = die->getFaces(); + if(!die->hasBeenDisplayed()) + { + resulStr+=QStringLiteral("%1").arg(die->getValue()); + die->displayed(); + face = die->getFaces(); - if(die->hasChildrenValue()) - { - resulStr+=QStringLiteral(" ["); - for(qint64 i : die->getListValue()) + if(die->hasChildrenValue()) { - resulStr+=QStringLiteral("%1 ").arg(i); + resulStr+=QStringLiteral(" ["); + for(qint64 i : die->getListValue()) + { + resulStr+=QStringLiteral("%1 ").arg(i); + } + resulStr.remove(resulStr.size()-1,1); + resulStr+=QStringLiteral("]"); } - resulStr.remove(resulStr.size()-1,1); - resulStr+=QStringLiteral("]"); + resulStr+=QStringLiteral(", "); } - resulStr+=QStringLiteral(", "); } - } - resulStr.remove(resulStr.size()-2,2); + resulStr.remove(resulStr.size()-2,2); - if(!resulStr.isEmpty()) - { - stream << dieValue.arg(face).arg(resulStr); - } + if(!resulStr.isEmpty()) + { + stream << dieValue.arg(face).arg(resulStr); + } + } } - } - else if(result->hasResultOfType(Result::STRING)) - { - stream << result->getResult(Result::STRING).toString(); + else if(result->hasResultOfType(Result::STRING)) + { + stream << result->getResult(Result::STRING).toString(); + } + + result = result->getPrevious(); } - result = result->getPrevious(); - } + QTextStream out(stdout); + out << str << "you rolled: " <<m_command << endl; + out << endl; - QTextStream out(stdout); - out << str << "you rolled: " <<m_command << endl; - out << endl; + resultList << QStringLiteral("%1, you rolled:%3").arg(str.simplified()).arg(m_command) ; + } - resultList << QStringLiteral("%1, you rolled:%3").arg(str.simplified()).arg(m_command) ; + return resultList.join('\n'); } - - return resultList.join('\n'); -} -QList<qreal> DiceParser::getLastIntegerResults() -{ - QList<qreal> resultValues; - for(auto node : m_startNodes) + QList<qreal> DiceParser::getLastIntegerResults() { - ExecutionNode* next = getLeafNode(node); - Result* result=next->getResult(); - bool scalarDone = false; - while((result!=nullptr)&&(!scalarDone)) + QList<qreal> resultValues; + for(auto node : m_startNodes) { - if(result->hasResultOfType(Result::SCALAR)) + ExecutionNode* next = getLeafNode(node); + Result* result=next->getResult(); + bool scalarDone = false; + while((result!=nullptr)&&(!scalarDone)) { - resultValues << result->getResult(Result::SCALAR).toReal(); - scalarDone=true; + if(result->hasResultOfType(Result::SCALAR)) + { + resultValues << result->getResult(Result::SCALAR).toReal(); + scalarDone=true; + } + result=result->getPrevious(); } - result=result->getPrevious(); } + return resultValues; } - return resultValues; -} -QStringList DiceParser::getStringResult( ) -{ - QStringList stringListResult; - for(auto node : m_startNodes) + QStringList DiceParser::getStringResult( ) { - ExecutionNode* next = getLeafNode(node); - QString str; - Result* result=next->getResult(); - bool found = false; - while((nullptr!=result) && (!found) ) + QStringList stringListResult; + for(auto node : m_startNodes) { - if(result->hasResultOfType(Result::STRING)) + ExecutionNode* next = getLeafNode(node); + QString str; + Result* result=next->getResult(); + bool found = false; + while((nullptr!=result) && (!found) ) { - str = result->getResult(Result::STRING).toString(); - found = true; + if(result->hasResultOfType(Result::STRING)) + { + str = result->getResult(Result::STRING).toString(); + found = true; + } + result = result->getPrevious(); } - result = result->getPrevious(); + stringListResult << str; } - stringListResult << str; + return stringListResult; } - return stringListResult; -} -QStringList DiceParser::getAllStringResult(bool& hasAlias) -{ - //QStringList allResult; - QStringList stringListResult; - for(auto node : m_startNodes) + QStringList DiceParser::getAllStringResult(bool& hasAlias) { - ExecutionNode* next = getLeafNode(node); - Result* result=next->getResult(); - - while(nullptr!=result) + //QStringList allResult; + QStringList stringListResult; + for(auto node : m_startNodes) { - if(result->hasResultOfType(Result::STRING)) + ExecutionNode* next = getLeafNode(node); + Result* result=next->getResult(); + + while(nullptr!=result) { - StringResult* stringResult = dynamic_cast<StringResult*>(result); - if(nullptr!=stringResult) + if(result->hasResultOfType(Result::STRING)) { - stringListResult << stringResult->getText(); - hasAlias = stringResult->hasHighLight(); + StringResult* stringResult = dynamic_cast<StringResult*>(result); + if(nullptr!=stringResult) + { + stringListResult << stringResult->getText(); + hasAlias = stringResult->hasHighLight(); + } } + result = result->getPrevious(); } - result = result->getPrevious(); } + return stringListResult; } - return stringListResult; -} -QStringList DiceParser::getAllDiceResult(bool& hasAlias) -{ - QStringList stringListResult; - for(auto node : m_startNodes) + QStringList DiceParser::getAllDiceResult(bool& hasAlias) { - ExecutionNode* next = getLeafNode(node); - Result* result=next->getResult(); - QList<Die*> dieListResult; - - while(nullptr!=result) + QStringList stringListResult; + for(auto node : m_startNodes) { - if(result->hasResultOfType(Result::DICE_LIST)) + ExecutionNode* next = getLeafNode(node); + Result* result=next->getResult(); + QList<Die*> dieListResult; + + while(nullptr!=result) { - DiceResult* stringResult = dynamic_cast<DiceResult*>(result); - if(nullptr!=stringResult) + if(result->hasResultOfType(Result::DICE_LIST)) { - for(auto die : stringResult->getResultList()) + DiceResult* stringResult = dynamic_cast<DiceResult*>(result); + if(nullptr!=stringResult) { - if(!dieListResult.contains(die)) + for(auto die : stringResult->getResultList()) { - dieListResult.removeAll(die); - dieListResult << die; + if(!dieListResult.contains(die)) + { + dieListResult.removeAll(die); + dieListResult << die; + } } + hasAlias = true; } - hasAlias = true; } + result = result->getPrevious(); } - result = result->getPrevious(); - } - for(Die* die : dieListResult) - { - if(die->isHighlighted()) + for(Die* die : dieListResult) { - for(qint64 value : die->getListValue()) + if(die->isHighlighted()) { + for(qint64 value : die->getListValue()) + { - stringListResult << QString::number(value); + stringListResult << QString::number(value); + } } } } - } - return stringListResult; -} + return stringListResult; + } -void DiceParser::getDiceResultFromAllInstruction(QList<ExportedDiceResult>& resultList) -{ - for(auto start : m_startNodes) + void DiceParser::getDiceResultFromAllInstruction(QList<ExportedDiceResult>& resultList) { - ExecutionNode* next = getLeafNode(start); - Result* result=next->getResult(); - ExportedDiceResult nodeResult; - while(nullptr!=result) + for(auto start : m_startNodes) { - if(result->hasResultOfType(Result::DICE_LIST)) + ExecutionNode* next = getLeafNode(start); + Result* result=next->getResult(); + qDebug() << "result id" << result->toString(true); + ExportedDiceResult nodeResult; + while(nullptr!=result) { - DiceResult* diceResult = dynamic_cast<DiceResult*>(result); - QList<HighLightDice> list; - quint64 faces = 0; - - for(Die* die : diceResult->getResultList()) + if(result->hasResultOfType(Result::DICE_LIST)) { - faces = die->getFaces(); - HighLightDice hlDice(die->getListValue(),die->isHighlighted(),die->getColor(), die->hasBeenDisplayed(),die->getFaces()); - list.append(hlDice); + DiceResult* diceResult = dynamic_cast<DiceResult*>(result); + QList<HighLightDice> list; + quint64 faces = 0; + + for(Die* die : diceResult->getResultList()) + { + faces = die->getFaces(); + qDebug() << "face" << faces; + HighLightDice hlDice(die->getListValue(),die->isHighlighted(),die->getColor(), die->hasBeenDisplayed(),die->getFaces()); + list.append(hlDice); + } + nodeResult.insert(static_cast<int>(faces),list); } - nodeResult.insert(static_cast<int>(faces),list); + result = result->getPrevious(); } - result = result->getPrevious(); + resultList.append(nodeResult); } - resultList.append(nodeResult); + qDebug() << resultList.size(); } -} -void DiceParser::getLastDiceResult(QList<ExportedDiceResult>& diceValuesList,bool& homogeneous) -{ - for(auto start : m_startNodes) + void DiceParser::getLastDiceResult(QList<ExportedDiceResult>& diceValuesList,bool& homogeneous) { - ExportedDiceResult diceValues; - ExecutionNode* next = getLeafNode(start); - Result* result=next->getResult(); - while(nullptr!=result) + for(auto start : m_startNodes) { - if(result->hasResultOfType(Result::DICE_LIST)) + ExportedDiceResult diceValues; + ExecutionNode* next = getLeafNode(start); + Result* result=next->getResult(); + while(nullptr!=result) { - DiceResult* diceResult = dynamic_cast<DiceResult*>(result); - if(nullptr!=diceResult) + if(result->hasResultOfType(Result::DICE_LIST)) { - if(homogeneous) + DiceResult* diceResult = dynamic_cast<DiceResult*>(result); + if(nullptr!=diceResult) { + if(homogeneous) + { - homogeneous = diceResult->isHomogeneous(); - } - quint64 face=0; - ListDiceResult listpair; - for(Die* die : diceResult->getResultList()) - { - if(!die->hasBeenDisplayed()) + homogeneous = diceResult->isHomogeneous(); + } + quint64 face=0; + ListDiceResult listpair; + for(Die* die : diceResult->getResultList()) { - QList<qint64> valuesResult; - valuesResult.append(die->getValue()); - die->displayed(); - face = die->getFaces(); - if(die->hasChildrenValue()) + if(!die->hasBeenDisplayed()) { - for(qint64 i : die->getListValue()) + QList<qint64> valuesResult; + valuesResult.append(die->getValue()); + die->displayed(); + face = die->getFaces(); + if(die->hasChildrenValue()) { - valuesResult.append(i); + for(qint64 i : die->getListValue()) + { + valuesResult.append(i); + } } + HighLightDice hlDice(valuesResult,die->isHighlighted(),die->getColor(), die->hasBeenDisplayed(),0); + listpair.append(hlDice); } - HighLightDice hlDice(valuesResult,die->isHighlighted(),die->getColor(), die->hasBeenDisplayed(),0); - listpair.append(hlDice); } - } - if(!listpair.isEmpty()) - { - if(!diceValues.contains(face)) - { - diceValues.insert(face,listpair); - } - else + if(!listpair.isEmpty()) { - ListDiceResult tmp = diceValues.value(face); - tmp.append(listpair); - diceValues.insert(face,tmp); - } + if(!diceValues.contains(face)) + { + diceValues.insert(face,listpair); + } + else + { + ListDiceResult tmp = diceValues.value(face); + tmp.append(listpair); + diceValues.insert(face,tmp); + } + } } } + result = result->getPrevious(); + } + if(!diceValues.isEmpty()) + { + diceValuesList.append(diceValues); } - result = result->getPrevious(); - } - if(!diceValues.isEmpty()) - { - diceValuesList.append(diceValues); } } -} -QString DiceParser::getDiceCommand() const -{ - return m_command; -} - -bool DiceParser::hasIntegerResultNotInFirst() -{ - bool result= false; - for(auto node : m_startNodes) + QString DiceParser::getDiceCommand() const { - result |= hasResultOfType(Result::SCALAR,node); + return m_command; } - return result; -} -bool DiceParser::hasDiceResult() -{ - bool result= false; - for(auto node : m_startNodes) + bool DiceParser::hasIntegerResultNotInFirst() { - result |= hasResultOfType(Result::DICE_LIST,node); + bool result= false; + for(auto node : m_startNodes) + { + result |= hasResultOfType(Result::SCALAR,node); + } + return result; } - return result; -} -bool DiceParser::hasStringResult() -{ - bool result = false; - for(auto node : m_startNodes) + + bool DiceParser::hasDiceResult() { - result |= hasResultOfType(Result::STRING,node); + bool result= false; + for(auto node : m_startNodes) + { + result |= hasResultOfType(Result::DICE_LIST,node); + } + return result; } - return result; -} -bool DiceParser::hasResultOfType(Result::RESULT_TYPE type, ExecutionNode* node, bool notthelast) -{ - bool scalarDone = false; - ExecutionNode* next = getLeafNode(node); - Result* result=next->getResult(); - while((result!=nullptr)&&(!scalarDone)) + bool DiceParser::hasStringResult() { - if(result->hasResultOfType(type) && ((!notthelast)||(notthelast && (nullptr!=result->getPrevious())))) + bool result = false; + for(auto node : m_startNodes) { - scalarDone=true; + result |= hasResultOfType(Result::STRING,node); } - result=result->getPrevious(); + return result; } - return scalarDone; -} -QList<qreal> DiceParser::getSumOfDiceResult() -{ - QList<qreal> resultValues; - for(auto node : m_startNodes) + bool DiceParser::hasResultOfType(Result::RESULT_TYPE type, ExecutionNode* node, bool notthelast) { - qreal resultValue=0; + bool scalarDone = false; ExecutionNode* next = getLeafNode(node); Result* result=next->getResult(); - bool found = false; - while((nullptr!=result)&&(!found)) + while((result!=nullptr)&&(!scalarDone)) + { + if(result->hasResultOfType(type) && ((!notthelast)||(notthelast && (nullptr!=result->getPrevious())))) + { + scalarDone=true; + } + result=result->getPrevious(); + } + return scalarDone; + } + QList<qreal> DiceParser::getSumOfDiceResult() + { + QList<qreal> resultValues; + for(auto node : m_startNodes) { - if(result->hasResultOfType(Result::DICE_LIST)) + qreal resultValue=0; + ExecutionNode* next = getLeafNode(node); + Result* result=next->getResult(); + bool found = false; + while((nullptr!=result)&&(!found)) { - DiceResult* myDiceResult = dynamic_cast<DiceResult*>(result); - if(nullptr!=myDiceResult) + if(result->hasResultOfType(Result::DICE_LIST)) { - for(Die* die : myDiceResult->getResultList()) + DiceResult* myDiceResult = dynamic_cast<DiceResult*>(result); + if(nullptr!=myDiceResult) { - resultValue+=die->getValue(); + for(Die* die : myDiceResult->getResultList()) + { + resultValue+=die->getValue(); + } + found = true; } - found = true; } + result = result->getPrevious(); } - result = result->getPrevious(); + resultValues << resultValue; } - resultValues << resultValue; + return resultValues; } - return resultValues; -} -int DiceParser::getStartNodeCount() const -{ - return m_startNodes.size(); -} -ExecutionNode* DiceParser::getLeafNode(ExecutionNode* start) -{ - ExecutionNode* next = start; - while(nullptr != next->getNextNode() ) + int DiceParser::getStartNodeCount() const { - next = next->getNextNode(); + return m_startNodes.size(); } - return next; -} - -bool DiceParser::readDice(QString& str,ExecutionNode* & node) -{ - DiceOperator currentOperator; - - if(readDiceOperator(str,currentOperator)) + ExecutionNode* DiceParser::getLeafNode(ExecutionNode* start) { - if(currentOperator==D) + ExecutionNode* next = start; + while(nullptr != next->getNextNode() ) { - qint64 max; - qint64 min; - bool unique = (ParsingToolBox::UNIQUE == m_parsingToolbox->readListOperator(str)) ? - true : false; - Die::ArithmeticOperator op; + next = next->getNextNode(); + } + return next; + } - bool hasOp= m_parsingToolbox->readArithmeticOperator(str,op); - if(m_parsingToolbox->readNumber(str,max)) + bool DiceParser::readDice(QString& str,ExecutionNode* & node) + { + DiceOperator currentOperator; + + if(readDiceOperator(str,currentOperator)) + { + if(currentOperator==D) { - if(max<1) + qint64 max; + qint64 min; + bool unique = (ParsingToolBox::UNIQUE == m_parsingToolbox->readListOperator(str)) ? + true : false; + Die::ArithmeticOperator op; + + bool hasOp= m_parsingToolbox->readArithmeticOperator(str,op); + if(m_parsingToolbox->readNumber(str,max)) { - m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Dice with %1 face(s) does not exist. Please, put a value higher than 0").arg(max)); - return false; - } - DiceRollerNode* drNode = new DiceRollerNode(max); - drNode->setUnique(unique); - if(hasOp) - { - drNode->setOperator(op); + if(max<1) + { + m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Dice with %1 face(s) does not exist. Please, put a value higher than 0").arg(max)); + return false; + } + DiceRollerNode* drNode = new DiceRollerNode(max); + drNode->setUnique(unique); + if(hasOp) + { + drNode->setOperator(op); + } + node = drNode; + ExecutionNode* current = drNode; + while(readOption(str,current)) + { + current = ParsingToolBox::getLatestNode(current); + } + return true; } - node = drNode; - ExecutionNode* current = drNode; - while(readOption(str,current)) + else if(m_parsingToolbox->readDiceRange(str,min,max)) { - current = ParsingToolBox::getLatestNode(current); + DiceRollerNode* drNode = new DiceRollerNode(max,min); + drNode->setUnique(unique); + if(hasOp) + { + drNode->setOperator(op); + } + node = drNode; + ExecutionNode* current = drNode; + while(readOption(str,current)) + { + current = ParsingToolBox::getLatestNode(current); + } + return true; } - return true; } - else if(m_parsingToolbox->readDiceRange(str,min,max)) + else if(currentOperator ==L) { - DiceRollerNode* drNode = new DiceRollerNode(max,min); - drNode->setUnique(unique); - if(hasOp) + QStringList list; + QList<Range> listRange; + ParsingToolBox::LIST_OPERATOR op = m_parsingToolbox->readListOperator(str); + if(m_parsingToolbox->readList(str,list,listRange)) { - drNode->setOperator(op); + ListSetRollNode* lsrNode = new ListSetRollNode(); + lsrNode->setRangeList(listRange); + if(op == ParsingToolBox::UNIQUE) + { + lsrNode->setUnique(true); + } + lsrNode->setListValue(list); + node = lsrNode; + return true; } - node = drNode; - ExecutionNode* current = drNode; - while(readOption(str,current)) + else { - current = ParsingToolBox::getLatestNode(current); + m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("List is missing after the L operator. Please, add it (e.g : 1L[sword,spear,gun,arrow])")); } - return true; } + } - else if(currentOperator ==L) + + return false; + + } + bool DiceParser::readDiceOperator(QString& str,DiceOperator& op) + { + QStringList listKey = m_mapDiceOp->keys(); + for(const QString& key : listKey) { - QStringList list; - QList<Range> listRange; - ParsingToolBox::LIST_OPERATOR op = m_parsingToolbox->readListOperator(str); - if(m_parsingToolbox->readList(str,list,listRange)) + if(str.startsWith(key,Qt::CaseInsensitive)) { - ListSetRollNode* lsrNode = new ListSetRollNode(); - lsrNode->setRangeList(listRange); - if(op == ParsingToolBox::UNIQUE) - { - lsrNode->setUnique(true); - } - lsrNode->setListValue(list); - node = lsrNode; + str=str.remove(0,key.size()); + op = m_mapDiceOp->value(key); return true; } - else - { - m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("List is missing after the L operator. Please, add it (e.g : 1L[sword,spear,gun,arrow])")); - } } - + return false; } - - return false; - -} -bool DiceParser::readDiceOperator(QString& str,DiceOperator& op) -{ - QStringList listKey = m_mapDiceOp->keys(); - for(const QString& key : listKey) + bool DiceParser::readCommand(QString& str,ExecutionNode* & node) { - if(str.startsWith(key,Qt::CaseInsensitive)) + if(m_commandList->contains(str)) { - str=str.remove(0,key.size()); - op = m_mapDiceOp->value(key); + if(str== QLatin1String("help")) + { + HelpNode* help = new HelpNode(); + if(!m_helpPath.isEmpty()) + { + help->setHelpPath(m_helpPath); + } + node = help; + + } + else if(str== QLatin1String("la")) + { + node = new ListAliasNode(m_aliasList); + } return true; } + return false; } - return false; -} -bool DiceParser::readCommand(QString& str,ExecutionNode* & node) -{ - if(m_commandList->contains(str)) + + bool DiceParser::readDiceExpression(QString& str,ExecutionNode* & node) { - if(str== QLatin1String("help")) + bool returnVal=false; + + ExecutionNode* next = nullptr; + if(readDice(str,next)) { - HelpNode* help = new HelpNode(); - if(!m_helpPath.isEmpty()) + ExecutionNode* latest = next; + while(readOption(str,latest)) { - help->setHelpPath(m_helpPath); + while(nullptr!=latest->getNextNode()) + { + latest = latest->getNextNode(); + } } - node = help; + node = next; + returnVal = true; } - else if(str== QLatin1String("la")) + else { - node = new ListAliasNode(m_aliasList); + returnVal = false; } - return true; + return returnVal; } - return false; -} - -bool DiceParser::readDiceExpression(QString& str,ExecutionNode* & node) -{ - bool returnVal=false; - - ExecutionNode* next = nullptr; - if(readDice(str,next)) + bool DiceParser::readInstructionOperator(QChar c) { - ExecutionNode* latest = next; - while(readOption(str,latest)) + if(c == ';') { - while(nullptr!=latest->getNextNode()) - { - latest = latest->getNextNode(); - } + return true; } - - node = next; - returnVal = true; - } - else - { - returnVal = false; + return false; } - return returnVal; -} -bool DiceParser::readInstructionOperator(QChar c) -{ - if(c == ';') + bool DiceParser::readInstructionList(QString& str) { - return true; - } - return false; -} -bool DiceParser::readInstructionList(QString& str) -{ - if(str.isEmpty()) - return false; + if(str.isEmpty()) + return false; - bool hasInstruction = false; - bool readInstruction = true; - while(readInstruction) - { - ExecutionNode* startNode = nullptr; - bool keepParsing = readExpression(str,startNode); - if(nullptr != startNode) + bool hasInstruction = false; + bool readInstruction = true; + while(readInstruction) { - hasInstruction = true; - m_startNodes.push_back(startNode); - auto latest = startNode; - if(keepParsing) + ExecutionNode* startNode = nullptr; + bool keepParsing = readExpression(str,startNode); + if(nullptr != startNode) { - latest = ParsingToolBox::getLatestNode(latest); - keepParsing =!str.isEmpty(); - while(keepParsing) + hasInstruction = true; + m_startNodes.push_back(startNode); + auto latest = startNode; + if(keepParsing) { - auto before = str; - ExecutionNode* operatorNode = nullptr; - if(readOperator(str,operatorNode)) + latest = ParsingToolBox::getLatestNode(latest); + keepParsing =!str.isEmpty(); + while(keepParsing) { - latest->setNextNode(operatorNode); - latest = ParsingToolBox::getLatestNode(latest); + auto before = str; + ExecutionNode* operatorNode = nullptr; + if(readOperator(str,operatorNode)) + { + latest->setNextNode(operatorNode); + latest = ParsingToolBox::getLatestNode(latest); + } + keepParsing = (!str.isEmpty() & (before!=str)); } - keepParsing = (!str.isEmpty() & (before!=str)); } - } - if( !str.isEmpty() && readInstructionOperator(str[0])) - { - str=str.remove(0,1); + if( !str.isEmpty() && readInstructionOperator(str[0])) + { + str=str.remove(0,1); + } + else + { + QString result; + QString comment; + if(m_parsingToolbox->readComment(str,result,comment)) + { + m_command.remove(comment); + m_comment = result; + } + readInstruction = false; + } } else { - QString result; - QString comment; - if(m_parsingToolbox->readComment(str,result,comment)) - { - m_command.remove(comment); - m_comment = result; - } readInstruction = false; } } - else - { - readInstruction = false; - } + return hasInstruction; } - return hasInstruction; -} -bool DiceParser::readOperator(QString& str,ExecutionNode*& nodeResult) -{ - if(str.isEmpty() /*|| nullptr == previous*/) + bool DiceParser::readOperator(QString& str,ExecutionNode*& nodeResult) { - return false; - } + if(str.isEmpty() /*|| nullptr == previous*/) + { + return false; + } - Die::ArithmeticOperator op; - if(m_parsingToolbox->readArithmeticOperator(str,op)) - { - ScalarOperatorNode* node = new ScalarOperatorNode(); - node->setArithmeticOperator(op); - ExecutionNode* nodeExec = nullptr; - if(readExpression(str,nodeExec)) + Die::ArithmeticOperator op; + if(m_parsingToolbox->readArithmeticOperator(str,op)) { - node->setInternalNode(nodeExec); - if(nullptr==nodeExec) + ScalarOperatorNode* node = new ScalarOperatorNode(); + node->setArithmeticOperator(op); + ExecutionNode* nodeExec = nullptr; + if(readExpression(str,nodeExec)) { - delete node; - return false; - } - ExecutionNode* nodeExecOrChild = nodeExec; - ExecutionNode* parent = nullptr; + node->setInternalNode(nodeExec); + if(nullptr==nodeExec) + { + delete node; + return false; + } + ExecutionNode* nodeExecOrChild = nodeExec; + ExecutionNode* parent = nullptr; - while((nullptr!=nodeExecOrChild) && (node->getPriority()<nodeExecOrChild->getPriority())) - { - parent = nodeExecOrChild; - nodeExecOrChild = nodeExecOrChild->getNextNode(); - } - // management of operator priority - if((nullptr != nodeExecOrChild)&&(nodeExec != nodeExecOrChild)) - { - // good 1 1 2 ; bad 1 0 4 - if(nodeExecOrChild->getPriority()>=node->getPriority()) + while((nullptr!=nodeExecOrChild) && (node->getPriority()<nodeExecOrChild->getPriority())) { - node->setNextNode(nodeExecOrChild); - parent->setNextNode(nullptr); + parent = nodeExecOrChild; + nodeExecOrChild = nodeExecOrChild->getNextNode(); } + // management of operator priority + if((nullptr != nodeExecOrChild)&&(nodeExec != nodeExecOrChild)) + { + // good 1 1 2 ; bad 1 0 4 + if(nodeExecOrChild->getPriority()>=node->getPriority()) + { + node->setNextNode(nodeExecOrChild); + parent->setNextNode(nullptr); + } + } + else if(node->getPriority()>=nodeExec->getPriority()) + { + node->setNextNode(nodeExec->getNextNode()); + nodeExec->setNextNode(nullptr); + } + + nodeResult = node; + //previous->setNextNode(node); + + return true; } - else if(node->getPriority()>=nodeExec->getPriority()) + else { - node->setNextNode(nodeExec->getNextNode()); - nodeExec->setNextNode(nullptr); + delete node; } - - nodeResult = node; - //previous->setNextNode(node); - - return true; } - else + /* else { - delete node; - } + while(readOption(str,previous)) + { + previous = ParsingToolBox::getLatestNode(previous); + } + }*/ + return false; } - /* else + bool DiceParser::hasSeparator()const { - while(readOption(str,previous)) - { - previous = ParsingToolBox::getLatestNode(previous); - } - }*/ - return false; -} -bool DiceParser::hasSeparator()const -{ - return m_currentTreeHasSeparator; -} -DiceRollerNode* DiceParser::addRollDiceNode(qint64 faces,ExecutionNode* previous) -{ - DiceRollerNode* mydiceRoller= new DiceRollerNode(faces); - previous->setNextNode(mydiceRoller); - return mydiceRoller; -} -ExplodeDiceNode* DiceParser::addExplodeDiceNode(qint64 value,ExecutionNode* previous) -{ - ExplodeDiceNode* explodeDiceNode= new ExplodeDiceNode(); - NumberNode* node = new NumberNode(); - node->setNumber(value); - BooleanCondition* condition = new BooleanCondition(); - condition->setValueNode(node); - condition->setOperator(BooleanCondition::Equal); - m_parsingToolbox->isValidValidator(previous,condition); - explodeDiceNode->setValidator(condition); - previous->setNextNode(explodeDiceNode); - return explodeDiceNode; -} -bool DiceParser::readOption(QString& str,ExecutionNode* previous)//, -{ - if(str.isEmpty()) + return m_currentTreeHasSeparator; + } + DiceRollerNode* DiceParser::addRollDiceNode(qint64 faces,ExecutionNode* previous) { - return false; + DiceRollerNode* mydiceRoller= new DiceRollerNode(faces); + previous->setNextNode(mydiceRoller); + return mydiceRoller; } - - ExecutionNode* node = nullptr; - bool found=false; - - for(int i = 0; ((i<m_OptionOp->keys().size())&&(!found));++i ) + ExplodeDiceNode* DiceParser::addExplodeDiceNode(qint64 value,ExecutionNode* previous) { - QString key =m_OptionOp->keys().at(i); + ExplodeDiceNode* explodeDiceNode= new ExplodeDiceNode(); + NumberNode* node = new NumberNode(); + node->setNumber(value); + BooleanCondition* condition = new BooleanCondition(); + condition->setValueNode(node); + condition->setOperator(BooleanCondition::Equal); + m_parsingToolbox->isValidValidator(previous,condition); + explodeDiceNode->setValidator(condition); + previous->setNextNode(explodeDiceNode); + return explodeDiceNode; + } + bool DiceParser::readOption(QString& str,ExecutionNode* previous)//, + { + if(str.isEmpty()) + { + return false; + } + + ExecutionNode* node = nullptr; + bool found=false; - if(str.startsWith(key)) + for(int i = 0; ((i<m_OptionOp->keys().size())&&(!found));++i ) { + QString key =m_OptionOp->keys().at(i); - str=str.remove(0,key.size()); - auto operatorName = m_OptionOp->value(key); - switch(operatorName) + if(str.startsWith(key)) { - case Keep: - { - qint64 myNumber=0; - bool ascending = m_parsingToolbox->readAscending(str); - if(m_parsingToolbox->readNumber(str,myNumber)) + str=str.remove(0,key.size()); + auto operatorName = m_OptionOp->value(key); + switch(operatorName) { - node = m_parsingToolbox->addSort(previous,ascending); - KeepDiceExecNode* nodeK = new KeepDiceExecNode(); - nodeK->setDiceKeepNumber(myNumber); - node->setNextNode(nodeK); - node = nodeK; - found = true; - } - } - break; - case KeepAndExplode: - { - qint64 myNumber=0; - bool ascending = m_parsingToolbox->readAscending(str); - if(m_parsingToolbox->readNumber(str,myNumber)) + case Keep: { - /* if(!hasDice) + qint64 myNumber=0; + bool ascending = m_parsingToolbox->readAscending(str); + + if(m_parsingToolbox->readNumber(str,myNumber)) { - previous = addRollDiceNode(DEFAULT_FACES_NUMBER,previous); - }*/ - DiceRollerNode* nodeTmp = dynamic_cast<DiceRollerNode*>(previous); - if(nullptr!=nodeTmp) + node = m_parsingToolbox->addSort(previous,ascending); + KeepDiceExecNode* nodeK = new KeepDiceExecNode(); + nodeK->setDiceKeepNumber(myNumber); + node->setNextNode(nodeK); + node = nodeK; + found = true; + } + } + break; + case KeepAndExplode: + { + qint64 myNumber=0; + bool ascending = m_parsingToolbox->readAscending(str); + if(m_parsingToolbox->readNumber(str,myNumber)) { + /* if(!hasDice) + { + previous = addRollDiceNode(DEFAULT_FACES_NUMBER,previous); + }*/ + DiceRollerNode* nodeTmp = dynamic_cast<DiceRollerNode*>(previous); + if(nullptr!=nodeTmp) + { - previous = addExplodeDiceNode(nodeTmp->getFaces(),previous); - } + previous = addExplodeDiceNode(nodeTmp->getFaces(),previous); + } - node = m_parsingToolbox->addSort(previous,ascending); + node = m_parsingToolbox->addSort(previous,ascending); - KeepDiceExecNode* nodeK = new KeepDiceExecNode(); - nodeK->setDiceKeepNumber(myNumber); + KeepDiceExecNode* nodeK = new KeepDiceExecNode(); + nodeK->setDiceKeepNumber(myNumber); - node->setNextNode(nodeK); - node = nodeK; - found = true; + node->setNextNode(nodeK); + node = nodeK; + found = true; + } } - } - break; - case Filter: - { - Validator* validator = m_parsingToolbox->readCompositeValidator(str); - if(nullptr!=validator) + break; + case Filter: { - m_parsingToolbox->isValidValidator(previous,validator); + Validator* validator = m_parsingToolbox->readCompositeValidator(str); + if(nullptr!=validator) + { + m_parsingToolbox->isValidValidator(previous,validator); - FilterNode* filterNode = new FilterNode(); - filterNode->setValidator(validator); + FilterNode* filterNode = new FilterNode(); + filterNode->setValidator(validator); - previous->setNextNode(filterNode); - node = filterNode; - found = true; + previous->setNextNode(filterNode); + node = filterNode; + found = true; + } } - } - break; - case Sort: - { - bool ascending = m_parsingToolbox->readAscending(str); - node = m_parsingToolbox->addSort(previous,ascending); - /*if(!hasDice) - { - m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Sort Operator does not support default dice. You should add dice command before the s")); - }*/ - found = true; - } - break; - case Count: - { - Validator* validator = m_parsingToolbox->readCompositeValidator(str); - if(nullptr!=validator) + break; + case Sort: { - m_parsingToolbox->isValidValidator(previous,validator); - - CountExecuteNode* countNode = new CountExecuteNode(); - countNode->setValidator(validator); - - previous->setNextNode(countNode); - node = countNode; + bool ascending = m_parsingToolbox->readAscending(str); + node = m_parsingToolbox->addSort(previous,ascending); + /*if(!hasDice) + { + m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Sort Operator does not support default dice. You should add dice command before the s")); + }*/ found = true; } - else + break; + case Count: { - m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Validator is missing after the c operator. Please, change it")); + Validator* validator = m_parsingToolbox->readCompositeValidator(str); + if(nullptr!=validator) + { + m_parsingToolbox->isValidValidator(previous,validator); + + CountExecuteNode* countNode = new CountExecuteNode(); + countNode->setValidator(validator); + + previous->setNextNode(countNode); + node = countNode; + found = true; + } + else + { + m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Validator is missing after the c operator. Please, change it")); + } } - } - break; - case Reroll: - case RerollUntil: - case RerollAndAdd: - // Todo: I think that Exploding and Rerolling could share the same code - { - Validator* validator = m_parsingToolbox->readCompositeValidator(str); - if(nullptr!=validator) + break; + case Reroll: + case RerollUntil: + case RerollAndAdd: + // Todo: I think that Exploding and Rerolling could share the same code { - if(!m_parsingToolbox->isValidValidator(previous,validator)) + Validator* validator = m_parsingToolbox->readCompositeValidator(str); + if(nullptr!=validator) { - m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Validator is missing after the %1 operator. Please, change it") - .arg(operatorName == Reroll ? "r" : "a" )); - } + 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" )); + } + + auto reroll = (operatorName==RerollAndAdd || operatorName==Reroll); + auto addingMode = (operatorName==RerollAndAdd); + RerollDiceNode* rerollNode = new RerollDiceNode(reroll, addingMode); + ExecutionNode* nodeParam = nullptr; + if(readParameterNode(str,nodeParam)) + { + rerollNode->setInstruction(nodeParam); + } + rerollNode->setValidator(validator); + previous->setNextNode(rerollNode); + node = rerollNode; + found = true; - auto reroll = (operatorName==RerollAndAdd || operatorName==Reroll); - auto addingMode = (operatorName==RerollAndAdd); - RerollDiceNode* rerollNode = new RerollDiceNode(reroll, addingMode); - ExecutionNode* nodeParam = nullptr; - if(readParameterNode(str,nodeParam)) + } + else { - rerollNode->setInstruction(nodeParam); + m_errorMap.insert(ExecutionNode::BAD_SYNTAXE, + QObject::tr("Validator is missing after the %1 operator. Please, change it") + .arg(operatorName==Reroll? QStringLiteral("r") + :operatorName==RerollUntil? QStringLiteral("R") + :operatorName==RerollAndAdd? QStringLiteral("a") + :"")); } - rerollNode->setValidator(validator); - previous->setNextNode(rerollNode); - node = rerollNode; - found = true; } - else + break; + case Explode: { - m_errorMap.insert(ExecutionNode::BAD_SYNTAXE, - QObject::tr("Validator is missing after the %1 operator. Please, change it") - .arg(operatorName==Reroll? QStringLiteral("r") - :operatorName==RerollUntil? QStringLiteral("R") - :operatorName==RerollAndAdd? QStringLiteral("a") - :"")); - } + Validator* validator = m_parsingToolbox->readCompositeValidator(str); + if(nullptr!=validator) + { + if(!m_parsingToolbox->isValidValidator(previous,validator)) + { + m_errorMap.insert(ExecutionNode::ENDLESS_LOOP_ERROR,QObject::tr("This condition %1 introduces an endless loop. Please, change it").arg(validator->toString())); + } + ExplodeDiceNode* explodedNode = new ExplodeDiceNode(); + explodedNode->setValidator(validator); + previous->setNextNode(explodedNode); + node = explodedNode; + found = true; - } - break; - case Explode: - { - Validator* validator = m_parsingToolbox->readCompositeValidator(str); - if(nullptr!=validator) - { - if(!m_parsingToolbox->isValidValidator(previous,validator)) + } + else { - m_errorMap.insert(ExecutionNode::ENDLESS_LOOP_ERROR,QObject::tr("This condition %1 introduces an endless loop. Please, change it").arg(validator->toString())); + m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Validator is missing after the e operator. Please, change it")); } - ExplodeDiceNode* explodedNode = new ExplodeDiceNode(); - explodedNode->setValidator(validator); - previous->setNextNode(explodedNode); - node = explodedNode; + } + break; + case Merge: + { + MergeNode* mergeNode = new MergeNode(); + mergeNode->setStartList(&m_startNodes); + previous->setNextNode(mergeNode); + node = mergeNode; found = true; } - else + break; + case Bind: { - m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Validator is missing after the e operator. Please, change it")); - } - } - break; - case Merge: - { - MergeNode* mergeNode = new MergeNode(); - mergeNode->setStartList(&m_startNodes); - previous->setNextNode(mergeNode); - node = mergeNode; - found = true; - - } - break; - case Bind: - { - BindNode* bindNode = new BindNode(); - bindNode->setStartList(&m_startNodes); - previous->setNextNode(bindNode); - node = bindNode; - found = true; + BindNode* bindNode = new BindNode(); + bindNode->setStartList(&m_startNodes); + previous->setNextNode(bindNode); + node = bindNode; + found = true; - } - break; - case Painter: - { - PainterNode* painter = new PainterNode(); - m_parsingToolbox->readPainterParameter(painter,str); - previous->setNextNode(painter); - node = painter; - found = true; - } - break; - case ifOperator: - { - IfNode* nodeif = new IfNode(); - nodeif->setConditionType(m_parsingToolbox->readConditionType(str)); - Validator* validator = m_parsingToolbox->readCompositeValidator(str); - if(nullptr!=validator) + } + break; + case Painter: { - ExecutionNode* trueNode = nullptr; - ExecutionNode* falseNode = nullptr; - if(readIfInstruction(str,trueNode,falseNode)) + PainterNode* painter = new PainterNode(); + m_parsingToolbox->readPainterParameter(painter,str); + previous->setNextNode(painter); + node = painter; + found = true; + } + break; + case ifOperator: + { + IfNode* nodeif = new IfNode(); + nodeif->setConditionType(m_parsingToolbox->readConditionType(str)); + Validator* validator = m_parsingToolbox->readCompositeValidator(str); + if(nullptr!=validator) { - nodeif->setInstructionTrue(trueNode); - nodeif->setInstructionFalse(falseNode); - nodeif->setValidator(validator); - previous->setNextNode(nodeif); - node = nodeif; - found = true; + ExecutionNode* trueNode = nullptr; + ExecutionNode* falseNode = nullptr; + if(readIfInstruction(str,trueNode,falseNode)) + { + nodeif->setInstructionTrue(trueNode); + nodeif->setInstructionFalse(falseNode); + nodeif->setValidator(validator); + previous->setNextNode(nodeif); + node = nodeif; + found = true; + } + else + { + delete nodeif; + } } else { delete nodeif; } + break; } - else + case Split: { - delete nodeif; + SplitNode* splitnode = new SplitNode(); + previous->setNextNode(splitnode); + node = splitnode; + found = true; } - break; - } - case Split: - { - SplitNode* splitnode = new SplitNode(); - previous->setNextNode(splitnode); - node = splitnode; - found = true; - } - break; - case Group: - { - qint64 groupNumber=0; - if(m_parsingToolbox->readNumber(str,groupNumber)) + break; + case Group: { - GroupNode* groupNode = new GroupNode(); - groupNode->setGroupValue(groupNumber); - previous->setNextNode(groupNode); - node = groupNode; - found = true; + qint64 groupNumber=0; + if(m_parsingToolbox->readNumber(str,groupNumber)) + { + GroupNode* groupNode = new GroupNode(); + groupNode->setGroupValue(groupNumber); + previous->setNextNode(groupNode); + node = groupNode; + found = true; + } } - } - break; + break; + } } } + return found; } - return found; -} -bool DiceParser::readIfInstruction(QString& str,ExecutionNode*& trueNode,ExecutionNode*& falseNode) -{ - if(readBlocInstruction(str,trueNode)) + bool DiceParser::readIfInstruction(QString& str,ExecutionNode*& trueNode,ExecutionNode*& falseNode) { - if(readBlocInstruction(str,falseNode)) + if(readBlocInstruction(str,trueNode)) { + if(readBlocInstruction(str,falseNode)) + { + return true; + } return true; } - return true; + return false; } - return false; -} -bool DiceParser::readParameterNode(QString& str, ExecutionNode* & node) -{ - if(str.startsWith("(")) + bool DiceParser::readParameterNode(QString& str, ExecutionNode* & node) { - str=str.remove(0,1); - if(readExpression(str,node)) + if(str.startsWith("(")) { - if(str.startsWith(")")) + str=str.remove(0,1); + if(readExpression(str,node)) { - str=str.remove(0,1); - return true; + if(str.startsWith(")")) + { + str=str.remove(0,1); + return true; + } } } + return false; } - return false; -} -bool DiceParser::readBlocInstruction(QString& str,ExecutionNode*& resultnode) -{ - if(str.startsWith('{')) + bool DiceParser::readBlocInstruction(QString& str,ExecutionNode*& resultnode) { - str=str.remove(0,1); - ExecutionNode* node = nullptr; - Die::ArithmeticOperator op; - ScalarOperatorNode* scalarNode = nullptr; - if(m_parsingToolbox->readArithmeticOperator(str,op)) + if(str.startsWith('{')) { - scalarNode = new ScalarOperatorNode(); - scalarNode->setArithmeticOperator(op); - } - if(readExpression(str,node)) - { - if(str.startsWith('}')) + str=str.remove(0,1); + ExecutionNode* node = nullptr; + Die::ArithmeticOperator op; + ScalarOperatorNode* scalarNode = nullptr; + if(m_parsingToolbox->readArithmeticOperator(str,op)) { - if(nullptr==scalarNode) - { - resultnode = node; - } - else + scalarNode = new ScalarOperatorNode(); + scalarNode->setArithmeticOperator(op); + } + if(readExpression(str,node)) + { + if(str.startsWith('}')) { - resultnode = scalarNode; - scalarNode->setInternalNode(node); + if(nullptr==scalarNode) + { + resultnode = node; + } + else + { + resultnode = scalarNode; + scalarNode->setInternalNode(node); + } + str=str.remove(0,1); + return true; } - str=str.remove(0,1); - return true; } } + return false; } - return false; -} - -QString DiceParser::getComment() const -{ - return m_comment; -} -void DiceParser::setComment(const QString &comment) -{ - m_comment = comment; -} + QString DiceParser::getComment() const + { + return m_comment; + } -QMap<ExecutionNode::DICE_ERROR_CODE,QString> DiceParser::getErrorMap() -{ - QMap<ExecutionNode::DICE_ERROR_CODE,QString> map; + void DiceParser::setComment(const QString &comment) + { + m_comment = comment; + } - for(auto start : m_startNodes) + QMap<ExecutionNode::DICE_ERROR_CODE,QString> DiceParser::getErrorMap() { - auto mapTmp = start->getExecutionErrorMap(); - for(auto key : mapTmp.keys()) + QMap<ExecutionNode::DICE_ERROR_CODE,QString> map; + + for(auto start : m_startNodes) { - map.insertMulti(key,mapTmp[key]); + auto mapTmp = start->getExecutionErrorMap(); + for(auto key : mapTmp.keys()) + { + map.insertMulti(key,mapTmp[key]); + } } + return map; } - return map; -} -QString DiceParser::humanReadableError() -{ - QMapIterator<ExecutionNode::DICE_ERROR_CODE,QString> i(m_errorMap); - QString str(""); - while (i.hasNext()) + QString DiceParser::humanReadableError() { - i.next(); - str.append(i.value()); - str.append(QStringLiteral("\n")); + QMapIterator<ExecutionNode::DICE_ERROR_CODE,QString> i(m_errorMap); + QString str(""); + while (i.hasNext()) + { + i.next(); + str.append(i.value()); + str.append(QStringLiteral("\n")); + } + + ///list + QMapIterator<ExecutionNode::DICE_ERROR_CODE,QString> j(getErrorMap()); + while(j.hasNext()) + { + j.next(); + str.append(j.value()); + str.append(QStringLiteral("\n")); + } + return str; } - ///list - QMapIterator<ExecutionNode::DICE_ERROR_CODE,QString> j(getErrorMap()); - while(j.hasNext()) + QString DiceParser::humanReadableWarning() { - j.next(); - str.append(j.value()); - str.append(QStringLiteral("\n")); + QMapIterator<ExecutionNode::DICE_ERROR_CODE,QString> i(m_warningMap); + QString str(""); + while (i.hasNext()) + { + i.next(); + str.append(i.value()); + str.append(QStringLiteral("\n")); + } + return str; } - return str; -} - -QString DiceParser::humanReadableWarning() -{ - QMapIterator<ExecutionNode::DICE_ERROR_CODE,QString> i(m_warningMap); - QString str(""); - while (i.hasNext()) + + void DiceParser::writeDownDotTree(QString filepath) { - i.next(); - str.append(i.value()); - str.append(QStringLiteral("\n")); - } - return str; -} + if(m_startNodes.empty()) + return; -void DiceParser::writeDownDotTree(QString filepath) -{ - if(m_startNodes.empty()) - return; + QString str(QStringLiteral("digraph ExecutionTree {\n")); + for(auto start : m_startNodes) + { + start->generateDotTree(str); + } + str.append(QStringLiteral("}\n")); - QString str(QStringLiteral("digraph ExecutionTree {\n")); - for(auto start : m_startNodes) + QFile file(filepath); + if(file.open(QIODevice::WriteOnly)) + { + QTextStream in(&file); + in << str; + } + } + void DiceParser::setPathToHelp(QString l) { - start->generateDotTree(str); + m_helpPath = l; } - str.append(QStringLiteral("}\n")); - - QFile file(filepath); - if(file.open(QIODevice::WriteOnly)) + void DiceParser::setVariableDictionary(const QHash<QString,QString>& variables) { - QTextStream in(&file); - in << str; + ParsingToolBox::setVariableHash(variables); } -} -void DiceParser::setPathToHelp(QString l) -{ - m_helpPath = l; -} -void DiceParser::setVariableDictionary(const QHash<QString,QString>& variables) -{ - ParsingToolBox::setVariableHash(variables); -} |