#open "syntax";; #open "printf";; let rec eval_int e rho = match eval e rho with Constante c -> c | _ -> failwith "Pas une constante" and eval e rho = match e with Entier i -> Constante i | Variable n -> assoc n rho | Plus (e,e') -> Constante ((eval_int e rho) + (eval_int e' rho)) | Minus (e,e') -> Constante ((eval_int e rho) - (eval_int e' rho)) | Times (e,e') -> Constante ((eval_int e rho) * (eval_int e' rho)) | Div (e,e') -> Constante ((eval_int e rho) / (eval_int e' rho)) | Uminus e -> Constante (- (eval_int e rho)) | Ifz (e, e', e'') -> if (eval_int e rho) = 0 then (eval e' rho) else (eval e'' rho) | Let (x,e,e') -> eval e' ((x,eval e rho) :: rho) | Letrec (x,e,e') -> begin match e with Fun (body,rho') -> let rec rho_rec = (x, Closure(body, rho', rho_rec)) :: rho in eval e' rho_rec | _ -> failwith "On ne peut définir récursivement que des fonctions" end | Fun (x,e) -> Closure (x,e,rho) | Application (e,e') -> begin match eval e rho with Closure (x,e'',rho'') -> eval e'' ((x, eval e' rho) :: rho'') | _ -> failwith "On ne peut appliquer qu'une fonction" end;; let main() = begin try let lexbuf = lexing__create_lexer_channel std_in in while true do try printf "%d\n" (eval_int (parser__Main lexer__Token lexbuf) []) with Not_found -> print_endline "Variable ou fonction non définie" | Parse_error -> print_endline "Erreur de syntaxe" done with End_of_file -> () end; flush stdout ;; printexc__f main ();;