ocaml_printer.ml 4.12 KB
Newer Older
Mário Pereira's avatar
Mário Pereira committed
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
(********************************************************************)
(*                                                                  *)
(*  The Why3 Verification Platform   /   The Why3 Development Team  *)
(*  Copyright 2010-2016   --   INRIA - CNRS - Paris-Sud University  *)
(*                                                                  *)
(*  This software is distributed under the terms of the GNU Lesser  *)
(*  General Public License version 2.1, with the special exception  *)
(*  on linking described in file LICENSE.                           *)
(*                                                                  *)
(********************************************************************)

(** Printer for extracted OCaml code *)

open Compile
open Format
open Pmodule
open Theory
open Ident
open Printer
open Pp

module Print = struct

  open ML

  let ocaml_keywords =
    ["and"; "as"; "assert"; "asr"; "begin";
     "class"; "constraint"; "do"; "done"; "downto"; "else"; "end";
     "exception"; "external"; "false"; "for"; "fun"; "function";
     "functor"; "if"; "in"; "include"; "inherit"; "initializer";
     "land"; "lazy"; "let"; "lor"; "lsl"; "lsr"; "lxor"; "match";
     "method"; "mod"; "module"; "mutable"; "new"; "object"; "of";
     "open"; "or"; "private"; "rec"; "sig"; "struct"; "then"; "to";
     "true"; "try"; "type"; "val"; "virtual"; "when"; "while"; "with";
     "raise";]

Mário Pereira's avatar
Mário Pereira committed
37
  let iprinter, aprinter =
Mário Pereira's avatar
Mário Pereira committed
38
    let isanitize = sanitizer char_to_alpha char_to_alnumus in
Mário Pereira's avatar
Mário Pereira committed
39
40
41
    let lsanitize = sanitizer char_to_lalpha char_to_alnumus in
    create_ident_printer ocaml_keywords ~sanitizer:isanitize,
    create_ident_printer ocaml_keywords ~sanitizer:lsanitize
Mário Pereira's avatar
Mário Pereira committed
42
43
44
45
46

  let print_ident fmt id =
    let s = id_unique iprinter id in
    fprintf fmt "%s" s

Mário Pereira's avatar
Mário Pereira committed
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
  (* let print_lident = print_qident ~sanitizer:Strings.uncapitalize *)
  (* let print_uident = print_qident ~sanitizer:Strings.capitalize *)

  let print_tv fmt tv =
    fprintf fmt "'%s" (id_unique aprinter tv)

  let star fmt () = fprintf fmt " *@ "

  let rec print_ty fmt = function
    | Tvar id ->
       print_tv fmt id
    | Ttuple [] ->
       fprintf fmt "unit"
    | Ttuple tl ->
       fprintf fmt "%a" (print_list star print_ty) tl
    | Tapp (ts, []) ->
       print_ident fmt ts
    | Tapp (ts, [ty]) ->
       fprintf fmt "%a@ %a" print_ty ty print_ident ts
    | Tapp (ts, tl) ->
       fprintf fmt "(%a)@ %a"
               (print_list comma print_ty) tl
               print_ident ts

  let print_vsty fmt (v, ty) =
    fprintf fmt "%a:@ %a" print_ident v print_ty ty

  let print_vs_arg fmt vs =
    fprintf fmt "@[(%a)@]" print_vsty vs

  let rec print_ty fmt = function
    | Tvar id ->
       print_tv fmt id
    | Ttuple [] ->
       fprintf fmt "unit"
    | Ttuple tl ->
       fprintf fmt "%a" (print_list star print_ty) tl
    | Tapp (ts, []) ->
       print_ident fmt ts
    | _ -> assert false (* TODO *)

Mário Pereira's avatar
Mário Pereira committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  let print_path =
    print_list dot pp_print_string (* point-free *)

  let print_path_id fmt = function
    | [], id -> print_ident fmt id
    | p, id  -> fprintf fmt "%a.%a" print_path p print_ident id

  let print_theory_name fmt th =
    print_path_id fmt (th.th_path, th.th_name)

  let print_module_name fmt m =
    print_theory_name fmt m.mod_theory

  let rec print_enode fmt = function
    | Eident id ->
       print_ident fmt id
Mário Pereira's avatar
Mário Pereira committed
104
105
106
107
108
    | Elet (id, e1, e2) ->
       fprintf fmt "@[<hov 2>let @[%a@] =@ @[%a@]@] in@ %a"
               print_ident id
               print_expr e1
               print_expr e2
Mário Pereira's avatar
Mário Pereira committed
109
110
    | _ -> assert false (* TODO *)

Mário Pereira's avatar
Mário Pereira committed
111
  and print_expr fmt e =
Mário Pereira's avatar
Mário Pereira committed
112
113
114
    print_enode fmt e.e_node

  let print_decl fmt = function
Mário Pereira's avatar
Mário Pereira committed
115
116
    | Dlet (isrec, [id, vl, e]) ->
       fprintf fmt "@[<hov 2>%s %a@ %a =@ %a@]"
Mário Pereira's avatar
Mário Pereira committed
117
118
               (if isrec then "let rec" else "let")
               print_ident id
Mário Pereira's avatar
Mário Pereira committed
119
               (print_list space print_vs_arg) vl
Mário Pereira's avatar
Mário Pereira committed
120
121
122
123
124
125
126
127
128
129
130
131
132
               print_expr e;
       fprintf fmt "@\n@\n"
    | _ -> assert false (* TODO *)

end

let extract_module fmt m =
  fprintf fmt
    "(* This file has been generated from Why3 module %a *)@\n@\n"
    Print.print_module_name m;
  let mdecls = Translate.module_ m in
  print_list nothing Print.print_decl fmt mdecls;
  fprintf fmt "@."