main.ml 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. (*
  21. Print.phase;
  22. Typecheck.phase;
  23. Extern_vars.phase;
  24. Dim_reduce.phase;
  25. Bool_op.phase;
  26. Assemble.phase;
  27. Peephole.phase;
  28. Print.phase;
  29. *)
  30. ]
  31. (* Main function, returns exit status
  32. * () -> int *)
  33. let main () =
  34. let args = {
  35. infile = None;
  36. outfile = None;
  37. verbose = 2;
  38. cpp = true;
  39. } in
  40. let args_spec = [
  41. ("-o", Arg.String (fun s -> args.outfile <- Some s),
  42. "Output file (defaults to foo.s for foo.cvc)");
  43. ("-v", Arg.Int (fun i -> args.verbose <- i),
  44. "Set verbosity (0|1|2)");
  45. ("-nocpp", Arg.Unit (fun i -> args.cpp <- false),
  46. "Disable C preprocessor");
  47. ] in
  48. let usage = "Usage: " ^ Sys.argv.(0) ^ " [-o <file>] [-nocpp] [-v <verbosity>] [<file>]" in
  49. try
  50. try
  51. Arg.parse args_spec (fun s -> args.infile <- Some s) usage;
  52. compile args;
  53. 0
  54. with
  55. | InvalidNode ->
  56. raise (CompileError "invalid node")
  57. | InvalidInput name ->
  58. raise (CompileError ("invalid input for phase \"" ^ name ^ "\""))
  59. | NodeError (node, msg) ->
  60. raise (LocError (Util.locof node, msg))
  61. with
  62. | CompileError msg ->
  63. eprintf "Error: %s\n" msg;
  64. 1
  65. | LocError (loc, msg) ->
  66. prerr_loc_msg loc ("Error: " ^ msg) args.verbose;
  67. 1
  68. | EmptyError ->
  69. 1
  70. let _ = exit (main ())