-
Notifications
You must be signed in to change notification settings - Fork 125
/
ocamlPrint.ml
696 lines (655 loc) · 20.5 KB
/
ocamlPrint.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
(*
Copyright © 2011 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(* CF mli *)
(* shorthand *)
module O = Ocaml
(* -- *)
module type Printer =
sig
type t
type 'a printer = t -> 'a -> unit
(* open OcamlAst *) open Ocaml
val const_expr : const_expr printer
val const_type_expr : const_type_expr printer
val type_name : type_name printer
val type_expr : type_expr printer
val pattern : pattern printer
val param_formel : param_formel printer
val param_effectif : param_effectif printer
val mlIdent : mlIdent printer
val code : code printer
val expr : expr printer
end
module type X =
sig
type t
val output_string : t -> string -> unit
end
(**
<!> The function used for print stident should return the original
name in case of a source. ident. (stident)
*)
let ident = Ident.stident
module Make ( X : X ) : Printer with type t = X.t =
struct
open X
type t = X.t
type 'a printer = t -> 'a -> unit
let paren printer oc a =
output_string oc "(";
printer oc a;
output_string oc ")"
let output_int oc i = output_string oc (string_of_int i)
let output_float oc f = output_string oc (string_of_float f)
let output_bool oc b = output_string oc (string_of_bool b)
let output_ident oc id =
match ident id with
| "*" -> output_string oc " * " (* accidental comments... *)
| idstr -> output_string oc idstr
let output_concat_map oc sep map =
let rec aux = function
| [] -> ()
| [last] -> map oc last
| t::q ->
map oc t;
output_string oc sep;
aux q
in aux
(* open OcamlAst *) open Ocaml
let const_expr oc = function
| String s ->
output_string oc "\"";
output_string oc (String.escaped s);
output_string oc "\""
| Int i ->
if i < 0 then (
output_string oc "(";
output_int oc i;
output_string oc ")"
)
else
output_int oc i
| Float f ->
if f < 0. then (
output_string oc "(";
output_float oc f;
output_string oc ")"
)
else
output_float oc f
| Bool b -> output_bool oc b
| Char c ->
output_string oc "'";
output_string oc (Char.escaped c);
output_string oc "'"
| Unit ->
output_string oc "()"
let const_type_expr oc = function
| TypeString -> output_string oc "string"
| TypeInt -> output_string oc "int"
| TypeInt64 -> output_string oc "int64"
| TypeFloat -> output_string oc "float"
| TypeBool -> output_string oc "bool"
| TypeUnit -> output_string oc "unit"
let type_name oc = output_concat_map oc "." output_string
let rec type_expr oc = function
| TypeTuple li ->
output_string oc "( ";
output_concat_map oc " * " type_expr li;
output_string oc " )";
| TypeVar s -> output_string oc s
| TypeName ([], name) -> type_name oc name
| TypeName (tl, name) ->
output_string oc "( ";
output_concat_map oc ", " type_expr tl;
output_string oc " ) ";
type_name oc name
| TypeConst c -> const_type_expr oc c
| TypeRef a ->
output_string oc "( ";
type_expr oc a;
output_string oc " ) ref";
| TypeRecord stl ->
output_string oc "{ ";
output_concat_map oc " ; " record stl;
output_string oc " }";
| TypeConstructor stl ->
output_concat_map oc " | " constructor stl
| TypeArrow (a, b) ->
(* FIXME, type within an arrow cannot be parenthesized for TypeLabels (for .mli files).
Is it safe to drop the parens altogether? *)
begin match a with
| TypeLabel _ ->
type_expr oc a;
output_string oc " -> ";
type_expr oc b
| _ ->
output_string oc "(";
type_expr oc a;
output_string oc ") -> ";
type_expr oc b
end
| TypeVerbatim v ->
output_string oc v
| TypeLabel (opt, vname, vtype) ->
if opt then
output_string oc "?";
output_string oc vname;
output_string oc ":";
type_expr oc vtype
and record oc = function
| mutable_, field, type_expr' ->
(if mutable_ then output_string oc "mutable ");
output_string oc field;
output_string oc " : ";
type_expr oc type_expr'
and constructor oc = function
| name, None ->
output_string oc name
| name, Some of_type_expr ->
output_string oc name;
output_string oc " of ";
type_expr oc of_type_expr
let rec pattern oc = function
| PatVar v -> output_ident oc v
| PatList (a, b) ->
output_string oc "(";
pattern oc a;
output_string oc ") :: (";
pattern oc b;
output_string oc ")"
| PatEmptyList -> output_string oc "[]"
| PatRecord spl ->
output_string oc "{ ";
output_concat_map oc " ; " pattern_record spl;
output_string oc " }"
| PatConstructor (s, []) | PatVariant (s, []) -> mlIdent oc s
| PatConstructor (s, pl) | PatVariant (s, pl) ->
mlIdent oc s;
output_string oc " ( ";
output_concat_map oc ", " pattern pl;
output_string oc " )";
| PatPVariant (s, []) ->
output_string oc "`";
mlIdent oc s
| PatPVariant (s, pl) ->
output_string oc "`";
mlIdent oc s;
output_string oc " ( ";
output_concat_map oc ", " pattern pl;
output_string oc " )";
| PatConst c -> const_expr oc c
| PatAny -> output_string oc "_"
| PatAnnot (p, ty) ->
output_string oc "( (";
pattern oc p;
output_string oc ") : ";
type_expr oc ty;
output_string oc " )"
| PatAs (p, s) ->
output_string oc "( ";
pattern oc p;
output_string oc " ) as ";
output_ident oc s
| PatTuple pl ->
output_string oc "( ";
output_concat_map oc ", " pattern pl;
output_string oc " )"
| PatArray pl ->
output_string oc "[| ";
output_concat_map oc " ; " pattern pl;
output_string oc " |]"
| O.PatLazy p ->
output_string oc "lazy " ;
pattern oc p
| O.PatOr pl ->
output_string oc "( ";
output_concat_map oc " | " pattern pl;
output_string oc " )"
and pattern_record oc = function
| field, pattern' ->
output_string oc field;
output_string oc " = ";
pattern oc pattern'
and param_formel oc = function
| Label (s, None, None) ->
output_string oc "~";
output_string oc s
| Label (_, None, Some _) -> assert false (* TODO *)
(* (type_expr_to_fb (acc ++ "~(" ++ s ++ " : ") ty) ++ ")" *)
| Label (_, Some _, None) -> assert false (* TODO *)
(* pattern_to_fb (acc ++ "~" ++ s ++ " : ") n *)
| Label (_, Some _, Some _) -> assert false (* TODO *)
(* (type_expr_to_fb ((pattern_to_fb (acc ++ "~" ++ s ++ ":(") n) ++ " : ") ty) ++ ")" *)
| Opt (s, None, None) ->
output_string oc "?";
output_string oc s
| Opt (_, Some _, None) -> assert false (* TODO *)
(* (type_expr_to_fb (acc ++ "?(" ++ s ++ " : ") ty) ++ ")" *)
| Opt (s, None, Some e) ->
output_string oc "?(";
output_string oc s;
output_string oc " = ";
expr oc e;
output_string oc ")"
| Opt (_, Some _, Some _) -> assert false
(* (expr_to_fb ((type_expr_to_fb (acc ++ "?((" ++ s ++ " : ") ty) ++ ") = ") e) ++ ")" *)
| Pat p -> pattern oc p
and param_effectif oc = function
| Labeled (s, None) ->
output_string oc "~";
output_string oc s
| Labeled (s, Some e) ->
output_string oc "~";
output_string oc s;
output_string oc ":(";
expr oc e;
output_string oc ")"
| Pated (id, false) ->
mlIdent oc id
| Pated (id, true) ->
output_string oc "(";
mlIdent oc id;
output_string oc ")"
and mlIdent oc = function
| [] -> assert false
| idents -> output_concat_map oc "." output_ident idents
and code oc =
let iter e =
expr oc e;
output_string oc "\n" in
List.iter iter
and expr_type oc = function
| [], name, type_expr' ->
output_string oc name;
output_string oc " = ";
type_expr oc type_expr'
| params, name, type_expr' ->
output_string oc "(";
output_concat_map oc ", " output_string params;
output_string oc ") ";
output_string oc name;
output_string oc " = ";
type_expr oc type_expr'
and expr_let oc = function
| param_formel', expr' ->
param_formel oc param_formel';
begin
let expr' =
match expr' with
| Abs (params, expr') ->
output_string oc " ";
output_concat_map oc " " param_formel params;
expr'
| _ -> expr'
in
output_string oc " = ";
expr oc expr'
end
and expr_record oc = function
| field, expr' ->
output_string oc field;
output_string oc " = ";
expr oc expr'
and expr_pattern oc = function
| pattern', guard, expr' ->
pattern oc pattern';
let () =
match guard with
| None -> ()
| Some guard ->
output_string oc " when ";
expr oc guard
in
output_string oc " -> ";
expr oc expr'
and labelparam oc v =
begin match v with
| Var (
Labeled(_, _)
) ->
expr oc v
| _ -> paren expr oc v
end
and expr oc = function
| Type [] -> assert false (* TODO: HdList.t in type definition *)
| Type defs ->
output_string oc "type ";
output_concat_map oc " and " expr_type defs
| Val (name, type_expr') ->
output_string oc "val ";
output_ident oc name;
output_string oc " : ";
type_expr oc type_expr'
| Open mlIdent' ->
output_string oc "open ";
mlIdent oc mlIdent'
(*
| Module of string * expr option * code * expr option
[Module(name, functor, contents, [Some e])] is a local module definition.
[Module(name, functor, contents, None)] is a global module definition.*)
| Module (name, None, code', None) ->
output_string oc "module ";
output_string oc name;
output_string oc " = struct\n";
code oc code';
output_string oc "end"
| Module (_, _, _, _) -> assert false
| ModuleType (name, code') ->
output_string oc "module type ";
output_string oc name;
output_string oc " = ";
code oc code' (* code' *should* be an expr (Signature ...) ! *)
| Structure code' ->
output_string oc "struct\n";
code oc code';
output_string oc "end"
| Signature (Inlined code') ->
output_string oc "sig\n";
code oc code';
output_string oc "end"
| Signature (Referenced _) -> assert false (* TODO *)
(* Used to print functor signatures (for .mli files) *)
| DeclareFunctor (name, args, (Some sign), (Structure [])) ->
(* Yeah, that's ugly, we could use an option ^^^^^^^^^^ *)
output_string oc "module ";
output_string oc name ;
output_string oc " :" ;
List.iter
(function
| s, None -> output_string oc ("\nfunctor " ^ s ^ " ->")
| s, Some si ->
output_string oc "\nfunctor ("; output_string oc s;
output_string oc " : "; expr oc si; output_string oc ") ->")
args;
output_string oc "\n" ; expr oc sign
| DeclareFunctor (name, args, sig_opt, content) ->
(* of string * (string * expr option) list * expr option * expr *)
output_string oc "module ";
output_string oc name;
List.iter
(function
| s, None -> output_string oc ("\n" ^ s)
| s, Some si ->
output_string oc "\n("; output_string oc s;
output_string oc " : "; expr oc si; output_string oc ")")
args;
(match sig_opt with
| None -> ()
| Some si -> output_string oc " :"; expr oc si) ;
output_string oc " = " ;
expr oc content
| Constructor (ml, []) ->
mlIdent oc ml
| Constructor (ml, list) ->
mlIdent oc ml;
output_string oc "(";
output_concat_map oc ", " expr list;
output_string oc ")";
| ConstructorPV (ml, []) ->
output_string oc "`";
mlIdent oc ml
| ConstructorPV (ml, list) ->
output_string oc "`";
mlIdent oc ml;
output_string oc "(";
output_concat_map oc ", " expr list;
output_string oc ")";
| Const const_expr' ->
const_expr oc const_expr'
| Var p ->
param_effectif oc p
| MakeRef expr' ->
output_string oc "ref (";
expr oc expr';
output_string oc ")"
| GetRef expr' ->
output_string oc "!(";
expr oc expr';
output_string oc ")"
| SetRef (e, f) ->
expr oc e;
output_string oc " := ";
expr oc f
| SetMutable (e, f) ->
expr oc e;
output_string oc " <- ";
expr oc f
| Lazy e ->
output_string oc "lazy (";
expr oc e;
output_string oc ")"
| Tuple pl ->
output_string oc "(";
output_concat_map oc ", " expr pl;
output_string oc ")"
| Cons (hd, (Cons (_, _) as tl)) ->
(* This is a kludge, maybe separate cons's for [1,2,3] and (1::(2::(3::[])))??? *)
let rec build acc = function
| Cons (a, l) -> build (a::acc) l
| EmptyList -> List.rev acc
| _ ->
(* Too much work to arrange for type sharing with_out_channel *)
(*output_string stderr "\nocamlPrint.ml Cons error <<<";
expr stderr e;
output_string stderr ">>>\n";*)
assert false
in
let list = build [hd] tl in
output_string oc "[ ";
output_concat_map oc " ; " expr list;
output_string oc " ]"
| Cons (hd, tl) ->
output_string oc "((";
expr oc hd;
output_string oc ")::(";
expr oc tl;
output_string oc "))"
| EmptyList ->
output_string oc "[]"
| Cond (if_, then_, else_) ->
output_string oc "if ( ";
expr oc if_;
output_string oc " ) then ( ";
expr oc then_;
output_string oc " ) else ( ";
expr oc else_;
output_string oc " )";
| App (e, f) ->
let rec print_fun e =
match e with
| App (e, f) ->
begin match e with
| Var (Pated (_, false)) -> expr oc e;
| _ -> print_fun e
end;
output_string oc " ";
labelparam oc f
| _ -> paren expr oc e;
in begin
output_string oc "(";
print_fun e;
output_string oc " ";
labelparam oc f;
output_string oc ")";
end
| Abs (params, e) ->
let e, params =
let rec uncons acc e =
match e with
| Abs(params, e) ->
uncons (params :: acc) e
| _ -> e, (List.flatten (List.rev acc))
in uncons [params] e
in
begin
output_string oc "(fun ";
output_concat_map oc " " param_formel params;
output_string oc " -> ";
expr oc e;
output_string oc ")"
end
| Let [] | Letrec [] | Letin ([], _) | Letrecin ([], _) -> assert false (* TODO: HdList.t *)
| Let binds ->
output_string oc "let ";
output_concat_map oc "\nand " expr_let binds;
| Letrec binds ->
output_string oc "let rec ";
output_concat_map oc "\nand " expr_let binds;
| Letin (binds, e) ->
output_string oc "let ";
output_concat_map oc "\nand " expr_let binds;
output_string oc " in\n";
expr oc e
| Letrecin (binds, e) ->
output_string oc "let rec ";
output_concat_map oc "\nand " expr_let binds;
output_string oc " in\n";
expr oc e
| Record (rec_opt,fields) ->
output_string oc "{ ";
(match rec_opt with Some r -> (output_string oc r; output_string oc " with ") | None -> ());
output_concat_map oc " ; " expr_record fields;
output_string oc " }"
| Dot (((Var _) as a), s) ->
expr oc a;
output_string oc ".";
output_string oc s
| Dot (a, s) ->
output_string oc "(";
expr oc a;
output_string oc ").";
output_string oc s
| Match (e, pel) ->
output_string oc "(match ";
expr oc e;
output_string oc " with\n";
output_concat_map oc "\n| " expr_pattern pel;
output_string oc ")";
| Sequence (e, f) ->
expr oc e;
output_string oc " ; ";
expr oc f
| Annot (e, ty) ->
output_string oc "( ( ";
expr oc e;
output_string oc " ) : ";
type_expr oc ty;
output_string oc " )"
| Function [] -> assert false
| Function pel ->
output_string oc "function ";
output_concat_map oc "\n| " expr_pattern pel
| Exception (s, None) ->
output_string oc "exception ";
output_string oc s;
| Exception (s, Some type_expr') ->
output_string oc "exception ";
output_string oc s;
output_string oc " of ";
type_expr oc type_expr'
| Raise (ml, None) ->
output_string oc "raise ";
mlIdent oc ml
| Raise (ml, Some e) ->
output_string oc "raise ";
output_string oc "(";
mlIdent oc ml;
output_string oc "( ";
expr oc e;
output_string oc " ))";
| Try (e, with_) ->
output_string oc "(try (\n";
expr oc e;
output_string oc "\n) with\n";
output_concat_map oc "\n| " expr_pattern with_;
output_string oc ")\n" (* We may be in a pat match... *)
| AnArray el ->
output_string oc "[| ";
output_concat_map oc " ; " (paren expr) el;
output_string oc " |]";
| Comment comment ->
output_string oc "(* ";
output_string oc comment;
output_string oc " *) "
| Comments (comment, expr') ->
output_string oc "( (* ";
output_string oc comment;
output_string oc " *) ";
expr oc expr';
output_string oc ")"
| Assert e ->
output_string oc "(assert ";
expr oc e;
output_string oc ")";
| LineAnnot (n, f, e) ->
output_string oc "\n#";
output_int oc n;
output_string oc " \"";
output_string oc f;
output_string oc "\"\n";
expr oc e
| Verbatim s -> output_string oc s
end
module X_Output : X with type t = out_channel =
struct
type t = out_channel
let output_string = Pervasives.output_string
end
module X_Buf : X with type t = Buffer.t =
struct
type t = Buffer.t
let output_string = Buffer.add_string
end
module X_Fmt : X with type t = Format.formatter =
struct
type t = Format.formatter
let output_string = Format.pp_print_string
end
module X_FBuf : X with type t = FBuffer.t ref =
struct
type t = FBuffer.t ref
let output_string t s =
let t' = FBuffer.add !t s in
t := t'
end
module Output = Make ( X_Output )
module Buf = Make ( X_Buf )
module Fmt = Make ( X_Fmt )
module FBuf = Make ( X_FBuf )
module Deprecated =
struct
type 'a printer = FBuffer.t -> 'a -> FBuffer.t
let const_expr fb c =
let r = ref fb in
FBuf.const_expr r c;
!r
let type_expr fb t =
let r = ref fb in
FBuf.type_expr r t;
!r
end
(* {6 Pretty printer} *)
type 'a pprinter = 'a LangPrint.pprinter
let pp_list = Base.Format.pp_list
(* beware for error message, oneline should be set to true *)
(*
<!> Rather than trying to hack LangPrint with optionnal arguments,
OcamlPrint defines his own pp_parameters.
*)
let pp_parameters pp name fmt params =
match params with
| [] -> Format.pp_print_string fmt name
| [p] -> Format.fprintf fmt "%a %s" pp p name
| _ -> Format.fprintf fmt "(%a) %s" (pp_list ", " pp) params name