Etape 2 : free_variables (à tester)
This commit is contained in:
parent
6e0deecba5
commit
1921101d45
1 changed files with 14 additions and 4 deletions
|
@ -120,16 +120,26 @@ let free_variables =
|
||||||
| [ s ] -> f s
|
| [ s ] -> f s
|
||||||
| s :: xs -> M.union (f s) (unions f xs)
|
| s :: xs -> M.union (f s) (unions f xs)
|
||||||
in
|
in
|
||||||
|
(*Une variable libre est une variable qui peut être substitué*)
|
||||||
|
|
||||||
|
(*TODO : rajouter des explications pour While Define et Fun*)
|
||||||
let rec fvs = function
|
let rec fvs = function
|
||||||
| S.Literal _ -> M.empty
|
| S.Literal _ -> M.empty
|
||||||
| S.Variable x -> M.singleton x
|
| S.Variable x -> M.singleton x
|
||||||
| S.While (cond, e) -> failwith "Students! This is your job!"
|
| S.While (cond, e) -> unions fvs [cond;e]
|
||||||
| S.Define (vd, a) -> failwith "Students! This is your job!"
|
| 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.ReadBlock (a, b) -> unions fvs [ a; b ]
|
||||||
| S.Apply (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.WriteBlock (a, b, c) | S.IfThenElse (a, b, c) -> unions fvs [ a; b; c ]
|
||||||
| S.AllocateBlock a -> fvs a
|
| 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) ->
|
| S.Switch (a, b, c) ->
|
||||||
let c =
|
let c =
|
||||||
match c with
|
match c with
|
||||||
|
@ -139,7 +149,7 @@ let free_variables =
|
||||||
unions fvs ((a :: ExtStd.Array.present_to_list b) @ c)
|
unions fvs ((a :: ExtStd.Array.present_to_list b) @ c)
|
||||||
in
|
in
|
||||||
fun e -> M.elements (fvs e)
|
fun e -> M.elements (fvs e)
|
||||||
;;
|
|
||||||
|
|
||||||
(** A closure compilation environment relates an identifier to the way
|
(** A closure compilation environment relates an identifier to the way
|
||||||
it is accessed in the compiled version of the function's
|
it is accessed in the compiled version of the function's
|
||||||
|
|
Reference in a new issue