std::vector<ExpressionItem*> CalculateSequenceForPriority(const std::vector<ExpressionItem*> i_sequence, OperatorPriority i_priority) { assert(i_sequence.size() != 0); std::vector<ExpressionItem*> result; size_t currentLoc = 0; while (currentLoc < i_sequence.size()) { switch (i_sequence[currentLoc]->Type()) { case ExpressionItemTypeOperand: { Operand* operand = dynamic_cast<Operand*>(i_sequence[currentLoc]); result.push_back(new Operand(operand->GetValue(), operand->NestingLevel(), 0)); currentLoc++; break; } case ExpressionItemTypeBinaryOperator: { BinaryOperator* bOperator = dynamic_cast<BinaryOperator*>(i_sequence[currentLoc]); if (bOperator->Priority() == i_priority) { assert(result.size() != 0); assert(result.back()->Type() == ExpressionItemTypeOperand); assert(currentLoc < i_sequence.size() - 1); assert(i_sequence[currentLoc + 1]->Type() == ExpressionItemTypeOperand); Operand* left = dynamic_cast<Operand*>(result.back()); Operand* right = dynamic_cast<Operand*>(i_sequence[currentLoc + 1]); double value = bOperator->CalculateResultForOperands(*left, *right); delete left; result.back() = new Operand(value, right->NestingLevel(), 0); currentLoc += 2; } else { result.push_back(BinaryOperator::Copy(bOperator)); currentLoc++; } break; } default: assert(false); } } return result; }
int OperatorFactory::Priority(char p) { BinaryOperator* protoType = GetOperatorPrototype(p); return (protoType!=0)?protoType->Priority():0; }