Skip to content

Commit

Permalink
chore: more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sorawee committed Feb 10, 2024
1 parent 77d745f commit 272097e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 62 deletions.
1 change: 1 addition & 0 deletions lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(public_name pretty_expressive)
(name pretty_expressive)
(modules (:standard \ prelude))
; (libraries batteries)
(modules_without_implementation signature))

(mdx (files :standard *.mli)
Expand Down
2 changes: 2 additions & 0 deletions lib/printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ module MakeCompat (C : Signature.CostFactory): (Signature.PrinterCompatT with ty
let (<>) = (^^)
end

(* $MDX part-begin=default_cost_factory *)
let default_cost_factory ~page_width ?computation_width () =
(module struct
type t = int * int
Expand Down Expand Up @@ -369,3 +370,4 @@ let default_cost_factory ~page_width ?computation_width () =
let debug (o, h) = Printf.sprintf "(%d %d)" o h

end: Signature.CostFactory with type t = int * int)
(* $MDX part-end *)
60 changes: 30 additions & 30 deletions lib/printer.mli
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,33 @@ val default_cost_factory : page_width:int -> ?computation_width:int -> unit ->
Internally, [default_cost_factory] is defined as:
{@ocaml skip[
let default_cost_factory ~page_width ?computation_width () =
(module struct
type t = int * int
let limit = match computation_width with
| None -> (float_of_int page_width) *. 1.2 |> int_of_float
| Some computation_width -> computation_width
let text pos len =
let stop = pos + len in
if stop > page_width then
let maxwc = max page_width pos in
let a = maxwc - page_width in
let b = stop - maxwc in
(b * (2*a + b), 0)
else
(0, 0)
let newline _ = (0, 1)
let combine (o1, h1) (o2, h2) =
(o1 + o2, h1 + h2)
let le (o1, h1) (o2, h2) =
if o1 = o2 then h1 <= h2 else o1 < o2
let debug (o, h) = Printf.sprintf "(%d %d)" o h
end: Signature.CostFactory with type t = int * int)
]} *)
{@ocaml file=printer.ml,part=default_cost_factory[
let default_cost_factory ~page_width ?computation_width () =
(module struct
type t = int * int
let limit = match computation_width with
| None -> (float_of_int page_width) *. 1.2 |> int_of_float
| Some computation_width -> computation_width
let text pos len =
let stop = pos + len in
if stop > page_width then
let maxwc = max page_width pos in
let a = maxwc - page_width in
let b = stop - maxwc in
(b * (2*a + b), 0)
else
(0, 0)
let newline _ = (0, 1)
let combine (o1, h1) (o2, h2) =
(o1 + o2, h1 + h2)
let le (o1, h1) (o2, h2) =
if o1 = o2 then h1 <= h2 else o1 < o2
let debug (o, h) = Printf.sprintf "(%d %d)" o h
end: Signature.CostFactory with type t = int * int)
]} *)
64 changes: 32 additions & 32 deletions test/pretty_expressive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,42 +30,42 @@ let print_doc_group (w : int) =
let test_choice_doc_80 () =
Alcotest.(check string) "same string"
(String.concat "\n"
[ "while (true) {"
; " f();"
; " if (done()) exit();"
; "}"
[ "while (true) {" ;
" f();" ;
" if (done()) exit();" ;
"}"
])
(print_doc_choice 80)

let test_choice_doc_20 () =
Alcotest.(check string) "same string"
(String.concat "\n"
[ "while (true) {"
; " f();"
; " if (done())"
; " exit();"
; "}"
[ "while (true) {" ;
" f();" ;
" if (done())" ;
" exit();" ;
"}"
])
(print_doc_choice 20)

let test_group_doc_80 () =
Alcotest.(check string) "same string"
(String.concat "\n"
[ "while (true) {"
; " f();"
; " if (done()) exit();"
; "}"
[ "while (true) {" ;
" f();" ;
" if (done()) exit();" ;
"}"
])
(print_doc_group 80)

let test_group_doc_20 () =
Alcotest.(check string) "same string"
(String.concat "\n"
[ "while (true) {"
; " f();"
; " if (done())"
; " exit();"
; "}"
[ "while (true) {" ;
" f();" ;
" if (done())" ;
" exit();" ;
"}"
])
(print_doc_group 20)

Expand Down Expand Up @@ -101,19 +101,19 @@ let example_sexp = List [Atom "a"; Atom "b"; Atom "c"; Atom "d"]
let test_sexp_4 () =
Alcotest.(check string) "same string"
(String.concat "\n"
[ "(a"
; " b"
; " c"
; " d)"
[ "(a" ;
" b" ;
" c" ;
" d)"
])
(print_sexp example_sexp 4)

let test_sexp_6 () =
Alcotest.(check string) "same string"
(String.concat "\n"
[ "(a b"
; " c"
; " d)"
[ "(a b" ;
" c" ;
" d)"
])
(print_sexp example_sexp 6)

Expand All @@ -124,13 +124,13 @@ let test_sexp_10 () =
(print_sexp example_sexp 10)

let suite =
[ "choice; w = 80", `Quick, test_choice_doc_80
; "choice; w = 20", `Quick, test_choice_doc_20
; "group; w = 80", `Quick, test_group_doc_80
; "group; w = 20", `Quick, test_group_doc_20
; "sexp; w = 4", `Quick, test_sexp_4
; "sexp; w = 6", `Quick, test_sexp_6
; "sexp; w = 10", `Quick, test_sexp_10
[ "choice; w = 80", `Quick, test_choice_doc_80 ;
"choice; w = 20", `Quick, test_choice_doc_20 ;
"group; w = 80", `Quick, test_group_doc_80 ;
"group; w = 20", `Quick, test_group_doc_20 ;
"sexp; w = 4", `Quick, test_sexp_4 ;
"sexp; w = 6", `Quick, test_sexp_6 ;
"sexp; w = 10", `Quick, test_sexp_10
]

let () =
Expand Down

0 comments on commit 272097e

Please sign in to comment.