Skip to content

Commit

Permalink
Printing of unit and tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
clarus committed Feb 27, 2014
1 parent d82bd14 commit be5115c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,14 @@ Lists:
* `separate : t -> t list -> t` Concatenate the list of documents with no space but adding a separator in between. `separate sep [d1; ...; dn]` is like `d1 ^-^ sep ^-^ d2 ^-^ sep ... sep ^-^ dn`.

OCaml values:
* `OCaml.unit : unit -> t` Pretty-print the unit value.
* `OCaml.bool : bool -> t` Pretty-print a `bool`.
* `OCaml.int : int -> t` Pretty-print an `int`.
* `OCaml.float : float -> t` Pretty-print a `float`.
* `OCaml.string : string -> t` Pretty-print a `string`.
* `OCaml.option : ('a -> t) -> 'a option -> t` Pretty-print an `option`.
* `OCaml.list : ('a -> t) -> 'a list -> t` Pretty-print a `list`.
* `OCaml.tuple : t list -> t` Pretty-print a tuple of values.

A pretty-printer for the pretty-printer itself:
* `Debug.pp_document : t -> t` Pretty-print a document's structure.
Expand Down
6 changes: 6 additions & 0 deletions smartPrint.ml
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ let lines (s : string) : t =
(split s (fun c -> c = '\n') 0 0)

module OCaml = struct
let unit (_ : unit) : t =
!^ "()"

let bool (b : bool) : t =
!^ (string_of_bool b)

Expand All @@ -374,6 +377,9 @@ module OCaml = struct

let list (d : 'a -> t) (l : 'a list) : t =
brakets @@ nest_all (space ^^ separate (!^ ";" ^^ space) (List.map d l) ^^ space)

let tuple (ds : t list) : t =
parens @@ nest @@ separate (!^ "," ^^ space) ds
end

module Debug = struct
Expand Down
6 changes: 6 additions & 0 deletions smartPrint.mli
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ val separate : t -> t list -> t
(** {1 OCaml values} *)
(** Pretty-printing of OCaml values. *)
module OCaml : sig
(** Pretty-print the unit value. *)
val unit : unit -> t

(** Pretty-print a [bool]. *)
val bool : bool -> t

Expand All @@ -111,6 +114,9 @@ module OCaml : sig

(** Pretty-print a [list]. *)
val list : ('a -> t) -> 'a list -> t

(** Pretty-print a tuple of values. *)
val tuple : t list -> t
end

(** {1 Debugging} *)
Expand Down
3 changes: 2 additions & 1 deletion test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ let main () =
print_document @@ nest @@ lines "adipisicing elit,\n\nsed do eiusmod tempo incididunt ut labore et dolore";
print_document @@ indent @@ nest @@ lines "adipisicing elit,\n\nsed do eiusmod tempo incididunt ut labore et dolore";
print_document @@ indent @@ pp false @@ Let ("x", Var "x", Let ("x", Var "x", Var "y"));
print_document @@ pp false @@ App (Var "f", Let ("x", Var "x", Var "y"))
print_document @@ pp false @@ App (Var "f", Let ("x", Var "x", Var "y"));
print_document @@ OCaml.unit () ^^ OCaml.tuple [!^ "x"; OCaml.int 0]

;;main ()
4 changes: 4 additions & 0 deletions test.out
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,7 @@ f
let x = x in
y
*************************
GroupOne (false, "()", Space, "(", GroupOne (true, "x", ",", Space, Space, "0"), ")")
*************************
() (x, 0)
*************************

0 comments on commit be5115c

Please sign in to comment.