(* Top-level of the MicroC compiler: scan & parse the input,
   check the resulting AST, generate LLVM IR, and dump the module *)

(* This code was original from MicroC and modified by:
Keir Lauritzen, Leon Song, and Guy Yardeni*)


type action = Ast | Compile


let _ = 
    let action = if Array.length Sys.argv > 1 then
        List.assoc Sys.argv.(1) [ ("-a", Ast);
                                ("-c", Compile) ]
    else Compile in
    let lexbuf = Lexing.from_channel stdin in 
    let ast = Parser.program Scanner.next_token lexbuf in
    match action with 
        Ast -> print_string (Ast. string_of_program ast)
        | Compile -> 
            let sast = Semant.convert ast in
            let _ = Check.check sast in
            let m = Codegen.translate sast in
            Llvm_analysis.assert_valid_module m;
            print_string (Llvm.string_of_llmodule m);;
