main.ml 2.0 KB

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