package mopsa

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file pp.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
(**

  Copyright (c) 2017-2019 Aymeric Fromherz and The MOPSA Project

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
 *)

(**
   Pp - Pretty printer of the AST.
*)

open Cst
open Ast
open Format

let str = pp_print_string

let rec print_var fmt v =
  fprintf fmt "%s@@%d" v.name v.uid

and print_program fmt p = print_stmt fmt p.prog_body

and print_stmt fmt (stmt: Ast.stmt) =
  match stmt.skind with
  | S_assign (v, e) -> fprintf fmt "%a = %a" print_exp v print_exp e
  | S_type_annot (v, e) -> fprintf fmt "%a: %a" print_exp v print_exp e
  | S_expression e -> print_exp fmt e
  | S_while (test, body, orelse) ->
    fprintf fmt
      "while (%a):@\n@[<h 2>  %a@]%a"
      print_exp test
      print_stmt body
      print_orelse orelse
  | S_break -> str fmt "break"
  | S_continue -> str fmt "continue"
  | S_block sl ->
    pp_print_list ~pp_sep:(fun fmt () -> fprintf fmt "@\n") print_stmt fmt sl
  | S_aug_assign (v, op, e) -> fprintf fmt "%a %a= %a" print_exp v print_binop op print_exp e
  | S_if (test, body, orelse) ->
    fprintf fmt
      "if %a:@\n@[<h 2>  %a@]%a"
      print_exp test
      print_stmt body
      print_orelse orelse

  | S_function(func) ->
    fprintf fmt "def %a(%a):@\n@[<h 2>  %a@]"
      print_var func.func_var
      (pp_print_list ~pp_sep:(fun fmt () -> fprintf fmt ",@ ") print_var) func.func_parameters
      print_stmt func.func_body
  | S_class(cls) ->
    fprintf fmt "class %a(%a):@\n@[<h 2>  %a@]"
      print_var cls.cls_var
      (pp_print_list ~pp_sep:(fun fmt () -> fprintf fmt ",@ ") print_exp) cls.cls_bases
      print_stmt cls.cls_body
  | S_for (target, iter, body, orelse) ->
    fprintf fmt
      "for %a in %a:@\n@[<h 2>  %a@]%a"
      print_exp target
      print_exp iter
      print_stmt body
      print_orelse orelse
  | S_return e -> fprintf fmt "return %a" print_exp e
  | S_raise(e, c) ->
    fprintf fmt "raise %a%a"
      (fun fmt e -> match e with None -> () | Some e -> print_exp fmt e) e
      (fun fmt c -> match e with None -> () | Some c -> fprintf fmt " from %a" print_exp c) c
  | S_try (body, excepts, orelse, finally) ->
    fprintf fmt "try:@\n@[<h 2>  %a@]@\n%a%a%a"
      print_stmt body
      (pp_print_list ~pp_sep:(fun fmt () -> fprintf fmt "@\n") print_except) excepts
      print_orelse orelse
      print_finally finally

  | S_import(mdl, asname, vroot) ->
    fprintf fmt "import %s<%a>%a"
      mdl
      print_var vroot
      (fun fmt -> function None -> () | Some name -> fprintf fmt " as %a" print_var name) asname

  | S_import_from(mdl, name, _, asname) ->
    fprintf fmt "from %s import %s as %a"
      mdl
      name
      print_var asname

  | S_delete e -> fprintf fmt "del %a" print_exp e

  | S_assert(e, None) -> fprintf fmt "assert %a" print_exp e
  | S_assert(e, Some msg) -> fprintf fmt "assert %a, %a" print_exp e print_exp msg

  | S_with(ctx, asname, body) ->
    fprintf fmt "with %a%a:@\n@[<h 2>  %a@]"
      print_exp ctx
      (fun fmt -> function None -> () | Some name -> fprintf fmt " as %a" print_exp name) asname
      print_stmt body

  | S_pass -> str fmt "pass"

and print_except fmt (exc: except) =
  let e, v, body = exc in
  fprintf fmt "except %a%a:@\n@[<h 2>  %a@]"
    (fun fmt e -> match e with
       | None -> ()
       | Some e -> print_exp fmt e
    ) e
    (fun fmt v -> match v with
       | None -> ()
       | Some v -> fprintf fmt " as %a" print_var v
    ) v
    print_stmt body

and print_number fmt (n: Cst.number) =
  match n with
  | Cst.Int i -> Z.pp_print fmt i
  | Cst.Float f -> pp_print_float fmt f
  | Cst.Imag s -> pp_print_string fmt s

and print_exp fmt exp =
  match exp.ekind with
  | E_ellipsis -> str fmt "..."
  | E_true -> str fmt "True"
  | E_false -> str fmt "False"
  | E_none -> str fmt "Py_None"
  | E_notimplemented -> str fmt "NotImplemented"
  | E_num(n) -> print_number fmt n
  | E_str(s)-> fprintf fmt "\"%s\"" s
  | E_bytes(s)-> fprintf fmt "b\"%s\"" s
  | E_attr(obj, attr) -> fprintf fmt "%a.%s" print_exp obj attr
  | E_id(v) -> print_var fmt v
  | E_call(f, args, keywords) ->
    fprintf fmt "%a(%a%a%a)"
      print_exp f
      (pp_print_list ~pp_sep:(fun fmt () -> fprintf fmt ", ") print_exp) args
      (fun fmt -> function [] -> () | _ -> pp_print_string fmt ", ") keywords
      (pp_print_list ~pp_sep:(fun fmt () -> fprintf fmt ", ")
         (fun fmt -> function
            | (None, kwd) -> fprintf fmt "**%a" print_exp kwd
            | (Some k, v) -> fprintf fmt "%s = %a" k print_exp v
         )
      ) keywords
  | E_generator_comp(e, comps) ->
    fprintf fmt "(%a %a)"
      print_exp e
      (pp_print_list ~pp_sep:(fun fmt () -> fprintf fmt "@ ") print_comp) comps
  | E_list_comp(e, comps) ->
    fprintf fmt "[%a %a]"
      print_exp e
      (pp_print_list ~pp_sep:(fun fmt () -> fprintf fmt "@ ") print_comp) comps
  | E_set_comp(e, comps) ->
    fprintf fmt "{%a %a}"
      print_exp e
      (pp_print_list ~pp_sep:(fun fmt () -> fprintf fmt "@ ") print_comp) comps
  | E_dict_comp(k, v, comps) ->
    fprintf fmt "{%a: %a %a}"
      print_exp k
      print_exp v
      (pp_print_list ~pp_sep:(fun fmt () -> fprintf fmt "@ ") print_comp) comps
  | E_list(el) ->
    fprintf fmt "[%a]"
      (pp_print_list ~pp_sep:(fun fmt () -> fprintf fmt ",@ ") print_exp) el
  | E_index_subscript(obj, index) ->
    fprintf fmt "%a[%a]"
      print_exp obj
      print_exp index
  | E_slice_subscript(obj, lower, upper, step) ->
    fprintf fmt "%a[%a:%a:%a]"
      print_exp obj
      print_exp lower
      print_exp upper
      print_exp step
  | E_tuple(el) ->
    fprintf fmt "(%a)"
      (pp_print_list ~pp_sep:(fun fmt () -> fprintf fmt ",@ ") print_exp) el
  | E_set(el) ->
    fprintf fmt "set(%a)"
      (pp_print_list ~pp_sep:(fun fmt () -> fprintf fmt ",@ ") print_exp) el
  | E_dict(keys, values) ->
    fprintf fmt "{%a}"
      (pp_print_list ~pp_sep:(fun fmt () -> fprintf fmt ",@ ")
         (fun fmt (k, v) -> fprintf fmt "%a: %a" print_exp k print_exp v)
      ) (List.combine keys values)
  | E_if (test, body, orelse) ->
    fprintf fmt
      "%a if %a else %a"
      print_exp body
      print_exp test
      print_exp orelse
  | E_yield(e) ->
    fprintf fmt "yield %a" print_exp e
  | E_yield_from(e) ->
    fprintf fmt "yield from %a" print_exp e
  | E_binop(e1, op, e2) ->
    fprintf fmt "( %a ) %a ( %a )"
      print_exp e1
      print_binop op
      print_exp e2
  | E_unop(op, e) ->
    fprintf fmt "%a ( %a )"
      print_unop op
      print_exp e
  | E_lambda l ->
    fprintf fmt "lambda %a: %a"
      (pp_print_list ~pp_sep:(fun fmt () -> fprintf fmt ", ") print_var) l.lambda_parameters
      print_exp l.lambda_body
  | E_multi_compare(left, ops, rights) ->
    let l = List.combine ops rights in
    fprintf fmt "%a %a"
      print_exp left
      (pp_print_list ~pp_sep:(fun fmt () -> fprintf fmt " ")
         (fun fmt (op, right) -> fprintf fmt "%a %a" print_binop op print_exp right)
      ) l

and print_comp fmt comp =
  let (target, iter, conds) = comp in
  fprintf fmt "for %a in %a %a"
    print_exp target
    print_exp iter
    (pp_print_list ~pp_sep:(fun fmt () -> fprintf fmt "@ ") (fun fmt e -> fprintf fmt "if %a" print_exp e)) conds

and print_binop fmt = function
  | O_arithmetic op -> print_arithop fmt op
  | O_comparison op -> print_cmpop fmt op
  | O_bool op -> print_boolop fmt op

and print_arithop fmt = function
  | Add -> str fmt "+"
  | Sub -> str fmt "-"
  | Mult -> str fmt "*"
  | MatMult -> str fmt "@"
  | Div -> str fmt "/"
  | Mod -> str fmt "%"
  | Pow -> str fmt "**"
  | LShift -> str fmt "<<"
  | RShift -> str fmt ">>"
  | BitOr -> str fmt "|"
  | BitXor -> str fmt "^"
  | BitAnd -> str fmt "&"
  | FloorDiv -> str fmt "//"

and print_boolop fmt = function
  | And -> str fmt "and"
  | Or -> str fmt "or"

and print_unop fmt = function
   | Invert -> str fmt "~"
   | Not -> str fmt "not"
   | UAdd -> str fmt "+"
   | USub -> str fmt "-"

and print_cmpop fmt = function
  | Eq -> str fmt "=="
  | NotEq -> str fmt "!="
  | Lt -> str fmt "<"
  | LtE -> str fmt "<="
  | Gt -> str fmt ">"
  | GtE -> str fmt ">="
  | Is -> str fmt "is"
  | IsNot -> str fmt "is not"
  | In -> str fmt "in"
  | NotIn -> str fmt "not in"

and print_orelse fmt = function
    | None -> ()
    | Some s ->
      fprintf fmt "@\nelse:@\n@[<h 2>  %a@]"
        print_stmt s

and print_finally fmt = function
    | None -> ()
    | Some s ->
      fprintf fmt "@\nfinally:@\n@[<h 2>  %a@]"
        print_stmt s
OCaml

Innovation. Community. Security.