run.ml 2.24 KB
Newer Older
1
2
open Ident

3
let () = State.close ();;
4

5
6
let dump = ref None
let src  = ref []
7
let args = ref []
8

9
let specs =
10
  [ "-dump", Arg.String (fun s -> dump := Some s), 
11
          " specify filename for persistency";
12
    "-quiet", Arg.Set Cduce.quiet, 
13
14
15
16
17
18
19
           "suppress normal output (typing, results)";
    "-v", Arg.Unit (fun () -> 
		      Printf.eprintf "CDuce, version %s\n" 
		      Cduce.version; exit 0),
       "    print CDuce version";
    "--", Arg.Rest (fun s -> args := s :: !args),
       "    the following argument are passed to the CDuce program (in argv)";
20
 ]
21

22
23
let () =
  Arg.parse specs (fun s -> src := s :: !src) 
24
    "cduce [options] [CDuce files] -- [arguments for the CDuce program]\n\nOptions:"
25

26
let ppf = 
27
  if !Cduce.quiet then Format.formatter_of_buffer (Buffer.create 1023)
28
29
  else Format.std_formatter
let ppf_err = Format.err_formatter
30
31
32
33

let do_file s =
  let (src, chan) = 
    if s = "" then (`Stream, stdin) else (`File s, open_in s) in
34
  Location.push_source src;
35
  let input = Stream.of_channel chan in
36
37
38
39
40
41
42
43
  if Stream.peek input = Some '#' then
    (
      let rec count n =
	match Stream.next input with
	  | '\n' -> n
	  | _ -> count (n + 1) in
      Wlexer.set_delta_loc (count 1)
    );
44
  let ok = Cduce.run ppf ppf_err input in
45
  if s <> "" then close_in chan;
46
  if not ok then (Format.fprintf ppf_err "@."; exit 1)
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

  

let main () =
  (match !dump with
     | Some f ->
	 (try 
	    Format.fprintf ppf "Restoring state: ";
	    let chan = open_in_bin f in
	    let s = Marshal.from_channel chan in
	    close_in chan;
	    State.set s;
	    Format.fprintf ppf "done ...@."
	  with Sys_error _ ->  
	    Format.fprintf ppf "failed ...@.")
62
63
64
65
     | None -> 
	 let l = List.rev_map Value.string_latin1 !args in
	 let l = Value.sequence l in
	 let t = Sequence.star Sequence.string in
66
	 Cduce.enter_global_value (ident (U.mk "argv")) l t
67
  );
68
69
  (match !src with
     | [] -> 
70
71
72
	 Format.fprintf ppf 
	   "CDuce %s\nNo script specified; using stdin ...@."
	   Cduce.version;
73
74
75
76
77
78
79
80
81
82
83
84
85
	 do_file ""
     | l -> List.iter do_file l);
  (match !dump with
     | Some f ->
	 Format.fprintf ppf "Saving state ...@\n";
	 let s = State.get () in
	 let chan = open_out_bin f in
	 Marshal.to_channel chan s [ Marshal.Closures ];
	 close_out chan
     | None -> ())



86
87
let () = main ()