-
Notifications
You must be signed in to change notification settings - Fork 1
/
symbol.sml
47 lines (39 loc) · 1.16 KB
/
symbol.sml
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
signature SYMBOL =
sig
eqtype symbol
val symbol : string -> symbol
val name : symbol -> string
val eq : symbol * symbol -> bool
type 'a table
val empty : 'a table
val enter : 'a table * symbol * 'a -> 'a table
val find : 'a table * symbol -> 'a option
val look : 'a table * symbol -> 'a
end
structure Symbol :> SYMBOL =
struct
type symbol = string * int
structure H = HashTable
exception Symbol
val nextsym = ref 0
val sizeHint = 128
val hashtable : (string, int) H.hash_table =
H.mkTable(HashString.hashString, op = ) (sizeHint, Symbol)
fun symbol name =
case H.find hashtable name
of SOME i => (name, i)
| NONE => let val i = !nextsym
in nextsym := i + 1;
H.insert hashtable (name, i);
(name, i)
end
fun name(s, n) = s
fun eq((_,n1): symbol, (_,n2): symbol) = n1 = n2
structure Table = IntMapTable(type key = symbol
fun getInt(s, n) = n)
type 'a table= 'a Table.table
val empty = Table.empty
val enter = Table.enter
val find = Table.find
val look = Table.lookup
end