diff options
| author | 2025-02-09 06:05:05 +0100 | |
|---|---|---|
| committer | 2025-02-09 06:05:05 +0100 | |
| commit | 6fcb5ca46927f7baab744e117af9eb1ce5b74838 (patch) | |
| tree | 803b14b1315dfed095705d0c417b19c970541535 /src | |
| parent | b8486f92408afa1a0c71d3f62d93f49ac8bebc60 (diff) | |
| download | OneRoll-6fcb5ca46927f7baab744e117af9eb1ce5b74838.tar.gz OneRoll-6fcb5ca46927f7baab744e117af9eb1ce5b74838.zip | |
[Dice] add functions: floor, ceil, round
Diffstat (limited to 'src')
| -rw-r--r-- | src/libparser/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/libparser/include/diceparser/parsingtoolbox.h | 9 | ||||
| -rw-r--r-- | src/libparser/node/roundnode.cpp | 71 | ||||
| -rw-r--r-- | src/libparser/node/roundnode.h | 31 | ||||
| -rw-r--r-- | src/libparser/parsingtoolbox.cpp | 57 | ||||
| -rw-r--r-- | src/tests/dice/tst_dice.cpp | 2 |
6 files changed, 166 insertions, 6 deletions
diff --git a/src/libparser/CMakeLists.txt b/src/libparser/CMakeLists.txt index 80b9a25..7937506 100644 --- a/src/libparser/CMakeLists.txt +++ b/src/libparser/CMakeLists.txt @@ -67,6 +67,8 @@ SET( dice_sources ${CMAKE_CURRENT_SOURCE_DIR}/node/repeaternode.cpp ${CMAKE_CURRENT_SOURCE_DIR}/node/switchcasenode.cpp ${CMAKE_CURRENT_SOURCE_DIR}/node/replacevaluenode.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/node/roundnode.cpp + ) include(install_helper OPTIONAL RESULT_VARIABLE installFound) diff --git a/src/libparser/include/diceparser/parsingtoolbox.h b/src/libparser/include/diceparser/parsingtoolbox.h index 8cd9c25..2700ae4 100644 --- a/src/libparser/include/diceparser/parsingtoolbox.h +++ b/src/libparser/include/diceparser/parsingtoolbox.h @@ -36,6 +36,7 @@ #include <diceparser/diceparser_global.h> class Range; class RepeaterNode; +class RoundNode; class DiceAlias; class ExplodeDiceNode; class SwitchCaseNode; @@ -92,7 +93,10 @@ public: enum Function { - REPEAT + REPEAT, + FLOOR, + CEIL, + ROUND }; enum OptionOperator { @@ -167,7 +171,8 @@ public: static void readSubtitutionParameters(SubtituteInfo& info, QString& rest); static bool readPainterParameter(PainterNode* painter, QString& str); static bool readComma(QString& str); - bool readReaperArguments(RepeaterNode* node, QString& source); + bool readRepeaterArguments(RepeaterNode* node, QString& source); + bool readRoundArguments(RoundNode* node, QString& source); bool readExpression(QString& str, ExecutionNode*& node); bool readInstructionOperator(QChar c); bool readNode(QString& str, ExecutionNode*& node); diff --git a/src/libparser/node/roundnode.cpp b/src/libparser/node/roundnode.cpp new file mode 100644 index 0000000..8f825f5 --- /dev/null +++ b/src/libparser/node/roundnode.cpp @@ -0,0 +1,71 @@ +#include "roundnode.h" + +#include <diceparser/parsingtoolbox.h> + +RoundNode::RoundNode(Mode mode) : m_scalarResult(new ScalarResult), m_mode(mode) {} + +void RoundNode::run(ExecutionNode* previous) +{ + m_previousNode= previous; + + if(m_cmd == nullptr) + return; + + m_cmd->execute(this); + auto internal= ParsingToolBox::getLeafNode(m_cmd); + if(!internal) + return; + + auto endResult= internal->getResult(); + + auto scalar= endResult->getResult(Dice::RESULT_TYPE::SCALAR).toDouble(); + + int resVal; + switch(m_mode) + { + case FLOOR: + resVal= std::floor(scalar); + break; + case CEIL: + resVal= std::ceil(scalar); + break; + case ROUND: + resVal= std::round(scalar); + break; + } + + m_scalarResult->setValue(resVal); + m_result= m_scalarResult.get(); +} + +QString RoundNode::toString(bool withLabel) const +{ + + return withLabel ? QString("%1 [label=\"RoundNode\"]").arg(m_id) : m_id; +} + +qint64 RoundNode::getPriority() const +{ + qint64 priority= 0; + if(nullptr != m_nextNode) + { + priority= m_nextNode->getPriority(); + } + + return priority; +} + +ExecutionNode* RoundNode::getCopy() const +{ + RoundNode* node= new RoundNode(m_mode); + if(nullptr != m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; +} + +void RoundNode::setCommand(ExecutionNode* cmd) +{ + m_cmd= cmd; +} diff --git a/src/libparser/node/roundnode.h b/src/libparser/node/roundnode.h new file mode 100644 index 0000000..0bf49b7 --- /dev/null +++ b/src/libparser/node/roundnode.h @@ -0,0 +1,31 @@ +#ifndef ROUNDNODE_H +#define ROUNDNODE_H + +#include "executionnode.h" +#include "scalarresult.h" + +class RoundNode : public ExecutionNode +{ +public: + enum Mode { + FLOOR, + CEIL, + ROUND + }; + RoundNode(Mode mode); + + // ExecutionNode interface +public: + void run(ExecutionNode *previous); + QString toString(bool withLabel) const; + qint64 getPriority() const; + ExecutionNode *getCopy() const; + void setCommand(ExecutionNode* cmd); + +private: + std::unique_ptr<ScalarResult> m_scalarResult; + ExecutionNode* m_cmd= nullptr; + Mode m_mode{ROUND}; +}; + +#endif // ROUNDNODE_H diff --git a/src/libparser/parsingtoolbox.cpp b/src/libparser/parsingtoolbox.cpp index a7b089d..b0d41a7 100644 --- a/src/libparser/parsingtoolbox.cpp +++ b/src/libparser/parsingtoolbox.cpp @@ -61,6 +61,7 @@ #include "node/variablenode.h" #include "operationcondition.h" #include "range.h" +#include "roundnode.h" #include "validatorlist.h" QHash<QString, QString> ParsingToolBox::m_variableHash; @@ -118,6 +119,9 @@ ParsingToolBox::ParsingToolBox() m_OptionOp.insert(QStringLiteral("T"), TransformOption); m_functionMap.insert({QStringLiteral("repeat"), REPEAT}); + m_functionMap.insert({QStringLiteral("floor"), FLOOR}); + m_functionMap.insert({QStringLiteral("ceil"), CEIL}); + m_functionMap.insert({QStringLiteral("round"), ROUND}); m_nodeActionMap.insert(QStringLiteral("@"), JumpBackward); @@ -1438,7 +1442,7 @@ void ParsingToolBox::readSubtitutionParameters(SubtituteInfo& info, QString& res info.setLength(info.length() + sizeS - rest.size()); } -bool ParsingToolBox::readReaperArguments(RepeaterNode* node, QString& source) +bool ParsingToolBox::readRepeaterArguments(RepeaterNode* node, QString& source) { if(!readOpenParentheses(source)) return false; @@ -1466,6 +1470,26 @@ bool ParsingToolBox::readReaperArguments(RepeaterNode* node, QString& source) return false; } + +bool ParsingToolBox::readRoundArguments(RoundNode* node, QString& source) +{ + if(!readOpenParentheses(source)) + return false; + + ExecutionNode* startNode= nullptr; + auto instruction= readExpression(source, startNode); + if(startNode == nullptr || !instruction) + { + m_errorMap.insert(Dice::ERROR_CODE::BAD_SYNTAXE, QObject::tr("Can read the paramater for Round Function.")); + return false; + } + + if(!readCloseParentheses(source)) + return false; + + node->setCommand(startNode); + return true; +} bool ParsingToolBox::readExpression(QString& str, ExecutionNode*& node) { ExecutionNode* operandNode= nullptr; @@ -2338,12 +2362,39 @@ bool ParsingToolBox::readFunction(QString& str, ExecutionNode*& node) case REPEAT: { auto repeaterNode= new RepeaterNode(); - if(ParsingToolBox::readReaperArguments(repeaterNode, str)) + if(ParsingToolBox::readRepeaterArguments(repeaterNode, str)) { node= repeaterNode; } } break; + case FLOOR: + { + auto roundNode= new RoundNode(RoundNode::FLOOR); + if(ParsingToolBox::readRoundArguments(roundNode, str)) + { + node= roundNode; + } + } + break; + case CEIL: + { + auto roundNode= new RoundNode(RoundNode::CEIL); + if(ParsingToolBox::readRoundArguments(roundNode, str)) + { + node= roundNode; + } + } + break; + case ROUND: + { + auto roundNode= new RoundNode(RoundNode::ROUND); + if(ParsingToolBox::readRoundArguments(roundNode, str)) + { + node= roundNode; + } + } + break; } } } @@ -2410,7 +2461,6 @@ std::vector<ExecutionNode*> ParsingToolBox::readInstructionList(QString& str, bo std::vector<ExecutionNode*> startNodes; - bool hasInstruction= false; bool readInstruction= true; while(readInstruction) { @@ -2418,7 +2468,6 @@ std::vector<ExecutionNode*> ParsingToolBox::readInstructionList(QString& str, bo bool keepParsing= readExpression(str, startNode); if(nullptr != startNode) { - hasInstruction= true; startNodes.push_back(startNode); auto latest= startNode; if(keepParsing) diff --git a/src/tests/dice/tst_dice.cpp b/src/tests/dice/tst_dice.cpp index d6b9de3..0332504 100644 --- a/src/tests/dice/tst_dice.cpp +++ b/src/tests/dice/tst_dice.cpp @@ -399,6 +399,8 @@ void TestDice::commandsTest_data() QTest::addRow("cmd91") << "1d20|3i:[>1]{\"Success\"}{\"Failure\"}"; QTest::addRow("cmd92") << "4d10k3;4d10k3;4d10k3;[$1,$2,$3]s;\"Score @4\""; QTest::addRow("cmd93") << "4d10e(10)10"; + QTest::addRow("cmd94") << "15/7;floor($1);ceil($1);round($1)"; + QTest::addRow("cmd94") << "15/7;floor(15/7);ceil(15/7);round(15/7)"; } void TestDice::rangedCommandsTest() |