snoeathurc.h
This commit is contained in:
@ -144,18 +144,55 @@ let rec evalExp (e : UntypedExp, vtab : VarTable, ftab : FunTable) : Value =
|
||||
e.g., `And (e1, e2, pos)` should not evaluate `e2` if `e1` already
|
||||
evaluates to false.
|
||||
*)
|
||||
| Times(_, _, _) ->
|
||||
failwith "Unimplemented interpretation of multiplication"
|
||||
| Divide(_, _, _) ->
|
||||
failwith "Unimplemented interpretation of division"
|
||||
| And (_, _, _) ->
|
||||
failwith "Unimplemented interpretation of &&"
|
||||
| Or (_, _, _) ->
|
||||
failwith "Unimplemented interpretation of ||"
|
||||
| Not(_, _) ->
|
||||
failwith "Unimplemented interpretation of not"
|
||||
| Negate(_, _) ->
|
||||
failwith "Unimplemented interpretation of negate"
|
||||
| Times(e1, e2, pos) ->
|
||||
let res1 = evalExp(e1, vtab, ftab)
|
||||
let res2 = evalExp(e2, vtab, ftab)
|
||||
match (res1, res2) with
|
||||
| (IntVal n1, IntVal n2) -> IntVal (n1*n2)
|
||||
| (IntVal _, _) -> reportWrongType "right operand of *" Int res2 (expPos e2)
|
||||
| (_, _) -> reportWrongType "left operand of *" Int res1 (expPos e1)
|
||||
| Divide(e1, e2, pos) ->
|
||||
let res1 = evalExp(e1, vtab, ftab)
|
||||
let res2 = evalExp(e2, vtab, ftab)
|
||||
match (res1, res2) with
|
||||
| (IntVal n1, IntVal n2) ->
|
||||
if (n2 = 0) then
|
||||
raise (MyError("division by 0 error", pos))
|
||||
else IntVal (n1/n2)
|
||||
| (IntVal _, _) -> reportWrongType "right operand of /" Int res2 (expPos e2)
|
||||
| (_, _) -> reportWrongType "left operand of /" Int res1 (expPos e1)
|
||||
| And (e1, e2, pos) ->
|
||||
let res1 = evalExp(e1, vtab, ftab)
|
||||
match res1 with
|
||||
| BoolVal false -> BoolVal false
|
||||
| BoolVal true ->
|
||||
let res2 = evalExp(e2, vtab, ftab)
|
||||
match res2 with
|
||||
| BoolVal p -> BoolVal p
|
||||
| _ -> reportWrongType "right operand of *" Bool res1 (expPos e1)
|
||||
| _ -> reportWrongType "left operand of *" Bool res1 (expPos e1)
|
||||
|
||||
| Or (e1, e2, pos) ->
|
||||
let res1 = evalExp(e1, vtab, ftab)
|
||||
match res1 with
|
||||
| BoolVal true -> BoolVal true
|
||||
| BoolVal false ->
|
||||
let res2 = evalExp(e2, vtab, ftab)
|
||||
match res2 with
|
||||
| BoolVal p -> BoolVal p
|
||||
| _ -> reportWrongType "right operand of *" Bool res1 (expPos e1)
|
||||
| _ -> reportWrongType "left operand of *" Bool res1 (expPos e1)
|
||||
|
||||
| Not(e, pos) ->
|
||||
let res = evalExp(e, vtab, ftab)
|
||||
match res with
|
||||
| BoolVal p -> BoolVal (not p)
|
||||
| _ -> reportWrongType "operand of *" Bool res (expPos e)
|
||||
| Negate(e, pos) ->
|
||||
let res = evalExp(e, vtab, ftab)
|
||||
match res with
|
||||
| IntVal i -> IntVal (0-i)
|
||||
| _ -> reportWrongType "operand of *" Int res (expPos e)
|
||||
| Equal(e1, e2, pos) ->
|
||||
let r1 = evalExp(e1, vtab, ftab)
|
||||
let r2 = evalExp(e2, vtab, ftab)
|
||||
@ -248,8 +285,14 @@ let rec evalExp (e : UntypedExp, vtab : VarTable, ftab : FunTable) : Value =
|
||||
the value of `a`; otherwise raise an error (containing
|
||||
a meaningful message).
|
||||
*)
|
||||
| Replicate (_, _, _, _) ->
|
||||
failwith "Unimplemented interpretation of replicate"
|
||||
| Replicate (narg, aarg, _, pos) ->
|
||||
let n = evalExp(narg, vtab, ftab)
|
||||
let a = evalExp(aarg, vtab, ftab)
|
||||
match n with
|
||||
| IntVal size when (size >= 0) ->
|
||||
let a_array = List.replicate size a
|
||||
ArrayVal (a_array, valueType a)
|
||||
| _ -> reportWrongType "argument of \"Replicate\"" Int n pos
|
||||
|
||||
(* TODO project task 2: `filter(p, arr)`
|
||||
pattern match the implementation of map:
|
||||
@ -259,15 +302,34 @@ let rec evalExp (e : UntypedExp, vtab : VarTable, ftab : FunTable) : Value =
|
||||
under predicate `p`, i.e., `p(a) = true`;
|
||||
- create an `ArrayVal` from the (list) result of the previous step.
|
||||
*)
|
||||
| Filter (_, _, _, _) ->
|
||||
failwith "Unimplemented interpretation of filter"
|
||||
| Filter (farg, arrayarg, _, pos) ->
|
||||
let arr = evalExp(arrayarg, vtab, ftab)
|
||||
match arr with
|
||||
| ArrayVal (a_arr, a_type) ->
|
||||
let new_array = (List.filter (fun x ->
|
||||
let x_value = evalFunArg (farg, vtab, ftab, pos, [x])
|
||||
match x_value with
|
||||
| BoolVal p -> p
|
||||
| _ -> reportWrongType "argument of \"Filter\"" Bool x_value pos
|
||||
) a_arr)
|
||||
ArrayVal (new_array, a_type)
|
||||
| _ -> reportWrongType "argument of \"Filter\"" Int arr pos
|
||||
|
||||
|
||||
(* TODO project task 2: `scan(f, ne, arr)`
|
||||
Implementation similar to reduce, except that it produces an array
|
||||
of the same type and length to the input array `arr`.
|
||||
*)
|
||||
| Scan (_, _, _, _, _) ->
|
||||
failwith "Unimplemented interpretation of scan"
|
||||
| Scan (farg, ne, arrayexp, _, pos) ->
|
||||
let arr = evalExp(arrayexp, vtab, ftab)
|
||||
let init_e = evalExp(ne, vtab, ftab)
|
||||
match arr with
|
||||
| ArrayVal (a_arr, a_type) ->
|
||||
let new_array = (List.scan (fun e x ->
|
||||
evalFunArg (farg, vtab, ftab, pos, [e;x])
|
||||
) init_e a_arr)
|
||||
ArrayVal (List.tail new_array, a_type)
|
||||
| _ -> reportWrongType "argument of \"Scan\"" Int arr pos
|
||||
|
||||
| Read (t,p) ->
|
||||
let str = Console.ReadLine()
|
||||
|
Reference in New Issue
Block a user