From d461396636f42fe6de9c499aec0057f844b906e1 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 6 Nov 2023 20:26:28 +0100 Subject: [PATCH 1/3] function recursive --- flap/src/hopix/hopixInterpreter.ml | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) 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 _ -> From 2546d6d16b6d75137bd9956217e6f082823fcc63 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 6 Nov 2023 20:55:37 +0100 Subject: [PATCH 2/3] apply --- flap/src/hopix/hopixInterpreter.ml | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/flap/src/hopix/hopixInterpreter.ml b/flap/src/hopix/hopixInterpreter.ml index 0e8341e..3814997 100644 --- a/flap/src/hopix/hopixInterpreter.ml +++ b/flap/src/hopix/hopixInterpreter.ml @@ -385,7 +385,7 @@ and expression' environment memory e = E, M ⊢ e ⇓ v, M' and E = [runtime.environment], M = [runtime.memory]. *) -and expression _pos environment _memory = function +and expression _pos environment memory = function | Literal l -> literal_expression l.value | Variable (id, _) -> (* On cherche l'id dans l'environnement *) @@ -403,7 +403,7 @@ and expression _pos environment _memory = function (* Cas pour le Tuple vide * Un tuple vide ne contient rien (logique), donc on utilise un VUnit*) VUnit - | Tuple list_exp -> VTuple (List.map (expression' environment _memory) list_exp) + | Tuple list_exp -> VTuple (List.map (expression' environment memory) list_exp) | Sequence _ -> (* TODO *) failwith "Students! This is your job (Sequence)!" @@ -413,12 +413,10 @@ and expression _pos environment _memory = function | Fun (FunctionDefinition _) -> (* TODO *) failwith "Students! This is your job (Fun)!" - | Apply _ -> - (* TODO *) - failwith "Students! This is your job (Apply)!" + | Apply (f, x) -> apply_expression f x environment memory | Ref ref -> - let dref = expression' environment _memory ref in - VLocation (Memory.allocate _memory Mint.one dref) + let dref = expression' environment memory ref in + VLocation (Memory.allocate memory Mint.one dref) | Assign _ -> (* TODO *) failwith "Students! This is your job (Assign)!" @@ -441,6 +439,17 @@ and expression _pos environment _memory = function (* On ignore le type car on interprète *) VUnit +and apply_expression f x environment memory = + let x_val = expression' environment memory x in + match expression' environment memory f with + | VPrimitive (_, f) -> + (* Fonction "primitive" *) + f memory x_val + | VClosure (env_fn, pattern, expr) -> + (* Fonction *) + failwith "Students! This is your job (Apply)!" + | _ -> assert false (* By typing *) + and literal_expression = function | LInt n -> VInt n | LChar c -> VChar c From ce199651276925a6154acb80f356e4f360d35a7b Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 6 Nov 2023 20:57:21 +0100 Subject: [PATCH 3/3] todo --- flap/src/hopix/hopixInterpreter.ml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/flap/src/hopix/hopixInterpreter.ml b/flap/src/hopix/hopixInterpreter.ml index 3814997..483f469 100644 --- a/flap/src/hopix/hopixInterpreter.ml +++ b/flap/src/hopix/hopixInterpreter.ml @@ -445,8 +445,9 @@ and apply_expression f x environment memory = | VPrimitive (_, f) -> (* Fonction "primitive" *) f memory x_val - | VClosure (env_fn, pattern, expr) -> - (* Fonction *) + | VClosure (_env_fn, _pattern, _expr) -> + (* Fonction + * TODO: Pattern matching ici *) failwith "Students! This is your job (Apply)!" | _ -> assert false (* By typing *)