-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_espresso.ml
38 lines (34 loc) · 1.17 KB
/
read_espresso.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
#load "str.cma";;
let read_file filename =
try
let f = open_in filename in
let rec read accu =
try
let l = input_line f in
read (l::accu)
with End_of_file -> List.rev accu
in
let rec aux lines (nb_in,nb_out,inputs) =
match lines with
| line::r -> begin
if line = "" then (nb_in, nb_out,inputs)
else if line.[0] = '.' then begin
if line.[1] = 'i' && nb_in = 0 then
let nb_in = int_of_string (List.hd (Str.split (Str.regexp ".i ") line)) in
aux r (nb_in, nb_out,inputs)
else if line.[1] = 'o' && nb_out = 0 then
let nb_out = int_of_string (List.hd (Str.split (Str.regexp ".o ") line)) in
aux r (nb_in, nb_out, inputs)
else failwith "malforme"
end else
if nb_in = 0 || nb_out = 0 then failwith "pas de nb entre/sorti defini"
else
match (Str.split (Str.regexp " ") line) with
| (i:string)::(o:string)::[] when (i<>"" && o<>"") -> aux r (nb_in, nb_out, (i,o)::inputs)
| _ -> failwith "erreur"
end
| _ -> (nb_in, nb_out, inputs)
in let lines = read [] in if lines = [] then failwith "vide" else
close_in f; aux lines (0,0,[])
with _ -> print_string "not found";(0,0,[])
;;