comparison lab2/dict.ml @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bfdcc3820b32
1 (* lab2/dict.ml *)
2 (* Copyright (c) 2017 J. M. Spivey *)
3
4 (* Environments are implemented using a library module that
5 represents mappings by balanced binary trees. *)
6
7 type ident = string
8
9 type ptype =
10 Integer
11 | Boolean
12 | Array of int * ptype
13 | Void
14
15 (* |def| -- definitions in environment *)
16 type def =
17 { d_tag: ident; (* Name *)
18 d_type: ptype; (* Type *)
19 d_lab: string } (* Global label *)
20
21 module IdMap = Map.Make(struct type t = ident let compare = compare end)
22
23 type environment = Env of def IdMap.t
24
25 let can f x = try f x; true with Not_found -> false
26
27 (* |define| -- add a definition *)
28 let define d (Env e) =
29 if can (IdMap.find d.d_tag) e then raise Exit;
30 Env (IdMap.add d.d_tag d e)
31
32 (* |lookup| -- find definition of an identifier *)
33 let lookup x (Env e) = IdMap.find x e
34
35 (* |init_env| -- empty environment *)
36 let init_env = Env IdMap.empty