diff options
| -rw-r--r-- | booleancondition.cpp | 4 | ||||
| -rw-r--r-- | booleancondition.h | 1 | ||||
| -rw-r--r-- | diceParser.pro | 18 | ||||
| -rw-r--r-- | diceparser.cpp | 47 | ||||
| -rw-r--r-- | diceparser.h | 13 | ||||
| -rw-r--r-- | irc/mainwindow.h | 2 | ||||
| -rw-r--r-- | main.cpp | 24 | ||||
| -rw-r--r-- | node/countexecutenode.cpp | 4 | ||||
| -rw-r--r-- | node/executionnode.cpp | 17 | ||||
| -rw-r--r-- | node/executionnode.h | 5 | ||||
| -rw-r--r-- | node/helpnode.cpp | 2 | ||||
| -rw-r--r-- | node/jumpbackwardnode.cpp | 33 | ||||
| -rw-r--r-- | node/jumpbackwardnode.h | 5 | ||||
| -rw-r--r-- | node/numbernode.cpp | 2 | ||||
| -rw-r--r-- | node/rerolldicenode.cpp | 2 | ||||
| -rw-r--r-- | node/scalaroperatornode.cpp | 33 | ||||
| -rw-r--r-- | node/scalaroperatornode.h | 1 | ||||
| -rw-r--r-- | node/startingnode.cpp | 2 | ||||
| -rw-r--r-- | range.cpp | 4 | ||||
| -rw-r--r-- | range.h | 2 | ||||
| -rw-r--r-- | result.cpp | 9 | ||||
| -rw-r--r-- | result.h | 18 | ||||
| -rw-r--r-- | validator.h | 3 |
23 files changed, 215 insertions, 36 deletions
diff --git a/booleancondition.cpp b/booleancondition.cpp index c7763e3..49e055a 100644 --- a/booleancondition.cpp +++ b/booleancondition.cpp @@ -75,3 +75,7 @@ void BooleanCondition::setValue(qint64 v) { m_value=v; } +QString BooleanCondition::toString() +{ + return QString("BooleanCondition_op_%1_value_%2").arg(m_operator).arg(m_value); +} diff --git a/booleancondition.h b/booleancondition.h index 4418650..ea5eefd 100644 --- a/booleancondition.h +++ b/booleancondition.h @@ -35,6 +35,7 @@ public: void setOperator(LogicOperator m); void setValue(qint64); + QString toString(); private: LogicOperator m_operator; diff --git a/diceParser.pro b/diceParser.pro index 219eb0a..b3ed2a0 100644 --- a/diceParser.pro +++ b/diceParser.pro @@ -9,8 +9,6 @@ QT += core QT -= gui include(node/node.pri) -include(gui/gui.pri) -include(irc/irc.pri) TARGET = diceParser CONFIG += console @@ -18,6 +16,22 @@ CONFIG -= app_bundle TEMPLATE = app +#CONFIG+= IRC +#CONFIG+= GUI + + +IRC { +include(irc/irc.pri) +QT += gui widgets +DEFINES+= HAVE_IRC +message("test") +} +GUI { +include(gui/gui.pri) +QT += gui widgets +DEFINES+= HAVE_GUI +} + SOURCES += main.cpp \ diceparser.cpp \ diff --git a/diceparser.cpp b/diceparser.cpp index 2be2e08..3f1c21c 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -35,6 +35,7 @@ #include "node/explosedicenode.h" #include "node/parenthesesnode.h" #include "node/helpnode.h" +#include "node/jumpbackwardnode.h" #define DEFAULT_FACES_NUMBER 10 @@ -44,6 +45,7 @@ DiceParser::DiceParser() m_mapDiceOp = new QMap<QString,DiceOperator>(); m_mapDiceOp->insert("D",D); + m_mapDiceOp->insert("L",L); m_OptionOp = new QMap<QString,OptionOperator>(); m_OptionOp->insert(QObject::tr("k"),Keep); @@ -53,7 +55,7 @@ DiceParser::DiceParser() m_OptionOp->insert(QObject::tr("r"),Reroll); m_OptionOp->insert(QObject::tr("e"),Explosing); m_OptionOp->insert(QObject::tr("a"),RerollAndAdd); - m_OptionOp->insert(QObject::tr("@"),JumpBackward); + //m_OptionOp->insert(QObject::tr("@"),JumpBackward); @@ -63,6 +65,9 @@ DiceParser::DiceParser() m_aliasMap->insert("nwod","D10e10c[>7]"); m_aliasMap->insert("nwod","D10e10c[>7]"); + m_nodeActionMap = new QMap<QString,NodeAction>(); + m_nodeActionMap->insert("@",JumpBackward); + m_commandList = new QList<QString>(); m_commandList->append("help"); @@ -166,6 +171,11 @@ bool DiceParser::readExpression(QString& str,ExecutionNode* & node) node = operandNode; return true; } + else if(readNode(str,operandNode)) + { + node = operandNode; + return true; + } else { ExecutionNode* diceNode=NULL; @@ -183,6 +193,26 @@ bool DiceParser::readExpression(QString& str,ExecutionNode* & node) } return true; } +bool DiceParser::readNode(QString& str,ExecutionNode* & node) +{ + /*k case JumpBackward: + { + JumpBackwardNode* jumpNode = new JumpBackwardNode(); + previous->setNextNode(jumpNode); + node = jumpNode; + isFine=true; + }*/ + QString key= str.left(1); + if(m_nodeActionMap->contains(key)) + { + JumpBackwardNode* jumpNode = new JumpBackwardNode(); + node = jumpNode; + str=str.remove(0,1); + readOption(str,jumpNode,true); + return true; + } + return false; +} void DiceParser::Start() { @@ -332,11 +362,11 @@ bool DiceParser::readCommand(QString& str,ExecutionNode* & node) node = new HelpNode(); return true; } + return false; } bool DiceParser::readDiceExpression(QString& str,ExecutionNode* & node) { - int number=1; bool returnVal=false; ExecutionNode* next = NULL; @@ -579,10 +609,7 @@ bool DiceParser::readOption(QString& str,ExecutionNode* previous, bool hasDice) isFine = true; } } - case JumpBackward: - { - } } } @@ -598,7 +625,6 @@ QList<ExecutionNode::ERROR_CODE> DiceParser::getErrorList() bool DiceParser::readOperand(QString& str,ExecutionNode* & node) { int myNumber=1; - ExecutionNode* myNode=NULL; if(m_parsingToolbox->readNumber(str,myNumber)) { @@ -610,3 +636,12 @@ bool DiceParser::readOperand(QString& str,ExecutionNode* & node) } return false; } +void DiceParser::displayDotTree() +{ + QString str("digraph ExecutionTree {\n"); + m_start->generateDotTree(str); + str.append("}"); + + qDebug()<< str; + +} diff --git a/diceparser.h b/diceparser.h index 035c56e..22dead1 100644 --- a/diceparser.h +++ b/diceparser.h @@ -60,12 +60,17 @@ public: */ enum DiceOperator {D,L}; + /** + * @brief The DiceSymbol enum + */ + enum NodeAction {JumpBackward}; + /** * @brief The OptionOperator enum gathering all options availables for result. */ - enum OptionOperator {KeepAndExplose,Keep,Reroll,Explosing,Sort,Count,RerollAndAdd,JumpBackward}; + enum OptionOperator {KeepAndExplose,Keep,Reroll,Explosing,Sort,Count,RerollAndAdd}; /** * @brief DiceParser default constructor @@ -93,6 +98,7 @@ public: bool readExpression(QString& str,ExecutionNode* & node); + void displayDotTree(); private: /** @@ -163,12 +169,17 @@ private: bool readInstructionOperator(QChar c); + bool readNode(QString& str,ExecutionNode* & node); + + private: QMap<QString,DiceOperator>* m_mapDiceOp; QMap<QString,OptionOperator>* m_OptionOp; + QMap<QString,NodeAction>* m_nodeActionMap; QMap<QString,QString>* m_aliasMap; QList<QString>* m_commandList; + ExecutionNode* m_start; ExecutionNode* m_current; QString m_command; diff --git a/irc/mainwindow.h b/irc/mainwindow.h index 9098e56..6a819fd 100644 --- a/irc/mainwindow.h +++ b/irc/mainwindow.h @@ -16,7 +16,7 @@ class MainWindow : public QMainWindow public: explicit MainWindow(QWidget *parent = 0); - ~MainWindow(); + virtual ~MainWindow(); private: Ui::MainWindow *ui; @@ -19,28 +19,32 @@ * 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" -#include "irc/mainwindow.h" int main(int argc, char *argv[]) { + #ifdef HAVE_IRC QApplication a(argc, argv); MainWindow main; +#endif DiceParser* myParser = new DiceParser(); QStringList commands; - commands<< "3d10c[>=6]-@c[=1]" - << "3d10-@c[>1]" - << "3d10c[>=6]-c[=1]" - << "3d10c[>=5]"; - /*<< "3nwod" + commands << "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" @@ -64,7 +68,7 @@ int main(int argc, char *argv[]) << "100/8" << "100*3*8" << "help" - << "100*3*8";*/ + << "100*3*8"; if(argc>1) { @@ -81,6 +85,7 @@ int main(int argc, char *argv[]) if(myParser->parseLine(cmd)) { myParser->Start(); + // myParser->displayDotTree(); myParser->displayResult(); } else @@ -88,7 +93,8 @@ int main(int argc, char *argv[]) qDebug() << "echec"; } } + #ifdef HAVE_IRC main.show(); return a.exec(); - +#endif } diff --git a/node/countexecutenode.cpp b/node/countexecutenode.cpp index a663544..d0084a3 100644 --- a/node/countexecutenode.cpp +++ b/node/countexecutenode.cpp @@ -20,7 +20,7 @@ void CountExecuteNode::run(ExecutionNode *previous) { return; } - DiceResult* previous_result = static_cast<DiceResult*>(previous->getResult()); + DiceResult* previous_result = dynamic_cast<DiceResult*>(previous->getResult()); if(NULL!=previous_result) { m_result->setPrevious(previous_result); @@ -41,7 +41,7 @@ void CountExecuteNode::run(ExecutionNode *previous) } QString CountExecuteNode::toString() const { - return "CountExecuteNode"; + return "CountExecuteNode"+m_validator->toString(); } qint64 CountExecuteNode::getPriority() const { diff --git a/node/executionnode.cpp b/node/executionnode.cpp index e1b71b0..bc002ce 100644 --- a/node/executionnode.cpp +++ b/node/executionnode.cpp @@ -34,3 +34,20 @@ ExecutionNode* ExecutionNode::getPreviousNode() const { return m_previousNode; } +void ExecutionNode::generateDotTree(QString& s) +{ + s.append(toString()); + if(NULL!=m_nextNode) + { + s.append(" -> "); + s.append(m_nextNode->toString()); + s.append(" [label=\"nextNode\"];\n"); + m_nextNode->generateDotTree(s); + } + else + { + s.append(" -> "); + s.append("NULL"); + s.append(" [label=\"nextNode\"];\n"); + } +} diff --git a/node/executionnode.h b/node/executionnode.h index e10ccb6..2a82470 100644 --- a/node/executionnode.h +++ b/node/executionnode.h @@ -59,6 +59,11 @@ public: virtual QList<ExecutionNode::ERROR_CODE> getErrorList(); /** + * @brief generateDotTree + */ + virtual void generateDotTree(QString&); + + /** * @brief getHelp * @return */ diff --git a/node/helpnode.cpp b/node/helpnode.cpp index 9a0504e..bae4b21 100644 --- a/node/helpnode.cpp +++ b/node/helpnode.cpp @@ -31,7 +31,7 @@ void HelpNode::run(ExecutionNode* previous) } QString HelpNode::toString()const { - return QObject::tr("Rolisteam Dice Parser: Full documentation at: https://github.com/obiwankennedy/DiceParser/blob/master/HelpMe.md"); + return QObject::tr("Rolisteam Dice Parser: Full documentation at: https://github.com/obiwankennedy/DiceParser/blob/master/HelpMe.md\n"); } qint64 HelpNode::getPriority() const diff --git a/node/jumpbackwardnode.cpp b/node/jumpbackwardnode.cpp index c311ed2..c8b392b 100644 --- a/node/jumpbackwardnode.cpp +++ b/node/jumpbackwardnode.cpp @@ -1,32 +1,37 @@ #include "jumpbackwardnode.h" + JumpBackwardNode::JumpBackwardNode() { + m_diceResult =new DiceResult(); + m_result = m_diceResult; } qint64 JumpBackwardNode::getPriority() const { - return 1; + return 4; } QString JumpBackwardNode::toString() const { - return QString(); + return QString("JumpBackwardNode"); } void JumpBackwardNode::run(ExecutionNode* previous) { + m_previousNode = previous; ExecutionNode* parent = previous; bool found=false; - int i = 1; + int i = 3; Result* result=NULL; - while((NULL!=parent)&&(found)) + while((NULL!=parent)&&(!found)) { + result = parent->getResult(); - if(NULL!=result) + if((NULL!=result)) { --i; - if(i==0) + if((i==0)&&(parent->getResult()->hasResultOfType(Result::DICE_LIST))) { found =true; } @@ -34,5 +39,19 @@ void JumpBackwardNode::run(ExecutionNode* previous) parent = parent->getPreviousNode(); } - m_result = result; + DiceResult* diceResult = dynamic_cast<DiceResult*>(result); + foreach(Die* die,diceResult->getResultList()) + { + Die* tmpdie = new Die(); + *tmpdie=*die; + m_diceResult->insertResult(tmpdie); + } + + //*m_result = *result; + m_result->setPrevious(parent->getResult()); + + if(NULL!=m_nextNode) + { + m_nextNode->run(this); + } } diff --git a/node/jumpbackwardnode.h b/node/jumpbackwardnode.h index d211385..ba25c7b 100644 --- a/node/jumpbackwardnode.h +++ b/node/jumpbackwardnode.h @@ -2,7 +2,7 @@ #define JUMPBACKWARDNODE_H #include "executionnode.h" - +#include "diceresult.h" class JumpBackwardNode : public ExecutionNode { public: @@ -20,7 +20,8 @@ public: * @return */ virtual qint64 getPriority() const; - +private: + DiceResult* m_diceResult; }; diff --git a/node/numbernode.cpp b/node/numbernode.cpp index 4e9d9a3..51a859f 100644 --- a/node/numbernode.cpp +++ b/node/numbernode.cpp @@ -25,7 +25,7 @@ void NumberNode::setNumber(qint64 a) } QString NumberNode::toString() const { - return QString("NumberNode:%1").arg(m_number); + return QString("NumberNode_%1").arg(m_number); } qint64 NumberNode::getPriority() const { diff --git a/node/rerolldicenode.cpp b/node/rerolldicenode.cpp index 56bef61..81b5e01 100644 --- a/node/rerolldicenode.cpp +++ b/node/rerolldicenode.cpp @@ -8,7 +8,7 @@ RerollDiceNode::RerollDiceNode() } void RerollDiceNode::run(ExecutionNode* previous) { -m_previousNode = previous; + m_previousNode = previous; if((NULL!=previous)&&(NULL!=previous->getResult())) { DiceResult* previous_result = static_cast<DiceResult*>(previous->getResult()); diff --git a/node/scalaroperatornode.cpp b/node/scalaroperatornode.cpp index 6cef1a9..67fc344 100644 --- a/node/scalaroperatornode.cpp +++ b/node/scalaroperatornode.cpp @@ -38,7 +38,10 @@ void ScalarOperatorNode::run(ExecutionNode* previous) Result* internalResult = internal->getResult(); m_result->setPrevious(internalResult); - m_internalNode->getResult()->setPrevious(previousResult); + if(NULL!=m_internalNode->getResult()) + { + m_internalNode->getResult()->setPrevious(previousResult); + } switch(m_myOperator) { @@ -112,3 +115,31 @@ qint64 ScalarOperatorNode::getPriority() const else return 2; } +void ScalarOperatorNode::generateDotTree(QString& s) +{ + s.append(toString()); + if(NULL!=m_nextNode) + { + s.append(" -> "); + s.append(m_nextNode->toString()); + s.append(" [label=\"nextNode\"];\n"); + m_nextNode->generateDotTree(s); + } + else + { + s.append(" -> "); + s.append("NULL"); + s.append(" [label=\"nextNode\"];\n"); + } + QString str; + str.append("\n"); + if(NULL!=m_internalNode) + { + str.append(toString()); + str.append(" -> "); + str.append(m_internalNode->toString()); + str.append(" [label=\"internalNode\"];\n"); + m_internalNode->generateDotTree(str); + } + s.append(str); +} diff --git a/node/scalaroperatornode.h b/node/scalaroperatornode.h index 14f759b..2b4f68a 100644 --- a/node/scalaroperatornode.h +++ b/node/scalaroperatornode.h @@ -19,6 +19,7 @@ public: virtual QString toString()const; virtual qint64 getPriority() const; + void generateDotTree(QString& s); private: qint64 add(qint64,qint64); diff --git a/node/startingnode.cpp b/node/startingnode.cpp index 7ce1e38..48ac4a4 100644 --- a/node/startingnode.cpp +++ b/node/startingnode.cpp @@ -15,7 +15,7 @@ void StartingNode::run(ExecutionNode*) } QString StartingNode::toString() const { - return "Starting node"; + return "StartingNode"; } qint64 StartingNode::getPriority() const { @@ -52,3 +52,7 @@ qint64 Range::hasValid(Die* m,bool recursive) const } return 0; } +QString Range::toString() +{ + return QString("Range_%1_%2").arg(m_start).arg(m_end); +} @@ -33,6 +33,8 @@ public: virtual qint64 hasValid(Die* b,bool recursive) const; + virtual QString toString(); + private: qint64 m_start; qint64 m_end; @@ -45,3 +45,12 @@ bool Result::hasResultOfType(RESULT_TYPE type) const { return (m_resultTypes & type); } +void Result::generateDotTree(QString& s) +{ + s.append(toString()); + +} +QString Result::toString() +{ + QString(); +} @@ -58,10 +58,26 @@ public: * @brief setPrevious */ virtual void setPrevious(Result*); + /** + * @brief isStringResult + * @return + */ bool isStringResult(); + /** + * @brief getStringResult + * @return + */ QString getStringResult(); - + /** + * @brief generateDotTree + */ + void generateDotTree(QString&); + /** + * @brief toString + * @return + */ + QString toString(); protected: int m_resultTypes;/// @brief private: diff --git a/validator.h b/validator.h index c4622d8..78f3e9c 100644 --- a/validator.h +++ b/validator.h @@ -24,12 +24,15 @@ #include <Qt> #include "die.h" +#include <QString> class Validator { public: Validator(); virtual qint64 hasValid(Die* b,bool recursive) const = 0 ; + + virtual QString toString()=0; }; #endif // VALIDATOR_H |