%% $Id: Eval.oz,v 1.1 2007/10/22 06:58:39 leavens Exp leavens $ %% Evaluation of ASTs written using the following grammar %% ::= boolLit( ) %% | intLit( ) %% | subExp( ) %% | equalExp( ) %% | ifExp( ) declare fun {Eval E} case E of boolLit( _ ) then E [] 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 raise badEqualExp(E) end end [] ifExp(E1 E2 E3) then case {Eval E1} of boolLit(B) then if B then {Eval E2} else {Eval E3} end else raise badIflExp(E) end end end end