Advertisement
Guest User

Untitled

a guest
Dec 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.31 KB | None | 0 0
  1. (* Top-level of the AP++ compiler: scan & parse the input,
  2.    check the resulting AST and generate an SAST from it, generate LLVM IR,
  3.    and dump the module *)
  4.  
  5. type action = Ast | Sast | LLVM_IR | Compile
  6.  
  7. let () =
  8.   let action = ref Compile in
  9.   let set_action a () = action := a in
  10.   let speclist = [
  11.     ("-a", Arg.Unit (set_action Ast), "Print the AST");
  12.     ("-s", Arg.Unit (set_action Sast), "Print the SAST");
  13.     ("-l", Arg.Unit (set_action LLVM_IR), "Print the generated LLVM IR");
  14.     ("-c", Arg.Unit (set_action Compile),
  15.       "Check and print the generated LLVM IR (default)");
  16.   ] in  
  17.   let usage_msg = "usage: ./ap++.native [-a|-s|-l|-c] [file.mc]" in
  18.   let channel = ref stdin in
  19.   Arg.parse speclist (fun filename -> channel := open_in filename) usage_msg;
  20.  
  21.   let lexbuf = Lexing.from_channel !channel in
  22.   let ast = Parser.program Scanner.token lexbuf in  
  23.   match !action with
  24.     Ast -> print_string (Ast.string_of_program ast)
  25.   | _ -> let sast = Semant.check ast in
  26.     match !action with
  27.       Ast     -> ()
  28.     | Sast    -> print_string (Sast.string_of_sprogram sast)
  29.     | LLVM_IR -> print_string (Llvm.string_of_llmodule (Codegen.translate sast))
  30.     | Compile -> let m = Codegen.translate sast in
  31.     Llvm_analysis.assert_valid_module m;
  32.     print_string (Llvm.string_of_llmodule m)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement