annotate lab2/dict.ml @ 1:b5139af1a420 tip basis

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