Source

olfend / ident.ml

Full commit
(* deBruijn *)

type t = string * int

let cntr = ref 0
let reset () = cntr := 0
let create_id =
  fun () ->
    incr cntr;
    !cntr

let create name = name, create_id ()
let create_with_stamp name stamp = name, stamp
let compare = compare

(* sexps *)

let t_of_sexp = function
  | Sexplib.Sexp.Atom s ->
      let pos = String.rindex s '_' in
      let idx = int_of_string (String.sub s (pos+1) (String.length s - pos - 1)) in
      let name = String.sub s 0 (pos - 1) in
      name, idx
  | _ -> assert false

let name (name, _) = name
let recreate id = create (name id)
let stamp (_, stamp) = stamp
let to_string (name, idx) = Printf.sprintf "%s__%d" name idx
let sexp_of_t ident = Sexplib.Sexp.Atom (to_string ident)

let ppr ident = Treeprint.Printer.string (to_string ident)
let format = Treeprint.Printer.format ppr