Exemple #1
0
static int EvalCompound(expressionADT exp)
{
    char op;
    int lhs, rhs;

    op = ExpOperator(exp);
    if (op == '=') {
        rhs = EvalExp(ExpRHS(exp));
        SetIdentifierValue(ExpIdentifier(ExpLHS(exp)), rhs);
        return (rhs);
    }
    lhs = EvalExp(ExpLHS(exp));
    rhs = EvalExp(ExpRHS(exp));
    switch (op) {
      case '+': return (lhs + rhs);
      case '-': return (lhs - rhs);
      case '*': return (lhs * rhs);
      case '/': return (lhs / rhs);
      default:  Error("Illegal operator");
    }
}
Exemple #2
0
static int EvalCompound(expADT exp, environmentADT env){
    char op;
    int lhs, rhs;
	valueADT lValue, rValue;

    op = ExpOperator(exp);

	lValue = Eval(ExpLHS(exp), NewClosure(env));
    rValue = Eval(ExpRHS(exp), NewClosure(env));
	
	lhs = GetIntValue(lValue);
	rhs = GetIntValue(rValue);
		


    switch (op) {
      case '+': return (lhs + rhs);
      case '-': return (lhs - rhs);
      case '*': return (lhs * rhs);
	  case '/': if (rhs == 0) Error("Division by zero\n"); else return (lhs / rhs);
      default:  Error("Illegal operator");
    }
}