This commit is contained in:
Nicolas PENELOUX 2023-11-07 12:22:26 +01:00
commit ff6ab31442

View file

@ -352,16 +352,38 @@ and value_definition runtime = function
(expression' runtime.environment runtime.memory expr)
in
{ runtime with environment = env' }
<<<<<<< HEAD
(*
| RecFunctions _rf -> let env' = define_rec_functions environment _rf
in
{ runtime with environment = env' }
=======
| RecFunctions rf ->
>>>>>>> ce199651276925a6154acb80f356e4f360d35a7b
(* 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'
(* TODO : commentaire explicatif *)
@ -378,14 +400,14 @@ 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 *)
Environment.lookup id.position id.value environment
| Tagged(constructor,_,list_t) -> (* On ignore le type car on interprète *)
(* TODO *)
VTagged(constructor.value, List.map(expression' environment _memory) list_t)
VTagged(constructor.value, List.map(expression' environment memory) list_t)
| Record(label,list_r) -> failwith ("Todo corriger l'erreur de type")
(*TODO : réparer *)
(*VRecord(List.map(label.value, expression' environment _memory) list_r)*)
@ -396,32 +418,27 @@ and expression _pos environment _memory = function
(* Cas pour le Tuple vide
* Un tuple vide ne contient rien (logique), donc on utilise un VUnit*)
VUnit
(*Sinon, on associe pour chaque*)
| Tuple list_exp -> VTuple (List.map (expression' environment _memory) list_exp)
| Sequence(list_exp) -> failwith ("Todo corriger l'erreur")
(*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)!"
| Define _ ->
(* TODO *)
failwith "Students! This is your job (Define)!"
| Fun _ ->
| Fun (FunctionDefinition _) ->
(* TODO *)
failwith "Students! This is your job (Fun)!"
| Apply(a,b) -> apply_values a b environment _memory
(* TODO *)
failwith "Students! This is your job (Apply)!"
| Apply (f, x) -> apply_expression f x environment memory
| Ref ref ->
let reference = expression' environment _memory ref in (* On créé un VLocation et on alloue de la mémoire pour la valeur de la référence*)
VLocation (Memory.allocate _memory Mint.one reference)
let dref = expression' environment memory ref in
VLocation (Memory.allocate memory Mint.one dref)
| Assign _ ->
(* TODO *)
failwith "Students! This is your job (Assign)!"
| Read read ->
let loc = value_as_location (expression' environment _memory read) in
let loc = value_as_location (expression' environment memory read) in
(match loc with
| Some (adr) -> Memory.read (Memory.dereference _memory adr) (Mint.zero) (* On lis la valeur de la mémoire*)
| Some (adr) -> Memory.read (Memory.dereference memory adr) (Mint.zero) (* On lis la valeur de la mémoire*)
| None -> failwith "erreur") (* TODO : faire une vrai erreur *)
| Case _ ->
(* TODO *)
@ -449,6 +466,18 @@ and apply_values a b env mem =
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
* TODO: Pattern matching ici *)
failwith "Students! This is your job (Apply)!"
| _ -> assert false (* By typing *)
and literal_expression = function
| LInt n -> VInt n
| LChar c -> VChar c