run.ml 5.76 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
let topstub = ref false
12

13
let version () =
14
15
16
  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>>;
17
18
  Printf.eprintf "Supported features: \n";
  List.iter (fun (n,d) -> Printf.eprintf "- %s: %s\n" n d) (Config.descrs ());
19
20
  exit 0

21
let specs =
22
  [ "--compile", Arg.Set compile,
23
             "compile the given CDuce file";
24
25
    "-c", Arg.Set compile,
      "       same as --compile";
26
27
28
29
    "--run", Arg.Set run,
         "    execute the given .cdo files";
    "--verbose", Arg.Set Cduce.verbose,
             "(for --compile) show types of exported values";
30
    "--obj-dir",  Arg.String (fun s -> out_dir := s :: !out_dir),
31
             "(for --compile) directory for the compiled .cdo file";
32
    "-I", Arg.String (fun s -> Librarian.obj_path := s::!Librarian.obj_path),
33
      "       add one directory to the lookup path for .cdo and .cmi files";
34
35
    "--stdin", Arg.Unit (fun () -> src := "" :: !src),
           "  read CDuce script on standard input";
36
    "--arg", Arg.Rest (fun s -> args := s :: !args),
37
38
39
40
41
42
         "    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";
43
44
    "--no", Arg.String Config.inhibit,
        "     disable a feature (cduce -v to get a list of features)";
45
46
    "--debug", Arg.Unit (fun () -> Stats.set_verbosity Stats.Summary),
           "  print profiling/debugging information";
47
    "-v", Arg.Unit version,
48
      "       print CDuce version, and list built-in optional features";
49
    "--version", Arg.Unit version,
50
             "print CDuce version, and list built-in optional features";
51
    "--mlstub", Arg.Set mlstub,
52
            " produce stub ML code from a compiled unit";
53
54
    "--topstub", Arg.Set topstub,
             "produce stub ML code for a toplevel from a primitive file";
55
 ]
56

57
let ppf = Format.std_formatter
58
59
let ppf_err = Format.err_formatter

60
61
62
63
let err s =
  prerr_endline s;
  exit 1

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

99
100
101

let bol = ref true

102
103
104
let outflush s =
  output_string stdout s;
  flush stdout
105
106

let toploop () =
107
108
109
110
111
112
  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
113
    with Unix.Unix_error (_,_,_) -> 
114
      fun () -> ()
115
116
117
118
119
120
  in
  let quit () = 
    outflush "\n";
    restore ();
    exit 0
  in
121
  Format.fprintf ppf "        CDuce version %s\n@." <:symbol<cduce_version>>;
122
123
124
  Sys.set_signal Sys.sigquit (Sys.Signal_handle (fun _ -> quit ()));
  Sys.catch_break true;
  Cduce.toplevel := true;
125
  Librarian.run_loaded := true;
126
127
128
  let buf_in = Buffer.create 1024 in
  Location.push_source (`Buffer buf_in);
  let read _i =
129
    if !bol then 
130
      if !Ulexer.in_comment then outflush "* " else outflush "> ";
131
132
    try 
      let c = input_char stdin in
133
      Buffer.add_char buf_in c;
134
135
136
137
      bol := c = '\n';
      Some c
    with Sys.Break -> quit () 
  in
138
139
  let input = Stream.from read in
  let rec loop () =
140
141
    outflush "# ";
    bol := false;
142
    Buffer.clear buf_in;
143
144
    ignore (Cduce.topinput ppf ppf_err input);
    while (input_char stdin != '\n') do () done;
145
    loop () in
146
147
  (try loop () with End_of_file -> ());
  restore ()
148

149
150
151
152
let argv args = 
  Value.sequence (List.rev_map Value.string_latin1 args)

let main () = 
153
  at_exit (fun () -> Stats.dump Format.std_formatter);
154
  Location.set_viewport (Html.create false);
155
  match mode () with
156
    | `Toplevel args ->
157
	Config.init_all ();
158
	Builtin.argv := argv args;
159
	toploop ()
160
    | `Script (f,args) ->
161
	Config.init_all ();
162
163
	Builtin.argv := argv args;
	Cduce.compile_run f
164
    | `Compile (f,o) ->
165
	Config.init_all ();
166
	Cduce.compile f o
167
    | `Run (f,args) ->
168
	Config.init_all ();
169
170
	Builtin.argv := argv args;
	Cduce.run f
171
172
173
    | `Mlstub f ->
	Config.init_all ();
	Librarian.prepare_stub f
174
175
176
    | `Topstub f ->
	Config.init_all ();
	!Librarian.make_wrapper f