Ejemplo n.º 1
0
Result Calculator::run(Expression expr)
{
    auto rpn = _parser.toRPN(expr);
    RPNStack calcStack;

    while (!rpn.empty()) {
        auto element = rpn.top();
        if (!_parser.isOperator(element)) {
            calcStack.push(element);
            rpn.pop();
            continue;
        }
        auto rhs = BigInteger(calcStack.top());
        calcStack.pop();
        auto lhs = BigInteger(calcStack.top());
        calcStack.pop();

        BigInteger result;

        if (element == "*") {
            result = lhs * rhs;
        } else if (element == "+") {
            result = lhs + rhs;
        } else if (element == "-") {
            result = lhs - rhs;
        }

        calcStack.push(result.value());
        rpn.pop();
    }

    return calcStack.top();
}
Ejemplo n.º 2
0
void RPNGo::evaluate(RPNScript & scr, RPNStack & st) const
{
	RPNElem * op = st.pop();
	RPNLabel * label = dynamic_cast<RPNLabel *>(op);

	if (label == 0)
		throw RPNError("Operand is not label");
	scr.goToCmd(label->get());
	delete op;
}
Ejemplo n.º 3
0
void RPNGoFalse::evaluate(RPNScript & scr, RPNStack & st) const
{
	RPNConst * opl = st.pop();
	RPNConst * ope = st.pop();
	RPNLabel * label = dynamic_cast<RPNLabel *>(opl);
	RPNInt * exp = dynamic_cast<RPNInt *>(ope);

	if (exp == 0)
		throw RPNError("Operand 1 is not integer");
	if (label == 0)
		throw RPNError("Operand 2 is not label");


	if (exp->get() == 0)
		scr.goToCmd(label->get());
	else
		scr.nextCmd();

	delete opl;
	delete ope;
}