X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;ds=sidebyside;f=ocaml%2Fparser.ml;h=fbce730c78915d11457b701ee46691fef2cae988;hb=dfcac7819cb61c55bf59e56f38f28be70fc95be7;hp=2b0e04dd0104ad79b71ca9a55c20030c31050e5f;hpb=6fc1858e757263b4e651f92fa8aaa3ecee1ab626;p=fireball-separation.git diff --git a/ocaml/parser.ml b/ocaml/parser.ml index 2b0e04d..fbce730 100644 --- a/ocaml/parser.ml +++ b/ocaml/parser.ml @@ -1,8 +1,14 @@ -exception ParsingError of string;; +type term = + | Var of int + | App of term * term + | Lam of term +;; -let mk_app x y = Num.mk_app x y;; -let mk_lam x = `Lam(true, x);; -let mk_var x = `Var(x, -666);; +let mk_app x y = App(x, y);; +let mk_lam x = Lam x;; +let mk_var x = Var x;; + +exception ParsingError of string;; let isAlphaNum c = let n = Char.code c in (48 <= n && n <= 90) || (95 <= n && n <= 122) ;; @@ -10,9 +16,9 @@ let isSpace c = c = ' ' || c = '\n' || c = '\t' ;; (* FIXME *) let mk_var' (bound, free) x = - if List.mem x bound + if x <> "@" && List.mem x bound then free, mk_var (Util.index_of x bound) - else if List.mem x free + else if x <> "@" && List.mem x free then free, mk_var (List.length bound + Util.index_of x free) else (free @ [x]), mk_var (List.length bound + List.length free) ;; @@ -30,10 +36,10 @@ let explode s = ;; let implode l = - let res = Bytes.create (List.length l) in + let res = String.create (List.length l) in let rec aux i = function | [] -> res - | c :: l -> Bytes.set res i c; aux (i + 1) l in + | c :: l -> String.set res i c; aux (i + 1) l in aux 0 l ;; @@ -45,7 +51,12 @@ let rec strip_spaces = function let read_var s = let rec aux = function | [] -> None, [] - | c::cs as x -> if isAlphaNum c + | c::cs as x -> + if c = '@' then + (if cs <> [] && (let hd = List.hd cs in hd = '@' || isAlphaNum hd) + then raise (ParsingError ("Unexpected `"^String.make 1 (List.hd cs)^"` after `@`.")) + else Some['@'], cs) + else if isAlphaNum c then match aux cs with | (Some x), cs' -> Some (c :: x), cs' | None, cs' -> (Some [c]), cs' @@ -60,7 +71,7 @@ let read_var' (bound, free as vars) s = | Some varname, cs -> let free, v = mk_var' vars varname in Some [v], cs, (bound, free) - | _, _ -> raise (ParsingError ("Can't read variable")) + | None, _ -> raise (ParsingError ("Can't read variable")) ;; let rec read_smt vars = @@ -141,7 +152,7 @@ let _ = prerr_endline (">>>" ^ string_of_term (parse "(\\x. x y z z1 k) z1 z j") *******************************************************************************) -let problem_of_string s = +(* let problem_of_string s = let lines = Str.split (Str.regexp "[\n\r\x0c\t;]+") s in let head, lines = List.hd lines, List.tl lines in let name = String.trim (String.sub head 1 (String.length head - 1)) in @@ -236,4 +247,20 @@ let from_file path = let txt = String.concat "\n" (List.rev !lines) in let problems = Str.split (Str.regexp "[\n\r]+\\$") txt in List.map problem_of_string (List.tl (List.map ((^) "$") problems)) +;; *) + +let parse x = + match read_smt ([],[]) (explode x) with + | Some [y], [], _ -> y + | _, _, _ -> assert false +;; + + +let parse_many strs = + let f (x, y) z = match read_smt y (explode z) with + | Some[tm], [], vars -> (tm :: x, vars) + | _, _, _ -> assert false + in let aux = List.fold_left f ([], ([], [])) + in let (tms, (_, free)) = aux strs + in (List.rev tms, free) ;;