-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretty_test.ml
60 lines (52 loc) · 1.31 KB
/
pretty_test.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
module P = Pretty
let ( <> ) a b = P.append a b
let bracket l x r =
P.group (
P.text l
<> P.indent 2 (P.line " " <> x)
<> P.line " "
<> P.text r
)
type tree =
| Node of string * tree list
let rec show_tree (Node (s, ts)) =
let rec show_trees xs =
match xs with
| [] -> P.empty
| [t] -> show_tree t
| t :: ts -> show_tree t <> P.text "," <> P.line " " <> show_trees ts in
let show_bracket xs =
match xs with
| [] -> P.empty
| ts -> P.text "[" <> P.indent 1 (show_trees ts) <> P.text "]" in
P.group (P.text s <> P.indent (String.length s) (show_bracket ts))
let rec show_tree' (Node (s, ts)) =
let rec show_trees ts =
match ts with
| [] -> P.empty
| [t] -> show_tree' t
| t :: ts -> show_tree' t <> P.text "," <> P.line " " <> show_trees ts in
let show_bracket x =
match x with
| [] -> P.empty
| ts -> bracket "[" (show_trees ts) "]" in
P.text s <> show_bracket ts
let tree =
let node x y = Node (x, y) in
node "aaa" [
node "bbbbb" [
node "ccc" [];
node "dd" [];
];
node "eee" [];
node "ffff" [
node "gg" [];
node "hhh" [];
node "ii" [];
];
]
let _ =
P.print ~width:20 (show_tree tree);
print_string "\n";
P.print ~width:20 (show_tree' tree);
print_string "\n";