Skip to content

Commit

Permalink
Merge pull request #771 from yallop/fix-ctypes-top
Browse files Browse the repository at this point in the history
Reduce ctypes.top implementation to a single module.
  • Loading branch information
yallop authored May 18, 2024
2 parents 2e2788b + a7bd08a commit 7a30f08
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 155 deletions.
73 changes: 0 additions & 73 deletions src/ctypes-top/ctypes_printers.ml

This file was deleted.

41 changes: 0 additions & 41 deletions src/ctypes-top/ctypes_printers.mli

This file was deleted.

86 changes: 45 additions & 41 deletions src/ctypes-top/install_ctypes_printers.ml
Original file line number Diff line number Diff line change
@@ -1,51 +1,55 @@
(* Adapted from Anil Madhavapeddy's ocaml-uri package. *)

let printers = [ "Ctypes_printers.format_typ";
"Ctypes_printers.format_fn";
"Ctypes_printers.format_sint";
"Ctypes_printers.format_long";
"Ctypes_printers.format_llong";
"Ctypes_printers.format_uchar";
"Ctypes_printers.format_uint8";
"Ctypes_printers.format_uint16";
"Ctypes_printers.format_uint32";
"Ctypes_printers.format_uint64";
"Ctypes_printers.format_size_t";
"Ctypes_printers.format_ushort";
"Ctypes_printers.format_uint";
"Ctypes_printers.format_ulong";
"Ctypes_printers.format_ullong";
"Ctypes_printers.format_pointer";
"Ctypes_printers.format_struct";
"Ctypes_printers.format_union";
"Ctypes_printers.format_array";
"Ctypes_printers.format_ocaml";
"Ctypes_printers.format_clock_t";
"Ctypes_printers.format_dev_t";
"Ctypes_printers.format_ino_t";
"Ctypes_printers.format_mode_t";
"Ctypes_printers.format_nlink_t";
"Ctypes_printers.format_off_t";
"Ctypes_printers.format_pid_t";
"Ctypes_printers.format_size_t";
"Ctypes_printers.format_ssize_t";
"Ctypes_printers.format_time_t";
"Ctypes_printers.format_useconds_t";
"Ctypes_printers.format_ldouble";
"Ctypes_printers.format_complexld";]

let printers = [ "fun fmt -> Ctypes.format_typ fmt";
"fun fmt -> Ctypes.format_fn fmt";
"Signed.SInt.pp";
"Signed.Long.pp";
"Signed.LLong.pp";
"Unsigned.UChar.pp";
"Unsigned.UInt8.pp";
"Unsigned.UInt16.pp";
"Unsigned.UInt32.pp";
"Unsigned.UInt64.pp";
"Unsigned.Size_t.pp";
"Unsigned.UShort.pp";
"Unsigned.UInt.pp";
"Unsigned.ULong.pp";
"Unsigned.ULLong.pp";
"fun fmt v -> let open Ctypes in
let typ = ptr (reference_type v) in
Format.fprintf fmt \"(%a) %a\" (fun fmt -> format_typ fmt) typ (format typ) v";
"fun fmt v -> Ctypes.(format (reference_type (addr v)) fmt v)";
"fun fmt v -> Ctypes.(format (reference_type (addr v)) fmt v)";
"fun fmt v -> Ctypes.(format CArray.(array (length v) (reference_type (start v))) fmt v)";
"fun fmt (Ctypes_static.OCamlRef (_, _, ty) as v) -> Ctypes.format (Ctypes_static.OCaml ty) fmt v";
"fun fmt v -> Ctypes.format PosixTypes.clock_t fmt v";
"fun fmt v -> Ctypes.format PosixTypes.dev_t fmt v";
"fun fmt v -> Ctypes.format PosixTypes.ino_t fmt v";
"fun fmt v -> Ctypes.format PosixTypes.mode_t fmt v";
"fun fmt v -> Ctypes.format PosixTypes.nlink_t fmt v";
"fun fmt v -> Ctypes.format PosixTypes.off_t fmt v";
"fun fmt v -> Ctypes.format PosixTypes.pid_t fmt v";
"PosixTypes.Ssize.pp";
"PosixTypes.Time.pp";
"Ctypes.format PosixTypes.useconds_t";
"(fun fmt v -> Format.fprintf fmt \"<ldouble %s>\"
(LDouble.to_string v))";
"(fun fmt v -> Format.fprintf fmt \"<complexld %s + %si>\"
(LDouble.to_string (ComplexL.re v)) (LDouble.to_string (ComplexL.im v)))";

]

let eval_string
?(print_outcome = false) ?(err_formatter = Format.err_formatter) str =
let lexbuf = Lexing.from_string str in
let phrase = !Toploop.parse_toplevel_phrase lexbuf in
Toploop.execute_phrase print_outcome err_formatter phrase

let rec install_printers = function
| [] -> true
| printer :: printers ->
let cmd = Printf.sprintf "#install_printer %s;;" printer in
eval_string cmd && install_printers printers
let install_printer printer =
begin
ignore (eval_string (Printf.sprintf "let _printer = (%s);;" printer));
ignore (eval_string "#install_printer _printer;;")
end

let () =
if not (install_printers printers) then
Format.eprintf "Problem installing ctypes-printers@."
let () = List.iter install_printer printers

0 comments on commit 7a30f08

Please sign in to comment.