diff --git a/flap/src/hopix/hopixInterpreter.ml b/flap/src/hopix/hopixInterpreter.ml index 03c9b9e..0e8341e 100644 --- a/flap/src/hopix/hopixInterpreter.ml +++ b/flap/src/hopix/hopixInterpreter.ml @@ -352,12 +352,30 @@ and value_definition runtime = function (expression' runtime.environment runtime.memory expr) in { runtime with environment = env' } - | RecFunctions _rf -> + | RecFunctions rf -> (* Ici on ajoute les noms des fonctions à l'environnement * pour qu'elles puissent s'appeller dans leur corps de fonction * => Retourne l'environnement modifié *) (* TODO *) - runtime + { runtime with environment = define_rec runtime.environment rf } + +and define_rec env rf = + (* Ajoute les fonctions récursives dans l'environnement + * par défaut on dit qu'elle renvoie Unit *) + let env' = + List.fold_left + (fun curr_env (id, _, _) -> Environment.bind curr_env id.value VUnit) + env + rf + in + (* On associe les fonctions avec leur contenu en ignorant le type via + * une closure en les rajoutant à l'environnement *) + List.iter + ((fun env'' (name, _, FunctionDefinition (pattern, expr)) -> + Environment.update name.position name.value env'' (VClosure (env'', pattern, expr))) + env') + rf; + env' and expression' environment memory e = expression (position e) environment memory (value e) @@ -392,7 +410,7 @@ and expression _pos environment _memory = function | Define _ -> (* TODO *) failwith "Students! This is your job (Define)!" - | Fun _ -> + | Fun (FunctionDefinition _) -> (* TODO *) failwith "Students! This is your job (Fun)!" | Apply _ ->