34/72, ajout assign/while/define et correction Record

This commit is contained in:
Nicolas PENELOUX 2023-11-07 20:32:03 +01:00
parent 97b8205daf
commit 095b120963

View file

@ -385,7 +385,7 @@ and expression' environment memory e =
E, M e v, M' E, M e v, M'
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, _) ->
@ -393,28 +393,27 @@ and expression _pos environment memory = function
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 *)
VTagged(constructor.value, List.map(expression' environment memory) list_t) VTagged(constructor.value, List.map(expression' environment memory) list_t)
| Record(labels,_) -> (* | Record(labels,_) ->
VRecord(List.map((label_gvalue_pair environment memory) labels))*) VRecord(List.map((pair_labels_gvalue) (environment) (memory)) labels)
failwith ("oui")
| Field(expr,label,_) -> (* On ignore la liste de type *) | Field(expr,label,_) -> field_value expr label environment memory
field_value expr label environment memory (* On ignore la liste de type *)
| 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 : on evalue chaque expression, puis on récupère la dernière à avoir été évalué.
Le dernier élément se trouve facilement en faisant un reverse de liste et en récupérant la tête. *)
| Sequence(list_expr) -> (let vs = List.map (expression' environment memory) list_expr in List.hd (List.rev vs)) | Sequence(list_expr) -> (let vs = List.map (expression' environment memory) list_expr in List.hd (List.rev vs))
| Define(value_def,expr) -> | Define(value_def,expr) ->
(* let runtime = value_definition { environment; memory} value_def in
let new_runtime = environment memory value_def in expression' runtime.environment runtime.memory expr
expression' (new_runtime.environment new_runtime.memory expr)*)
failwith("TODO")
| Fun (FunctionDefinition(pattern,expr)) -> VClosure(environment, pattern, expr) | Fun (FunctionDefinition(pattern,expr)) -> VClosure(environment, pattern, expr)
@ -424,9 +423,9 @@ and expression _pos environment memory = function
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(expr1,expr2) -> (*assign_value expr1 expr2 environment memory*)
(* TODO *) failwith ("todo")
failwith "Students! This is your job (Assign)!"
| Read read -> | 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 (match loc with
@ -435,26 +434,56 @@ and expression _pos environment memory = function
| Case(expr,branch) -> (*case_value expr branch environment memory*) | 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(expr1,expr2,expr3) -> if_then_else_value expr1 expr2 expr3 environment memory | IfThenElse(expr1,expr2,expr3) -> if_then_else_value expr1 expr2 expr3 environment memory
| While _ ->
(* TODO *) | While(expr1,expr2) -> while_value expr1 expr2 environment memory pos
failwith "Students! This is your job (While)!"
| For _ -> | For _ ->
(* TODO *) (* TODO *)
failwith "Students! This is your job (For)!" failwith "Students! This is your job (For)!"
| TypeAnnotation _ ->
| TypeAnnotation _ -> VUnit
(* On ignore le type car on interprète *) (* On ignore le type car on interprète *)
VUnit
(* TODO a finir (à commencer plutôt) (* TODO a finir (à commencer plutôt)
and case_value expr branch environment memory = and case_value expr branch environment memory =
let v = expression' environment memory expr in *) let v = expression' environment memory expr in *)
(* 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
(* On regarde ensuite si*)
match value_as_location vall with
(*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))
*)
and pair_labels_gvalue environment memory (lab, expr) =
(Position.value lab, expression' environment memory expr)
and while_value expr1 expr2 environment memory pos =
let cond = expression' environment memory expr1 in (* On récupère la valeur de la condition *)
match value_as_bool cond with
| true ->
let expr' = expression' environment memory expr2 in
expression pos environment memory (While(expr1,expr2))
| false -> VUnit
and if_then_else_value expr1 expr2 expr3 environment memory = 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 *) let cond = expression' environment memory expr1 in (* On récupère la valeur de la condition *)
if value_as_bool cond = true then match value_as_bool cond with
expression' environment memory expr2 else (* SI c'est true, alors on évalue la première expression*) | true -> expression' environment memory expr2
expression' environment memory expr3 (* sinon la deuxième *) (* SI c'est true, alors on évalue la première expression*)
| false -> expression' environment memory expr3 (* sinon la deuxième *)