aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorRenaud G <renaud@rolisteam.org>2016-01-30 17:52:00 +0100
committerRenaud G <renaud@rolisteam.org>2016-01-30 17:52:00 +0100
commit530c87f71366810549b718160bc59bda51f876b0 (patch)
tree5245554bdb3ea5289f448c270f273ac783c079b2
parentc11896701cdc28e6a363af9242c111164f14e074 (diff)
downloadOneRoll-530c87f71366810549b718160bc59bda51f876b0.tar.gz
OneRoll-530c87f71366810549b718160bc59bda51f876b0.zip
-rework arithmetic operator
-rw-r--r--diceparser.cpp48
-rw-r--r--diceparser.h1
-rw-r--r--node/scalaroperatornode.cpp36
-rw-r--r--node/scalaroperatornode.h10
4 files changed, 61 insertions, 34 deletions
diff --git a/diceparser.cpp b/diceparser.cpp
index 8d96d5a..98a8d29 100644
--- a/diceparser.cpp
+++ b/diceparser.cpp
@@ -711,12 +711,12 @@ bool DiceParser::readOperator(QString& str,ExecutionNode* previous)
return false;
}
- ScalarOperatorNode* node = new ScalarOperatorNode();
- if(node->setOperatorChar(str[0]))
+ ScalarOperatorNode::ArithmeticOperator op;
+ if(m_parsingToolbox->readArithmeticOperator(str,op))
{
-
+ ScalarOperatorNode* node = new ScalarOperatorNode();
+ node->setArithmeticOperator(op);
ExecutionNode* nodeExec = NULL;
- str=str.remove(0,1);//removal of one character
if(readExpression(str,nodeExec))
{
node->setInternalNode(nodeExec);
@@ -742,7 +742,6 @@ bool DiceParser::readOperator(QString& str,ExecutionNode* previous)
else if(readInstructionOperator(str[0]))
{
str=str.remove(0,1);
- delete node;
ExecutionNode* nodeExec = NULL;
if(readExpression(str,nodeExec))
{
@@ -757,7 +756,6 @@ bool DiceParser::readOperator(QString& str,ExecutionNode* previous)
}
else
{
- delete node;
while(readOption(str,previous,false))
{
previous = getLatestNode(previous);
@@ -973,27 +971,43 @@ bool DiceParser::readOption(QString& str,ExecutionNode* previous, bool hasDice)/
}
bool DiceParser::readIfInstruction(QString& str,ExecutionNode*& trueNode,ExecutionNode*& falseNode)
{
+ if(readBlocInstruction(str,trueNode))
+ {
+ if(readBlocInstruction(str,falseNode))
+ {
+ return true;
+ }
+ return true;
+ }
+ return false;
+}
+bool DiceParser::readBlocInstruction(QString& str,ExecutionNode*& resultnode)
+{
if(str.startsWith('{'))
{
str=str.remove(0,1);
ExecutionNode* node;
+ ScalarOperatorNode::ArithmeticOperator op;
+ ScalarOperatorNode* scalarNode = NULL;
+ if(m_parsingToolbox->readArithmeticOperator(str,op))
+ {
+ scalarNode = new ScalarOperatorNode();
+ scalarNode->setArithmeticOperator(op);
+ }
if(readExpression(str,node))
{
if(str.startsWith('}'))
{
- trueNode = node;
- str=str.remove(0,1);
- if(str.startsWith('{'))
+ if(NULL==scalarNode)
+ {
+ resultnode = node;
+ }
+ else
{
- str=str.remove(0,1);
- ExecutionNode* node2;
- readExpression(str,node2);
- if(str.startsWith('}'))
- {
- falseNode=node2;
- return true;
- }
+ resultnode = scalarNode;
+ scalarNode->setInternalNode(node);
}
+ str=str.remove(0,1);
return true;
}
}
diff --git a/diceparser.h b/diceparser.h
index a1fc8b8..e1a5ee5 100644
--- a/diceparser.h
+++ b/diceparser.h
@@ -312,6 +312,7 @@ private:
ParsingToolBox* m_parsingToolbox;
QString m_helpPath;
bool m_currentTreeHasSeparator;
+ bool readBlocInstruction(QString &str, ExecutionNode *&resultnode);
};
#endif // DICEPARSER_H
diff --git a/node/scalaroperatornode.cpp b/node/scalaroperatornode.cpp
index 67fda8d..22bbdac 100644
--- a/node/scalaroperatornode.cpp
+++ b/node/scalaroperatornode.cpp
@@ -26,13 +26,13 @@
ScalarOperatorNode::ScalarOperatorNode()
- : m_internalNode(NULL),m_scalarResult(new ScalarResult()),m_operator(PLUS)
+ : m_internalNode(NULL),m_scalarResult(new ScalarResult()),m_arithmeticOperator(PLUS)
{
- m_scalarOperationList.insert('+',PLUS);
+ /*m_scalarOperationList.insert('+',PLUS);
m_scalarOperationList.insert('-',MINUS);
m_scalarOperationList.insert('x',MULTIPLICATION);
m_scalarOperationList.insert('*',MULTIPLICATION);
- m_scalarOperationList.insert('/',DIVIDE);
+ m_scalarOperationList.insert('/',DIVIDE);*/
m_result = m_scalarResult;
}
@@ -72,7 +72,7 @@ void ScalarOperatorNode::run(ExecutionNode* previous)
m_internalNode->getResult()->setPrevious(previousResult);
}
- switch(m_operator)
+ switch(m_arithmeticOperator)
{
case PLUS:
m_scalarResult->setValue(add(previousResult->getResult(Result::SCALAR).toReal(),internalResult->getResult(Result::SCALAR).toReal()));
@@ -99,7 +99,7 @@ void ScalarOperatorNode::run(ExecutionNode* previous)
}
}
-bool ScalarOperatorNode::setOperatorChar(QChar c)
+/*bool ScalarOperatorNode::setOperatorChar(QChar c)
{
if(m_scalarOperationList.contains(c))
{
@@ -107,7 +107,7 @@ bool ScalarOperatorNode::setOperatorChar(QChar c)
return true;
}
return false;
-}
+}*/
void ScalarOperatorNode::setInternalNode(ExecutionNode* node)
{
@@ -134,16 +134,26 @@ qint64 ScalarOperatorNode::multiple(qint64 a,qint64 b)
{
return a*b;
}
+ScalarOperatorNode::ArithmeticOperator ScalarOperatorNode::getArithmeticOperator() const
+{
+ return m_arithmeticOperator;
+}
+
+void ScalarOperatorNode::setArithmeticOperator(const ScalarOperatorNode::ArithmeticOperator &arithmeticOperator)
+{
+ m_arithmeticOperator = arithmeticOperator;
+}
+
QString ScalarOperatorNode::toString(bool wl) const
{
QString op="";
- switch(m_operator)
+ switch(m_arithmeticOperator)
{
- case PLUS:
- op="+";
- break;
- case MINUS:
- op="-";
+ case PLUS:
+ op="+";
+ break;
+ case MINUS:
+ op="-";
break;
case MULTIPLICATION:
op="*";
@@ -166,7 +176,7 @@ QString ScalarOperatorNode::toString(bool wl) const
}
qint64 ScalarOperatorNode::getPriority() const
{
- if((m_operator==PLUS)||(m_operator==MINUS))
+ if((m_arithmeticOperator==PLUS)||(m_arithmeticOperator==MINUS))
{
return 1;
}
diff --git a/node/scalaroperatornode.h b/node/scalaroperatornode.h
index ca6f3ac..a8f930a 100644
--- a/node/scalaroperatornode.h
+++ b/node/scalaroperatornode.h
@@ -35,11 +35,11 @@
class ScalarOperatorNode : public ExecutionNode
{
public:
- enum ScalarOperator {PLUS,MINUS,DIVIDE,MULTIPLICATION};
+ enum ArithmeticOperator {PLUS,MINUS,DIVIDE,MULTIPLICATION};
ScalarOperatorNode();
virtual ~ScalarOperatorNode();
virtual void run(ExecutionNode*);
- bool setOperatorChar(QChar c);
+
void setInternalNode(ExecutionNode* node);
virtual QString toString(bool wl)const;
@@ -52,6 +52,9 @@ public:
*/
virtual QMap<ExecutionNode::ERROR_CODE,QString> getExecutionErrorMap();
+ ScalarOperatorNode::ArithmeticOperator getArithmeticOperator() const;
+ void setArithmeticOperator(const ScalarOperatorNode::ArithmeticOperator &arithmeticOperator);
+
private:
static qint64 add(qint64,qint64);
static qint64 substract(qint64,qint64);
@@ -59,10 +62,9 @@ private:
static qint64 multiple(qint64,qint64);
private:
- ScalarOperator m_operator;
+ ArithmeticOperator m_arithmeticOperator;
ScalarResult* m_scalarResult;
ExecutionNode* m_internalNode;
- QMap<QChar,ScalarOperator> m_scalarOperationList;
};
#endif // SCALAROPERATORNODE_H