cduce.ml 11.6 KB
Newer Older
1
open Location
2
open Ident
3

4
5
ifdef ML_INTERFACE then module ML = Ml_ocaml;;

6
7
8
exception InvalidInputFilename of string
exception InvalidObjectFilename of string

9
10
(* if set to false toplevel exception aren't cought. 
 * Useful for debugging with OCAMLRUNPARAM="b" *)
11
12
let catch_exceptions = true

13
14
15
16
17
18
19
20
(* retuns a filename without the suffix suff if any *) 
let prefix filename suff =
  if Filename.check_suffix filename suff then
    try
      Filename.chop_extension filename
    with Invalid_argument filename -> failwith "Not a point in the suffix?"
  else filename

21
let toplevel = ref false
22
let verbose = ref false
23

24
let typing_env = State.ref "Cduce.typing_env" Builtin.env
25
26
let compile_env = State.ref "Cduce.compile_env" Compile.empty

27
let get_global_value cenv v =
28
  Eval.var (Compile.find v !compile_env)
29
30
31

let get_global_type v =
  Typer.find_value v !typing_env
32

33
let enter_global_value x v t =
34
  typing_env := Typer.enter_value x t !typing_env;
35
36
  compile_env := Compile.enter_global !compile_env x; 
  Eval.push v
37
  
38
39
40
41
42
let rec is_abstraction = function
  | Ast.Abstraction _ -> true
  | Ast.LocatedExpr (_,e) -> is_abstraction e
  | _ -> false

43
let print_norm ppf d = 
44
  Location.protect ppf 
45
    (fun ppf -> Types.Print.print ppf ((*Types.normalize*) d))
46

47
48
49
50
let print_sample ppf s =
  Location.protect ppf
    (fun ppf -> Sample.print ppf s)

51
52
53
let print_protect ppf s =
  Location.protect ppf (fun ppf -> Format.fprintf ppf "%s" s)

54
55
let print_value ppf v =
  Location.protect ppf (fun ppf -> Value.print ppf v)
56

57
58
59
60
let dump_value ppf x t v =
  Format.fprintf ppf "@[val %a : @[%a = %a@]@]@."
    U.print (Id.value x) print_norm t print_value v

61
62
63
let dump_env ppf tenv cenv =
  Format.fprintf ppf "Types:%a@." Typer.dump_types tenv;
  Format.fprintf ppf "Namespace prefixes:@\n%a" Typer.dump_ns tenv;
64
  Format.fprintf ppf "Namespace prefixes used for pretty-printing:@.%t"
65
    Ns.InternalPrinter.dump;
66
  Format.fprintf ppf "Schemas: %s@."
67
    (String.concat " " (List.map U.get_str (Typer.get_schema_names ())));
68
  Format.fprintf ppf "Values:@.";
69
70
  Typer.iter_values tenv
    (fun x t -> dump_value ppf x t (get_global_value cenv x))
71

72
73
74
let directive_help ppf =
  Format.fprintf ppf
"Toplevel directives:
75
76
77
78
79
80
81
82
  #quit;;                 quit the interpreter
  #env;;                  dump current environment
  #reinit_ns;;            reinitialize namespace processing
  #help;;                 shows this help message
  #dump_value <expr>;;    dump an XML-ish representation of the resulting
                          value of a given expression
  #print_schema <name>;;
  #print_type <name>;;
83
84
"

85
let rec print_exn ppf = function
86
87
  | Location (loc, w, exn) ->
      Format.fprintf ppf "Error %a:@." Location.print_loc (loc,w);
88
      Format.fprintf ppf "%a" Location.html_hilight (loc,w); 
89
      print_exn ppf exn
90
  | Value.CDuceExn v ->
91
      Format.fprintf ppf "Uncaught CDuce exception: @[%a@]@."
92
        print_value v
93
  | Typer.WrongLabel (t,l) ->
94
      Format.fprintf ppf "Wrong record selection; field %a " 
95
        Label.print (LabelPool.value l);
96
      Format.fprintf ppf "not present in an expression of type:@.%a@."
97
        print_norm t
98
  | Typer.ShouldHave (t,msg) ->
99
      Format.fprintf ppf "This expression should have type:@.%a@.%a@." 
100
        print_norm t
101
        print_protect msg
102
  | Typer.ShouldHave2 (t1,msg,t2) ->
103
      Format.fprintf ppf "This expression should have type:@.%a@.%a %a@." 
104
        print_norm t1
105
        print_protect msg
106
        print_norm t2
107
  | Typer.Error s ->
108
      Format.fprintf ppf "%a@." print_protect s
109
  | Typer.Constraint (s,t) ->
110
      Format.fprintf ppf "This expression should have type:@.%a@." 
111
        print_norm t;
112
      Format.fprintf ppf "but its inferred type is:@.%a@." 
113
        print_norm s;
114
      Format.fprintf ppf "which is not a subtype, as shown by the sample:@.%a@." 
115
	print_sample (Sample.get (Types.diff s t))
116
  | Typer.NonExhaustive t ->
117
118
      Format.fprintf ppf "This pattern matching is not exhaustive@.";
      Format.fprintf ppf "Residual type:@.%a@."
119
	print_norm t;
120
      Format.fprintf ppf "Sample:@.%a@." print_sample (Sample.get t)
121
122
123
  | Typer.UnboundId (x,tn) ->
      Format.fprintf ppf "Unbound identifier %a%s@." U.print (Id.value x)
        (if tn then " (it is a type name)" else "")
124
125
126
127
  | Ulexer.Error (i,j,s) ->
      let loc = Location.loc_of_pos (i,j), `Full in
      Format.fprintf ppf "Error %a:@." Location.print_loc loc;
      Format.fprintf ppf "%a%s" Location.html_hilight loc s
128
  | Parser.Error s | Stream.Error s -> 
129
      Format.fprintf ppf "Parsing error: %a@." print_protect s
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
  | Librarian.InconsistentCrc id ->
      Format.fprintf ppf "Link error:@.";
      let name = Encodings.Utf8.to_string (Types.CompUnit.value id) in
      Format.fprintf ppf "Inconsistent checksum (compilation unit: %s)@."
	name
  | Librarian.NoImplementation id ->
      Format.fprintf ppf "Link error:@.";
      let name = Encodings.Utf8.to_string (Types.CompUnit.value id) in
      Format.fprintf ppf "No implementation found for compilation unit: %s@."
	name
  | Librarian.Loop id ->
      Format.fprintf ppf "Compilation error:@.";
      let name = Encodings.Utf8.to_string (Types.CompUnit.value id) in
      Format.fprintf ppf "Loop between compilation unit (compilation unit: %s)@."
	name
  | InvalidInputFilename f ->
      Format.fprintf ppf "Compilation error:@.";
      Format.fprintf ppf "Source filename must have extension .cd@.";
  | InvalidObjectFilename f ->
      Format.fprintf ppf "Compilation error:@.";
150
      Format.fprintf ppf "Object filename must have extension .cdo and no path@.";
151
152
153
154
  | Librarian.InvalidObject f ->
      Format.fprintf ppf "Invalid object file %s@." f
  | Librarian.CannotOpen f ->
      Format.fprintf ppf "Cannot open file %s@." f
155
  | Location.Generic s ->
156
      Format.fprintf ppf "%a@." print_protect s
157
  | exn ->
158
(*      raise exn *)
159
      Format.fprintf ppf "%a@." print_protect (Printexc.to_string exn)
160

161

162
163
164
let eval_quiet tenv cenv e = 
  let (e,_) = Typer.type_expr tenv e in
  let e = Compile.compile_eval cenv e in 
165
166
  Eval.expr e

167
let debug ppf tenv cenv = function
168
  | `Subtype (t1,t2) ->
169
      Format.fprintf ppf "[DEBUG:subtype]@.";
170
171
      let t1 = Types.descr (Typer.typ tenv t1)
      and t2 = Types.descr (Typer.typ tenv t2) in
172
173
      let s = Types.subtype t1 t2 in
      Format.fprintf ppf "%a %a %a : %b@." print_norm t1 print_protect "<=" print_norm t2 s
174
  | `Sample t ->
175
176
      Format.fprintf ppf "[DEBUG:sample]@.";
      (try
177
	 let t = Types.descr (Typer.typ tenv t) in
178
179
180
	 Format.fprintf ppf "%a@." print_sample (Sample.get t)
       with Not_found ->
	 Format.fprintf ppf "Empty type : no sample !@.")
181
  | `Filter (t,p) -> 
182
      Format.fprintf ppf "[DEBUG:filter]@.";
183
184
      let t = Typer.typ tenv t
      and p = Typer.pat tenv p in
185
186
      let f = Patterns.filter (Types.descr t) p in
      List.iter (fun (x,t) ->
187
		   Format.fprintf ppf " %a:%a@." U.print (Id.value x)
188
189
		     print_norm (Types.descr t)) f
  | `Accept p ->
190
      Format.fprintf ppf "[DEBUG:accept]@.";
191
      let p = Typer.pat tenv p in
192
      let t = Patterns.accept p in
193
      Format.fprintf ppf " %a@." Types.Print.print (Types.descr t)
194
  | `Compile (t,pl) ->
195
      Format.fprintf ppf "[DEBUG:compile]@.";
196
197
      let t = Typer.typ tenv t
      and pl = List.map (Typer.pat tenv) pl in
198
      Patterns.Compile.debug_compile ppf t pl
199
200
  | `Explain (t,e) ->
      Format.fprintf ppf "[DEBUG:explain]@.";
201
202
      let t = Typer.typ tenv t in
      (match Explain.explain (Types.descr t) (eval_quiet tenv cenv e) with
203
204
205
206
207
	 | Some p ->
	     Format.fprintf ppf "Explanation: @[%a@]@." 
	       Explain.print_path p
	 | None ->
	     Format.fprintf ppf "Explanation: value has given type@.")
208

209

210
211
212
213
214
215
216
217
218
219
let flush_ppf ppf = Format.fprintf ppf "@."

let directive ppf tenv cenv = function
  | `Debug d ->
      debug ppf tenv cenv d
  | `Quit ->
      (if !toplevel then raise End_of_file)
  | `Env ->
      dump_env ppf tenv cenv
  | `Print_schema schema ->
220
      Schema_common.print_schema ppf (Typer.get_schema schema);
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
      flush_ppf ppf
  | `Print_type name ->
      Typer.dump_type ppf tenv name;
      flush_ppf ppf
  | `Print_schema_type schema_ref ->
      Typer.dump_schema_type ppf schema_ref;
      flush_ppf ppf
  | `Reinit_ns ->
      Typer.set_ns_table_for_printer tenv
  | `Help ->
      directive_help ppf
  | `Dump pexpr ->
      Value.dump_xml ppf (eval_quiet tenv cenv pexpr);
      flush_ppf ppf

let print_id_opt ppf = function
  | None -> Format.fprintf ppf "-"
  | Some id -> Format.fprintf ppf "val %a" U.print (Id.value id)

let print_value_opt ppf = function
  | None -> ()
  | Some v -> Format.fprintf ppf " = %a" print_value v

let show ppf id t v =
  Format.fprintf ppf "@[%a : @[%a%a@]@]@."
    print_id_opt id 
    print_norm t 
    print_value_opt v

let phrases ppf phs =
  let (tenv,cenv,_) = 
    Compile.comp_unit 
      ~run:true ~show:(show ppf) 
      ~loading:(fun cu -> Librarian.import cu; Librarian.run Value.nil cu)
      ~directive:(directive ppf)
      !typing_env !compile_env phs in
  typing_env := tenv;
  compile_env := cenv
259

260
261
262
let catch_exn ppf_err exn =
  if not catch_exceptions then raise exn;
  match exn with
263
264
265
266
267
268
269
270
  | (End_of_file | Failure _ | Not_found | Invalid_argument _ | Sys.Break) 
      as e -> 
      raise e
  | exn -> 
      print_exn ppf_err exn;
      Format.fprintf ppf_err "@."

let parse rule input =
271
272
  try Parser.localize_exn (fun () -> rule input)
  with e -> Parser.sync (); raise e
273

274
let run rule ppf ppf_err input =
275
  try phrases ppf (parse rule input); true
276
  with exn -> catch_exn ppf_err exn; false
277

278
let script = run Parser.prog
279
let topinput = run Parser.top_phrases
280

281
282
283
ifdef ML_INTERFACE then 
  let check_ml cu id out_dir out =
    try
284
285
286
287
288
289
      let name = cu ^ ".cmi" in
      let file = List.find ( 
	fun dir -> Sys.file_exists ( Filename.concat dir name ) 
      ) !Librarian.obj_path in
      if file = "" then raise Not_found;
      let file = Filename.concat file name in
290
      (* Load Caml .cmi file. *)
291
      let ml_cu = ML.CompUnit.from_bytecode file cu
292
293
294
295
      and cd_cu = Ml_cduce.CompUnit.from_types_cu cu id in
      (* Check file interfaces. *)
      Ml_checker.run ml_cu cd_cu;
      (* Generate interface file. *)
296
      String.set cu 0 ( Char.lowercase ( String.get cu 0 ) );
297
298
299
300
      let out = open_out ( Filename.concat out_dir (cu ^ ".ml") ) in
      let fmt = Format.formatter_of_out_channel out in
      Ml_generator.ML.generate fmt ml_cu cd_cu;
      close_out out;
301
    with Not_found -> 
302
303
304
305
      Format.eprintf "Warning: no valid Caml interface found.@."
else 
  let check_ml cu id out_dir out = ();;

306
let compile src out_dir =
307
  try
308
309
310
311
312
313
314
315
316
    if not (Filename.check_suffix src ".cd")
    then raise (InvalidInputFilename src);
    let cu = Filename.chop_suffix (Filename.basename src) ".cd" in
    let out_dir = 
      match out_dir with
	| None -> Filename.dirname src
	| Some x -> x in
    let out = Filename.concat out_dir (cu ^ ".cdo") in
    let id = Types.CompUnit.mk (U.mk_latin1 cu) in
317
    Librarian.compile !verbose id src;
318
    Librarian.save id out;
319
    check_ml cu id out_dir out;
320
    exit 0
321
322
323
  with exn -> catch_exn Format.err_formatter exn; exit 1
  
let compile_run src argv =
324
  try
325
326
327
328
    if not (Filename.check_suffix src ".cd")
    then raise (InvalidInputFilename src);
    let cu = Filename.chop_suffix (Filename.basename src) ".cd" in
    let id = Types.CompUnit.mk (U.mk_latin1 cu) in
329
    Librarian.compile !verbose id src;
330
331
    Librarian.run argv id
  with exn -> catch_exn Format.err_formatter exn; exit 1
332
333

let run obj argv =  
334
  try
335
    if not (Filename.check_suffix obj ".cdo") || (Filename.basename obj <> obj)
336
337
338
    then raise (InvalidObjectFilename obj);
    let cu = Filename.chop_suffix (Filename.basename obj) ".cdo" in
    let id = Types.CompUnit.mk (U.mk_latin1 cu) in
339
340
341
    Librarian.import id;
    Librarian.run argv id
  with exn -> catch_exn Format.err_formatter exn; exit 1
342

343
344

let dump_env ppf = dump_env ppf !typing_env !compile_env