S32 check_multiplication(const IOperand &first, const IOperand &second) { S32 value1 = getValueI(first.toString()); S32 value2 = getValueI(second.toString()); long long result = (long long)value1 * (long long) value2; if (first.getType() == Int32 || second.getType() == Int32) { if ((S32)result != result) throw Erreur(Overflow); else if (result / (long long)value2 != (long long)value1) throw Erreur(Overflow); return (S32)result; } else if (first.getType() == Int16 || second.getType() == Int16) { if ((S16)result != result) throw Erreur(Overflow); else if (result /(long long)value2 != (long long)value1) throw Erreur(Overflow); return (S16)result; } else { if ((S8)result != result) throw Erreur(Overflow); else if (result /(long long)value2 != (long long)value1) throw Erreur(Overflow); return (S8)result; } }
S32 check_soustraction(const IOperand &first, const IOperand &second) { S32 value1 = getValueI(first.toString()); S32 value2 = getValueI(second.toString()); long long result = (long long)value1 - (long long)value2; if (first.getType() == Int16 || second.getType() == Int16) { if ((S16)result != result) throw Erreur(Overflow); return ((S16)result); } else if (first.getType() == Int8 || second.getType() == Int8) { if ((S8)result != result) throw Erreur(Overflow); return ((S8)result); } else { if ((S32)result != result) throw Erreur(Overflow); return ((S32)result); } }
S32 check_modulo(const IOperand &first, const IOperand &second) { S32 value1 = getValueI(first.toString()); S32 value2 = getValueI(second.toString()); long long result; result = (long long)value1 % (long long)value2; if (first.getType() == Int32 || second.getType() == Int32) return (S32)result; else if (first.getType() == Int16 || second.getType() == Int16) return (S16)result; else return (S8)result; }
S32 check_addition(const IOperand &first, const IOperand &second) { S32 value1 = getValueI(first.toString()); S32 value2 = getValueI(second.toString()); S32 result = value1 + value2; if (first.getType() == Int16 || second.getType() == Int16) result = (S16)result; else if (first.getType() == Int8 || second.getType() == Int8) result = (S8)result; if (value1 == 0 || value2 == 0); else if (value1 > 0 && value2 > 0) { if (result < value1 || result < value2) throw Erreur(Overflow); } else if (getValueI(first.toString()) < 0 && getValueI(second.toString()) < 0) { if (result > value1 || result > value2) throw Erreur(Underflow); } return result; }
void testOverloadMod(void) { Hatchery hatchery; IOperand *firstOperand = hatchery.createOperand(Int8, "8"); IOperand *secondOperand = hatchery.createOperand(Float, "42.50"); IOperand *thirdOperand = *secondOperand % *firstOperand; if (UnitTests::isNotEqual(thirdOperand->getType(), Float)) { printError("Add Overload is not returning the right type for IOperand"); } if (UnitTests::isNotEqual(thirdOperand->toString(), "2")) { printError("Add Overload is not returning the right value for IOperand"); } }
void VM::assert( IOperand const & o ) { std::istringstream iss; double thisValue; double operandValue; iss.str(_stack.back()->toString()); iss >> thisValue; iss.clear(); iss.str(o.toString()); iss >> operandValue; bool operandsHaveSameType = (_stack.back()->getType() == o.getType()); bool operandsAreEqual = (thisValue == operandValue); if (!(operandsAreEqual && operandsHaveSameType)) throw AssertionFailedException(); }
bool VmStack::assert(void) { IOperand *last; if (this->stack.empty()) { throw VmStack::Error("Stack", "assert on a empty stack"); return false; } else { last = this->stack.back(); if (!last || last->toString() != this->argument.second || last->getType() != this->argument.first) throw VmStack::Error("Execution", "assert is not verify"); } return true; }
bool VmStack::print(void) { IOperand *p; if (this->stack.empty()) { throw VmStack::Error("Stack", "print on a empty stack"); return false; } else { p = this->stack.back(); if (p->getType() == Int8) std::cout << Transform::stringToValue<int8>(p->toString()) << std::endl; else throw VmStack::Error("Execution", "print on a non-int8 value"); } return true; }