Ajout de for ? je sais plus honnêtement le projet me casse la tête
This commit is contained in:
parent
095b120963
commit
67434aab60
1 changed files with 68 additions and 14 deletions
|
@ -439,33 +439,71 @@ and expression pos environment memory = function
|
|||
|
||||
| While(expr1,expr2) -> while_value expr1 expr2 environment memory pos
|
||||
|
||||
| For _ ->
|
||||
(* TODO *)
|
||||
failwith "Students! This is your job (For)!"
|
||||
| For(id,expr1,expr2,expr3) -> for_value id expr1 expr2 expr3 environment memory
|
||||
|
||||
|
||||
| TypeAnnotation _ -> VUnit
|
||||
(* On ignore le type car on interprète *)
|
||||
|
||||
(* TODO a finir (à commencer plutôt)
|
||||
(* TODO a finir (à commencer plutôt)*)
|
||||
(*
|
||||
and case_value expr branch environment memory =
|
||||
let v = expression' environment memory expr in *)
|
||||
(*On calcule d'abord l'expression*)
|
||||
let v = expression' environment memory expr in
|
||||
match v with
|
||||
|*)
|
||||
|
||||
and for_value id expr1 expr2 expr3 environment memory =
|
||||
let borne_inf = value_as_int (expression' environment memory expr1) in
|
||||
let borne_sup = value_as_int (expression' environment memory expr2) in
|
||||
match borne_inf, borne_sup with (* On regarde que les borne_inf et borne_sup ont bien une valeur d'entier*)
|
||||
(* Si c'est le cas, alors nous sommes bien dans une boucle for et on effectue ses opérations*)
|
||||
| Some(borne_inf), Some(borne_sup) -> boucle_for id borne_inf borne_sup expr3 environment memory
|
||||
(* On appelle une seconde fonction car pour évalué la boucle for, il faudra rappeler la boucle
|
||||
en augmentant l'indice de la borne inférieur de 1 à chaque appelle. *)
|
||||
| _ -> error [position expr1; position expr2] "erreur"
|
||||
|
||||
and boucle_for id borne_inf borne_sup expr3 environment memory =
|
||||
if borne_inf <= borne_sup (*Cas où nous sommes dans la boucle*)
|
||||
then
|
||||
let env' = (* On lis l'identifier avec la borne inférieur*)
|
||||
Environment.bind environment id.value (int_as_value borne_inf) in
|
||||
let calcul = expression' environment memory expr3 in
|
||||
boucle_for id (Mint.add borne_inf Mint.one) borne_sup expr3 env' memory
|
||||
|
||||
else
|
||||
VUnit (*Cas où nous ne sommes plus dans la boucle, on renvoie un VUnit*)
|
||||
|
||||
|
||||
(* Assign *)
|
||||
(* On commence par récupérer l'évaluation de expr1, qui correspond à la valeur à laquelle est affecté expr2*)
|
||||
|
||||
(* and assign_value expr1 expr2 environment memory =
|
||||
let vall = expression' environment memory expr1 in
|
||||
|
||||
(*
|
||||
and assign_value expr1 expr2 environment memory =
|
||||
let vall = value_as_location (expression' environment memory expr1) in
|
||||
|
||||
(* On regarde ensuite si*)
|
||||
match value_as_location vall with
|
||||
match vall with
|
||||
| None -> error [position expr1; position expr2] "erreur assign"
|
||||
| Some(v) -> (*assign_calcul environment memory expr2 v*)
|
||||
|
||||
(*TODO : corriger le bug unit*)
|
||||
| None -> failwith "erreur"
|
||||
| Some(v) ->
|
||||
(let value = expression' environment memory expr2 in
|
||||
let mem = Memory.dereference(memory) (v) in
|
||||
Memory.write (mem) Mint.zero (value))
|
||||
*)
|
||||
Memory.write mem Mint.zero value)
|
||||
|
||||
(*TODO corriger le bug avec UNIT*)
|
||||
|
||||
and assign_calcul environment memory expr2 v =
|
||||
let value = expression' environment memory expr2 in
|
||||
let mem = Memory.dereference memory v in
|
||||
match mem with
|
||||
| Some(m) -> Memory.write (m) (Mint.zero) (value)
|
||||
| _ -> failwith "erreur"*)
|
||||
|
||||
|
||||
|
||||
|
||||
and pair_labels_gvalue environment memory (lab, expr) =
|
||||
(Position.value lab, expression' environment memory expr)
|
||||
|
||||
|
@ -504,8 +542,10 @@ and apply_expression f x environment memory =
|
|||
(* Fonction "primitive" *)
|
||||
f memory x_val
|
||||
| VClosure (_env_fn, _pattern, _expr) ->
|
||||
(*
|
||||
let pattern _env_fn (value pattern) x_val with
|
||||
(* Fonction
|
||||
* TODO: Pattern matching ici *)
|
||||
* TODO: Pattern matching ici *)*)
|
||||
failwith "Students! This is your job (Apply)!"
|
||||
| _ -> assert false (* By typing *)
|
||||
|
||||
|
@ -514,6 +554,20 @@ and literal_expression = function
|
|||
| LChar c -> VChar c
|
||||
| LString s -> VString s
|
||||
|
||||
(*
|
||||
and pattern environment pattern expression =
|
||||
match pattern, expression with
|
||||
| PLiteral pl, _ -> literal_pattern pl
|
||||
| PWildcard, _ -> environment
|
||||
| PTypeAnnotation(new_pattern,_), _ -> pattern environment new_pattern expression
|
||||
|
||||
and litteral_pattern pl =
|
||||
match pl with
|
||||
| LInt n -> VInt n
|
||||
| LChar c -> VChar c
|
||||
| LString s -> VString s
|
||||
*)
|
||||
|
||||
(** This function returns the difference between two runtimes. *)
|
||||
and extract_observable runtime runtime' =
|
||||
let rec substract new_environment env env' =
|
||||
|
|
Reference in a new issue