main.ml 2.0 KB

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