| 1 |
open Ident
|
| 2 |
|
| 3 |
let () = State.close ();;
|
| 4 |
|
| 5 |
let dump = ref None
|
| 6 |
let src = ref []
|
| 7 |
let args = ref []
|
| 8 |
|
| 9 |
let version () =
|
| 10 |
Printf.eprintf "CDuce, version %s\n" Cduce_config.version;
|
| 11 |
Printf.eprintf "built on %s\n" Cduce_config.build_date;
|
| 12 |
Printf.eprintf "using OCaml %s compiler\n"
|
| 13 |
(if Cduce_config.native then "native" else "bytecode");
|
| 14 |
exit 0
|
| 15 |
|
| 16 |
let specs =
|
| 17 |
[ "--dump", Arg.String (fun s -> dump := Some s),
|
| 18 |
" specify filename for persistency";
|
| 19 |
"--quiet", Arg.Set Cduce.quiet,
|
| 20 |
" suppress normal output (typing, results)";
|
| 21 |
"-v", Arg.Unit version,
|
| 22 |
" print CDuce version";
|
| 23 |
"--version", Arg.Unit version,
|
| 24 |
"print CDuce version";
|
| 25 |
"--license", Arg.Unit (fun () ->
|
| 26 |
Printf.eprintf "\n\
|
| 27 |
The CDuce interpreter is distributed under the terms of the Q Public \n\
|
| 28 |
License version 1.0 (included in the sources). The Choice of Law section\n\
|
| 29 |
been modified from the original Q Public.\n\n
|
| 30 |
"; exit 0),
|
| 31 |
"print CDuce license";
|
| 32 |
"--arg", Arg.Rest (fun s -> args := s :: !args),
|
| 33 |
" the arguments that follow are passed to the CDuce program (in argv)";
|
| 34 |
|
| 35 |
]
|
| 36 |
|
| 37 |
let () =
|
| 38 |
Arg.parse specs (fun s -> src := s :: !src)
|
| 39 |
"\nUsage:\ncduce [OPTIONS ...] [FILE ...] [--arg argument ...]\n\nOptions:"
|
| 40 |
|
| 41 |
let ppf =
|
| 42 |
if !Cduce.quiet then Format.formatter_of_buffer (Buffer.create 1023)
|
| 43 |
else Format.std_formatter
|
| 44 |
let ppf_err = Format.err_formatter
|
| 45 |
|
| 46 |
let do_file s =
|
| 47 |
let (src, chan) =
|
| 48 |
if s = "" then (`Stream, stdin) else (`File s, open_in s) in
|
| 49 |
Location.push_source src;
|
| 50 |
let input = Stream.of_channel chan in
|
| 51 |
if Stream.peek input = Some '#' then
|
| 52 |
(
|
| 53 |
let rec count n =
|
| 54 |
match Stream.next input with
|
| 55 |
| '\n' -> n
|
| 56 |
| _ -> count (n + 1) in
|
| 57 |
Wlexer.set_delta_loc (count 1)
|
| 58 |
);
|
| 59 |
let ok = Cduce.run ppf ppf_err input in
|
| 60 |
if s <> "" then close_in chan;
|
| 61 |
if not ok then (Format.fprintf ppf_err "@."; exit 1)
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
let main () =
|
| 66 |
(match !dump with
|
| 67 |
| Some f ->
|
| 68 |
(try
|
| 69 |
Format.fprintf ppf "Restoring state: ";
|
| 70 |
let chan = open_in_bin f in
|
| 71 |
let s = Marshal.from_channel chan in
|
| 72 |
close_in chan;
|
| 73 |
State.set s;
|
| 74 |
Format.fprintf ppf "done ...@."
|
| 75 |
with Sys_error _ ->
|
| 76 |
Format.fprintf ppf "failed ...@.")
|
| 77 |
| None ->
|
| 78 |
let l = List.rev_map Value.string_latin1 !args in
|
| 79 |
let l = Value.sequence l in
|
| 80 |
let t = Sequence.star Sequence.string in
|
| 81 |
Cduce.enter_global_value (ident (U.mk "argv")) l t
|
| 82 |
);
|
| 83 |
(match !src with
|
| 84 |
| [] ->
|
| 85 |
Format.fprintf ppf
|
| 86 |
"CDuce version %s\nNo script specified; using stdin ...@."
|
| 87 |
Cduce_config.version;
|
| 88 |
do_file ""
|
| 89 |
| l -> List.iter do_file l);
|
| 90 |
(match !dump with
|
| 91 |
| Some f ->
|
| 92 |
Format.fprintf ppf "Saving state ...@\n";
|
| 93 |
let s = State.get () in
|
| 94 |
let chan = open_out_bin f in
|
| 95 |
Marshal.to_channel chan s [ Marshal.Closures ];
|
| 96 |
close_out chan
|
| 97 |
| None -> ())
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
let () = main ()
|
| 102 |
|