-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbol.sml
48 lines (38 loc) · 1.01 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
48
signature SYMBOL =
sig
eqtype symbol
val symbol : string -> symbol
val name : symbol -> string
type 'a table
val empty : 'a table
val enter : 'a table * symbol * 'a -> 'a table
val look : 'a table * symbol -> 'a option
val eq : symbol * symbol -> bool
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: string,n: int): string = s
structure Table = IntMapTable(type key = symbol
fun getInt(s,n) = n)
fun eq (a,b) = a = b
type 'a table= 'a Table.table
val empty = Table.empty
val enter = Table.enter
val look = Table.look
end
structure S = Symbol