run.ml 5.57 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
20
21
22
23
24
25
  exit 0

let license () =
  Printf.eprintf "\n\
The CDuce interpreter is distributed under the terms of the Q Public \n\
License version 1.0 (included in the sources). The Choice of Law section\n\
been modified from the original Q Public.\n\n
"; 
26
27
  exit 0

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

62
let ppf = Format.std_formatter
63
64
let ppf_err = Format.err_formatter

65
66
67
68
let err s =
  prerr_endline s;
  exit 1

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

101
102
103

let bol = ref true

104
105
106
let outflush s =
  output_string stdout s;
  flush stdout
107
108

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

147
148
149
150
let argv args = 
  Value.sequence (List.rev_map Value.string_latin1 args)

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