From 0b241c72c408be4834ca73c56fe21391abb2e2c4 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Tue, 16 Apr 2024 14:24:06 +0200 Subject: [PATCH] -alpha: take one input separated by & --- Makefile | 3 +++ dune | 3 ++- main.ml | 22 ++++++++++++---------- tests/alpha_equiv/basic.lam | 1 + tests/alpha_equiv/basic.ml | 1 - tests/alpha_equiv/basic_alpha.ml | 1 - tests/alpha_equiv/basic_alpha2.ml | 1 - tests/alpha_equiv/portee.lam | 1 + 8 files changed, 19 insertions(+), 14 deletions(-) create mode 100644 tests/alpha_equiv/basic.lam delete mode 100644 tests/alpha_equiv/basic.ml delete mode 100644 tests/alpha_equiv/basic_alpha.ml delete mode 100644 tests/alpha_equiv/basic_alpha2.ml create mode 100644 tests/alpha_equiv/portee.lam diff --git a/Makefile b/Makefile index e2b6871..a03c10b 100644 --- a/Makefile +++ b/Makefile @@ -2,3 +2,6 @@ pieuvre: main.ml lam.ml affichage.ml lexer.mll parser.mly dune build cp _build/default/main.exe pieuvre chmod +w pieuvre + +clean: + rm -rf _build pieuvre diff --git a/dune b/dune index ca7b91a..55542a5 100644 --- a/dune +++ b/dune @@ -1,5 +1,6 @@ (executable - (name main)) + (name main) + (libraries str)) (ocamllex lexer) (menhir diff --git a/main.ml b/main.ml index 81a11c8..63c2ebf 100644 --- a/main.ml +++ b/main.ml @@ -15,7 +15,7 @@ let interpret e = end let nom_fichier = ref "" -let equiv_fichier = ref "" +let alpha = ref false let parse_channel c = let lexbuf = Lexing.from_channel c in @@ -23,7 +23,7 @@ let parse_channel c = let recupere_entree () = let optlist = [ - ("-alphaequiv", Arg.Set_string equiv_fichier, "Vérifie l'alpha équivalence avec un autre fichier"); + ("-alpha", Arg.Set alpha, "Vérifie l'alpha équivalence de deux termes séparés par &"); ] in let usage = "Bienvenue à bord." in (* message d'accueil, option -help *) @@ -34,18 +34,20 @@ let recupere_entree () = (fun s -> nom_fichier := s) (* la fonction a declencher lorsqu'on recupere un string qui n'est pas une option : ici c'est le nom du fichier, et on stocke cette information dans la reference nom_fichier *) usage; (* le message d'accueil *) - let lam1 = try + try let where_from = match !nom_fichier with | "" -> stdin | s -> open_in s in - parse_channel where_from + if !alpha then + let input_str = In_channel.input_all where_from in + match Str.split (Str.regexp "&") input_str with + [s1; s2] -> AlphaEquiv ( + Parser.main Lexer.token (Lexing.from_string (s1^"\n")), + Parser.main Lexer.token (Lexing.from_string s2) + ) + | _ -> failwith "Alpha-equivalence: nombre de delimiteurs incorrect" + else Simple (parse_channel where_from) with e -> (Printf.printf "problème de saisie\n"; raise e) - in if !equiv_fichier <> "" then - let lam2 = try parse_channel (open_in !equiv_fichier) - with e -> (Printf.printf "problème de saisie\n"; raise e) - in AlphaEquiv (lam1, lam2) - else Simple lam1 - (* la fonction principale *) let run () = diff --git a/tests/alpha_equiv/basic.lam b/tests/alpha_equiv/basic.lam new file mode 100644 index 0000000..7c42171 --- /dev/null +++ b/tests/alpha_equiv/basic.lam @@ -0,0 +1 @@ +fun (x:A) => fun (y:A) => y & fun (u:A) => fun (v:A) => v diff --git a/tests/alpha_equiv/basic.ml b/tests/alpha_equiv/basic.ml deleted file mode 100644 index 8540c76..0000000 --- a/tests/alpha_equiv/basic.ml +++ /dev/null @@ -1 +0,0 @@ -fun (x:A) => x y z diff --git a/tests/alpha_equiv/basic_alpha.ml b/tests/alpha_equiv/basic_alpha.ml deleted file mode 100644 index 1c62fad..0000000 --- a/tests/alpha_equiv/basic_alpha.ml +++ /dev/null @@ -1 +0,0 @@ -fun (y:A) => y y z diff --git a/tests/alpha_equiv/basic_alpha2.ml b/tests/alpha_equiv/basic_alpha2.ml deleted file mode 100644 index f49d703..0000000 --- a/tests/alpha_equiv/basic_alpha2.ml +++ /dev/null @@ -1 +0,0 @@ -fun (a:A) => a y z diff --git a/tests/alpha_equiv/portee.lam b/tests/alpha_equiv/portee.lam new file mode 100644 index 0000000..327e1f6 --- /dev/null +++ b/tests/alpha_equiv/portee.lam @@ -0,0 +1 @@ +fun (x:A) => x & fun (y:A) => x