main.ml 2.0 KB

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