]> matita.cs.unibo.it Git - fireball-separation.git/blob - ocaml/parser.ml
Allow comments (#) at the end of terms
[fireball-separation.git] / ocaml / parser.ml
1 exception ParsingError of string;;\r
2 \r
3 let mk_app x y = Num.mk_app x y;;\r
4 let mk_lam x = `Lam(true, x);;\r
5 let mk_var x = `Var(x, -666);;\r
6 \r
7 let isAlphaNum c = let n = Char.code c in\r
8  (48 <= n && n <= 90) || (95 <= n && n <= 122) ;;\r
9 let isSpace c = c = ' ' || c = '\n' || c = '\t' ;;\r
10 \r
11 (* FIXME *)\r
12 let mk_var' (bound, free) x =\r
13   if List.mem x bound\r
14   then free, mk_var (Util.index_of x bound)\r
15   else if List.mem x free\r
16        then free, mk_var (List.length bound + Util.index_of x free)\r
17        else (free @ [x]), mk_var (List.length bound + List.length free)\r
18 ;;\r
19 \r
20 let mk_app' = function\r
21   | [] ->  assert false\r
22   | t :: ts -> List.fold_left mk_app t ts\r
23 ;;\r
24 \r
25 let explode s =\r
26  let len = String.length s in\r
27   let rec aux i l =\r
28     if i >= len || s.[i] = '#' then l else aux (i+1) (s.[i] :: l)\r
29   in List.rev (aux 0 [])\r
30 ;;\r
31 \r
32 let implode l =\r
33   let res = String.create (List.length l) in\r
34   let rec aux i = function\r
35     | [] -> res\r
36     | c :: l -> res.[i] <- c; aux (i + 1) l in\r
37   aux 0 l\r
38 ;;\r
39 \r
40 let rec strip_spaces = function\r
41   | c::cs when isSpace c -> strip_spaces cs\r
42   | cs -> cs\r
43 ;;\r
44 \r
45 let read_var s =\r
46   let rec aux = function\r
47   | [] -> None, []\r
48   | c::cs as x -> if isAlphaNum c\r
49     then match aux cs with\r
50          | (Some x), cs' -> Some (c :: x), cs'\r
51          | None, cs' -> (Some [c]), cs'\r
52     else None, x\r
53   in match aux s with\r
54     | None, y -> None, y\r
55     | Some x, y -> Some (implode x), y\r
56 ;;\r
57 \r
58 let read_var' (bound, free as vars) s =\r
59   match read_var s with\r
60   | Some varname, cs ->\r
61     let free, v = mk_var' vars varname in\r
62     Some [v], cs, (bound, free)\r
63   | _, _ -> raise (ParsingError ("Can't read variable"))\r
64 ;;\r
65 \r
66 let rec read_smt vars =\r
67   let check_if_lambda cs = match read_var cs with\r
68     | None, _ -> false\r
69     | Some x, cs -> match strip_spaces cs with\r
70       | [] -> false\r
71       | c::_ -> c = '.'\r
72   in let read_lambda (bound, free) cs = (\r
73     match read_var (strip_spaces cs) with\r
74       | Some varname, cs ->\r
75       let vars' = (varname::bound, free) in\r
76       (match strip_spaces cs with\r
77         | [] -> raise (ParsingError "Lambda expression incomplete")\r
78         | c::cs -> (if c = '.' then (match read_smt vars' cs with\r
79           | None, _, _ -> raise (ParsingError "Lambda body expected")\r
80           | Some [x], y, (_, free) -> Some [mk_lam x], y, (bound, free)\r
81           | Some _, _, _ -> assert false\r
82           ) else raise (ParsingError "Expected `.` after variable in lambda")\r
83         ))\r
84       | _, _ -> assert false\r
85  ) in let rec aux vars cs =\r
86   match strip_spaces cs with\r
87   | [] -> None, [], vars\r
88   | c::_ as x ->\r
89       let tms, cs, vars = (\r
90            if c = '(' then read_pars vars x\r
91       else if c = ')' then (None, x, vars)\r
92       else if check_if_lambda x then read_lambda vars x\r
93       else read_var' vars x) in\r
94       match tms with\r
95       | Some [tm] -> (\r
96         match aux vars cs with\r
97           | None, cs, vars -> Some [tm], cs, vars\r
98           | Some ts, cs, vars -> Some (tm :: ts), cs, vars\r
99         )\r
100       | Some _ -> assert false\r
101       | None -> None, x, vars\r
102   in fun cs -> match aux vars cs with\r
103     | None, cs, vars -> None, cs, vars\r
104     | Some ts, cs, vars -> Some [mk_app' ts], cs, vars\r
105 and read_pars vars = function\r
106   | [] -> None, [], vars\r
107   | c::cs -> if c = '(' then (\r
108     let tm, cs, vars = read_smt vars cs in\r
109     let cs = strip_spaces cs in\r
110     match cs with\r
111       | [] -> None, [], vars\r
112       | c::cs -> if c = ')' then tm, cs, vars else raise (ParsingError "Missing `)`")\r
113     ) else assert false\r
114 ;;\r
115 \r
116 let parse x =\r
117   match read_smt ([],[]) (explode x) with\r
118   | Some [y], [], _ -> y\r
119   | _, _, _ -> assert false\r
120 ;;\r
121 \r
122 let parse_many strs =\r
123   let f (x, y) z = match read_smt y (explode z) with\r
124   | Some[tm], [], vars -> (tm :: x, vars)\r
125   | _, _, _ -> assert false\r
126   in let aux = List.fold_left f ([], ([], []))\r
127   in let (tms, (_, free)) = aux strs\r
128   in (List.rev tms, free)\r
129 ;;\r
130 \r
131 let problem_of_string s =\r
132  let lines = Str.split (Str.regexp "[\n\r\x0c\t;]+") s in\r
133  let head, lines = List.hd lines, List.tl lines in\r
134  let name = String.trim (String.sub head 1 (String.length head - 1)) in\r
135  let lines = List.filter ((<>) "") lines in\r
136  let aux (last, div, conv, ps) line =\r
137   let chr = String.sub line 0 1 in\r
138   let line = String.trim (String.sub line 1 (String.length line - 1)) in\r
139   if line = "" then chr, div, conv, ps else\r
140   let rec aux' chr line =\r
141    if chr = "#"\r
142     then chr, div, conv, ps\r
143    else if chr = "D"\r
144     then chr, line, conv, ps\r
145    else if chr = "C"\r
146     then chr, div, line::conv, ps\r
147    else if chr = "N"\r
148     then chr, div, conv, line::ps\r
149    else if chr = " "\r
150     then aux' last line\r
151    else raise (ParsingError\r
152      ("Unexpected at beginning of problem: `" ^ chr ^ "` " ^ line)) in\r
153   aux' chr line in\r
154  let _, div, conv, ps = List.fold_left aux ("#", "", [], []) lines in\r
155  let div_provided = div <> "" in\r
156  let div = if div_provided then div else "xxxxxx" in\r
157  let strs = [div] @ ps @ conv in\r
158 \r
159  if List.length ps = 0 && List.length conv = 0\r
160   then raise (ParsingError "Parsed empty problem");\r
161 \r
162  (* parse' *)\r
163  let (tms, free) = parse_many strs in\r
164  let n_bot = try Util.index_of "BOT" free with Not_found -> min_int in\r
165  let n_pac = try Util.index_of "PAC" free with Not_found -> min_int in\r
166  let n_bomb = try Util.index_of "BOMB" free with Not_found -> min_int in\r
167  let fix lev v =\r
168  if v = lev + n_bot then failwith "Example with `Bottom"\r
169   else if v = lev + n_pac then failwith "Example with `Pacman"\r
170    else if v = lev + n_bomb then failwith "Example with `Bomb"\r
171     else if v = lev then `Var(v, min_int) (* zero *)\r
172       else `Var(v,1) in (* 1 by default when variable not applied *)\r
173  (* Fix arity *)\r
174  let open Num in\r
175  let rec aux lev : nf -> nf = function\r
176  | `I((n,_), args) -> `I((n,(if lev = 0 then 0 else 1) + Listx.length args), Listx.map (aux lev) args)\r
177  | `Var(n,_) -> fix lev n\r
178  | `Lam(_,t) -> `Lam (true, aux (lev+1) t)\r
179  | `Match _ | `N _ -> assert false in\r
180 let all_tms = List.map (aux 0) (tms :> Num.nf list) in\r
181 \r
182 (* problem_of *)\r
183 let div, (ps, conv) = List.hd all_tms, Util.list_cut (List.length ps, List.tl all_tms) in\r
184 \r
185 let div = if not div_provided\r
186  then None\r
187  else match div with\r
188   | `I _ as t -> Some t\r
189   | _ -> raise (ParsingError "D is not an inert in the initial problem") in\r
190 let conv = Util.filter_map (\r
191  function\r
192  | #i_n_var as t -> Some t\r
193  | `Lam _ -> None\r
194  | _ -> raise (ParsingError "A C-term is not i_n_var")\r
195  ) conv in\r
196 let ps = List.map (\r
197  function\r
198   | #i_n_var as y -> y\r
199   | _ -> raise (ParsingError "An N-term is not i_n_var")\r
200  ) ps in\r
201  name, div, conv, ps, free\r
202 ;;\r
203 \r
204 \r
205 let from_file path =\r
206  let lines = ref ["#"] in\r
207  let chan = open_in path in\r
208  let _ = try\r
209   while true; do\r
210     lines := input_line chan :: !lines\r
211   done\r
212  with End_of_file ->\r
213   close_in chan in\r
214  let txt = String.concat "\n" (List.rev !lines) in\r
215  let problems = Str.split (Str.regexp "[\n\r]+\\$") txt in\r
216  List.map problem_of_string (List.tl (List.map ((^) "$") problems))\r
217 ;;\r