From dfcac7819cb61c55bf59e56f38f28be70fc95be7 Mon Sep 17 00:00:00 2001 From: acondolu Date: Wed, 26 Jul 2017 11:07:15 +0200 Subject: [PATCH] Copied old code back in parser, to make andrea8 great again --- ocaml/parser.ml | 36 +++++++++++++++++++++++++++++------- ocaml/parser.mli | 14 ++++++++++++-- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/ocaml/parser.ml b/ocaml/parser.ml index ea885d2..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) ;; @@ -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 ;; @@ -146,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 @@ -241,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) ;; diff --git a/ocaml/parser.mli b/ocaml/parser.mli index d131edb..8dba854 100644 --- a/ocaml/parser.mli +++ b/ocaml/parser.mli @@ -1,6 +1,11 @@ +type term = + | Var of int + | App of term * term + | Lam of term + exception ParsingError of string -val problem_of_string: +(* val problem_of_string: string -> string (* problem label *) * Num.i_var option (* div *) @@ -12,4 +17,9 @@ val from_file : string -> * Num.i_var option (* div *) * Num.i_n_var list (* conv *) * Num.i_n_var list (* ps *) - * string list (* names of free variables *)) list + * string list (* names of free variables *)) list *) + + (* parses a string to a term *) + val parse: string -> term + (* parse many strings/terms, and returns the list of parsed terms + the list of free variables; variable 0 is not used *) + val parse_many: string list -> term list * string list -- 2.39.2