main.ml 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. open Printf
  2. open Lexing
  3. open Ast
  4. open Util
  5. (* Compile CVC file to assembly code
  6. * in_channel -> int -> repr *)
  7. let compile args =
  8. let rec run_phases input = function
  9. | [] -> ()
  10. | h::t -> run_phases (h input) t
  11. in
  12. run_phases (Args args) [
  13. Load.phase;
  14. (*Print.phase;*)
  15. Parse.phase;
  16. Print.phase;
  17. Desug.phase;
  18. Print.phase;
  19. Context_analysis.phase;
  20. Typecheck.phase;
  21. (*
  22. Extern_vars.phase;
  23. Dim_reduce.phase;
  24. Print.phase;
  25. Print.phase;
  26. Bool_op.phase;
  27. Print.phase;
  28. Assemble.phase;
  29. Print.phase;
  30. Peephole.phase;
  31. Print.phase;
  32. *)
  33. ]
  34. (* Main function, returns exit status
  35. * () -> int *)
  36. let main () =
  37. let args = {
  38. infile = None;
  39. outfile = None;
  40. verbose = 2;
  41. cpp = true;
  42. } in
  43. let args_spec = [
  44. ("-o", Arg.String (fun s -> args.outfile <- Some s),
  45. "Output file (defaults to foo.s for foo.cvc)");
  46. ("-v", Arg.Int (fun i -> args.verbose <- i),
  47. "Set verbosity (0|1|2)");
  48. ("-nocpp", Arg.Unit (fun i -> args.cpp <- false),
  49. "Disable C preprocessor");
  50. ] in
  51. let usage = "Usage: " ^ Sys.argv.(0) ^ " [-o <file>] [-nocpp] [-v <verbosity>] [<file>]" in
  52. try
  53. try
  54. Arg.parse args_spec (fun s -> args.infile <- Some s) usage;
  55. compile args;
  56. 0
  57. with
  58. (*| InvalidNode ->
  59. raise (CompileError "invalid node")*)
  60. | InvalidInput name ->
  61. raise (CompileError ("invalid input for phase \"" ^ name ^ "\""))
  62. | NodeError (node, msg) ->
  63. raise (LocError (Util.locof node, msg))
  64. with
  65. | CompileError msg ->
  66. eprintf "Error: %s\n" msg;
  67. 1
  68. | LocError (loc, msg) ->
  69. prerr_loc_msg loc ("Error: " ^ msg) args.verbose;
  70. 1
  71. | EmptyError ->
  72. 1
  73. let _ = exit (main ())