int CompoundExp::eval(EvalState & state) { if (op == "=") { if (lhs->getType() != IDENTIFIER) { error("Illegal variable in assignment"); } int val = rhs->eval(state); state.setValue(((IdentifierExp *) lhs)->getName(), val); return val; } int left = lhs->eval(state); int right = rhs->eval(state); if (op == "+") return left + right; if (op == "-") return left - right; if (op == "*") return left * right; if (op == "/") { if (right == 0) { cout << "DIVIDE BY ZERO" << endl; error(""); } else { return left / right; } } return 0; }
void LetStmt::execute(EvalState & state) { state.setValue(identifier, exp->eval(state)); }
void InputStmt::execute(EvalState & state) { int value = getInteger("? "); state.setValue(identifier, value); }
void LetStmt::execute(EvalState &state) { int value = exp->eval(state); state.setValue(identifier, value); }