-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelping_funs.ml
More file actions
119 lines (78 loc) · 2.72 KB
/
helping_funs.ml
File metadata and controls
119 lines (78 loc) · 2.72 KB
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
open Netlist_ast
open Netlist
open Scheduler
let general_error = "Entrée invalide, veuillez réessayer."
let error_wrong_entry = "Entrée invalide. Entrez uniquement des bits 1 ou 0. Veuillez réessayer :"
let error_taille i = "Entrée invalide. Entrez une succession de "^ string_of_int i ^" bits. Veuillez réessayer."
let bool_of_int i = if i=1 then true else false
let int_of_bool x = if x then 1 else 0
let extract_val = function
|VBit b -> b
|_-> failwith "Ce n'est pas encore inmplémenté"
let int_of_barray t=
Array.fold_right(fun b n -> (2*n) + (if b then 1 else 0)) t 0
let calc_binop op x1 x2=
match op with
|And -> x1&&x2
|Or -> x1||x2
|Xor -> (x1||x2)&& not(x1&&x2)
|Nand -> not(x1 && x2)
let calc_mux a b c = if a then c else b
exception Not_Valid_Argument of int
let bitarray_of_string s n =
let a = Array.make n false in
let aux i c =
let c2 = int_of_string(Char.escaped c) in
if (c2 = 0 || c2 = 1) then
a.(i) <- bool_of_int(c2)
else raise (Not_Valid_Argument i)
in
String.iteri aux s;
VBitArray a
let string_of_array vect n =
let a = Bytes.create n in
let aux i b =
let carac = char_of_int (int_of_bool b) in
Bytes.set a i carac
in
Array.iteri aux vect;
Bytes.to_string a
let rec get_bit env nom =
try
let a = int_of_string (read_line()) in
if (a = 0 || a = 1) then
(Hashtbl.replace env nom (VBit (bool_of_int a)))
else begin
print_endline (error_wrong_entry);
(get_bit env nom)
end
with _ ->
begin
print_endline (general_error);
get_bit env nom end
let rec get_array env nom n =
try
(
let a = read_line() in
let long = String.length a in
print_endline (a);
if long <> n then ( print_endline (error_taille n); get_array env nom n )
else begin
try
(let res = bitarray_of_string a n in
Hashtbl.replace env nom res)
with
|Not_Valid_Argument _ -> (print_endline(error_wrong_entry);get_array env nom n)
| _ -> (print_endline(general_error) ;get_array env nom n)
end
)
with _ -> begin print_endline (error_wrong_entry); get_array env nom n end
let get env var_type nom =
print_endline ("Entrez la valeur de la variable "^nom^" :");
match Env.find nom var_type with
|TBit -> get_bit env nom
|TBitArray n -> get_array env nom n
let print_res env a =
match Hashtbl.find env a with
|VBit b -> print_endline(a ^ " = " ^string_of_int (int_of_bool b ) )
|VBitArray b -> print_endline(a ^ " = " ^(string_of_array b (Array.length b)) )