run.ml 2.17 KB
Newer Older
1
let () = State.close ();;
2

3
4
let dump = ref None
let src  = ref []
5
let args = ref []
6

7
let specs =
8
  [ "-dump", Arg.String (fun s -> dump := Some s), 
9
          " specify filename for persistency";
10
    "-quiet", Arg.Set Cduce.quiet, 
11
12
13
14
15
16
17
           "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)";
18
 ]
19

20
21
22
23
let () =
  Arg.parse specs (fun s -> src := s :: !src) 
    "cduce [options] [script]\n\nOptions:"

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

let do_file s =
  let (src, chan) = 
    if s = "" then (`Stream, stdin) else (`File s, open_in s) in
32
  Location.push_source src;
33
  let input = Stream.of_channel chan in
34
35
36
37
38
39
40
41
  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)
    );
42
  let ok = Cduce.run ppf ppf_err input in
43
  if s <> "" then close_in chan;
44
  if not ok then (Format.fprintf ppf_err "@."; exit 1)
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

  

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 ...@.")
60
61
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
	 Cduce.enter_global_value "argv" l t
  );
66
67
  (match !src with
     | [] -> 
68
69
70
	 Format.fprintf ppf 
	   "CDuce %s\nNo script specified; using stdin ...@."
	   Cduce.version;
71
72
73
74
75
76
77
78
79
80
81
82
83
	 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 -> ())



84
85
let () = main ()