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)
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é *)
@ -385,13 +377,6 @@ and define_rec env rf =
rf;
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 =
expression (position e) environment memory (value e)
@ -402,36 +387,43 @@ and expression' environment memory e =
and E = [runtime.environment], M = [runtime.memory]. *)
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)
| Record(label,list_r) -> failwith ("Todo corriger l'erreur de type")
(*TODO : réparer *)
(*VRecord(List.map(label.value, expression' environment _memory) list_r)*)
| Field _ ->
(* TODO *)
failwith "Students! This is your job (Field)!"
| Record(labels,_) -> (*
VRecord(List.map((label_gvalue_pair environment memory) labels))*)
failwith ("oui")
| Field(expr,label,_) -> (* On ignore la liste de type *)
field_value expr label environment memory
| Tuple [] ->
(* 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)
| Sequence _ ->
(* TODO *)
failwith "Students! This is your job (Sequence)!"
| Define _ ->
(* TODO *)
failwith "Students! This is your job (Define)!"
| Fun (FunctionDefinition _) ->
(* TODO *)
failwith "Students! This is your job (Fun)!"
| Sequence(list_expr) -> (let vs = List.map (expression' environment memory) list_expr in List.hd (List.rev vs))
| Define(value_def,expr) ->
(*
let new_runtime = environment memory value_def in
expression' (new_runtime.environment new_runtime.memory expr)*)
failwith("TODO")
| Fun (FunctionDefinition(pattern,expr)) -> VClosure(environment, pattern, expr)
| 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)
| Assign _ ->
(* TODO *)
failwith "Students! This is your job (Assign)!"
@ -440,12 +432,10 @@ and expression _pos environment memory = function
(match loc with
| 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 _ ->
| Case(expr,branch) -> (*case_value expr branch environment memory*)
(* TODO *)
failwith "Students! This is your job (Case)!"
| IfThenElse _ ->
(* TODO *)
failwith "Students! This is your job (IfThenElse)!"
| IfThenElse(expr1,expr2,expr3) -> if_then_else_value expr1 expr2 expr3 environment memory
| While _ ->
(* TODO *)
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 *)
VUnit
and apply_values a b env mem =
let nom_indetermine = expression' env mem b in
match expression' env mem a with
(*| VPrimitive(_,f) -> f mem nom_indetermine*)
(*TODO A FINIR *)
| VClosure(env',pattern,expr') -> failwith "Students ! The project is so boring !"
(* TODO a finir (à commencer plutôt)
and case_value expr branch environment memory =
let v = expression' environment memory expr in *)
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)