-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathast.ml
executable file
·67 lines (56 loc) · 2.03 KB
/
ast.ml
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
open Util
type var = string
type fun_id = int
exception UnboundVariable of var
exception IllformedExpression
exception IllformedDefuncExp
type binop = Plus | Minus | Divide | Multiply
type exp =
Var of var
| App of exp * exp
| Int of int
| Lam of var * exp
| Let of var * exp * exp
| Binop of binop * exp * exp
type value = VInt of int | VClosure of env * var * exp
and env = (var * value) list
let empty = []
let lookup g x =
try List.assoc x g with Not_found -> raise @@ UnboundVariable x
let extend g x v =
(x, v)::g
(* get free variables of an expression *)
let fv (e : exp) : var HashSet.t =
let h = HashSet.make() in
let rec fv (bv : var list) (e : exp) : unit =
match e with
| Int i -> ()
| Var x -> if List.mem x bv then () else HashSet.add h x
| Lam (x, e) -> fv (x::bv) e
| Let (x, e1, e2) -> fv bv e1; fv (x :: bv) e2
| App(e1, e2) | Binop (_, e1, e2) -> fv bv e1; fv bv e2 in
fv [] e; h
(*(* get free variables of an expression *)
let fv_defunc (e : defunc_exp) : var HashSet.t =
let h = HashSet.make() in
let rec fv (bv : var list) (e : defunc_exp) : unit =
match e with
| Int i -> ()
| Var x -> if List.mem x bv then () else HashSet.add h x
| Lam (x, e) -> fv (x::bv) e
| Let (x, e1, e2) -> fv bv e1; fv (x :: bv) e2
| App(e1, e2) | Binop (_, e1, e2) -> fv bv e1; fv bv e2 in
fv [] e; h
*)
type defunc_exp =
| Var of var
| Int of int
| App of defunc_exp * defunc_exp * int
| Lam of fun_id * var list
| Binop of binop * defunc_exp * defunc_exp * int
type defunc_value = VInt of int | VClosure of defunc_env * var * defunc_exp
and defunc_env = (var * defunc_value) list
(* each stored function has form (free variable list, arg, closure body)).
* Stored functions have unique fun_ids. *)
type stored_fun = var * defunc_exp * int
type closure_directory = (fun_id * stored_fun) list