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

Creusage de tête question 10

parent 9074d0ce
No related branches found
No related tags found
No related merge requests found
(5 / 1) + (8 * -9)
(fun x -> (fun y -> x - y)3 ) 2
(executables
(names main compiler)
(libraries utils basicExpr))
(libraries utils basicExpr funExpr))
......@@ -12,5 +12,5 @@ let rec eval env = function
| _, v -> (BinOp.eval op) (eval env e1) v
end
| Uminus e -> - (eval env e)
| App(_, _) -> failwith "Not yet supported" (* TODO *)
| Fun(_, _) -> failwith "Not yet supported"
| App(Fun(v, t), arg) -> eval ((v, (eval env arg))::env) t
| _ -> failwith "Unsupported operation"
open Ast
open BasicPfx.Ast
(* Question 5.2 *)
let increment env = List.map (fun (name, v) -> (name, v+1)) env
let rec generate = function
let rec generate env = function
| Const x -> [Push x]
| Binop(Badd,a,b) -> (generate b) @ (generate a) @ [Add]
| Binop(Bsub,a,b) -> (generate b) @ (generate a) @ [Sub]
| Binop(Bmul,a,b) -> (generate b) @ (generate a) @ [Mul]
| Binop(Bdiv,a,b) -> (generate b) @ (generate a) @ [Div]
| Binop(Bmod,a,b) -> (generate b) @ (generate a) @ [Rem]
| App(_, _) -> failwith "Not yet supported" (* TODO *)
| Fun(_, _) -> failwith "Not yet supported"
| Uminus x -> (generate x) @ [Push 0] @ [Sub]
| Var _ -> failwith "Not yet supported"
| Binop(Badd,a,b) -> (generate env b) @ (generate env a) @ [Add]
| Binop(Bsub,a,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) -> (generate env arg) @ generate ((v, 1)::env) t
| Uminus x -> (generate env x) @ [Push 0] @ [Sub]
| Var x -> [Push (List.assoc x env); Get]
(* 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) *)
\ No newline at end of file
(* Entry point of the program, should contain your main function: here it is
named parse_eval, it is the function provided after question 6.1 *)
open BasicExpr
open FunExpr
open Utils
(* The main function *)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment