diff options
| author | 2015-04-24 21:35:17 +0200 | |
|---|---|---|
| committer | 2015-04-24 21:35:17 +0200 | |
| commit | 8beb664172cf2c15abb4e12938b8978506471c02 (patch) | |
| tree | f033c9df5436abe8848a8b4d69353229028c02bd | |
| parent | 67fd3b480f543f2f18bd632ea85d829ba2102a20 (diff) | |
| parent | edfcca8d1a630ca7ca79133bd0b03af14fef6363 (diff) | |
| download | OneRoll-8beb664172cf2c15abb4e12938b8978506471c02.tar.gz OneRoll-8beb664172cf2c15abb4e12938b8978506471c02.zip | |
Merge branch 'master' of github.com:obiwankennedy/DiceParser
Conflicts:
parsingtoolbox.cpp
41 files changed, 858 insertions, 214 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..577e112 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,71 @@ +cmake_minimum_required(VERSION 2.8) + + +project(dice) + +#/net/rnd/src/qt/qt-everywhere-enterprise-src-5.3.0/linux-x86_64-gcc-4.7.2/lib/cmake +set(CMAKE_PREFIX_PATH "/net/rnd/src/qt/qt-everywhere-enterprise-src-5.3.0/linux-x86_64-gcc-4.7.2/lib/cmake") + +#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} +#${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}") + + +# Find includes in corresponding build directories +set(CMAKE_INCLUDE_CURRENT_DIR ON) +# Instruct CMake to run moc automatically when needed. +set(CMAKE_AUTOMOC ON) + +# Find the QtWidgets library +find_package(Qt5Core) + +set(EXECUTABLE_OUTPUT_PATH bin/) + +include_directories(${Qt5Core_INCLUDES}) +add_definitions(${Qt5Core_DEFINITIONS}) + +set(MODE "cli") +#file ( +# GLOB_RECURSE +# source_files +# *.cpp +# result/* +# node/* +#) +add_executable( + dice + diceparser.cpp + range.cpp + booleancondition.cpp + validator.cpp + die.cpp + parsingtoolbox.cpp + dicealias.cpp + result/result.cpp + result/scalarresult.cpp + result/stringresult.cpp + result/diceresult.cpp + node/countexecutenode.cpp +node/dicerollernode.cpp +node/executionnode.cpp +node/explosedicenode.cpp +node/helpnode.cpp +node/jumpbackwardnode.cpp +node/keepdiceexecnode.cpp +node/listaliasnode.cpp +node/listsetrollnode.cpp +node/numbernode.cpp +node/parenthesesnode.cpp +node/rerolldicenode.cpp +node/scalaroperatornode.cpp +node/sortresult.cpp +node/startingnode.cpp +cli/main.cpp + ) + +#add_executable(dice cli/main.cpp) + + +target_link_libraries(dice ${Qt5Core_LIBRARIES}) + +#qt5_use_modules() + @@ -71,6 +71,8 @@ Dice explose if their value are at the die maximum, the option sorts the resulti The dice list is sorted in descending order. +> 10d6sl +Roll 6 dice at 6 faces and then sort them ascendingly ### Counter > 3D10c[Validator] @@ -164,7 +166,6 @@ There are three kind of Validator: -Range -Boolean expression - Any operator which requires validator (such as `a,r,e,c') can use those three kind. ### Scalar diff --git a/booleancondition.cpp b/booleancondition.cpp index 267d7e9..de619e7 100644 --- a/booleancondition.cpp +++ b/booleancondition.cpp @@ -25,7 +25,7 @@ BooleanCondition::BooleanCondition() { } -qint64 BooleanCondition::hasValid(Die* b,bool recursive) const +qint64 BooleanCondition::hasValid(Die* b,bool recursive,bool unhighlight) const { QList<qint64> listValues; if(recursive) @@ -40,7 +40,6 @@ qint64 BooleanCondition::hasValid(Die* b,bool recursive) const qint64 sum= 0; foreach(qint64 value, listValues) { - switch(m_operator) { case Equal: @@ -58,11 +57,12 @@ qint64 BooleanCondition::hasValid(Die* b,bool recursive) const case LesserOrEqual: sum+= (value<=m_value)?1:0; break; - - } } - + if((unhighlight)&&(sum==0)) + { + b->setHighlighted(false); + } return sum; } diff --git a/booleancondition.h b/booleancondition.h index 02fadf9..8de8854 100644 --- a/booleancondition.h +++ b/booleancondition.h @@ -31,7 +31,7 @@ public: enum LogicOperator { Equal, GreaterThan, LesserThan, GreaterOrEqual, LesserOrEqual}; BooleanCondition(); - virtual qint64 hasValid(Die* b,bool recursive) const; + virtual qint64 hasValid(Die* b,bool recursive, bool unhighlight = false) const; void setOperator(LogicOperator m); void setValue(qint64); diff --git a/cli/cli.pri b/cli/cli.pri new file mode 100644 index 0000000..69aa44f --- /dev/null +++ b/cli/cli.pri @@ -0,0 +1,2 @@ +SOURCES += \ + $$PWD/main.cpp diff --git a/cli/main.cpp b/cli/main.cpp new file mode 100644 index 0000000..7476eb4 --- /dev/null +++ b/cli/main.cpp @@ -0,0 +1,246 @@ +/*************************************************************************** +* Copyright (C) 2014 by Renaud Guezennec * +* http://renaudguezennec.homelinux.org/accueil,3.html * +* * +* 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 <QStringList> +#include "diceparser.h" +#include <QCommandLineParser> +#include <QCommandLineOption> +#include <QDebug> + +QString diceToText(ExportedDiceResult& dice) +{ + QStringList resultGlobal; + foreach(int face, dice.keys()) + { + QStringList result; + ListDiceResult diceResult = dice.value(face); + bool previousHighlight=false; + QString patternColor("\e[0;31m"); + //patternColor = patternColorarg(); + foreach (DiceAndHighlight tmp, diceResult) + { + QStringList diceListStr; + QStringList diceListChildren; + if((previousHighlight)&&(!tmp.second)) + { + result << patternColor << result.join(',') <<"\e[0m"; + } + previousHighlight = tmp.second; + for(int i =0; i < tmp.first.size(); ++i) + { + quint64 dievalue = tmp.first[i]; + if(i==0) + { + diceListStr << QString::number(dievalue); + } + else + { + diceListChildren << QString::number(dievalue); + } + } + if(!diceListChildren.isEmpty()) + { + diceListStr << QString("[%1]").arg(diceListChildren.join(',')); + } + + result << diceListStr.join(' '); + } + if(previousHighlight) + { + QStringList list; + list << patternColor << result.join(',') << "\e[0m"; + result = list; + } + if(dice.keys().size()>1) + { + resultGlobal << QString(" d%2:(%1)").arg(result.join(' ')).arg(face); + } + else + { + resultGlobal << result; + } + } + return resultGlobal.join(' '); +} + +void startDiceParsing(QString& cmd,QString& treeFile,bool highlight) +{ + DiceParser* parser = new DiceParser(); + + if(parser->parseLine(cmd)) + { + parser->Start(); + // + if(treeFile.isEmpty()) + { + ExportedDiceResult list; + parser->getLastDiceResult(list); + QString diceText = diceToText(list); + QString scalarText; + QString str; + + if(parser->hasIntegerResultNotInFirst()) + { + scalarText = QString("%1").arg(parser->getLastIntegerResult()); + } + else if(!list.isEmpty()) + { + scalarText = QString("%1").arg(parser->getSumOfDiceResult()); + } + + str = QString("Result: \e[0;31m%1\e[0m, details:[%3 (%2)]").arg(scalarText).arg(diceText).arg(parser->getDiceCommand()); + + if(parser->hasStringResult()) + { + str = parser->getStringResult().replace("\n","<br/>"); + } + qDebug() << str; + + } + else + { + parser->writeDownDotTree(treeFile); + } + } +} + + + + +int main(int argc, char *argv[]) +{ + + QStringList commands; + QString cmd; + QString dotFileStr; + + bool colorb=false; + + QCommandLineParser optionParser; + QCommandLineOption color(QStringList() << "co"<< "color-off", "Disable color to highlight result"); + QCommandLineOption version(QStringList() << "v"<< "version", "Show the version and quit."); + QCommandLineOption reset(QStringList() << "reset-settings", "Erase the settings and use the default parameters"); + QCommandLineOption dotFile(QStringList() << "d"<<"dot-file", "Instead of rolling dice, generate the execution tree and write it in <dotfile>","dotfile"); + QCommandLineOption translation(QStringList() << "t"<<"translation", "path to the translation file: <translationfile>","translationfile"); + QCommandLineOption help(QStringList() << "h"<<"help", "Display this help"); + + if(!optionParser.addOption(color)) + { + qDebug()<< optionParser.errorText(); + } + + optionParser.addOption(version); + optionParser.addOption(reset); + optionParser.addOption(dotFile); + optionParser.addOption(translation); + optionParser.addOption(help); + + for(int i=1;i<argc;++i) + { + + commands << QString::fromLatin1(argv[i]); + } + + if(!optionParser.parse(commands)) + { + qDebug()<< optionParser.errorText(); + } + else + { + qDebug() << "no error"; + } + + + if(optionParser.isSet(color)) + { + qDebug() << "color"; + colorb = false; + } + else if(optionParser.isSet(version)) + { + return 0; + } + else if(optionParser.isSet(reset)) + { + return 0; + } + else if(optionParser.isSet(dotFile)) + { + dotFileStr = optionParser.value(dotFile); + } + else if(optionParser.isSet(translation)) + { + + } + else if(optionParser.isSet(help)) + { + cmd = "help"; + } + + cmd = commands.first(); + + qDebug() << "super 5" << cmd << dotFileStr << colorb; + startDiceParsing(cmd,dotFileStr,colorb); + + /*commands<< "10d10c[>6]+@c[=10]" + << "1L[cheminée,chocolat,épée,arc,chute de pierre]" + << "10d10c[>=6]-@c[=1]" + << "10d10c[>=6]-@c[=1]-@c[=1]" + << "10d10c[>6]+@c[=10]" + << "1+1D10" + << "3d10c[>=5]" + << "3nwod" + << "1+(4*3)D10" + << "2+4/4" + << "2D10*2D20*8" + <<"1+(4*3)D10" + <<"(4D6)D10" + << "1D100a[>=95]a[>=96]a[>=97]a[>=98]a[>=99]e[>=100]" + << "3D100" + << "4k3" + << "10D10e[>=6]sc[>=6]" + << "100190D6666666s" + << "10D10e10s" + << "10D10s" + << "15D10e10c[8-10]" + << "10d10e11" + << "1D8+2D6+7" + << "100190D6666666s" + << "D25" + << "D25+D10" + << "D25;D10" + << "8+8+8" + << "1D20-88" + << "100*1D20*2D6" + << "100/28*3" + << "100/8" + << "100*3*8" + << "help" + << "la" + << "400000D20/400000" + << "100*3*8";// +*/ + + + + return 0; +} diff --git a/diceParser.pro b/diceParser.pro index 2d72fcb..3a95496 100644 --- a/diceParser.pro +++ b/diceParser.pro @@ -18,8 +18,11 @@ TEMPLATE = app #CONFIG+= IRC #CONFIG+= GUI - - +CONFIG+= CLI +CLI { +DEFINES += CLI +include(cli/cli.pri)cd +} IRC { include(irc/irc.pri) QT += gui widgets @@ -33,8 +36,7 @@ DEFINES+= HAVE_GUI } -SOURCES += main.cpp \ - diceparser.cpp \ +SOURCES += diceparser.cpp \ result/diceresult.cpp \ range.cpp \ booleancondition.cpp \ @@ -43,7 +45,8 @@ SOURCES += main.cpp \ result/result.cpp \ result/scalarresult.cpp \ parsingtoolbox.cpp \ - result/stringresult.cpp + result/stringresult.cpp \ + dicealias.cpp HEADERS += \ @@ -56,7 +59,8 @@ HEADERS += \ result/result.h \ result/scalarresult.h \ result/parsingtoolbox.h \ - result/stringresult.h + result/stringresult.h \ + dicealias.h OTHER_FILES += README.md \ HelpMe.md diff --git a/dicealias.cpp b/dicealias.cpp new file mode 100644 index 0000000..f9c366c --- /dev/null +++ b/dicealias.cpp @@ -0,0 +1,99 @@ +/*************************************************************************** +* Copyright (C) 2014 by Renaud Guezennec * +* http://renaudguezennec.homelinux.org/accueil,3.html * +* * +* 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 "dicealias.h" +#include <QRegularExpression> + +DiceAlias::DiceAlias(QString cmd, QString key, bool isReplace) + : m_command(cmd),m_value(key) +{ + if(isReplace) + { + m_type = REPLACE; + } + else + { + m_type = REGEXP; + } +} + +DiceAlias::~DiceAlias() +{ + +} + +bool DiceAlias::resolved(QString & str) +{ + if((m_type == REPLACE)&&(str.contains(m_command))) + { + str.replace(m_command,m_value); + return true; + } + else if(m_type == REGEXP) + { + QRegularExpression exp(m_command); + str.replace(exp,m_value); + return true; + } + return false; +} + +void DiceAlias::setCommand(QString key) +{ + m_command = key; +} + +void DiceAlias::setValue(QString value) +{ + m_value = value; +} + +void DiceAlias::setType(RESOLUTION_TYPE type) +{ + m_type = type; +} +QString DiceAlias::getCommand() +{ + return m_command; +} + +QString DiceAlias::getValue() +{ + return m_value; +} + +bool DiceAlias::isReplace() +{ + return (m_type == REPLACE) ? true : false; +} + + +void DiceAlias::setReplace(bool b) +{ + if(b) + { + m_type= REPLACE; + } + else + { + m_type = REGEXP; + } +} diff --git a/dicealias.h b/dicealias.h new file mode 100644 index 0000000..ebf7a02 --- /dev/null +++ b/dicealias.h @@ -0,0 +1,92 @@ +/*************************************************************************** +* Copyright (C) 2014 by Renaud Guezennec * +* http://renaudguezennec.homelinux.org/accueil,3.html * +* * +* 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. * +***************************************************************************/ +#ifndef DICEALIAS_H +#define DICEALIAS_H + +#include <QString> +/** + * @brief The DiceAlias class + */ +class DiceAlias +{ +public: + + enum RESOLUTION_TYPE { REPLACE,REGEXP}; + /** + * @brief DiceAlias + * @param cmd + * @param key + * @param isReplace + */ + DiceAlias(QString cmd, QString key, bool isReplace = true); + /** + * @brief ~DiceAlias + */ + virtual ~DiceAlias(); + /** + * @brief resolved + * @param str + * @return + */ + bool resolved(QString & str); + /** + * @brief setCommand + * @param key + */ + void setCommand(QString key); + /** + * @brief setValue + * @param value + */ + void setValue(QString value); + /** + * @brief setType + */ + void setType(RESOLUTION_TYPE ); + + /** + * @brief getCommand + * @return + */ + QString getCommand(); + /** + * @brief getValue + * @return + */ + QString getValue(); + /** + * @brief isReplace + * @return + */ + bool isReplace(); + /** + * @brief setReplace + */ + void setReplace(bool); +private: + QString m_command; + QString m_value; + RESOLUTION_TYPE m_type; + +}; + +#endif // DICEALIAS_H diff --git a/diceparser.cpp b/diceparser.cpp index 2c533dc..c710f55 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -23,7 +23,7 @@ #include <QDebug> #include <QStringList> #include <QObject> - +#include <QFile> #include "node/startingnode.h" #include "node/scalaroperatornode.h" @@ -42,6 +42,7 @@ #define DEFAULT_FACES_NUMBER 10 DiceParser::DiceParser() + : m_start(NULL) { m_parsingToolbox = new ParsingToolBox(); @@ -60,12 +61,11 @@ DiceParser::DiceParser() //m_OptionOp->insert(QObject::tr("@"),JumpBackward); - - m_aliasMap = new QMap<QString,QString>; - m_aliasMap->insert("l5r","D10k"); - m_aliasMap->insert("l5R","D10K"); - m_aliasMap->insert("nwod","D10e10c[>7]"); - m_aliasMap->insert("nwod","D10e10c[>7]"); + m_aliasList = new QList<DiceAlias*>(); + /*m_aliasList->append(new DiceAlias("l5r","D10k")); + m_aliasList->append(new DiceAlias("l5R","D10K")); + m_aliasList->append(new DiceAlias("nwod","D10e10c[>7]")); + m_aliasList->append(new DiceAlias("(.*)wod(.*)","\\1d10e[=10]c[>=\\2]-@c[=1]",false));*/ m_nodeActionMap = new QMap<QString,NodeAction>(); m_nodeActionMap->insert("@",JumpBackward); @@ -76,6 +76,44 @@ DiceParser::DiceParser() m_commandList->append(QObject::tr("la")); } +DiceParser::~DiceParser() +{ + if(NULL!=m_commandList) + { + delete m_commandList; + m_commandList = NULL; + } + if(NULL!=m_nodeActionMap) + { + delete m_nodeActionMap; + m_nodeActionMap = NULL; + } + if(NULL!=m_OptionOp) + { + delete m_OptionOp; + m_OptionOp = NULL; + } + if(NULL!=m_mapDiceOp) + { + delete m_mapDiceOp; + m_mapDiceOp = NULL; + } + if(NULL!=m_parsingToolbox) + { + delete m_parsingToolbox; + m_parsingToolbox = NULL; + } + if(NULL!=m_aliasList) + { + delete m_aliasList; + m_aliasList = NULL; + } + if(NULL!=m_start) + { + delete m_start; + m_start = NULL; + } +} ExecutionNode* DiceParser::getLatestNode(ExecutionNode* node) { @@ -88,19 +126,32 @@ ExecutionNode* DiceParser::getLatestNode(ExecutionNode* node) } QString DiceParser::convertAlias(QString str) { - foreach(QString cmd, m_aliasMap->keys()) + foreach(DiceAlias* cmd, *m_aliasList) { - if(str.contains(cmd)) - { - str.replace(cmd,m_aliasMap->value(cmd)); - } + cmd->resolved(str); } 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); + } +} bool DiceParser::parseLine(QString str) { m_errorMap.clear(); + if(NULL!=m_start) + { + delete m_start; + m_start = NULL; + } m_command = str; m_start = new StartingNode(); ExecutionNode* newNode = NULL; @@ -341,65 +392,65 @@ QString DiceParser::getStringResult() str = result->getResult(Result::STRING).toString(); found = true; } - result = result->getPrevious(); } return str; } -QString DiceParser::getLastDiceResult() +void DiceParser::getLastDiceResult(ExportedDiceResult& diceValues) { ExecutionNode* next = getLeafNode(); - QString str; - QTextStream stream(&str); Result* result=next->getResult(); - QString dieValue("D%1 : {%2} "); + while(NULL!=result) { if(result->hasResultOfType(Result::DICE_LIST)) { - DiceResult* myDiceResult = dynamic_cast<DiceResult*>(result); - if(NULL!=myDiceResult) + DiceResult* diceResult = dynamic_cast<DiceResult*>(result); + if(NULL!=diceResult) { - - QString resulStr; + bool hasResult = false; quint64 face=0; - foreach(Die* die, myDiceResult->getResultList()) + ListDiceResult listpair; + foreach(Die* die, diceResult->getResultList()) { if(!die->hasBeenDisplayed()) { - resulStr+=QString("%1").arg(die->getValue()); + QList<quint64> valuesResult; + hasResult=true; + valuesResult.append(die->getValue()); die->displayed(); face = die->getFaces(); - - if(die->hasChildrenValue()) { - resulStr+=" ["; foreach(qint64 i, die->getListValue()) { - - resulStr+=QString("%1,").arg(i); + valuesResult.append(i); } - resulStr.remove(resulStr.size()-1,1); - resulStr+="]"; } - resulStr+=", "; + QPair<QList<quint64>,bool> pair(valuesResult,die->isHighlighted()); + listpair.append(pair); } } - resulStr.remove(resulStr.size()-2,2); - - if(!resulStr.isEmpty()) + if(!listpair.isEmpty()) { - stream << dieValue.arg(face).arg(resulStr); + if(!diceValues.contains(face)) + { + diceValues.insert(face,listpair); + } + else + { + ListDiceResult tmp = diceValues.value(face); + tmp.append(listpair); + diceValues.insert(face,tmp); + } + } } } result = result->getPrevious(); } - - return str.simplified(); } QString DiceParser::getDiceCommand() { @@ -539,7 +590,7 @@ bool DiceParser::readCommand(QString& str,ExecutionNode* & node) } else if(str=="la") { - node = new ListAliasNode(m_aliasMap); + node = new ListAliasNode(m_aliasList); } return true; } @@ -599,6 +650,7 @@ bool DiceParser::readOperator(QString& str,ExecutionNode* previous) node->setInternalNode(nodeExec); if(NULL==nodeExec) { + delete node; return false; } if(node->getPriority()>=nodeExec->getPriority()) @@ -610,6 +662,10 @@ bool DiceParser::readOperator(QString& str,ExecutionNode* previous) return true; } + else + { + delete node; + } } else if(readInstructionOperator(str[0])) { @@ -851,20 +907,28 @@ bool DiceParser::readOperand(QString& str,ExecutionNode* & node) if(m_parsingToolbox->readNumber(str,myNumber)) { - NumberNode* myNumberNode = new NumberNode(); - myNumberNode->setNumber(myNumber); + NumberNode* numberNode = new NumberNode(); + numberNode->setNumber(myNumber); - node = myNumberNode; + node = numberNode; return true; } return false; } -void DiceParser::displayDotTree() +void DiceParser::writeDownDotTree(QString filepath) { QString str("digraph ExecutionTree {\n"); m_start->generateDotTree(str); str.append("}"); - qDebug()<< str; + + QFile file(filepath); + if(file.open(QIODevice::WriteOnly)) + { + QTextStream in(&file); + in << str; + } + + //qDebug()<< str; } diff --git a/diceparser.h b/diceparser.h index bec3975..4031bdc 100644 --- a/diceparser.h +++ b/diceparser.h @@ -32,6 +32,12 @@ #include "range.h" #include "booleancondition.h" #include "parsingtoolbox.h" +#include "dicealias.h" + + +typedef QPair<QList<quint64>,bool> DiceAndHighlight; +typedef QList<DiceAndHighlight > ListDiceResult; +typedef QMap<int,ListDiceResult > ExportedDiceResult; class ExploseDiceNode; /** @@ -82,6 +88,10 @@ public: * @brief DiceParser default constructor */ DiceParser(); + /** + * @brief ~DiceParser + */ + virtual ~DiceParser(); /** * @brief parseLine, method to call for starting the dice roll. It will parse the command and run the execution tree. @@ -111,7 +121,7 @@ public: /** * @brief displayDotTree */ - void displayDotTree(); + void writeDownDotTree(QString filepath); /** * @brief getLastIntegerResult * @return @@ -126,7 +136,7 @@ public: * @brief getLastDiceResult * @return */ - QString getLastDiceResult(); + void getLastDiceResult(ExportedDiceResult& diceValues); /** * @brief hasIntegerResultNotInFirst * @return @@ -157,6 +167,21 @@ public: * @return */ QString humanReadableError(); + /** + * @brief getAliases + * @return + */ + QList<DiceAlias*>* getAliases(); + /** + * @brief insertAlias + */ + void insertAlias(DiceAlias*, int); + /** + * @brief DiceParser::convertAlias + * @param str + * @return + */ + QString convertAlias(QString str); private: /** @@ -218,12 +243,6 @@ private: * @return */ bool readOperand(QString&,ExecutionNode* & node); - /** - * @brief DiceParser::convertAlias - * @param str - * @return - */ - QString convertAlias(QString str); /** * @brief getErrorList @@ -244,8 +263,17 @@ private: */ bool readNode(QString& str,ExecutionNode* & node); - + /** + * @brief getLeafNode + * @return + */ ExecutionNode* getLeafNode(); + + /** + * @brief hasResultOfType + * @param notthelast + * @return + */ bool hasResultOfType(Result::RESULT_TYPE,bool notthelast = false); @@ -253,7 +281,7 @@ private: QMap<QString,DiceOperator>* m_mapDiceOp; QMap<QString,OptionOperator>* m_OptionOp; QMap<QString,NodeAction>* m_nodeActionMap; - QMap<QString,QString>* m_aliasMap; + QList<DiceAlias*>* m_aliasList; QStringList* m_commandList; QMap<ExecutionNode::ERROR_CODE,QString> m_errorMap; diff --git a/diceparser.pri b/diceparser.pri index b9af1d7..cacca31 100644 --- a/diceparser.pri +++ b/diceparser.pri @@ -12,7 +12,8 @@ SOURCES += $$PWD/diceparser.cpp \ $$PWD/result/result.cpp \ $$PWD/result/scalarresult.cpp \ $$PWD/parsingtoolbox.cpp \ - $$PWD/result/stringresult.cpp + $$PWD/result/stringresult.cpp \ + $$PWD/dicealias.cpp HEADERS += \ @@ -25,7 +26,8 @@ HEADERS += \ $$PWD/result/result.h \ $$PWD/result/scalarresult.h \ $$PWD/result/parsingtoolbox.h \ - $$PWD/result/stringresult.h + $$PWD/result/stringresult.h \ + $$PWD/dicealias.h HEADERS += \ @@ -26,7 +26,7 @@ #include <QDebug> Die::Die() - : m_hasValue(false),m_displayStatus(false) + : m_hasValue(false),m_displayStatus(false),m_highlighted(true) { uint seed = quintptr(this) + QDateTime::currentDateTime().toMSecsSinceEpoch(); qsrand(seed); @@ -132,3 +132,12 @@ void Die::displayed() { m_displayStatus = true; } +void Die::setHighlighted(bool a) +{ + m_highlighted = a; +} + +bool Die::isHighlighted() +{ + return m_highlighted; +} @@ -109,6 +109,15 @@ public: * @brief displayed */ void displayed(); + /** + * @brief setHighlighted + */ + void setHighlighted(bool); + /** + * @brief isHighlighted + * @return + */ + bool isHighlighted(); private: qint64 m_value; @@ -116,6 +125,7 @@ private: bool m_selected; bool m_hasValue; bool m_displayStatus; + bool m_highlighted; quint64 m_faces; }; diff --git a/main.cpp b/main.cpp deleted file mode 100644 index 926e3d4..0000000 --- a/main.cpp +++ /dev/null @@ -1,103 +0,0 @@ -/*************************************************************************** -* Copyright (C) 2014 by Renaud Guezennec * -* http://renaudguezennec.homelinux.org/accueil,3.html * -* * -* 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. * -***************************************************************************/ -#ifdef HAVE_IRC -#include <QApplication> -#include "irc/mainwindow.h" -#endif - -#include <QStringList> -#include "diceparser.h" - -int main(int argc, char *argv[]) -{ - #ifdef HAVE_IRC - QApplication a(argc, argv); - - - MainWindow main; -#endif - DiceParser* myParser = new DiceParser(); - - QStringList commands; - - commands << "1L[cheminée,chocolat,épée,arc,chute de pierre]" - << "10d10c[>=6]-@c[=1]" - << "10d10c[>=6]-@c[=1]-@c[=1]" - << "10d10c[>6]+@c[=10]" - << "1+1D10" - << "3d10c[>=5]" - << "3nwod" - << "1+(4*3)D10" - << "2+4/4" - << "2D10*2D20*8" - <<"1+(4*3)D10" - <<"(4D6)D10" - << "1D100a[>=95]a[>=96]a[>=97]a[>=98]a[>=99]e[>=100]" - << "3D100" - << "4k3" - << "10D10e[>=6]sc[>=6]" - << "100190D6666666s" - << "10D10e10s" - << "10D10s" - << "15D10e10c[8-10]" - << "10d10e11" - << "1D8+2D6+7" - << "D25" - << "D25+D10" - << "D25;D10" - << "8+8+8" - << "1D20-88" - << "100*1D20*2D6" - << "100/28*3" - << "100/8" - << "100*3*8" - << "help" - << "la" - << "400000D20/400000" - << "100*3*8"; - - if(argc>1) - { - for(int i=1;i<argc;++i) - { - - commands << QString::fromLatin1(argv[i]); - } - } - foreach(QString cmd, commands) - { - if(myParser->parseLine(cmd)) - { - myParser->Start(); - // myParser->displayDotTree(); - myParser->displayResult(); - } - else - { - qDebug() << "echec"; - } - } - #ifdef HAVE_IRC - main.show(); - return a.exec(); -#endif -} diff --git a/node/countexecutenode.cpp b/node/countexecutenode.cpp index 281fc80..ff3d67b 100644 --- a/node/countexecutenode.cpp +++ b/node/countexecutenode.cpp @@ -4,7 +4,7 @@ CountExecuteNode::CountExecuteNode() - : m_scalarResult(new ScalarResult()) + : m_scalarResult(new ScalarResult()),m_validator(NULL) { m_result = m_scalarResult; } @@ -12,6 +12,13 @@ void CountExecuteNode::setValidator(Validator* validator) { m_validator = validator; } +CountExecuteNode::~CountExecuteNode() +{ + if(NULL!=m_validator) + { + delete m_validator; + } +} void CountExecuteNode::run(ExecutionNode *previous) { @@ -28,7 +35,7 @@ void CountExecuteNode::run(ExecutionNode *previous) qint64 sum = 0; foreach(Die* dice,diceList) { - sum+=m_validator->hasValid(dice,true); + sum+=m_validator->hasValid(dice,true,true); } m_scalarResult->setValue(sum); diff --git a/node/countexecutenode.h b/node/countexecutenode.h index c7ecdfd..519403b 100644 --- a/node/countexecutenode.h +++ b/node/countexecutenode.h @@ -16,6 +16,7 @@ public: * @brief CountExecuteNode */ CountExecuteNode(); + virtual ~CountExecuteNode(); /** * @brief run * @param previous diff --git a/node/dicerollernode.cpp b/node/dicerollernode.cpp index 1fd1a2f..f32a033 100644 --- a/node/dicerollernode.cpp +++ b/node/dicerollernode.cpp @@ -33,11 +33,9 @@ /// \brief DiceRollerNode::DiceRollerNode ////////////////////////////////////////////////// DiceRollerNode::DiceRollerNode(quint64 faces) - : m_faces(faces),m_myDiceResult(new DiceResult()) + : m_faces(faces),m_diceResult(new DiceResult()) { - m_mutex=new QMutex(); - m_result=m_myDiceResult; - + m_result=m_diceResult; } void DiceRollerNode::run(ExecutionNode* previous) { @@ -55,7 +53,7 @@ void DiceRollerNode::run(ExecutionNode* previous) Die* die = new Die(); die->setFaces(m_faces); die->roll(); - m_myDiceResult->insertResult(die); + m_diceResult->insertResult(die); } if(NULL!=m_nextNode) { diff --git a/node/dicerollernode.h b/node/dicerollernode.h index d50fe95..744a4b5 100644 --- a/node/dicerollernode.h +++ b/node/dicerollernode.h @@ -36,8 +36,7 @@ public: private: quint64 m_diceCount; quint64 m_faces; /// faces - DiceResult* m_myDiceResult; - QMutex* m_mutex; + DiceResult* m_diceResult; }; #endif // DICEROLLERNODE_H diff --git a/node/executionnode.cpp b/node/executionnode.cpp index 8bcd6e4..343e8d9 100644 --- a/node/executionnode.cpp +++ b/node/executionnode.cpp @@ -8,6 +8,16 @@ ExecutionNode::ExecutionNode() ExecutionNode::~ExecutionNode() { + if(NULL!=m_result) + { + delete m_result; + m_result = NULL; + } + if(NULL!=m_nextNode) + { + delete m_nextNode; + m_nextNode = NULL; + } } Result* ExecutionNode::getResult() diff --git a/node/explosedicenode.cpp b/node/explosedicenode.cpp index 42b3c2d..4eae270 100644 --- a/node/explosedicenode.cpp +++ b/node/explosedicenode.cpp @@ -1,7 +1,7 @@ #include "explosedicenode.h" ExploseDiceNode::ExploseDiceNode() - : m_diceResult(new DiceResult()) + : m_diceResult(new DiceResult()),m_validator(NULL) { m_result = m_diceResult; } @@ -33,6 +33,13 @@ void ExploseDiceNode::run(ExecutionNode* previous) } } } +ExploseDiceNode::~ExploseDiceNode() +{ + if(NULL!=m_validator) + { + delete m_validator; + } +} void ExploseDiceNode::setValidator(Validator* val) { m_validator = val; diff --git a/node/explosedicenode.h b/node/explosedicenode.h index b00af1a..f5d0f6e 100644 --- a/node/explosedicenode.h +++ b/node/explosedicenode.h @@ -13,6 +13,7 @@ class ExploseDiceNode : public ExecutionNode { public: ExploseDiceNode(); + virtual ~ExploseDiceNode(); virtual void run(ExecutionNode* previous = NULL); virtual void setValidator(Validator* ); virtual QString toString()const; diff --git a/node/jumpbackwardnode.h b/node/jumpbackwardnode.h index 4ea3d11..c6c1e4d 100644 --- a/node/jumpbackwardnode.h +++ b/node/jumpbackwardnode.h @@ -6,8 +6,14 @@ class JumpBackwardNode : public ExecutionNode { public: + /** + * @brief JumpBackwardNode allows to get result from remote node in the execution tree. + */ JumpBackwardNode(); - + /** + * @brief run - performs the actions + * @param previous + */ virtual void run(ExecutionNode* previous = NULL); /** diff --git a/node/keepdiceexecnode.cpp b/node/keepdiceexecnode.cpp index 62b4c8f..e8cfac8 100644 --- a/node/keepdiceexecnode.cpp +++ b/node/keepdiceexecnode.cpp @@ -26,6 +26,11 @@ m_previousNode = previous; diceList2 = diceList.mid(0,m_numberOfDice); + foreach(Die* tmp,diceList.mid(m_numberOfDice,-1)) + { + tmp->setHighlighted(false); + } + m_diceResult->setResultList(diceList2); if(NULL!=m_nextNode) { diff --git a/node/listaliasnode.cpp b/node/listaliasnode.cpp index 1d50c80..b85d4c9 100644 --- a/node/listaliasnode.cpp +++ b/node/listaliasnode.cpp @@ -1,7 +1,7 @@ #include "listaliasnode.h" -ListAliasNode::ListAliasNode(QMap<QString,QString>* apAlias) - : m_mapAlias(apAlias) +ListAliasNode::ListAliasNode(QList<DiceAlias*>* apAlias) + : m_aliasList(apAlias) { m_result = new StringResult(); } @@ -29,14 +29,13 @@ void ListAliasNode::run(ExecutionNode* previous ) m_nextNode->run(this); } } -QString ListAliasNode::toString()const +QString ListAliasNode::toString() const { QString result(QObject::tr("List of Alias:\n")); - foreach(QString key, m_mapAlias->keys()) + foreach(DiceAlias* key, *m_aliasList) { - result+=QString("%1 : %2\n").arg(key).arg(m_mapAlias->value(key)); + result+=QString("%1 : %2\n").arg(key->getCommand()).arg(key->getValue()); } - return result; } diff --git a/node/listaliasnode.h b/node/listaliasnode.h index d01d17a..ea70fe7 100644 --- a/node/listaliasnode.h +++ b/node/listaliasnode.h @@ -3,12 +3,12 @@ #include "executionnode.h" #include "result/stringresult.h" - +#include "dicealias.h" class ListAliasNode : public ExecutionNode { public: - ListAliasNode(QMap<QString,QString>* mapAlias); + ListAliasNode(QList<DiceAlias*>* mapAlias); /** * @brief run * @param previous @@ -27,7 +27,7 @@ public: virtual qint64 getPriority() const; private: - QMap<QString,QString>* m_mapAlias; + QList<DiceAlias*>* m_aliasList; }; #endif // LISTALIASNODE_H diff --git a/node/listsetrollnode.cpp b/node/listsetrollnode.cpp index 3ab9a69..777f7b0 100644 --- a/node/listsetrollnode.cpp +++ b/node/listsetrollnode.cpp @@ -6,6 +6,14 @@ ListSetRollNode::ListSetRollNode() { m_result = m_stringResult; } +ListSetRollNode::~ListSetRollNode() +{ + if(NULL!=m_diceResult) + { + delete m_diceResult; + m_diceResult =NULL; + } +} QStringList ListSetRollNode::getList() { diff --git a/node/listsetrollnode.h b/node/listsetrollnode.h index 26fb378..c7925b8 100644 --- a/node/listsetrollnode.h +++ b/node/listsetrollnode.h @@ -12,6 +12,7 @@ class ListSetRollNode : public ExecutionNode { public: ListSetRollNode(); + virtual ~ListSetRollNode(); virtual void run(ExecutionNode* previous = NULL); virtual QString toString()const; virtual qint64 getPriority() const; diff --git a/node/rerolldicenode.cpp b/node/rerolldicenode.cpp index 81b5e01..a7cc8c4 100644 --- a/node/rerolldicenode.cpp +++ b/node/rerolldicenode.cpp @@ -2,10 +2,18 @@ RerollDiceNode::RerollDiceNode() - : m_myDiceResult(new DiceResult()),m_adding(false) + : m_myDiceResult(new DiceResult()),m_adding(false),m_validator(NULL) { m_result=m_myDiceResult; } +RerollDiceNode::~RerollDiceNode() +{ + if(NULL!=m_validator) + { + delete m_validator; + m_validator = NULL; + } +} void RerollDiceNode::run(ExecutionNode* previous) { m_previousNode = previous; diff --git a/node/rerolldicenode.h b/node/rerolldicenode.h index 4674a3e..a97e448 100644 --- a/node/rerolldicenode.h +++ b/node/rerolldicenode.h @@ -12,16 +12,44 @@ class RerollDiceNode : public ExecutionNode { public: + /** + * @brief The ReRollMode enum + */ enum ReRollMode {EQUAL,LESSER,GREATER}; + /** + * @brief RerollDiceNode + */ RerollDiceNode(); + /** + * @brief ~RerollDiceNode + */ + virtual ~RerollDiceNode(); + /** + * @brief run + * @param previous + */ virtual void run(ExecutionNode* previous); + /** + * @brief setValidator + */ virtual void setValidator(Validator* ); + /** + * @brief toString + * @return + */ virtual QString toString()const; + /** + * @brief setAddingMode + */ virtual void setAddingMode(bool); + /** + * @brief getPriority + * @return + */ virtual qint64 getPriority() const; private: diff --git a/node/scalaroperatornode.cpp b/node/scalaroperatornode.cpp index 95fd077..34eb6b8 100644 --- a/node/scalaroperatornode.cpp +++ b/node/scalaroperatornode.cpp @@ -15,6 +15,14 @@ ScalarOperatorNode::ScalarOperatorNode() m_result = m_scalarResult; } +ScalarOperatorNode::~ScalarOperatorNode() +{ + if(NULL!=m_internalNode) + { + delete m_internalNode; + m_internalNode = NULL; + } +} void ScalarOperatorNode::run(ExecutionNode* previous) { @@ -43,7 +51,7 @@ void ScalarOperatorNode::run(ExecutionNode* previous) m_internalNode->getResult()->setPrevious(previousResult); } - switch(m_myOperator) + switch(m_operator) { case PLUS: m_scalarResult->setValue(add(previousResult->getResult(Result::SCALAR).toReal(),internalResult->getResult(Result::SCALAR).toReal())); @@ -74,7 +82,7 @@ bool ScalarOperatorNode::setOperatorChar(QChar c) { if(m_scalarOperationList.contains(c)) { - m_myOperator = m_scalarOperationList.value(c); + m_operator = m_scalarOperationList.value(c); return true; } return false; @@ -110,10 +118,14 @@ QString ScalarOperatorNode::toString() const } qint64 ScalarOperatorNode::getPriority() const { - if((m_myOperator==PLUS)||(m_myOperator==MINUS)) + if((m_operator==PLUS)||(m_operator==MINUS)) + { return 1; + } else + { return 2; + } } void ScalarOperatorNode::generateDotTree(QString& s) { diff --git a/node/scalaroperatornode.h b/node/scalaroperatornode.h index a67e296..7193118 100644 --- a/node/scalaroperatornode.h +++ b/node/scalaroperatornode.h @@ -12,6 +12,7 @@ class ScalarOperatorNode : public ExecutionNode public: enum ScalarOperator {PLUS,MINUS,DIVIDE,MULTIPLICATION}; ScalarOperatorNode(); + virtual ~ScalarOperatorNode(); virtual void run(ExecutionNode*); bool setOperatorChar(QChar c); void setInternalNode(ExecutionNode* node); @@ -28,7 +29,7 @@ private: qint64 multiple(qint64,qint64); private: - ScalarOperator m_myOperator; + ScalarOperator m_operator; ExecutionNode* m_internalNode; QMap<QChar,ScalarOperator> m_scalarOperationList; ScalarResult* m_scalarResult; diff --git a/parsingtoolbox.cpp b/parsingtoolbox.cpp index 63b5739..3e09bac 100644 --- a/parsingtoolbox.cpp +++ b/parsingtoolbox.cpp @@ -63,6 +63,14 @@ bool ParsingToolBox::readLogicOperator(QString& str,BooleanCondition::LogicOpera return false; } +ParsingToolBox::~ParsingToolBox() +{ + if(NULL!=m_logicOp) + { + delete m_logicOp; + m_logicOp = NULL; + } +} Validator* ParsingToolBox::readValidator(QString& str) { Validator* returnVal=NULL; @@ -181,7 +189,7 @@ bool ParsingToolBox::readList(QString& str,QStringList& list) { QString liststr = str.left(pos); list = liststr.split(","); - str=str.remove(0,liststr.size()+1); + str=str.remove(0,pos+1); return true; } } diff --git a/parsingtoolbox.h b/parsingtoolbox.h index 43dca4e..d7fdc31 100644 --- a/parsingtoolbox.h +++ b/parsingtoolbox.h @@ -35,8 +35,15 @@ class ParsingToolBox { public: + /** + * @brief ParsingToolBox + */ ParsingToolBox(); /** + * @brief ~ParsingToolBox + */ + virtual ~ParsingToolBox(); + /** * @brief addSort * @param e * @param b @@ -32,25 +32,28 @@ void Range::setValue(qint64 s,qint64 e) m_end=e; } -qint64 Range::hasValid(Die* m,bool recursive) const +qint64 Range::hasValid(Die* m,bool recursive, bool unhighlight) const { + qint64 result = 0; if(recursive) { - qint64 i = 0; foreach(qint64 value, m->getListValue()) { if((value>=m_start)&&(value<=m_end)) { - ++i; + ++result; } } - return i; } else if((m->getLastRolledValue()>=m_start)&&(m->getLastRolledValue()<=m_end)) { - return 1; + ++result; } - return 0; + if((unhighlight)&&(result==0)) + { + m->setHighlighted(false); + } + return result; } QString Range::toString() { @@ -31,7 +31,7 @@ public: Range(); void setValue(qint64,qint64); - virtual qint64 hasValid(Die* b,bool recursive) const; + virtual qint64 hasValid(Die* b,bool recursive,bool unlight = false) const; virtual QString toString(); virtual quint8 getValidRangeSize(quint64 faces) const; diff --git a/result/diceresult.cpp b/result/diceresult.cpp index 0cabb11..f87079a 100644 --- a/result/diceresult.cpp +++ b/result/diceresult.cpp @@ -35,17 +35,15 @@ QList<Die*>& DiceResult::getResultList() } void DiceResult::setResultList(QList<Die*> list) { + qDeleteAll(m_diceValues.begin(), m_diceValues.end()); m_diceValues.clear(); m_diceValues << list; } -//bool DiceResult::isScalar() const -//{ -// if(m_diceValues.size()==1) -// { -// return true; -// } -// return false; -//} +DiceResult::~DiceResult() +{ + qDeleteAll(m_diceValues.begin(), m_diceValues.end()); + m_diceValues.clear(); +} QVariant DiceResult::getResult(RESULT_TYPE type) { diff --git a/result/diceresult.h b/result/diceresult.h index 838a83d..b805e73 100644 --- a/result/diceresult.h +++ b/result/diceresult.h @@ -35,6 +35,10 @@ public: * @brief DiceResult */ DiceResult(); + /** + * @brief ~DiceResult + */ + virtual ~DiceResult(); /** * @brief getResultList diff --git a/result/stringresult.cpp b/result/stringresult.cpp index 4831a76..9f7d2e1 100644 --- a/result/stringresult.cpp +++ b/result/stringresult.cpp @@ -8,6 +8,10 @@ void StringResult::setText(QString text) { m_value=text; } +StringResult::~StringResult() +{ + +} QString StringResult::getText() const { diff --git a/result/stringresult.h b/result/stringresult.h index caa7e06..17c43cd 100644 --- a/result/stringresult.h +++ b/result/stringresult.h @@ -13,6 +13,10 @@ public: * @brief StringResult */ StringResult(); + /** + * @brief StringResult + */ + virtual ~StringResult(); /** * @brief setText * @param text diff --git a/validator.h b/validator.h index c9bb78e..d18a691 100644 --- a/validator.h +++ b/validator.h @@ -30,7 +30,7 @@ class Validator { public: Validator(); - virtual qint64 hasValid(Die* b,bool recursive) const = 0 ; + virtual qint64 hasValid(Die* b,bool recursive,bool unlight = false) const = 0 ; virtual QString toString()=0; virtual quint8 getValidRangeSize(quint64 faces) const = 0 ; |