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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
open Fmlib
open Common
type t =
{map: int list String_map.t; has_locs: bool; cnt: int}
let empty: t =
{map = String_map.empty; has_locs = false; cnt = 0}
let count (m:t): int =
m.cnt
let has_locals (m: t): bool =
m.has_locs
let find (name:string) (m:t): int list =
match String_map.maybe_find name m.map with
| None ->
[]
| Some lst ->
lst
let add_unnamed (m:t): t =
{m with cnt = 1 + m.cnt; has_locs = true}
let add (name:string) (global:bool) (m:t): t =
assert (name <> "");
if name = "_" then
add_unnamed m
else
{
map =
String_map.add
name
(match String_map.maybe_find name m.map with
| None ->
[m.cnt]
| Some lst ->
if global then
m.cnt :: lst
else
[m.cnt])
m.map;
has_locs =
not global;
cnt =
1 + m.cnt
}
let add_global (name:string) (m:t): t =
assert (name <> "");
assert (not (has_locals m));
add name true m
let add_local (name: string) (m:t) : t =
assert (name <> "");
add name false m