b0VIM 5.79 G awwaiidphlurm/par2/web/~awwaiid/projects/ocaml_learn/basic.ml3210#"! Utprr>oad<`poPOA8.%  } [ * )   ~ z k T N M / .  } p ` _ D 0  { j Y F 3   ~ [ Z F E *  ^D']-VB21@jiN:$vGx<in cl.courant <- res ; String.sub cl.chaine ct (res-ct) ;;let res = ext ct let rec ext n = if n "LET " ^ v ^ " = " ^ (pp_expression e) ;;| If (e,n) -> "IF "^(pp_expression e)^" THEN "^(string_of_int n)| Input v -> "INPUT " ^ v| Print e -> "PRINT " ^ (pp_expression e)| Goto n -> "GOTO " ^ (string_of_int n)Rem s -> "REM " ^ slet pp_instruction = function in ppg 0 ;; | _ -> ppg pr exp in if pr2 > pr then res else parenthese res in let res = (ppg pr2 e1)^(pp_opbin op)^(ppd pr2 e2) let pr2 = priority_ob op ExpBin (e1,op,e2) -> (* que pour les oprateurs binaires *) (* les sous-arbres droits ne diffrent *) and ppd pr exp = match exp with in if pr2 >= pr then res else parenthese res (* parenthse si la priorit n'est pas suprieure *) in let res = (ppg pr2 e1)^(pp_opbin op)^(ppd pr2 e2) let pr2 = priority_ob op | ExpBin (e1,op,e2) -> in if pr=0 then res else parenthese res let res = (pp_opunr op)^(ppg (priority_ou op) e) | ExpUnr (op,e) -> | ExpStr s -> "\"" ^ s ^ "\"" | ExpVar v -> v ExpInt n -> (string_of_int n)let rec ppg pr = function let pp_expression =let parenthese x = "(" ^ x ^ ")";; | NON -> "!";; OPPOSE -> "-"let pp_opunr = function | OU -> " | " | ET -> " & " | DIFF -> " <> " | SUPEQ -> " >= " | SUP -> " > " | INFEQ -> " <= " | INF -> " < " | EGAL -> " = " | DIV -> "/" | MOINS -> "-" | MOD -> "%" | MULT -> "*" PLUS -> "+"let pp_opbin = function | ET | OU -> 2;; | EGAL | INF | INFEQ | SUP | SUPEQ | DIFF -> 3 | MOD -> 4 | PLUS | MOINS -> 5 MULT | DIV -> 6let priority_ob = function | OPPOSE -> 7 NON -> 1let priority_ou = function(***************************************************************************) | End;; | Run | List ligne Ligne oftype phrase =type program = ligne list ;; };; inst : instruction num : int; {type ligne = | Let of string * expression;; | If of expression * int | Input of string | Print of expression | Goto of int Rem of stringtype instruction = | ExpBin of expression * op_bin * expression;; | ExpUnr of op_unr * expression | ExpStr of string | ExpVar of string ExpInt of inttype expression = | OU;; | ET | DIFF | SUPEQ | SUP | INFEQ | INF | EGAL | MOD | DIV | MULT | MOINS PLUStype op_bin =type op_unr = OPPOSE | NON ;;(*****************************************************************************)(***** Application : valuateur BASIC *****)(***** *****)(***** Devellopement d'applications avec Objective Caml *****)(*****************************************************************************)ad >sLVUT+ A .  u p K  b O I  ~ v u h c 8  go () ;;with Fin -> print_string "A bientt...\n";;loop { prog = []; env = [] }let rec loop etat = loop (une_commande etat) in print_string "Mini-BASIC version 0.1\n\n";try let go () = etat ;;print_string "\n"; print_int n ;print_string "runtime error at line ";| RunErreur n -> | ParseErreur -> print_string "syntax error\n"; etat LexerErreur -> print_string "Illegal character\n"; etat with | End -> raise Fin{etat with env = xetat.xenv }let xetat = run { ligne = 0; xprog = tprog; xenv = etat.env } in-> let tprog = assemble etat.prog in| Run | List -> (print_prog etat.prog ; etat )Ligne l -> { etat with prog = inserer l etat.prog }match parse (input_line stdin) with try print_string "> " ; flush stdout ;let une_commande etat =exception Fin ;;type etat_boucle = { prog:program; env:environnement } ;;print_newline () ;;List.iter print_ligne prog ;print_newline () ;let print_ligne x = print_string (pp_ligne x) ; print_newline () inlet print_prog prog = else ligne::l::prog ;;else if l.num=ligne.num then ligne::progif l.num < ligne.num then l::(inserer ligne prog)| l::prog -> [] -> [ligne]let rec inserer ligne p = match p with (***************************************************************************)if etat.ligne = -1 then etat else run (eval_inst etat) ;;let rec run etat = | _ -> runerr etat.ligne ;;| Vbool false -> { etat with ligne = ligne_suivante etat }Vbool true -> { etat with ligne = n }| If (t,n) -> match eval_exp etat.ligne etat.xenv t with xenv = ajoute v (Vint x) etat.xenv }in { etat with ligne = ligne_suivante etat;