From 1921101d451e0ae37dd1a15d2ba7660885749e26 Mon Sep 17 00:00:00 2001 From: Nicolas PENELOUX Date: Tue, 12 Dec 2023 16:13:31 +0100 Subject: [PATCH] =?UTF-8?q?Etape=202=20:=20free=5Fvariables=20(=C3=A0=20te?= =?UTF-8?q?ster)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- flap/src/fopix/hobixToFopix.ml | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/flap/src/fopix/hobixToFopix.ml b/flap/src/fopix/hobixToFopix.ml index d49fe01..518ad41 100644 --- a/flap/src/fopix/hobixToFopix.ml +++ b/flap/src/fopix/hobixToFopix.ml @@ -120,16 +120,26 @@ let free_variables = | [ s ] -> f s | s :: xs -> M.union (f s) (unions f xs) in + (*Une variable libre est une variable qui peut ĂȘtre substituĂ©*) + + (*TODO : rajouter des explications pour While Define et Fun*) let rec fvs = function | S.Literal _ -> M.empty | S.Variable x -> M.singleton x - | S.While (cond, e) -> failwith "Students! This is your job!" - | S.Define (vd, a) -> failwith "Students! This is your job!" + | S.While (cond, e) -> unions fvs [cond;e] + | S.Define (vd, a) -> + let liste_def_valeur = + match vd with + | S.SimpleValue (id,expr) -> [(id,expr)] + | S.RecFunctions (list) -> list + in + let id, expr = List.split liste_def_valeur in + M.diff (unions fvs (a::expr)) (M.of_list id) | S.ReadBlock (a, b) -> unions fvs [ a; b ] | S.Apply (a, b) -> unions fvs (a :: b) | S.WriteBlock (a, b, c) | S.IfThenElse (a, b, c) -> unions fvs [ a; b; c ] | S.AllocateBlock a -> fvs a - | S.Fun (xs, e) -> failwith "Students! This is your job!" + | S.Fun (xs, e) -> M.diff (fvs e) (M.of_list xs) | S.Switch (a, b, c) -> let c = match c with @@ -139,7 +149,7 @@ let free_variables = unions fvs ((a :: ExtStd.Array.present_to_list b) @ c) in fun e -> M.elements (fvs e) -;; + (** A closure compilation environment relates an identifier to the way it is accessed in the compiled version of the function's