Revision 90cc3b8e
Added by Lélio Brun over 3 years ago
src/utils/utils.ml | ||
---|---|---|
67 | 67 |
|
68 | 68 |
(* TODO: Lélio: why n+1? cf former def below *) |
69 | 69 |
(* if n < 0 then [] else x :: duplicate x (n - 1) *) |
70 |
let duplicate x n = List.init (n+1) (fun i -> x)
|
|
70 |
let duplicate x n = List.init (n+1) (fun _ -> x)
|
|
71 | 71 |
|
72 | 72 |
let enumerate n = List.init n (fun i -> i) |
73 | 73 |
|
... | ... | |
251 | 251 |
|
252 | 252 |
(* Generic pretty printing *) |
253 | 253 |
|
254 |
|
|
254 | 255 |
let pp_final_char_if_non_empty c l = |
255 | 256 |
(fun fmt -> match l with [] -> () | _ -> Format.fprintf fmt "%(%)" c) |
256 | 257 |
|
257 | 258 |
let pp_newline_if_non_empty l = |
258 | 259 |
(fun fmt -> match l with [] -> () | _ -> Format.fprintf fmt "@,") |
259 | 260 |
|
261 |
module Format = struct |
|
262 |
include Format |
|
263 |
open Format |
|
264 |
|
|
265 |
let with_out_file file f = |
|
266 |
let oc = open_out file in |
|
267 |
let fmt = formatter_of_out_channel oc in |
|
268 |
f fmt; |
|
269 |
close_out oc |
|
270 |
|
|
271 |
let pp_print_nothing _fmt _ = () |
|
272 |
|
|
273 |
let pp_print_cutcut fmt () = fprintf fmt "@,@," |
|
274 |
|
|
275 |
let pp_print_endcut s fmt () = fprintf fmt "%s@," s |
|
276 |
|
|
277 |
let pp_print_opar fmt () = pp_print_string fmt "(" |
|
278 |
let pp_print_cpar fmt () = pp_print_string fmt ")" |
|
279 |
let pp_print_obrace fmt () = pp_print_string fmt "{" |
|
280 |
let pp_print_cbrace fmt () = pp_print_string fmt "}" |
|
281 |
|
|
282 |
let pp_print_comma fmt () = fprintf fmt ",@ " |
|
283 |
let pp_print_semicolon fmt () = fprintf fmt ";@ " |
|
284 |
let pp_print_comma' fmt () = fprintf fmt "," |
|
285 |
let pp_print_semicolon' fmt () = fprintf fmt ";" |
|
286 |
|
|
287 |
let pp_open_vbox0 fmt () = pp_open_vbox fmt 0 |
|
288 |
|
|
289 |
let pp_print_list |
|
290 |
?(pp_prologue=pp_print_nothing) ?(pp_epilogue=pp_print_nothing) |
|
291 |
?(pp_op=pp_print_nothing) ?(pp_cl=pp_print_nothing) |
|
292 |
?(pp_open_box=fun fmt () -> pp_open_box fmt 0) |
|
293 |
?(pp_eol=pp_print_nothing) ?pp_sep pp_v fmt l = |
|
294 |
fprintf fmt "%a%a%a%a%a@]%a%a" |
|
295 |
(fun fmt l -> if l <> [] then pp_prologue fmt ()) l |
|
296 |
pp_op () |
|
297 |
pp_open_box () |
|
298 |
(pp_print_list ?pp_sep pp_v) l |
|
299 |
(fun fmt l -> if l <> [] then pp_eol fmt ()) l |
|
300 |
pp_cl () |
|
301 |
(fun fmt l -> if l <> [] then pp_epilogue fmt ()) l |
|
302 |
|
|
303 |
let pp_print_list_i |
|
304 |
?pp_prologue ?pp_epilogue ?pp_op ?pp_cl ?pp_open_box ?pp_eol ?pp_sep |
|
305 |
pp_v = |
|
306 |
let i = ref 0 in |
|
307 |
pp_print_list |
|
308 |
?pp_prologue ?pp_epilogue ?pp_op ?pp_cl ?pp_open_box ?pp_eol ?pp_sep |
|
309 |
(fun fmt x -> pp_v fmt !i x; incr i) |
|
310 |
|
|
311 |
let pp_print_list2 |
|
312 |
?pp_prologue ?pp_epilogue ?pp_op ?pp_cl ?pp_open_box ?pp_eol ?pp_sep |
|
313 |
pp_v fmt (l1, l2) = |
|
314 |
pp_print_list |
|
315 |
?pp_prologue ?pp_epilogue ?pp_op ?pp_cl ?pp_open_box ?pp_eol ?pp_sep |
|
316 |
pp_v fmt (List.combine l1 l2) |
|
317 |
|
|
318 |
let pp_print_list_i2 |
|
319 |
?pp_prologue ?pp_epilogue ?pp_op ?pp_cl ?pp_open_box ?pp_eol ?pp_sep |
|
320 |
pp_v fmt (l1, l2) = |
|
321 |
pp_print_list_i |
|
322 |
?pp_prologue ?pp_epilogue ?pp_op ?pp_cl ?pp_open_box ?pp_eol ?pp_sep |
|
323 |
(fun fmt i (x1, x2) -> pp_v fmt i x1 x2) fmt (List.combine l1 l2) |
|
324 |
|
|
325 |
let pp_print_parenthesized ?(pp_sep=pp_print_comma) = |
|
326 |
pp_print_list |
|
327 |
~pp_op:pp_print_opar |
|
328 |
~pp_cl:pp_print_cpar |
|
329 |
~pp_sep |
|
330 |
|
|
331 |
let pp_print_braced ?(pp_sep=pp_print_comma) = |
|
332 |
pp_print_list |
|
333 |
~pp_op:pp_print_obrace |
|
334 |
~pp_cl:pp_print_cbrace |
|
335 |
~pp_sep |
|
336 |
|
|
337 |
end |
|
338 |
|
|
260 | 339 |
let fprintf_list ?(eol:('a, formatter, unit) format = "") ~sep:sep f fmt l = |
261 | 340 |
Format.(pp_print_list ~pp_sep:(fun fmt () -> fprintf fmt "%(%)" sep) f fmt l); |
262 | 341 |
if l <> [] then Format.fprintf fmt "%(%)" eol |
Also available in: Unified diff
some rewriting in C backend pretty-printer