Guest User

Untitled

a guest
Dec 15th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. open Cmdliner
  2.  
  3. module type Config_t =
  4. sig
  5. type opt_level = O1 | O2 | O3
  6.  
  7. val int_of_opt : opt_level -> int
  8.  
  9. type config =
  10. { line_width : int
  11. ; colors : bool
  12. ; typecheck_only : bool
  13. ; opt_level : opt_level }
  14.  
  15. val initialize : config -> bool
  16.  
  17. exception Uninitialized
  18. val line_width : unit -> int
  19. val colors : unit -> bool
  20. val typecheck_only : unit -> bool
  21. val opt_level : unit -> opt_level
  22. end
  23.  
  24. module Config : Config_t =
  25. struct
  26. type opt_level = O1 | O2 | O3
  27. let int_of_opt = function
  28. | O1 -> 1
  29. | O2 -> 2
  30. | O3 -> 3
  31.  
  32. type config =
  33. { line_width : int
  34. ; colors : bool
  35. ; typecheck_only : bool
  36. ; opt_level : opt_level }
  37. exception Uninitialized
  38.  
  39. let initialized = ref false
  40. let line_width_ref = ref None
  41. let colors_ref = ref None
  42. let typecheck_only_ref = ref None
  43. let opt_level_ref = ref None
  44.  
  45. let initialize {line_width; colors; typecheck_only; opt_level} =
  46. match !initialized with
  47. | false ->
  48. initialized := true;
  49. line_width_ref := Some line_width;
  50. colors_ref := Some colors;
  51. typecheck_only_ref := Some typecheck_only;
  52. opt_level_ref := Some opt_level;
  53. true
  54. | true -> false
  55.  
  56. let get r =
  57. match !r with
  58. | None -> raise Uninitialized
  59. | Some x -> x
  60.  
  61. let line_width () = get line_width_ref
  62. let colors () = get colors_ref
  63. let typecheck_only () = get typecheck_only_ref
  64. let opt_level () = get opt_level_ref
  65. end
  66.  
  67. let line_width =
  68. let doc = "Desired line width for pretty-printed terms, defaulting to 80" in
  69. Arg.(value & opt int 80 & info ["width"] ~docv:"INT" ~doc)
  70.  
  71. let colors =
  72. let doc = "If true then pretty-printing will make use of colors" in
  73. Arg.(value & flag & info ["c"; "color"] ~docv:"BOOL" ~doc)
  74.  
  75. let typecheck_only =
  76. let doc = "If true then skip the evaluation of the program" in
  77. Arg.(value & flag & info ["q"; "quick"] ~docv:"BOOL" ~doc)
  78.  
  79. let opt_conv =
  80. let open Arg in
  81. let open Result in
  82. let open Config in
  83. conv
  84. ~docv:"OPT_LEVEL"
  85. ((fun s ->
  86. match conv_parser int s with
  87. | Ok i ->
  88. (match i with
  89. | 1 -> Ok O1
  90. | 2 -> Ok O2
  91. | 3 -> Ok O3
  92. | _ -> Error (`Msg ("Invalid optimization level " ^ string_of_int i)))
  93. | Error m -> Error m),
  94. fun f -> function
  95. | O1 -> conv_printer int f 1
  96. | O2 -> conv_printer int f 2
  97. | O3 -> conv_printer int f 3)
  98.  
  99. let opt_level =
  100. let doc = "The level of optimization to be applied to the program" in
  101. Arg.(value & opt opt_conv O1 & info ["O"; "opt"] ~docv:"{1, 2, 3}" ~doc)
  102.  
  103. let xc_info : Term.info =
  104. let doc = "A compiler for X" in
  105. let man = [`S Manpage.s_bugs] in
  106. Term.info "xc" ~version:"0.1" ~doc ~exits:Term.default_exits ~man
  107.  
  108. let main () =
  109. Printf.printf "Line width: %d\n" (Config.line_width ());
  110. Printf.printf "Colors: %b\n" (Config.colors ());
  111. Printf.printf "Quick: %b\n" (Config.typecheck_only ());
  112. Printf.printf "Optimization: %d\n" Config.(opt_level () |> int_of_opt)
  113.  
  114. let () =
  115. let go line_width colors typecheck_only opt_level =
  116. assert (Config.initialize {line_width; colors; typecheck_only; opt_level});
  117. main () in
  118. let t = Term.(const go $ line_width $ colors $ typecheck_only $ opt_level) in
  119. Term.exit @@ Term.(eval (t, xc_info))
Add Comment
Please, Sign In to add comment