Skip to content
Snippets Groups Projects
Commit 93aa43fe authored by Augustin Jaujay's avatar Augustin Jaujay
Browse files

Question 10.3 complète

parent 9bb44698
No related branches found
No related tags found
No related merge requests found
(fun x -> (fun y -> x - y) 3 ) 2
((fun x -> x + 1) 7)
......@@ -7,46 +7,12 @@ let add_var var env = env := (var, -1)::(!env) ; print_endline (var ^ " was adde
let rec generate env = function
| Const x -> print_endline ("Argument is being pushed");increment env; [Push x]
| Binop(Badd,a,b) -> (generate env b) @ (generate env a) @ [Add]
| Binop(Bsub,a,b) -> print_endline ("Bsub " ^ (string_of_expr a) ^ " " ^ (string_of_expr b)); (generate env b) @ (generate env a) @ [Sub]
| Binop(Bmul,a,b) -> (generate env b) @ (generate env a) @ [Mul]
| Binop(Bdiv,a,b) -> (generate env b) @ (generate env a) @ [Div]
| Binop(Bmod,a,b) -> (generate env b) @ (generate env a) @ [Rem]
| App(Fun(v, t), arg) -> (add_var v env); (generate env arg) @ [InstructionSeq (generate env t); Exec]
| Uminus x -> increment env; (generate env x) @ [Push 0] @ [Sub]
| Binop(Badd,a,b) -> let y = (generate env b) in y @ (generate env a) @ [Add]
| Binop(Bsub,a,b) -> let y = (generate env b) in y @ (generate env a) @ [Sub]
| Binop(Bmul,a,b) -> let y = (generate env b) in y @ (generate env a) @ [Mul]
| Binop(Bdiv,a,b) -> let y = (generate env b) in y @ (generate env a) @ [Div]
| Binop(Bmod,a,b) -> let y = (generate env b) in y @ (generate env a) @ [Rem]
| App(Fun(v, t), arg) -> (add_var v env); let y = (generate env arg) in y @ [InstructionSeq (generate env t); Exec; Swap; Pop]
| Uminus x -> (generate env x) @ [Push 0] @ [Sub]
| Var x -> print_endline ("Argument " ^ x ^ " is being called") ; increment env; [Push ((List.assoc x !env) - 1); Get]
| _ -> []
\ No newline at end of file
(* Remarques du 22/03: Il faut incrémenter env à chaque Push
Pistes: disjonction de cas pour les Binop où quand on reconnait
Const / Var / Uminus, on inc un des environnements (si c'est c
/ v / u et un binop on inc le binop) *)
(* Remarques de Claire 30/03: Les fonctions increment et add_var sont correctes.
Le problème semble la récursivité: il faudrait des appels de fonctions qui attendent
que la fonction appelée ait fini de s'exécuter pour passer à la suite. Sauf que ça
semble assez compliqué de mettre en place un système sync / async ou wait pour ça...
Je sais pas si l'ordre des prints est parfaitement fiable, cependant on constate
ici que c'est un peu le bordel (par exemple, seul x devrait passer à 0 lors du premier
increment, y n'est pas encore supposé avoir été ajouté).
File ./basic/tests/an_example.expr is being treated!
x was added to env with index -1
y was added to env with index -1
Bsub x y
Argument x is being called
y is now associated to index 0
x is now associated to index 0
Argument y is being called
y is now associated to index 1
x is now associated to index 1
Argument is being pushed
y is now associated to index 2
x is now associated to index 2
Argument is being pushed
y is now associated to index 3
x is now associated to index 3
0 args: Push 2 [Push 3 [Push 0 Get Push -1 Get Sub] Exec] Exec
Raised error Cannot get negative index in state executing Get with stack -1332 *)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment