diff options
| -rw-r--r-- | cli/main.cpp | 22 | ||||
| -rw-r--r-- | diceparser.cpp | 36 | ||||
| -rw-r--r-- | diceparser.h | 4 | ||||
| -rw-r--r-- | die.cpp | 20 | ||||
| -rw-r--r-- | die.h | 5 | ||||
| -rw-r--r-- | node/dicerollernode.cpp | 14 | ||||
| -rw-r--r-- | node/dicerollernode.h | 6 | ||||
| -rw-r--r-- | node/ifnode.cpp | 2 | ||||
| -rw-r--r-- | node/listsetrollnode.cpp | 4 | ||||
| -rw-r--r-- | operationcondition.cpp | 2 | ||||
| -rw-r--r-- | parsingtoolbox.cpp | 13 | ||||
| -rw-r--r-- | parsingtoolbox.h | 2 |
12 files changed, 92 insertions, 38 deletions
diff --git a/cli/main.cpp b/cli/main.cpp index 1b9fdad..462c826 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -117,10 +117,13 @@ QString diceToText(ExportedDiceResult& dice,bool highlight,bool homogeneous) return resultGlobal.join(' '); } -void startDiceParsing(QString& cmd,QString& treeFile,bool highlight) +void startDiceParsing(QStringList& cmds,QString& treeFile,bool highlight) { DiceParser* parser = new DiceParser(); + for(QString cmd : cmds) + { + if(parser->parseLine(cmd)) { // @@ -156,6 +159,10 @@ void startDiceParsing(QString& cmd,QString& treeFile,bool highlight) { str = parser->getStringResult(); } + if(!parser->getComment().isEmpty()) + { + out << "\033[1m" <<parser->getComment()<< "\033[0m\n"; + } out << str << "\n"; if(!treeFile.isEmpty()) { @@ -166,6 +173,7 @@ void startDiceParsing(QString& cmd,QString& treeFile,bool highlight) { out << parser->humanReadableError()<< "\n";; } + } } void usage() @@ -261,18 +269,14 @@ int main(int argc, char *argv[]) QStringList cmdList = optionParser.positionalArguments(); // qDebug()<< "rest"<< cmdList; - if(!cmdList.isEmpty()) - { - cmd = cmdList.first(); - } - if(!cmd.isEmpty()) - { - startDiceParsing(cmd,dotFileStr,colorb); + + startDiceParsing(cmdList,dotFileStr,colorb); if(optionParser.isSet(help)) { usage(); } - } + + return 0; } diff --git a/diceparser.cpp b/diceparser.cpp index 8331962..4c51d55 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -195,6 +195,8 @@ bool DiceParser::parseLine(QString str) bool DiceParser::readExpression(QString& str,ExecutionNode* & node) { ExecutionNode* operandNode=NULL; + QString result; + QString comment; if(m_parsingToolbox->readOpenParentheses(str)) { ExecutionNode* internalNode=NULL; @@ -255,6 +257,11 @@ bool DiceParser::readExpression(QString& str,ExecutionNode* & node) return false; } } + if(m_parsingToolbox->readComment(str,result,comment)) + { + m_command.remove(comment); + m_comment = result; + } return true; } bool DiceParser::readNode(QString& str,ExecutionNode* & node) @@ -601,16 +608,16 @@ bool DiceParser::readDice(QString& str,ExecutionNode* & node) { if(currentOperator==D) { - qint64 num; - qint64 end; - if(m_parsingToolbox->readNumber(str,num)) + qint64 max; + qint64 min; + if(m_parsingToolbox->readNumber(str,max)) { - if(num<1) + 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(num)); + 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(num); + DiceRollerNode* drNode = new DiceRollerNode(max); node = drNode; ExecutionNode* current = drNode; while(readOption(str,current)) @@ -619,11 +626,12 @@ bool DiceParser::readDice(QString& str,ExecutionNode* & node) } return true; } - else if(m_parsingToolbox->readDiceRange(str,num,end)) + else if(m_parsingToolbox->readDiceRange(str,min,max)) { - qint64 face = abs(num - end)+1; - DiceRollerNode* drNode = new DiceRollerNode(face,num); + // qint64 face = abs(num - end); + //qDebug() << face << end; + DiceRollerNode* drNode = new DiceRollerNode(max,min); node = drNode; ExecutionNode* current = drNode; while(readOption(str,current)) @@ -1083,6 +1091,16 @@ bool DiceParser::readBlocInstruction(QString& str,ExecutionNode*& resultnode) return false; } +QString DiceParser::getComment() const +{ + return m_comment; +} + +void DiceParser::setComment(const QString &comment) +{ + m_comment = comment; +} + QMap<ExecutionNode::DICE_ERROR_CODE,QString> DiceParser::getErrorMap() { return m_start->getExecutionErrorMap(); diff --git a/diceparser.h b/diceparser.h index 0bc32e8..a1e58e1 100644 --- a/diceparser.h +++ b/diceparser.h @@ -225,6 +225,9 @@ public: * @param variables */ void setVariableDictionary(QHash<QString,QString>* variables); + QString getComment() const; + void setComment(const QString &comment); + private: /** @@ -333,6 +336,7 @@ private: QString m_helpPath; bool m_currentTreeHasSeparator; bool readBlocInstruction(QString &str, ExecutionNode *&resultnode); + QString m_comment; }; #endif // DICEPARSER_H @@ -44,7 +44,7 @@ Die::Die(const Die& die) m_selected = die.m_selected; m_hasValue = die.m_hasValue; m_displayStatus = die.m_displayStatus; - m_faces = die.m_faces; + m_maxValue = die.m_maxValue; m_highlighted = die.m_highlighted; m_base = die.m_base; m_color = die.getColor(); @@ -103,11 +103,11 @@ void Die::replaceLastValue(qint64 value) void Die::roll(bool adding) { - if(m_faces!=0) + if(m_maxValue!=0) { //quint64 value=(qrand()%m_faces)+m_base; - std::uniform_int_distribution<qint64> dist(m_base,m_faces); + std::uniform_int_distribution<qint64> dist(m_base,m_maxValue); qint64 value = dist(m_rng); if((adding)||(m_rollResult.isEmpty())) { @@ -122,12 +122,12 @@ void Die::roll(bool adding) quint64 Die::getFaces() const { - return m_faces; + return abs(m_maxValue-m_base)+1; } void Die::setFaces(quint64 face) { - m_faces=face; + //m_maxValue=m_base+face-1; } qint64 Die::getLastRolledValue() { @@ -169,3 +169,13 @@ void Die::setColor(const QString &color) m_color = color; } +quint64 Die::getMaxValue() const +{ + return m_maxValue; +} + +void Die::setMaxValue(const quint64 &maxValue) +{ + m_maxValue = maxValue; +} + @@ -128,6 +128,9 @@ public: QString getColor() const; void setColor(const QString &color); + quint64 getMaxValue() const; + void setMaxValue(const quint64 &maxValue); + private: qint64 m_value; QList<qint64> m_rollResult; @@ -135,7 +138,7 @@ private: bool m_hasValue; bool m_displayStatus; bool m_highlighted; - quint64 m_faces; + quint64 m_maxValue; qint64 m_base; QString m_color; diff --git a/node/dicerollernode.cpp b/node/dicerollernode.cpp index 2641ab7..c3062c9 100644 --- a/node/dicerollernode.cpp +++ b/node/dicerollernode.cpp @@ -9,8 +9,8 @@ -DiceRollerNode::DiceRollerNode(quint64 faces,qint64 offset) - : m_faces(faces),m_diceResult(new DiceResult()),m_offset(offset) +DiceRollerNode::DiceRollerNode(quint64 max,qint64 min) + : m_max(max),m_diceResult(new DiceResult()),m_min(min) { m_result=m_diceResult; } @@ -33,8 +33,8 @@ void DiceRollerNode::run(ExecutionNode* previous) for(quint64 i=0; i < m_diceCount ; ++i) { Die* die = new Die(); - die->setFaces(m_faces); - die->setBase(m_offset); + die->setBase(m_min); + die->setMaxValue(m_max); die->roll(); //qDebug() << die->getValue() << "value"; m_diceResult->insertResult(die); @@ -49,13 +49,13 @@ void DiceRollerNode::run(ExecutionNode* previous) quint64 DiceRollerNode::getFaces() const { - return m_faces; + return abs(m_max-m_min)+1; } QString DiceRollerNode::toString(bool wl) const { if(wl) { - return QString("%1 [label=\"DiceRollerNode faces: %2\"]").arg(m_id).arg(m_faces); + return QString("%1 [label=\"DiceRollerNode faces: %2\"]").arg(m_id).arg(getFaces()); } else { @@ -75,7 +75,7 @@ qint64 DiceRollerNode::getPriority() const } ExecutionNode* DiceRollerNode::getCopy() const { - DiceRollerNode* node = new DiceRollerNode(m_faces,m_offset); + DiceRollerNode* node = new DiceRollerNode(m_max,m_min); if(NULL!=m_nextNode) { node->setNextNode(m_nextNode->getCopy()); diff --git a/node/dicerollernode.h b/node/dicerollernode.h index 4e2f9d7..ab5c47f 100644 --- a/node/dicerollernode.h +++ b/node/dicerollernode.h @@ -17,7 +17,7 @@ public: * @param faces, number of faces of dices * @param offset, first value of dice. */ - DiceRollerNode(quint64 faces, qint64 offset = 1); + DiceRollerNode(quint64 max, qint64 min = 1); /** * @brief run - starts to roll dice. @@ -46,9 +46,9 @@ public: //private members private: quint64 m_diceCount; - quint64 m_faces; /// faces + qint64 m_max; /// faces DiceResult* m_diceResult; - qint64 m_offset; + qint64 m_min; }; #endif // DICEROLLERNODE_H diff --git a/node/ifnode.cpp b/node/ifnode.cpp index b1ca1da..27b588f 100644 --- a/node/ifnode.cpp +++ b/node/ifnode.cpp @@ -137,7 +137,7 @@ void IfNode::run(ExecutionNode *previous) { Die* dice = new Die(); dice->setValue(value); - dice->setFaces(value); + dice->setMaxValue(value); if(m_validator->hasValid(dice,true,true)) { nextNode=m_true; diff --git a/node/listsetrollnode.cpp b/node/listsetrollnode.cpp index fa66710..488721a 100644 --- a/node/listsetrollnode.cpp +++ b/node/listsetrollnode.cpp @@ -99,7 +99,7 @@ void ListSetRollNode::computeFacesNumber(Die* die) { if(m_rangeList.isEmpty()) { - die->setFaces(m_values.size()); + die->setMaxValue(m_values.size()); } else { @@ -116,7 +116,7 @@ void ListSetRollNode::computeFacesNumber(Die* die) ++i; } //qDebug() << "set Faces"<<max; - die->setFaces(max); + die->setMaxValue(max); } } diff --git a/operationcondition.cpp b/operationcondition.cpp index 262ab97..6664c83 100644 --- a/operationcondition.cpp +++ b/operationcondition.cpp @@ -57,7 +57,7 @@ qint64 OperationCondition::hasValid(Die* b,bool recursive,bool unhighlight) cons case Modulo: { Die die; - die.setFaces(b->getFaces()); + die.setMaxValue(b->getMaxValue()); die.insertRollValue(value%m_value); sum+=m_boolean->hasValid(&die,recursive,false); } diff --git a/parsingtoolbox.cpp b/parsingtoolbox.cpp index a5051fc..3d2d2c3 100644 --- a/parsingtoolbox.cpp +++ b/parsingtoolbox.cpp @@ -658,3 +658,16 @@ void ParsingToolBox::readProbability(QStringList& str,QList<Range>& ranges) } } +bool ParsingToolBox::readComment(QString& str, QString & result, QString& comment) +{ + QString left = str; + str = str.trimmed(); + if(str.startsWith("#")) + { + comment = left; + str = str.remove(0,1); + result = str.trimmed(); + return true; + } + return false; +} diff --git a/parsingtoolbox.h b/parsingtoolbox.h index 5d198be..75c01c2 100644 --- a/parsingtoolbox.h +++ b/parsingtoolbox.h @@ -178,6 +178,8 @@ public: * @return */ static IfNode::ConditionType readConditionType(QString &str); + + bool readComment(QString& str, QString &,QString &); private: QMap<QString,BooleanCondition::LogicOperator>* m_logicOp; |