ajout IfThenElse/Fun/Field/début d'autres

This commit is contained in:
Nicolas PENELOUX 2023-11-07 14:16:54 +01:00
parent ff6ab31442
commit 97b8205daf

View file

@ -352,15 +352,7 @@ and value_definition runtime = function
(expression' runtime.environment runtime.memory expr) (expression' runtime.environment runtime.memory expr)
in in
{ runtime with environment = env' } { runtime with environment = env' }
<<<<<<< HEAD
(*
| RecFunctions _rf -> let env' = define_rec_functions environment _rf
in
{ runtime with environment = env' }
=======
| RecFunctions rf -> | RecFunctions rf ->
>>>>>>> ce199651276925a6154acb80f356e4f360d35a7b
(* Ici on ajoute les noms des fonctions à l'environnement (* Ici on ajoute les noms des fonctions à l'environnement
* pour qu'elles puissent s'appeller dans leur corps de fonction * pour qu'elles puissent s'appeller dans leur corps de fonction
* => Retourne l'environnement modifié *) * => Retourne l'environnement modifié *)
@ -385,13 +377,6 @@ and define_rec env rf =
rf; rf;
env' env'
(* TODO : commentaire explicatif *)
and define_rec_functions env _rf =
let environment = List.fold_left (fun env (id,_,_) ->
Environment.bind env (id.value) VUnit) env _rf (* En gros ici on bind toute les fonctions de l'env avec VUnit*)
in
*)
and expression' environment memory e = and expression' environment memory e =
expression (position e) environment memory (value e) expression (position e) environment memory (value e)
@ -402,36 +387,43 @@ and expression' environment memory e =
and E = [runtime.environment], M = [runtime.memory]. *) 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 | Literal l -> literal_expression l.value
| Variable (id, _) -> | Variable (id, _) ->
(* On cherche l'id dans l'environnement *) (* On cherche l'id dans l'environnement *)
Environment.lookup id.position id.value environment Environment.lookup id.position id.value environment
| Tagged(constructor,_,list_t) -> (* On ignore le type car on interprète *) | Tagged(constructor,_,list_t) -> (* On ignore le type car on interprète *)
(* TODO *) (* 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 *) | Record(labels,_) -> (*
(*VRecord(List.map(label.value, expression' environment _memory) list_r)*) VRecord(List.map((label_gvalue_pair environment memory) labels))*)
| Field _ -> failwith ("oui")
(* TODO *)
failwith "Students! This is your job (Field)!" | Field(expr,label,_) -> (* On ignore la liste de type *)
field_value expr label environment memory
| Tuple [] -> | Tuple [] ->
(* Cas pour le Tuple vide (* Cas pour le Tuple vide
* Un tuple vide ne contient rien (logique), donc on utilise un VUnit*) * Un tuple vide ne contient rien (logique), donc on utilise un VUnit*)
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 _ -> | Sequence(list_expr) -> (let vs = List.map (expression' environment memory) list_expr in List.hd (List.rev vs))
(* TODO *)
failwith "Students! This is your job (Sequence)!" | Define(value_def,expr) ->
| Define _ -> (*
(* TODO *) let new_runtime = environment memory value_def in
failwith "Students! This is your job (Define)!" expression' (new_runtime.environment new_runtime.memory expr)*)
| Fun (FunctionDefinition _) -> failwith("TODO")
(* TODO *)
failwith "Students! This is your job (Fun)!" | Fun (FunctionDefinition(pattern,expr)) -> VClosure(environment, pattern, expr)
| Apply (f, x) -> apply_expression f x environment memory | Apply (f, x) -> apply_expression f x environment memory
| Ref ref -> | Ref ref ->
let dref = expression' environment memory ref in let dref = expression' environment memory ref in
VLocation (Memory.allocate memory Mint.one dref) VLocation (Memory.allocate memory Mint.one dref)
| Assign _ -> | Assign _ ->
(* TODO *) (* TODO *)
failwith "Students! This is your job (Assign)!" failwith "Students! This is your job (Assign)!"
@ -440,12 +432,10 @@ and expression _pos environment memory = function
(match loc with (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 *) | None -> failwith "erreur") (* TODO : faire une vrai erreur *)
| Case _ -> | Case(expr,branch) -> (*case_value expr branch environment memory*)
(* TODO *) (* TODO *)
failwith "Students! This is your job (Case)!" failwith "Students! This is your job (Case)!"
| IfThenElse _ -> | IfThenElse(expr1,expr2,expr3) -> if_then_else_value expr1 expr2 expr3 environment memory
(* TODO *)
failwith "Students! This is your job (IfThenElse)!"
| While _ -> | While _ ->
(* TODO *) (* TODO *)
failwith "Students! This is your job (While)!" failwith "Students! This is your job (While)!"
@ -456,13 +446,25 @@ and expression _pos environment memory = function
(* On ignore le type car on interprète *) (* On ignore le type car on interprète *)
VUnit VUnit
and apply_values a b env mem = (* TODO a finir (à commencer plutôt)
let nom_indetermine = expression' env mem b in and case_value expr branch environment memory =
match expression' env mem a with let v = expression' environment memory expr in *)
(*| VPrimitive(_,f) -> f mem nom_indetermine*)
(*TODO A FINIR *)
| VClosure(env',pattern,expr') -> failwith "Students ! The project is so boring !"
and if_then_else_value expr1 expr2 expr3 environment memory =
let cond = expression' environment memory expr1 in (* On récupère la valeur de la condition *)
if value_as_bool cond = true then
expression' environment memory expr2 else (* SI c'est true, alors on évalue la première expression*)
expression' environment memory expr3 (* sinon la deuxième *)
and field_value expr label environment memory =
match expression' environment memory expr with
| VRecord record -> List.assoc label.value record
| _ -> assert false (* Cas où il n'y a pas de record, donc c'est une erreur*)
and label_gvalue_pair environment memory (label, expr) =
(Position.value label, expression' environment memory expr)