Source

olfend / const.ml

Full commit
open Sexplib.Sexp

type t = 
  | Int    of int
  | Float  of float
  | Bool   of bool  (* CR jfuruse: Bool is a variant, not a primitive type *)
  | String of string
  | Char   of char

let t_of_sexp = function
  | List [ Atom "#string"; Atom s ] -> String s
  | List [ Atom "#char"; Atom c ] -> Char c.[0]
  | List _ -> raise Not_found
  | Atom "#true"  -> Bool true
  | Atom "#false" -> Bool false
  | Atom v ->
      try Int (int_of_string v)
      with _ -> try Float (float_of_string v)
      with _ -> raise Not_found

let sexp_of_t = function
  | Int n   -> Atom (string_of_int n)
  | Float n -> Atom (string_of_float n)
  | Bool b  -> Atom (if b then "#true" else "#false")
  | String s -> List [ Atom "#string"; Atom s ]
  | Char c -> List [ Atom "#char"; Atom (String.make 1 c) ]