Source

olfend / rawdata.ml

Full commit
(* open Spotlib.Spot *)
open Sexplib.Sexp
open Sexplib.Conv

type variant_args = string * Rawtype.t list with sexp
type record_field = string * Rawtype.t with sexp

type kind = 
  | Variant of variant_args list
  | Record  of record_field list
  | Alias   of Rawtype.t

let sexp_of_kind = function
  | Variant xs -> List (Atom "Variant" :: List.map sexp_of_variant_args xs)
  | Record  xs -> List (Atom "Record" :: List.map sexp_of_record_field xs)
  | Alias   t  -> List [Atom "Alias"; Rawtype.sexp_of_t t ]

let kind_of_sexp = function
  | List (Atom "Variant" :: xs) -> Variant (List.map variant_args_of_sexp xs)
  | List (Atom "Record" :: xs) -> Record (List.map record_field_of_sexp xs)
  | List [Atom "Alias"; t] -> Alias (Rawtype.t_of_sexp t)
  | _ -> assert false

type decl = {
  name   : string;
  params : string list;
  kind   : kind 
}

let sexp_of_decl { name; params; kind } = 
  List [ Atom name;
         sexp_of_list sexp_of_string params;
         sexp_of_kind kind ]

let decl_of_sexp = function
  | List [ Atom name; params; kind ] ->
      { name; params = list_of_sexp string_of_sexp params; kind= kind_of_sexp kind }
  | sexp -> Format.eprintf "error: %a@." Sexplib.Sexp.pp_hum sexp; assert false