%% $Id: Eval.oz,v 1.2 2010/10/11 18:06:38 leavens Exp leavens $ %% Evaluation of ASTs written using the following grammar %% ::= boolLit( ) %% | intLit( ) %% | subExp( ) %% | equalExp( ) %% | ifExp( ) declare fun {Eval E} case E of boolLit(B) then boolLit(B) [] intLit(_) then E [] subExp(E1 E2) then case {Eval E1}#{Eval E2} of intLit(I1)#intLit(I2) then intLit(I1 - I2) else raise badSubExp(E) end end [] equalExp(E1 E2) then case {Eval E1}#{Eval E2} of intLit(I1)#intLit(I2) then boolLit(I1 == I2) [] boolLit(B1)#boolLit(B2) then boolLit(B1 == B2) else boolLit(false) end [] ifExp(E1 E2 E3) then case {Eval E1} of boolLit(true) then {Eval E2} [] boolLit(false) then {Eval E3} else raise badIfTest(E1) end end end end