diff options
| author | 2015-10-11 16:06:53 +0200 | |
|---|---|---|
| committer | 2015-10-11 16:06:53 +0200 | |
| commit | 2036d7d5e028c08b022390d607a2ead188e7cc90 (patch) | |
| tree | a527c6409927189d1a5d126ac1576eab7e93caf5 /parsingtoolbox.cpp | |
| parent | 84691771b54365160f5b1d54033e00187dbbf043 (diff) | |
| parent | 29f0126a7b8ae3479597252f5056d5e67b6ed10d (diff) | |
| download | OneRoll-2036d7d5e028c08b022390d607a2ead188e7cc90.tar.gz OneRoll-2036d7d5e028c08b022390d607a2ead188e7cc90.zip | |
Merge branch 'rangeInList'
resolved conflicts and merge rangeInList in master.
Conflicts:
HelpMe.md
diceparser.cpp
parsingtoolbox.cpp
result/result.cpp
result/stringresult.h
Diffstat (limited to 'parsingtoolbox.cpp')
| -rw-r--r-- | parsingtoolbox.cpp | 199 |
1 files changed, 175 insertions, 24 deletions
diff --git a/parsingtoolbox.cpp b/parsingtoolbox.cpp index 2d26d79..00072c9 100644 --- a/parsingtoolbox.cpp +++ b/parsingtoolbox.cpp @@ -33,6 +33,12 @@ ParsingToolBox::ParsingToolBox() m_logicOp->insert("<",BooleanCondition::LesserThan); m_logicOp->insert("=",BooleanCondition::Equal); m_logicOp->insert(">",BooleanCondition::GreaterThan); + + + m_logicOperation = new QMap<QString,CompositeValidator::LogicOperation>(); + m_logicOperation->insert("|",CompositeValidator::OR); + m_logicOperation->insert("^",CompositeValidator::EXCLUSIVE_OR); + m_logicOperation->insert("&",CompositeValidator::AND); } ExecutionNode* ParsingToolBox::addSort(ExecutionNode* e,bool b) { @@ -74,28 +80,23 @@ ParsingToolBox::~ParsingToolBox() Validator* ParsingToolBox::readValidator(QString& str) { Validator* returnVal=NULL; - bool expectSquareBrasket=false; - if((str.startsWith("["))) - { - str=str.remove(0,1); - expectSquareBrasket = true; - } + + bool isOk = true; + BooleanCondition::LogicOperator myLogicOp = BooleanCondition::Equal; - //bool hasReadLogicOperator = - readLogicOperator(str,myLogicOp); - int value=0; + bool hasReadLogicOperator = readLogicOperator(str,myLogicOp); + qint64 value=0; if(readNumber(str,value)) { - bool isOk = true; if(str.startsWith("-")) { str=str.remove(0,1); - int end=0; + qint64 end=0; if(readNumber(str,end)) { - if(expectSquareBrasket) + /* if(expectSquareBrasket) { if(str.startsWith("]")) { @@ -106,7 +107,7 @@ Validator* ParsingToolBox::readValidator(QString& str) { isOk=false; } - } + }*/ if(isOk) { str=str.remove(0,1); @@ -118,12 +119,12 @@ Validator* ParsingToolBox::readValidator(QString& str) } else { - if((expectSquareBrasket)&&(str.startsWith("]"))) + /* if((expectSquareBrasket)&&(str.startsWith("]"))) { str=str.remove(0,1); isOk=true; - } - if(isOk) + }*/ + //if(isOk) { BooleanCondition* condition = new BooleanCondition(); condition->setValue(value); @@ -134,14 +135,86 @@ Validator* ParsingToolBox::readValidator(QString& str) } return returnVal; } -bool ParsingToolBox::readNumber(QString& str, int& myNumber) +Validator* ParsingToolBox::readCompositeValidator(QString& str) +{ + bool expectSquareBrasket=false; + if((str.startsWith("["))) + { + str=str.remove(0,1); + expectSquareBrasket = true; + } + + Validator* tmp = readValidator(str); + CompositeValidator::LogicOperation opLogic; + + QVector<CompositeValidator::LogicOperation>* operators = new QVector<CompositeValidator::LogicOperation>(); + QList<Validator*>* validatorList = new QList<Validator*>(); + + while(NULL!=tmp) + { + bool hasOperator = readLogicOperation(str,opLogic); + if( hasOperator ) + { + operators->append(opLogic); + validatorList->append(tmp); + tmp = readValidator(str); + } + else + { + if((expectSquareBrasket)&&(str.startsWith("]"))) + { + str=str.remove(0,1); + //isOk=true; + } + + if(!validatorList->isEmpty()) + { + validatorList->append(tmp); + } + else + { + return tmp; + } + tmp = NULL; + } + + } + CompositeValidator* validator = new CompositeValidator(); + validator->setOperationList(operators); + validator->setValidatorList(validatorList); + + return validator; +} +bool ParsingToolBox::readLogicOperation(QString& str,CompositeValidator::LogicOperation& op) +{ + QString longKey; + foreach(QString tmp, m_logicOperation->keys()) + { + if(str.startsWith(tmp)) + { + if(longKey.size()<tmp.size()) + { + longKey = tmp; + } + } + } + if(longKey.size()>0) + { + str=str.remove(0,longKey.size()); + op = m_logicOperation->value(longKey); + return true; + } + + return false; +} + +bool ParsingToolBox::readNumber(QString& str, qint64& myNumber) { if(str.isEmpty()) return false; QString number; int i=0; - while(i<str.length() && ((str[i].isNumber()) || ( (i==0) && (str[i]=='-')))) { number+=str[i]; @@ -152,7 +225,7 @@ bool ParsingToolBox::readNumber(QString& str, int& myNumber) return false; bool ok; - myNumber = number.toInt(&ok); + myNumber = number.toLongLong(&ok); if(ok) { str=str.remove(0,number.size()); @@ -180,17 +253,18 @@ bool ParsingToolBox::readCloseParentheses(QString& str) else return false; } -bool ParsingToolBox::readList(QString& str,QStringList& list) +bool ParsingToolBox::readList(QString& str,QStringList& list,QList<Range>& ranges) { if(str.startsWith("[")) { str=str.remove(0,1); - int pos = str.indexOf("]"); + int pos = str.lastIndexOf("]"); if(-1!=pos) { QString liststr = str.left(pos); list = liststr.split(","); str=str.remove(0,pos+1); + readProbability(list,ranges); return true; } } @@ -208,8 +282,6 @@ bool ParsingToolBox::readAscending(QString& str) return true; } return false; - - } bool ParsingToolBox::isValidValidator(ExecutionNode* previous, Validator* val) { @@ -235,7 +307,7 @@ DiceRollerNode* ParsingToolBox::getDiceRollerNode(ExecutionNode* previous) previous = previous->getPreviousNode(); } } -bool ParsingToolBox::readDiceRange(QString& str,int& start, int& end) +bool ParsingToolBox::readDiceRange(QString& str,qint64& start, qint64& end) { bool expectSquareBrasket=false; @@ -272,3 +344,82 @@ bool ParsingToolBox::readDiceRange(QString& str,int& start, int& end) } } +ParsingToolBox::LIST_OPERATOR ParsingToolBox::readListOperator(QString& str) +{ + + if(str.startsWith('u')) + { + return UNIQUE; + } + return NONE; +} +void ParsingToolBox::readProbability(QStringList& str,QList<Range>& ranges) +{ + quint64 totalDistance=0; + quint64 undefDistance = 0; + int undefCount=0; + int maxValue = 0; + int i=0; + int j=0; + //range + foreach(QString line,str) + { + int pos = line.indexOf('['); + if(-1!=pos) + { + QString range = line.right(line.length()-pos); + line = line.left(pos); + str[j]=line; + qint64 start; + qint64 end; + if(readDiceRange(range,start,end)) + { + Range range; + range.setValue(start,end); + ranges.append(range); + totalDistance += end-start+1; + ++i; + } + else + { + Range range; + range.setStart(start); + ranges.append(range); + ++undefCount; + undefDistance +=start; + } + if((end>maxValue)||(i==1)) + { + maxValue = end; + } + } + ++j; + + } + + qint64 totalDistPourcent = totalDistance * undefDistance / (100-undefDistance); + + if(totalDistPourcent<undefCount) + { + totalDistPourcent = undefCount; + } + + for(int i = 0; i< ranges.size(); ++i) + { + Range tmp = ranges.at(i); + if(!tmp.isFullyDefined()) + { + int dist = tmp.getStart(); + tmp.setStart(maxValue+1); + maxValue+=1; + double truc = undefDistance*1.0/dist; + + tmp.setEnd(maxValue+(truc*totalDistPourcent)); + maxValue = maxValue+(truc*totalDistPourcent); + ranges[i]=tmp; + } + } + + + +} |