run.ml 5.29 KB
Newer Older
1
2
open Ident

3
let out_dir  = ref [] (* directory of the output file *)
4
let src  = ref []
5
let args = ref []
6

7
8
let compile = ref false
let run = ref false
9
let script = ref false
10
let mlstub = ref false
11

12
let version () =
13
14
15
  Printf.eprintf "CDuce, version %s\n" <:symbol<cduce_version>>;
  Printf.eprintf "built on %s\n" <:symbol<build_date>>;
  Printf.eprintf "using OCaml %s compiler\n" <:symbol<ocaml_compiler>>;
16
17
  Printf.eprintf "Supported features: \n";
  List.iter (fun (n,d) -> Printf.eprintf "- %s: %s\n" n d) (Config.descrs ());
18
19
  exit 0

20
let specs =
21
  [ "--compile", Arg.Set compile,
22
             "compile the given CDuce file";
23
24
25
26
    "--run", Arg.Set run,
         "    execute the given .cdo files";
    "--verbose", Arg.Set Cduce.verbose,
             "(for --compile) show types of exported values";
27
    "--obj-dir",  Arg.String (fun s -> out_dir := s :: !out_dir),
28
             "(for --compile) directory for the compiled .cdo file";
29
    "-I", Arg.String (fun s -> Librarian.obj_path := s::!Librarian.obj_path),
30
      "       add one directory to the lookup path for .cdo and .cmi files";
31
32
    "--stdin", Arg.Unit (fun () -> src := "" :: !src),
           "  read CDuce script on standard input";
33
    "--arg", Arg.Rest (fun s -> args := s :: !args),
34
35
36
37
38
39
         "    following arguments are passed to the CDuce program";
    "--script", Arg.Rest (fun s -> 
			    if not !script then (script := true;
						src := s :: !src)
			    else args := s :: !args),
            " the first argument after is the source, then the arguments";
40
41
    "--no", Arg.String Config.inhibit,
        "     disable a feature (cduce -v to get a list of features)";
42
43
    "--debug", Arg.Unit (fun () -> Stats.set_verbosity Stats.Summary),
           "  print profiling/debugging information";
44
    "-v", Arg.Unit version,
45
      "       print CDuce version, and list built-in optional features";
46
    "--version", Arg.Unit version,
47
             "print CDuce version, and list built-in optional features";
48
    "--mlstub", Arg.Set mlstub,
49
            " produce stub ML code from a compiled unit";
50
 ]
51

52
let ppf = Format.std_formatter
53
54
let ppf_err = Format.err_formatter

55
56
57
58
let err s =
  prerr_endline s;
  exit 1

59
let mode () =
60
  Arg.parse (specs @ !Cduce.extra_specs) (fun s -> src := s :: !src) 
61
    "Usage:\ncduce [OPTIONS ...] [FILE ...] [--arg argument ...]\n\nOptions:";
62
63
64
65
  if (!mlstub) then (
    match !src with [x] -> `Mlstub x | _ ->
      err "Please specify one .cdo file"
  ) else match (!compile,!out_dir,!run,!src,!args) with
66
67
68
69
70
    | false, _::_, _,  _, _   -> 
	err "--obj-dir option can be used only with --compile"
    | false, [], false, [],  args   -> `Toplevel args
    | false, [], false, [x], args   -> `Script (x,args)
    | false, [], false, _, _        ->
71
	err "Only one CDuce program can be executed at a time"
72
73
    | true,  [o], false, [x], []     -> `Compile (x,Some o) 
    | true,  [], false, [x], []     -> `Compile (x,None) 
74
    | true,  [], false, [], []      ->
75
	err "Please specify the CDuce program to be compiled"
76
    | true,  [], false, _, []       ->
77
	err "Only one CDuce program can be compiled at a time"
78
    | true,  _, false, _, []        ->
79
	err "Please specify only one output directory"
80
    | true,  _, false, _, _        ->
81
	err "No argument can be passed to programs at compile time"
82
83
    | false, _, true,  [x], args   -> `Run (x,args)
    | false, _, true,  [], _       ->
84
	err "Please specifiy the CDuce program to be executed"
85
    | false, _, true,   _, _       ->
86
	err "Only one CDuce program can be executed at a time"
87
    | true, _, true,   _,  _       ->
88
89
	err "The options --compile and --run are incompatible"
	
90

91
92
93

let bol = ref true

94
95
96
let outflush s =
  output_string stdout s;
  flush stdout
97
98

let toploop () =
99
100
101
102
103
104
  let restore = 
    try 
      let tcio = Unix.tcgetattr Unix.stdin in
      Unix.tcsetattr 
	Unix.stdin Unix.TCSADRAIN { tcio with Unix.c_vquit = '\004' };
      fun () -> Unix.tcsetattr Unix.stdin Unix.TCSADRAIN tcio
105
    with Unix.Unix_error (_,_,_) -> 
106
      fun () -> ()
107
108
109
110
111
112
  in
  let quit () = 
    outflush "\n";
    restore ();
    exit 0
  in
113
  Format.fprintf ppf "        CDuce version %s\n@." <:symbol<cduce_version>>;
114
115
116
  Sys.set_signal Sys.sigquit (Sys.Signal_handle (fun _ -> quit ()));
  Sys.catch_break true;
  Cduce.toplevel := true;
117
  Librarian.run_loaded := true;
118
  Location.push_source `Stream;
119
  let read i =
120
    if !bol then 
121
      if !Ulexer.in_comment then outflush "* " else outflush "> ";
122
123
124
125
126
127
    try 
      let c = input_char stdin in
      bol := c = '\n';
      Some c
    with Sys.Break -> quit () 
  in
128
129
  let input = Stream.from read in
  let rec loop () =
130
131
132
133
    outflush "# ";
    bol := false;
    ignore (Cduce.topinput ppf ppf_err input);
    while (input_char stdin != '\n') do () done;
134
    loop () in
135
136
  (try loop () with End_of_file -> ());
  restore ()
137

138
139
140
141
let argv args = 
  Value.sequence (List.rev_map Value.string_latin1 args)

let main () = 
142
  at_exit (fun () -> Stats.dump Format.std_formatter);
143
  Location.set_viewport (Html.create false);
144
  match mode () with
145
    | `Toplevel args ->
146
	Config.init_all ();
147
	Builtin.argv := argv args;
148
	toploop ()
149
    | `Script (f,args) ->
150
	Config.init_all ();
151
152
	Builtin.argv := argv args;
	Cduce.compile_run f
153
    | `Compile (f,o) ->
154
	Config.init_all ();
155
	Cduce.compile f o
156
    | `Run (f,args) ->
157
	Config.init_all ();
158
159
	Builtin.argv := argv args;
	Cduce.run f
160
161
162
    | `Mlstub f ->
	Config.init_all ();
	Librarian.prepare_stub f