aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt1
-rw-r--r--cli/main.cpp130
-rw-r--r--diceparser.cpp17
-rw-r--r--diceparser.h4
-rw-r--r--diceparser.pri2
-rw-r--r--die.h1
-rw-r--r--node/mergenode.cpp80
-rw-r--r--node/mergenode.h43
-rw-r--r--result/diceresult.cpp16
-rw-r--r--result/diceresult.h9
10 files changed, 257 insertions, 46 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9aae367..977d1f6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -38,6 +38,7 @@ add_executable(
node/executionnode.cpp
node/explosedicenode.cpp
node/helpnode.cpp
+ node/mergenode.cpp
node/jumpbackwardnode.cpp
node/keepdiceexecnode.cpp
node/listaliasnode.cpp
diff --git a/cli/main.cpp b/cli/main.cpp
index 9a966ba..dd21ea7 100644
--- a/cli/main.cpp
+++ b/cli/main.cpp
@@ -43,57 +43,102 @@
QTextStream out(stdout, QIODevice::WriteOnly);
-QString diceToText(ExportedDiceResult& dice,bool highlight)
+QString diceToText(ExportedDiceResult& dice,bool highlight,bool homogeneous)
{
QStringList resultGlobal;
- foreach(int face, dice.keys())
- {
- QStringList result;
- ListDiceResult diceResult = dice.value(face);
- //patternColor = patternColorarg();
- foreach (DiceAndHighlight tmp, diceResult)
- {
- QStringList diceListStr;
- QStringList diceListChildren;
-
+ foreach(int face, dice.keys())
+ {
+ QStringList result;
+ ListDiceResult diceResult = dice.value(face);
+ //patternColor = patternColorarg();
+ foreach (DiceAndHighlight tmp, diceResult)
+ {
+ QStringList diceListStr;
+ QStringList diceListChildren;
- for(int i =0; i < tmp.first.size(); ++i)
- {
- qint64 dievalue = tmp.first[i];
- QString prefix("%1");
- if((tmp.second)&&(highlight))
+ for(int i =0; i < tmp.first.size(); ++i)
+ {
+ qint64 dievalue = tmp.first[i];
+ QString prefix("%1");
+
+ if((tmp.second)&&(highlight))
+ {
+ prefix = "\e[0;31m%1\e[0m";
+ }
+
+ if(i==0)
+ {
+ diceListStr << prefix.arg(QString::number(dievalue));
+ }
+ else
+ {
+ diceListChildren << prefix.arg(QString::number(dievalue));
+ }
+ }
+ if(!diceListChildren.isEmpty())
{
- prefix = "\e[0;31m%1\e[0m";
+ diceListStr << QString("[%1]").arg(diceListChildren.join(' '));
}
- if(i==0)
+ result << diceListStr.join(' ');
+ // qDebug() << result << tmp.first << tmp.second;
+ }
+
+ if(dice.keys().size()>1)
+ {
+ resultGlobal << QString(" d%2:(%1)").arg(result.join(',')).arg(face);
+ }
+ else
+ {
+ resultGlobal << result;
+ }
+ }
+ /* }
+ else
+ {
+ foreach(int face, dice.keys())
+ {
+ QStringList result;
+ ListDiceResult diceResult = dice.value(face);
+ foreach (DiceAndHighlight tmp, diceResult)
+ {
+ QStringList diceListStr;
+ QStringList diceListChildren;
+
+
+ for(int i =0; i < tmp.first.size(); ++i)
{
- diceListStr << prefix.arg(QString::number(dievalue));
+ qint64 dievalue = tmp.first[i];
+ QString prefix("%1");
+
+ if((tmp.second)&&(highlight))
+ {
+ prefix = "\e[0;31m%1\e[0m";
+ }
+
+ if(i==0)
+ {
+ diceListStr << prefix.arg(QString::number(dievalue));
+ }
+ else
+ {
+ diceListChildren << prefix.arg(QString::number(dievalue));
+ }
}
- else
+ if(!diceListChildren.isEmpty())
{
- diceListChildren << prefix.arg(QString::number(dievalue));
+ diceListStr << QString("[%1]").arg(diceListChildren.join(' '));
}
- }
- if(!diceListChildren.isEmpty())
- {
- diceListStr << QString("[%1]").arg(diceListChildren.join(' '));
- }
-
- result << diceListStr.join(' ');
- // qDebug() << result << tmp.first << tmp.second;
- }
-
- if(dice.keys().size()>1)
- {
- resultGlobal << QString(" d%2:(%1)").arg(result.join(',')).arg(face);
- }
- else
- {
- resultGlobal << result;
- }
- }
+
+ result << diceListStr.join(' ');
+ // qDebug() << result << tmp.first << tmp.second;
+ }
+
+ resultGlobal << QString(" (%1) ").arg(result.join(','));
+
+ }
+ }*/
return resultGlobal.join(' ');
}
@@ -114,8 +159,9 @@ void startDiceParsing(QString& cmd,QString& treeFile,bool highlight)
}
ExportedDiceResult list;
- parser->getLastDiceResult(list);
- QString diceText = diceToText(list,highlight);
+ bool homogeneous = true;
+ parser->getLastDiceResult(list,homogeneous);
+ QString diceText = diceToText(list,highlight,homogeneous);
QString scalarText;
QString str;
diff --git a/diceparser.cpp b/diceparser.cpp
index 796c3ce..7fcf267 100644
--- a/diceparser.cpp
+++ b/diceparser.cpp
@@ -38,6 +38,7 @@
#include "node/jumpbackwardnode.h"
#include "node/listsetrollnode.h"
#include "node/listaliasnode.h"
+#include "node/mergenode.h"
#define DEFAULT_FACES_NUMBER 10
@@ -445,7 +446,7 @@ QStringList DiceParser::getAllDiceResult(bool& hasAlias)
return stringListResult;
}
-void DiceParser::getLastDiceResult(ExportedDiceResult& diceValues)
+void DiceParser::getLastDiceResult(ExportedDiceResult& diceValues,bool& homogeneous)
{
ExecutionNode* next = getLeafNode();
Result* result=next->getResult();
@@ -457,6 +458,11 @@ void DiceParser::getLastDiceResult(ExportedDiceResult& diceValues)
DiceResult* diceResult = dynamic_cast<DiceResult*>(result);
if(NULL!=diceResult)
{
+ if(homogeneous)
+ {
+
+ homogeneous = diceResult->isHomogeneous();
+ }
quint64 face=0;
ListDiceResult listpair;
foreach(Die* die, diceResult->getResultList())
@@ -936,7 +942,16 @@ bool DiceParser::readOption(QString& str,ExecutionNode* previous, bool hasDice)/
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();
+ previous->setNextNode(mergeNode);
+ node = mergeNode;
+ isFine = true;
+ }
+ break;
}
}
diff --git a/diceparser.h b/diceparser.h
index 9ef2327..bc92034 100644
--- a/diceparser.h
+++ b/diceparser.h
@@ -77,7 +77,7 @@ public:
/**
* @brief The OptionOperator enum gathering all options availables for result.
*/
- enum OptionOperator {KeepAndExplose,Keep,Reroll,Explosing,Sort,Count,RerollAndAdd};
+ enum OptionOperator {KeepAndExplose,Keep,Reroll,Explosing,Sort,Count,RerollAndAdd,Merge};
/**
* @brief The CommandOperator enum
*/
@@ -135,7 +135,7 @@ public:
* @brief getLastDiceResult
* @return
*/
- void getLastDiceResult(ExportedDiceResult& diceValues);
+ void getLastDiceResult(ExportedDiceResult& diceValues,bool& homogeneous);
/**
* @brief hasIntegerResultNotInFirst
* @return
diff --git a/diceparser.pri b/diceparser.pri
index 9fe29e3..912776c 100644
--- a/diceparser.pri
+++ b/diceparser.pri
@@ -48,6 +48,7 @@ HEADERS += \
$$PWD/node/parenthesesnode.h \
$$PWD/node/helpnode.h \
$$PWD/node/jumpbackwardnode.h \
+ $$PWD/node/mergenode.h \
$$PWD/node/listaliasnode.h \
$$PWD/node/listsetrollnode.h
@@ -65,6 +66,7 @@ SOURCES += \
$$PWD/node/parenthesesnode.cpp \
$$PWD/node/helpnode.cpp \
$$PWD/node/jumpbackwardnode.cpp \
+ $$PWD/node/mergenode.cpp \
$$PWD/node/listaliasnode.cpp \
$$PWD/node/listsetrollnode.cpp
diff --git a/die.h b/die.h
index ffd37a1..3ae90dd 100644
--- a/die.h
+++ b/die.h
@@ -133,6 +133,7 @@ private:
bool m_highlighted;
quint64 m_faces;
qint64 m_base;
+ QString m_color;
};
diff --git a/node/mergenode.cpp b/node/mergenode.cpp
new file mode 100644
index 0000000..5145bc8
--- /dev/null
+++ b/node/mergenode.cpp
@@ -0,0 +1,80 @@
+/***************************************************************************
+* 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 "mergenode.h"
+
+MergeNode::MergeNode()
+ : m_diceResult(new DiceResult())
+{
+ m_result = m_diceResult;
+}
+void MergeNode::run(ExecutionNode* previous)
+{
+ m_previousNode = previous;
+ if(NULL!=previous)
+ {
+ m_result->setPrevious(previous->getResult());
+ }
+ Result* tmpResult = previous->getResult();
+ while(NULL!=tmpResult)
+ {
+ DiceResult* dice = dynamic_cast<DiceResult*>(tmpResult);
+ if(NULL!=dice)
+ {
+ ///@todo improve here to set homogeneous while is really
+ m_diceResult->setHomogeneous(false);
+ foreach(Die* die, dice->getResultList())
+ {
+ if(!m_diceResult->getResultList().contains(die))
+ {
+ m_diceResult->getResultList().append(die);
+ }
+ }
+ }
+ tmpResult = tmpResult->getPrevious();
+ }
+
+ if(NULL!=m_nextNode)
+ {
+ m_nextNode->run(this);
+ }
+}
+
+QString MergeNode::toString(bool withLabel) const
+{
+ if(withLabel)
+ {
+ return QString("%1 [label=\"Merge Node %2\"]").arg(m_id).arg(m_number);
+ }
+ else
+ {
+ return m_id;
+ }
+}
+qint64 MergeNode::getPriority() const
+{
+ qint64 priority=0;
+ if(NULL!=m_nextNode)
+ {
+ priority = m_nextNode->getPriority();
+ }
+ return priority;
+}
diff --git a/node/mergenode.h b/node/mergenode.h
new file mode 100644
index 0000000..347975f
--- /dev/null
+++ b/node/mergenode.h
@@ -0,0 +1,43 @@
+/***************************************************************************
+* 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 MERGENODE_H
+#define MERGENODE_H
+
+#include "node/executionnode.h"
+#include "result/diceresult.h"
+
+/**
+ * @brief The MergeNode class is an ExecutionNode. It is dedicated to merge result of several commands.
+ */
+class MergeNode : public ExecutionNode
+{
+public:
+ MergeNode();
+ void run(ExecutionNode* previous);
+ virtual QString toString(bool withLabel)const;
+ virtual qint64 getPriority() const;
+private:
+ qint64 m_number;
+ DiceResult* m_diceResult;
+};
+
+#endif // NUMBERNODE_H
diff --git a/result/diceresult.cpp b/result/diceresult.cpp
index 74aa4e1..37db727 100644
--- a/result/diceresult.cpp
+++ b/result/diceresult.cpp
@@ -23,7 +23,8 @@
DiceResult::DiceResult()
{
- m_resultTypes= (DICE_LIST);
+ m_resultTypes= (DICE_LIST | SCALAR);
+ m_homogeneous = true;
}
void DiceResult::insertResult(Die* die)
{
@@ -33,6 +34,15 @@ QList<Die*>& DiceResult::getResultList()
{
return m_diceValues;
}
+bool DiceResult::isHomogeneous() const
+{
+ return m_homogeneous;
+}
+void DiceResult::setHomogeneous(bool b)
+{
+ m_homogeneous = b;
+}
+
void DiceResult::setResultList(QList<Die*> list)
{
qDeleteAll(m_diceValues.begin(), m_diceValues.end());
@@ -62,6 +72,10 @@ QVariant DiceResult::getResult(RESULT_TYPE type)
return QVariant();
}
+/*bool DiceResult::hasResultOfType(RESULT_TYPE type) const
+{
+ return (m_resultTypes & type);
+}*/
qreal DiceResult::getScalarResult()
{
if(m_diceValues.size()==1)
diff --git a/result/diceresult.h b/result/diceresult.h
index 84a4621..fe24277 100644
--- a/result/diceresult.h
+++ b/result/diceresult.h
@@ -66,11 +66,20 @@ public:
* @return
*/
virtual QString toString(bool wl);
+ /**
+ * @brief isHomogeneous
+ */
+ bool isHomogeneous() const;
+ /**
+ * @brief setHomogeneous
+ */
+ void setHomogeneous(bool);
private:
qreal getScalarResult();
private:
QList<Die*> m_diceValues;
+ bool m_homogeneous;
};
#endif // DICERESULT_H