main.ml 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. open Printf
  2. open Ast
  3. (* Compile infile to assembly code
  4. * in_channel -> int -> repr *)
  5. let compile infile verbose =
  6. let rec run_phases ir = function
  7. | [] -> ir
  8. | h::t -> run_phases (h ir) t
  9. in
  10. run_phases (Inputfile (infile, verbose)) [
  11. Parse.phase;
  12. Print.phase;
  13. Desug.phase;
  14. Print.phase;
  15. Context_analysis.phase;
  16. Print.phase;
  17. (*Typecheck.phase;*)
  18. (*Extern_vars.phase;*)
  19. (*Dim_reduce.phase;*)
  20. (*Bool_op.phase;*)
  21. (*Assemble.phase;*)
  22. (*Peephole.phase;*)
  23. (*Print.phase;*)
  24. ]
  25. (* Main function, returns exit status
  26. * () -> int *)
  27. let main () =
  28. let filename = ref None in
  29. let verbose = ref 1 in
  30. let args = [
  31. ("-v", Arg.Int (fun i -> verbose := i), "Set verbosity")
  32. ] in
  33. let usage = "Usage: " ^ Sys.argv.(0) ^ " [ -v VERBOSITY ] FILE" in
  34. try
  35. Arg.parse args (fun s -> filename := Some s) usage;
  36. let _ = compile !filename !verbose in
  37. 0
  38. with
  39. | CompileError msg ->
  40. prerr_endline ("Error: " ^ msg);
  41. -1
  42. | LocError (msg, loc) ->
  43. let (file, ystart, yend, xstart, xend) = loc in
  44. let yend = if yend = ystart then "" else "-" ^ string_of_int yend in
  45. let xend = if xend = xstart then "" else "-" ^ string_of_int xend in
  46. eprintf "Error: %s at %s:%d%s:%d%s" msg file ystart yend xstart xend;
  47. -1
  48. let _ = exit (main ())