diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lab2/dict.ml	Thu Oct 05 08:04:15 2017 +0100
@@ -0,0 +1,36 @@
+(* lab2/dict.ml *)
+(* Copyright (c) 2017 J. M. Spivey *)
+
+(* Environments are implemented using a library module that 
+   represents mappings by balanced binary trees. *)
+
+type ident = string
+
+type ptype = 
+    Integer 
+  | Boolean 
+  | Array of int * ptype
+  | Void
+
+(* |def| -- definitions in environment *)
+type def = 
+  { d_tag: ident;               (* Name *)
+    d_type: ptype;              (* Type *)
+    d_lab: string }             (* Global label *)
+
+module IdMap = Map.Make(struct type t = ident  let compare = compare end)
+
+type environment = Env of def IdMap.t
+
+let can f x = try f x; true with Not_found -> false
+
+(* |define| -- add a definition *)
+let define d (Env e) = 
+  if can (IdMap.find d.d_tag) e then raise Exit;
+  Env (IdMap.add d.d_tag d e)
+
+(* |lookup| -- find definition of an identifier *)
+let lookup x (Env e) = IdMap.find x e
+
+(* |init_env| -- empty environment *)
+let init_env = Env IdMap.empty