main.ml 2.0 KB

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