CS代考程序代写 ocaml interpreter Augment interpreter_bigstep.ml with the exception
Augment interpreter_bigstep.ml with the exception
exception Eval_error
and the function: eval : exp -> exp
let rec eval (e : exp) = … insert code …
The function evaluates the expressions of the language, and returns a value, or raises the OCaml exception Eval_error if the computation fails.
Test eval with the following inputs:
1) true => true
(eval True)
Output: True
2) false => false
(eval False)
Output: False
3) 0 => 0
(eval (Num 0))
Output: (Num 0)
4) iszero(0) => true
(eval (IsZero (Num 0)))
Output: True
5) iszero(1 + 1) => false
(eval (IsZero (Plus (Num 1, Num 1))))
Output: False
6) iszero((2 + -1) + 1) => false
(eval (IsZero (Plus (Plus (Num 2, Num (-1)), Num 1))))
Output: False
7) (-1 + 1) + (-1 + 1) => 0
(eval (Plus (Plus (Num (-1), Num 1), Plus (Num (-1), Num 1))))
Output: (Num 0)
8) -1 + ((2 * 2) + 1) => 4
(eval (Plus (Num (-1), Plus (Mult (Num 2, Num 2), Num 1))))
Output: (Num 4)
9) (((2 + -1) + 1) + -1) => 1
(eval (Plus (Plus (Plus (Num 2, Num (-1)), Num 1), Num (-1))))
Output: (Num 1)
10) iszero(-1 + 1) + 1 => Eval_error
(eval (Plus (IsZero (Plus (Num (-1), Num 1)), Num 1)))
Output: Eval_error
11) iszero(if iszero(0) then true else 0) => Eval_error
(eval (IsZero (If (IsZero (Num 0), True, Num 0))))
Output: Eval_error
12) iszero(if iszero(5 * 0) then (if false then 0 else iszero(-1 + 0)) else 0) => Eval_error
(eval
(IsZero
(If
( IsZero (Mult (Num 5, Num 0))
, If (False, Num 0, IsZero (Plus (Num (-1), Num 0)))
, Num 0 ))))
Output: Eval_error
13) if iszero(-1 + 1) then 2 else true => 2
(eval (If (IsZero (Plus (Num (-1), Num 1)), Num 2, True)))
Output: (Num 2)
14) if (if iszero((1 + -1) * 1) then false else true) then 1 * 2 else true => true
(eval
(If
( If (IsZero (Mult (Plus (Num 1, Num (-1)), Num 1)), False, True)
, Mult (Num 1, Num 2)
, True )))
Output: True
15) if (if iszero(0 * 0) then iszero(2) else 0) then 2 * (1 * 1) else ((((if iszero(0) then 1 else 0) + -1) + 1) + -1) + 1 => 1
(eval
(If
( If (IsZero (Mult (Num 0, Num 0)), IsZero (Num 2), Num 0)
, Mult (Num 2, Mult (Num 1, Num 1))
, Plus
( Plus
( Plus
( Plus (If (IsZero (Num 0), Num 1, Num 0), Num (-1))
, Num 1 )
, Num (-1) )
, Num 1 ) )))
Output: (Num 1)
16) if true then (if true then (if false then 0 else 1) * 1 else 5) else (4 * 1) + 1 => 1
(eval
(If
( True
, If (True, Mult (If (False, Num 0, Num 1), Num 1), Num 5)
, Plus (Mult (Num 4, Num 1), Num 1) )))
Output: (Num 1)
17) if iszero(if iszero(-1 + 2) then 0 else 1) then (if true then (if false then 0 * 6) else 5) else 5 => 5
(eval
(If
( IsZero (If (IsZero (Plus (Num (-1), Num 2)), Num 0, Num 1))
, If
( True
, If (False, Mult (Num 0, Num 6), Plus (Num 0, Num 1))
, Num 5 )
, Num 5 )))
Output: (Num 5)
18) if iszero(-1 + (1 + (-1 + 1))) then iszero(true) else 1 => Eval_error
(eval
(If
( IsZero (Plus (Num (-1), Plus (Num 1, Plus (Num (-1), Num 1))))
, IsZero True
, Num 1 )))
Output: Eval_error
19) 1 + (-1 + (if iszero(1 + (if true then 1 else 2)) then 1 + 2 else 2 * 2)) => 4
(eval
(Plus
( Num 1
, Plus
( Num (-1)
, If
( IsZero (Plus (Num 1, If (True, Num 1, Num 2)))
, Plus (Num 1, Num 2)
, Mult (Num 2, Num 2) ) ) )))
Output: (Num 4)
20) -1 + (if iszero(5 + -4) then 123 * (5 + -4) else iszero(0)) => Eval_error
(eval
(Plus
( Num (-1)
, If
( IsZero (Plus (Num 5, Num (-4)))
, Mult (Num 123, Plus (Num 5, Num (-4)))
, IsZero (Num 0) ) )))
Output: Eval_error