Source

Caml / OcamlMiniProj / _build / main.ml

open Ast

let print_params f l =
    let rec print_list_rec li = match li with
        | [] -> Printf.printf ")"
        | e::l -> f e; print_list_rec l
    in 
        Printf.printf "("; print_list_rec l

let rec print_expr = function
    | Int a -> Printf.printf "%i" a
    | Var s -> Printf.printf "%s" s
    | BinOp (a,s,b) -> print_expr a; Printf.printf " %s " s; print_expr b
    | UniOp (s,a) -> Printf.printf "%s" s; print_expr a
    | Call (s,l) -> Printf.printf "%s" s; print_params print_expr l

let rec print_statement_list f = function
    | [] -> ()
    | e::l -> f e; print_statement_list f l

let rec print_statement = function
    | Assign (s,expr) -> Printf.printf "%s = " s; print_expr expr;
                         print_endline ";"
    | Expr expr -> print_expr expr; print_endline ";"
    | If (expr,l1,[]) -> Printf.printf "if (";
                         print_expr expr; print_endline ")";
                         print_endline "{";
                         print_statement_list print_statement l1;
                         print_endline "}";
    | If (expr,l1,l2) -> Printf.printf "if (";
                         print_expr expr; print_endline ")";
                         print_endline "{";
                         print_statement_list print_statement l1;
                         print_endline "}";
                         print_endline "else";
                         print_endline "{";
                         print_statement_list print_statement l2;
                         print_endline "}"
    | While (expr,l) -> Printf.printf "while (";
                        print_expr expr; print_endline ")";
                        print_endline "{";
                        print_statement_list print_statement l;
                        print_endline "}"
    | Return expr -> Printf.printf "return "; print_expr expr; print_endline ";"

let rec print_fbody = function
    | [] -> ()
    | e::l -> print_statement e; print_fbody l

let print_fparams l =
    let rec print_list_rec li = match li with
        | [] -> print_endline ")"
        | e::[] -> Printf.printf "%s" e; print_endline ")"
        | e::l -> Printf.printf "%s," e; print_list_rec l
    in 
        Printf.printf "("; print_list_rec l

let print_fvars l =
    let rec print_list_rec li = match li with
        | [] -> print_endline ";"
        | e::[] -> Printf.printf "%s" e; print_endline ";"
        | e::l -> Printf.printf "%s," e; print_list_rec l
    in 
        Printf.printf "vars "; print_list_rec l

let rec print_func = function
    | [] -> ()
    | e::l -> Printf.printf "%s" e.fname;
              print_fparams e.fparams;
              print_endline "{";
              print_fvars e.fvars;
              print_fbody e.fbody;
              print_endline "}";
              print_newline ();
              print_func l

let print_main a =
    begin
        print_endline "main()";
        print_endline "{";
        print_fvars a.mainvars;
        print_fbody a.mainbody;
        print_endline "}";
    end

let printer a =
    begin
        print_func a.func;
        print_main a.main;
    end

let main () =
  begin
    if Array.length (Sys.argv) < 2 then
        failwith "Il manque le nom du fichier!";
    let cin = open_in Sys.argv.(1) in
    let ast =
      Parser.prg Lexer.token (Lexing.from_channel cin)
    in
      printer ast;
      exit 0;
 end

let _ = main ()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.