-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.ml
45 lines (36 loc) · 987 Bytes
/
atom.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
(* canopy global symbol table
*
* copyright (c) 2012 by jeffrey massung
* all rights reserved
*
* atom.ml
*)
(* atom type *)
type t = { name : string
; i : int
}
(* alias for AtomMap *)
type atom = t
(* use integers for mapping instead of atoms *)
module IntMap = Map.Make(struct type t = int let compare = compare end)
(* the one and only symbol table and current gensym counter *)
let symbols = Hashtbl.create 400
let gensym = ref 0
(* compare two atoms *)
let compare a b =
match a.i = b.i with
true -> 0
| false -> compare a.name b.name
(* map tree *)
module AtomMap = Map.Make(struct type t = atom let compare = compare end)
(* intern a new atom into the symbol table *)
let intern s =
try
Hashtbl.find symbols s
with Not_found ->
let sym = { name=s; i=(!gensym) } in
Hashtbl.add symbols s sym;
incr gensym;
sym
(* ensure that an atom exists, don't create if not *)
let intern_existing = Hashtbl.find symbols