-
Notifications
You must be signed in to change notification settings - Fork 0
/
Global.ml
executable file
·117 lines (91 loc) · 3.75 KB
/
Global.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
(*****************************************************************
*
* IMPERATOR
*
* Compiler from IMPERATOR file to OCaml
*
* Laboratoire Specification et Verification (ENS Cachan & CNRS, France)
* Author: Etienne Andre
* Created: 04/03/2009
* Last modified: 27/03/2009
*
****************************************************************)
(****************************************************************)
(** Exceptions *)
(****************************************************************)
exception ParsingError of string
exception InternalError of string
(****************************************************************)
(** Debug modes *)
(****************************************************************)
let mode_ERROR = 0
let mode_NODEBUG = 0
let mode_STANDARD = 1
let mode_LOW_DEBUG = 2
let mode_MEDIUM_DEBUG = 3
let mode_HIGH_DEBUG = 4
let mode_TOTAL_DEBUG = 5
(****************************************************************)
(** Global constants *)
(****************************************************************)
let default_beta = Num.div_num (Num.num_of_int 99) (Num.num_of_int 100)
(****************************************************************)
(** Global time counter *)
(****************************************************************)
let counter = ref (Unix.gettimeofday())
(** Get the value of the counter
@return float the value of counter with 3 digits after "." *)
let get_time() =
((float_of_int) (int_of_float ((Unix.gettimeofday() -. (!counter)) *. 1000.0))) /. 1000.0
(****************************************************************)
(** Useful functions *)
(****************************************************************)
(* Convert an array of string into a string *)
let string_of_array_of_string =
Array.fold_left (fun the_string s -> the_string ^ s) ""
(****************************************************************)
(** Messages *)
(****************************************************************)
(* Print a message in function of debug_level *)
let print_debug_message debug_mode debug_level message =
(* Only print the message if its debug_level is compatible with the debug_mode *)
if debug_mode >= debug_level then
(* Find number of blanks for indentation *)
let nb_spaces = if debug_level-1 > 0 then debug_level-1 else 0 in
(* Create blanks proportionnally to the debug_level (at least one space) *)
let spaces = " " ^ string_of_array_of_string (Array.make nb_spaces " ") in
(* Add new lines and blanks everywhere *)
let formatted_message = spaces ^ (Str.global_replace (Str.regexp "\n") ("\n" ^ spaces) message) in
(* Print message *)
print_string (formatted_message ^ "\n")
(* Print a warning *)
let print_warning message =
let spaces = " " in
(* Add new lines and blanks everywhere *)
let formatted_message = spaces ^ "*** Warning: " ^ (Str.global_replace (Str.regexp "\n") ("\n" ^ spaces) message) in
(* Print message *)
print_string (formatted_message ^ "\n")
(****************************************************************)
(** Terminating functions *)
(****************************************************************)
(* Abort program *)
let abort_program () =
print_debug_message mode_TOTAL_DEBUG mode_ERROR ("Program aborted (after " ^ (string_of_float (get_time())) ^ " seconds)");
print_newline();
flush stdout;
exit(0)
(* Terminate program *)
let terminate_program () =
print_newline();
print_debug_message mode_TOTAL_DEBUG mode_STANDARD ("Program successfully terminated (after " ^ (string_of_float (get_time())) ^ " seconds)");
print_newline();
flush stdout;
exit(0)
(*
let OrderedString =
struct
type t = string
let compare x y = if x = y then Equal else if x < y then Less else Greater
end
let StringSet = Set(OrderedString)
*)