pga.ml 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. open Types
  2. open Stringify
  3. let main () =
  4. let usage status =
  5. prerr_endline ("usage: " ^ Sys.argv.(0) ^ " command [args]");
  6. prerr_endline "command:";
  7. prerr_endline " help show this help page";
  8. prerr_endline " echo TERM pretty-print a program";
  9. prerr_endline " utf8 TERM print a program in UTF-8 format";
  10. prerr_endline " latex TERM print latex source for a program";
  11. prerr_endline " norm TERM get the norm of a program";
  12. prerr_endline " i I TERM get the Ith instruction of a program";
  13. prerr_endline " canon1 TERM transform to first canonical form";
  14. prerr_endline " canon2 TERM transform to second canonical form";
  15. prerr_endline " eq TERM TERM check for instruction-row equivalence";
  16. prerr_endline " dot TERM generate Dot code for a flow graph";
  17. prerr_endline "input program syntax:";
  18. prerr_endline " - write star (*) instead of omega sign";
  19. prerr_endline " - write dollar sign ($) instead of pound sign";
  20. prerr_endline "";
  21. prerr_endline "A single TERM argument may also be omitted and passed on";
  22. prerr_endline "stdin instead for convenient use of UNIX pipes, e.g.:";
  23. prerr_endline "$ ./pga canon1 '(a)*;!' | ./pga dot | dot -T png | display";
  24. exit status
  25. in
  26. let argc = Array.length Sys.argv in
  27. if argc = 1 then usage 1;
  28. let input_term i =
  29. let lexbuf =
  30. if argc > i
  31. then Lexing.from_string Sys.argv.(i)
  32. else Lexing.from_channel stdin
  33. in
  34. Parse.parse_with_error lexbuf
  35. in
  36. begin
  37. try
  38. match Sys.argv.(1) with
  39. | "help" ->
  40. usage 0
  41. | "echo" ->
  42. print_endline (string_of_program_ascii (input_term 2))
  43. | "utf8" ->
  44. print_endline (string_of_program_utf8 (input_term 2))
  45. | "latex" ->
  46. print_endline (string_of_program_latex (input_term 2))
  47. | "norm" ->
  48. print_endline (string_of_natural (Congruence.norm (input_term 2)))
  49. | "i" | "canon1" | "canon2" | "eq" | "dot" ->
  50. raise (Failure "not implemented")
  51. | _ ->
  52. usage 1
  53. with
  54. | Fatal_error msg ->
  55. prerr_endline msg;
  56. exit 1
  57. | Ins_error (i, msg) ->
  58. prerr_endline ("error on " ^ string_of_ins_ascii i ^ ": " ^ msg);
  59. exit 1
  60. | Program_error (p, msg) ->
  61. prerr_endline ("error on " ^ string_of_program_ascii p ^ ": " ^ msg);
  62. exit 1
  63. end;
  64. exit 0
  65. let () = main ()