-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathevaluation.ml
More file actions
86 lines (67 loc) · 2.7 KB
/
evaluation.ml
File metadata and controls
86 lines (67 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
(*
CS 51 Final Project
MiniML -- Evaluation
Spring 2017
*)
(* This module implements a small untyped ML-like language under
various operational semantics.
*)
open Expr ;;
(* Exception for evaluator runtime, generated by a runtime error *)
exception EvalError of string ;;
(* Exception for evaluator runtime, generated by an explicit "raise" construct *)
exception EvalException ;;
(* Environments and values *)
module type Env_type = sig
type env
type value =
| Val of expr
| Closure of (expr * env)
val create : unit -> env
val close : expr -> env -> value
val lookup : env -> varid -> value
val extend : env -> varid -> value ref -> env
val env_to_string : env -> string
val value_to_string : ?printenvp:bool -> value -> string
end
module Env : Env_type =
struct
type env = (varid * value ref) list
and value =
| Val of expr
| Closure of (expr * env)
(* Creates an empty environment *)
let create () : env = [] ;;
(* Creates a closure from an expression and the environment it's
defined in *)
let close (exp : expr) (env : env) : value =
failwith "close not implemented" ;;
(* Looks up the value of a variable in the environment *)
let lookup (env : env) (varname : varid) : value =
failwith "lookup not implemented" ;;
(* Returns a new environment just like env except that it maps the
variable varid to loc *)
let extend (env : env) (varname : varid) (loc : value ref) : env =
failwith "extend not implemented" ;;
(* Returns a printable string representation of a value; the flag
printenvp determines whether to include the environment in the
string representation when called on a closure *)
let value_to_string ?(printenvp : bool = true) (v : value) : string =
failwith "value_to_string not implemented" ;;
(* Returns a printable string representation of an environment *)
let env_to_string (env : env) : string =
failwith "env_to_string not implemented" ;;
end
;;
(* The evaluation function: Returns the result of type `value` of
evaluating the expression `exp` in the environment `env`. In this
initial implementation, we just convert the expression unchanged to
a value and return it. *)
(** The external evaluator, which can be either the identity function,
the substitution model version or the dynamic or lexical
environment model version. *)
let eval_t exp _env = exp ;;
let eval_s _ = failwith "eval_s not implemented" ;;
let eval_d _ = failwith "eval_d not implemented" ;;
let eval_l _ = failwith "eval_l not implemented" ;;
let evaluate = eval_t ;;