int EvalExp(expressionADT exp) { switch (ExpType(exp)) { case IntegerType: return (ExpInteger(exp)); case IdentifierType: return (GetIdentifierValue(ExpIdentifier(exp))); case CompoundType: return (EvalCompound(exp)); } }
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"); } }
valueADT Eval(expADT exp, environmentADT env){ string funcarg, funcname; valueADT value, valueCallExpress, valueCallArg, valueBody; environmentADT newEnviron; expADT callexpress; senseRecursion(FALSE); switch (ExpType(exp)){ case FuncExp: return NewFuncValue(GetFuncFormalArg(exp), GetFuncBody(exp), NewClosure(env)); break; case IfExp: if (controlExpression(GetIfRelOp(exp), GetIfLHSExpression(exp), GetIfRHSExpression(exp), NewClosure(env))) //check to see if control exp is valid return Eval(GetIfThenPart(exp), NewClosure(env)); else return Eval(GetIfElsePart(exp), NewClosure(env)); break; case CallExp: callexpress = GetCallExp(exp); valueCallArg = GetCallActualArg(exp); // argument for f valueCallExpress = Eval(callexpress, env); if (ValueType(valueCallExpress) == FuncValue){ //behöver inte skapa ny miljö utan ist hämta den som finns i funcvaluet. newEnviron = GetFuncValueClosure(valueCallExpress); funcarg = GetFuncValueFormalArg(valueCallExpress); DefineIdentifier(newEnviron, funcarg, valueCallArg, env); //define argument of function in body return Eval(GetFuncValueBody(valueCallExpress), newEnviron); } return valueCallExpress; break; case ConstExp: return NewIntegerValue(ExpInteger(exp)); break; case IdentifierExp: //printf(" %s ", ExpIdentifier(exp)); value = GetIdentifierValue(env, ExpIdentifier(exp)); return Eval(GetFuncValueBody(value), GetFuncValueClosure(value)); break; case CompoundExp: return NewIntegerValue(EvalCompound(exp, env)); break; default: break; } }