-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymtab.ml
50 lines (37 loc) · 999 Bytes
/
symtab.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
open Utils
type ('a, 'b) scope = ('a, 'b) Hashtbl.t
type ('a, 'b) t = (('a, 'b) scope) Stack.t
let empty () = Stack.create ()
let enter_scope st = Stack.push (Hashtbl.create 10) st
let exit_scope st = Stack.pop st |> ignore
let register st name data =
Hashtbl.add (Stack.top st) name data
let register_many st pairs =
List.iter (uncurry (register st)) pairs
let lookup st name =
let e = ref None in
Stack.iter (fun scope ->
try
e := Some (Hashtbl.find scope name)
with Not_found -> ()) st;
!e
let intern st name data =
try
Hashtbl.find (Stack.top st) name
with Not_found -> register st name data; data
let intern_init st name f =
try
Hashtbl.find (Stack.top st) name
with Not_found ->
let data = f () in
register st name data; data
let in_scope f = fun st ->
enter_scope st;
let res = f st in
exit_scope st;
res
let current st = Stack.top st
let map_scope f scope =
let acc = ref [] in
Hashtbl.iter (fun k v -> acc := (f k v)::!acc) scope;
!acc