X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=compiler%2Fsrc%2Flib%2Ftiger%2Ftiger_symbol.ml;h=a850406bb0e55001c57a077810b0ac732ecbf632;hb=528693fddf524eab49efab2ba6f8df19d95badf0;hp=eb32942e227ac09ca926df0eb2a9d78c678645bc;hpb=cda83e5ec171672a59966d2bd9cd28bd7a8fe083;p=tiger.ml.git diff --git a/compiler/src/lib/tiger/tiger_symbol.ml b/compiler/src/lib/tiger/tiger_symbol.ml index eb32942..a850406 100644 --- a/compiler/src/lib/tiger/tiger_symbol.ml +++ b/compiler/src/lib/tiger/tiger_symbol.ml @@ -5,19 +5,31 @@ type t = ; symbol : int } -let nextsym = ref 0 +let next = + let counter = ref 0 in + fun () -> + incr counter; + !counter let symbols = H.create 16 +let unique_of_string name = + {name; symbol = next ()} + let of_string name = match H.find_opt symbols name with | Some symbol -> {name; symbol} | None -> - incr nextsym; - let symbol = !nextsym in - H.replace symbols ~key:name ~data:symbol; - {name; symbol} + let t = unique_of_string name in + H.replace symbols ~key:t.name ~data:t.symbol; + t let to_string {name; _} = name + +let is_equal {symbol=s1; _} {symbol=s2; _} = + s1 = s2 + +let show {name; symbol} = + Printf.sprintf "Symbol[%S, %d]" name symbol