/[svn]/driver/cduce.ml
ViewVC logotype

Contents of /driver/cduce.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1055 - (hide annotations)
Tue Jul 10 18:19:44 2007 UTC (5 years, 11 months ago) by abate
File size: 12038 byte(s)
[r2004-03-16 14:20:55 by jdemouth] Nouveaux messages.

Original author: jdemouth
Date: 2004-03-16 14:20:55+00:00
1 abate 10 open Location
2 abate 225 open Ident
3 abate 10
4 abate 1019 ifdef ML_INTERFACE then module ML = Ml_ocaml;;
5    
6 abate 723 exception InvalidInputFilename of string
7     exception InvalidObjectFilename of string
8    
9 abate 924 (* if set to false toplevel exception aren't cought.
10     * Useful for debugging with OCAMLRUNPARAM="b" *)
11 abate 746 let catch_exceptions = true
12    
13 abate 707 (* retuns a filename without the suffix suff if any *)
14     let prefix filename suff =
15     if Filename.check_suffix filename suff then
16     try
17     Filename.chop_extension filename
18     with Invalid_argument filename -> failwith "Not a point in the suffix?"
19     else filename
20    
21 abate 446 let toplevel = ref false
22 abate 926 let verbose = ref false
23 abate 217
24 abate 686 let typing_env = State.ref "Cduce.typing_env" Builtin.env
25 abate 692 let compile_env = State.ref "Cduce.compile_env" Compile.empty
26 abate 431
27 abate 926 let get_global_value cenv v =
28 abate 924 Eval.var (Compile.find v !compile_env)
29 abate 692
30     let get_global_type v =
31     Typer.find_value v !typing_env
32    
33 abate 368 let enter_global_value x v t =
34 abate 692 typing_env := Typer.enter_value x t !typing_env;
35 abate 924 compile_env := Compile.enter_global !compile_env x;
36     Eval.push v
37 abate 692
38 abate 332 let rec is_abstraction = function
39     | Ast.Abstraction _ -> true
40     | Ast.LocatedExpr (_,e) -> is_abstraction e
41     | _ -> false
42    
43 abate 29 let print_norm ppf d =
44 abate 92 Location.protect ppf
45 abate 367 (fun ppf -> Types.Print.print ppf ((*Types.normalize*) d))
46 abate 29
47 abate 423 let print_sample ppf s =
48     Location.protect ppf
49     (fun ppf -> Sample.print ppf s)
50    
51 abate 541 let print_protect ppf s =
52     Location.protect ppf (fun ppf -> Format.fprintf ppf "%s" s)
53 abate 423
54 abate 92 let print_value ppf v =
55     Location.protect ppf (fun ppf -> Value.print ppf v)
56    
57 abate 695 let dump_value ppf x t v =
58     Format.fprintf ppf "@[val %a : @[%a = %a@]@]@."
59     U.print (Id.value x) print_norm t print_value v
60    
61 abate 926 let dump_env ppf tenv cenv =
62     Format.fprintf ppf "Types:%a@." Typer.dump_types tenv;
63     Format.fprintf ppf "Namespace prefixes:@\n%a" Typer.dump_ns tenv;
64 abate 553 Format.fprintf ppf "Namespace prefixes used for pretty-printing:@.%t"
65 abate 552 Ns.InternalPrinter.dump;
66 abate 788 Format.fprintf ppf "Schemas: %s@."
67 abate 812 (String.concat " " (List.map U.get_str (Typer.get_schema_names ())));
68 abate 691 Format.fprintf ppf "Values:@.";
69 abate 926 Typer.iter_values tenv
70     (fun x t -> dump_value ppf x t (get_global_value cenv x))
71 abate 107
72 abate 702 let directive_help ppf =
73     Format.fprintf ppf
74     "Toplevel directives:
75 abate 788 #quit;; quit the interpreter
76     #env;; dump current environment
77     #reinit_ns;; reinitialize namespace processing
78     #help;; shows this help message
79     #dump_value <expr>;; dump an XML-ish representation of the resulting
80     value of a given expression
81     #print_schema <name>;;
82     #print_type <name>;;
83 abate 702 "
84    
85 abate 10 let rec print_exn ppf = function
86 abate 522 | Location (loc, w, exn) ->
87     Format.fprintf ppf "Error %a:@." Location.print_loc (loc,w);
88 abate 622 Format.fprintf ppf "%a" Location.html_hilight (loc,w);
89 abate 91 print_exn ppf exn
90 abate 64 | Value.CDuceExn v ->
91 abate 433 Format.fprintf ppf "Uncaught CDuce exception: @[%a@]@."
92 abate 92 print_value v
93 abate 26 | Typer.WrongLabel (t,l) ->
94 abate 529 Format.fprintf ppf "Wrong record selection; field %a "
95 abate 542 Label.print (LabelPool.value l);
96 abate 529 Format.fprintf ppf "not present in an expression of type:@.%a@."
97 abate 29 print_norm t
98 abate 19 | Typer.ShouldHave (t,msg) ->
99 abate 622 Format.fprintf ppf "This expression should have type:@.%a@.%a@."
100 abate 29 print_norm t
101 abate 622 print_protect msg
102 abate 355 | Typer.ShouldHave2 (t1,msg,t2) ->
103 abate 622 Format.fprintf ppf "This expression should have type:@.%a@.%a %a@."
104 abate 355 print_norm t1
105 abate 622 print_protect msg
106 abate 355 print_norm t2
107 abate 421 | Typer.Error s ->
108 abate 622 Format.fprintf ppf "%a@." print_protect s
109 abate 421 | Typer.Constraint (s,t) ->
110 abate 433 Format.fprintf ppf "This expression should have type:@.%a@."
111 abate 29 print_norm t;
112 abate 433 Format.fprintf ppf "but its inferred type is:@.%a@."
113 abate 29 print_norm s;
114 abate 433 Format.fprintf ppf "which is not a subtype, as shown by the sample:@.%a@."
115 abate 423 print_sample (Sample.get (Types.diff s t))
116 abate 17 | Typer.NonExhaustive t ->
117 abate 433 Format.fprintf ppf "This pattern matching is not exhaustive@.";
118     Format.fprintf ppf "Residual type:@.%a@."
119 abate 29 print_norm t;
120 abate 433 Format.fprintf ppf "Sample:@.%a@." print_sample (Sample.get t)
121 abate 656 | Typer.UnboundId (x,tn) ->
122     Format.fprintf ppf "Unbound identifier %a%s@." U.print (Id.value x)
123     (if tn then " (it is a type name)" else "")
124 abate 668 | Ulexer.Error (i,j,s) ->
125     let loc = Location.loc_of_pos (i,j), `Full in
126     Format.fprintf ppf "Error %a:@." Location.print_loc loc;
127     Format.fprintf ppf "%a%s" Location.html_hilight loc s
128 abate 90 | Parser.Error s | Stream.Error s ->
129 abate 622 Format.fprintf ppf "Parsing error: %a@." print_protect s
130 abate 723 | Librarian.InconsistentCrc id ->
131     Format.fprintf ppf "Link error:@.";
132     let name = Encodings.Utf8.to_string (Types.CompUnit.value id) in
133     Format.fprintf ppf "Inconsistent checksum (compilation unit: %s)@."
134     name
135     | Librarian.NoImplementation id ->
136     Format.fprintf ppf "Link error:@.";
137     let name = Encodings.Utf8.to_string (Types.CompUnit.value id) in
138     Format.fprintf ppf "No implementation found for compilation unit: %s@."
139     name
140     | Librarian.Loop id ->
141     Format.fprintf ppf "Compilation error:@.";
142     let name = Encodings.Utf8.to_string (Types.CompUnit.value id) in
143     Format.fprintf ppf "Loop between compilation unit (compilation unit: %s)@."
144     name
145     | InvalidInputFilename f ->
146     Format.fprintf ppf "Compilation error:@.";
147     Format.fprintf ppf "Source filename must have extension .cd@.";
148     | InvalidObjectFilename f ->
149     Format.fprintf ppf "Compilation error:@.";
150 abate 724 Format.fprintf ppf "Object filename must have extension .cdo and no path@.";
151 abate 723 | Librarian.InvalidObject f ->
152     Format.fprintf ppf "Invalid object file %s@." f
153     | Librarian.CannotOpen f ->
154     Format.fprintf ppf "Cannot open file %s@." f
155 abate 91 | Location.Generic s ->
156 abate 622 Format.fprintf ppf "%a@." print_protect s
157 abate 10 | exn ->
158 abate 403 (* raise exn *)
159 abate 622 Format.fprintf ppf "%a@." print_protect (Printexc.to_string exn)
160 abate 10
161 abate 694
162 abate 926 let eval_quiet tenv cenv e =
163     let (e,_) = Typer.type_expr tenv e in
164     let e = Compile.compile_eval cenv e in
165 abate 924 Eval.expr e
166    
167 abate 926 let debug ppf tenv cenv = function
168 abate 224 | `Subtype (t1,t2) ->
169 abate 433 Format.fprintf ppf "[DEBUG:subtype]@.";
170 abate 926 let t1 = Types.descr (Typer.typ tenv t1)
171     and t2 = Types.descr (Typer.typ tenv t2) in
172 abate 541 let s = Types.subtype t1 t2 in
173     Format.fprintf ppf "%a %a %a : %b@." print_norm t1 print_protect "<=" print_norm t2 s
174 abate 407 | `Sample t ->
175 abate 433 Format.fprintf ppf "[DEBUG:sample]@.";
176     (try
177 abate 926 let t = Types.descr (Typer.typ tenv t) in
178 abate 433 Format.fprintf ppf "%a@." print_sample (Sample.get t)
179     with Not_found ->
180     Format.fprintf ppf "Empty type : no sample !@.")
181 abate 43 | `Filter (t,p) ->
182 abate 433 Format.fprintf ppf "[DEBUG:filter]@.";
183 abate 926 let t = Typer.typ tenv t
184     and p = Typer.pat tenv p in
185 abate 43 let f = Patterns.filter (Types.descr t) p in
186     List.iter (fun (x,t) ->
187 abate 433 Format.fprintf ppf " %a:%a@." U.print (Id.value x)
188 abate 43 print_norm (Types.descr t)) f
189     | `Accept p ->
190 abate 433 Format.fprintf ppf "[DEBUG:accept]@.";
191 abate 926 let p = Typer.pat tenv p in
192 abate 43 let t = Patterns.accept p in
193 abate 433 Format.fprintf ppf " %a@." Types.Print.print (Types.descr t)
194 abate 43 | `Compile (t,pl) ->
195 abate 433 Format.fprintf ppf "[DEBUG:compile]@.";
196 abate 926 let t = Typer.typ tenv t
197     and pl = List.map (Typer.pat tenv) pl in
198 abate 149 Patterns.Compile.debug_compile ppf t pl
199 abate 694 | `Explain (t,e) ->
200     Format.fprintf ppf "[DEBUG:explain]@.";
201 abate 926 let t = Typer.typ tenv t in
202     (match Explain.explain (Types.descr t) (eval_quiet tenv cenv e) with
203 abate 694 | Some p ->
204     Format.fprintf ppf "Explanation: @[%a@]@."
205     Explain.print_path p
206     | None ->
207     Format.fprintf ppf "Explanation: value has given type@.")
208 abate 66
209 abate 695
210 abate 926 let flush_ppf ppf = Format.fprintf ppf "@."
211 abate 692
212 abate 926 let directive ppf tenv cenv = function
213     | `Debug d ->
214     debug ppf tenv cenv d
215     | `Quit ->
216     (if !toplevel then raise End_of_file)
217     | `Env ->
218     dump_env ppf tenv cenv
219     | `Print_schema schema ->
220     Schema_common.print_schema ppf (Typer.get_schema schema);
221     flush_ppf ppf
222     | `Print_type name ->
223     Typer.dump_type ppf tenv name;
224     flush_ppf ppf
225     | `Print_schema_type schema_ref ->
226     Typer.dump_schema_type ppf schema_ref;
227     flush_ppf ppf
228     | `Reinit_ns ->
229     Typer.set_ns_table_for_printer tenv
230     | `Help ->
231     directive_help ppf
232     | `Dump pexpr ->
233     Value.dump_xml ppf (eval_quiet tenv cenv pexpr);
234     flush_ppf ppf
235 abate 66
236 abate 926 let print_id_opt ppf = function
237     | None -> Format.fprintf ppf "-"
238     | Some id -> Format.fprintf ppf "val %a" U.print (Id.value id)
239 abate 694
240 abate 926 let print_value_opt ppf = function
241     | None -> ()
242     | Some v -> Format.fprintf ppf " = %a" print_value v
243 abate 90
244 abate 926 let show ppf id t v =
245     Format.fprintf ppf "@[%a : @[%a%a@]@]@."
246     print_id_opt id
247     print_norm t
248     print_value_opt v
249    
250     let phrases ppf phs =
251     let (tenv,cenv,_) =
252     Compile.comp_unit
253     ~run:true ~show:(show ppf)
254     ~loading:(fun cu -> Librarian.import cu; Librarian.run Value.nil cu)
255     ~directive:(directive ppf)
256     !typing_env !compile_env phs in
257     typing_env := tenv;
258     compile_env := cenv
259    
260 abate 746 let catch_exn ppf_err exn =
261     if not catch_exceptions then raise exn;
262     match exn with
263 abate 698 | (End_of_file | Failure _ | Not_found | Invalid_argument _ | Sys.Break)
264     as e ->
265     raise e
266     | exn ->
267     print_exn ppf_err exn;
268     Format.fprintf ppf_err "@."
269    
270     let parse rule input =
271 abate 792 try Parser.localize_exn (fun () -> rule input)
272     with e -> Parser.sync (); raise e
273 abate 698
274 abate 431 let run rule ppf ppf_err input =
275 abate 792 try phrases ppf (parse rule input); true
276 abate 698 with exn -> catch_exn ppf_err exn; false
277 abate 21
278 abate 431 let script = run Parser.prog
279 abate 446 let topinput = run Parser.top_phrases
280 abate 691
281 abate 1019 ifdef ML_INTERFACE then
282     let check_ml cu id out_dir out =
283 abate 1055 let fnam = String.copy cu in
284     String.set fnam 0 ( Char.lowercase ( String.get fnam 0 ) );
285 abate 1019 try
286 abate 1055 let name = fnam ^ ".cmi" in
287 abate 1032 let file = List.find (
288     fun dir -> Sys.file_exists ( Filename.concat dir name )
289     ) !Librarian.obj_path in
290     if file = "" then raise Not_found;
291     let file = Filename.concat file name in
292     let ml_cu = ML.CompUnit.from_bytecode file cu
293 abate 1019 and cd_cu = Ml_cduce.CompUnit.from_types_cu cu id in
294     Ml_checker.run ml_cu cd_cu;
295     let out = open_out ( Filename.concat out_dir (cu ^ ".ml") ) in
296     let fmt = Format.formatter_of_out_channel out in
297     Ml_generator.ML.generate fmt ml_cu cd_cu;
298     close_out out;
299 abate 1055 with Not_found -> (
300     let name = fnam ^ ".mli" in
301     let file = List.find (
302     fun dir -> Sys.file_exists ( Filename.concat dir name )
303     ) !Librarian.obj_path in
304     if file <> "" then Format.eprintf "Warning: found %s.mli but no %s.cmi: forgotten compilation?@." fnam fnam;
305     )
306 abate 1019 else
307     let check_ml cu id out_dir out = ();;
308    
309 abate 723 let compile src out_dir =
310 abate 698 try
311 abate 723 if not (Filename.check_suffix src ".cd")
312     then raise (InvalidInputFilename src);
313     let cu = Filename.chop_suffix (Filename.basename src) ".cd" in
314     let out_dir =
315     match out_dir with
316     | None -> Filename.dirname src
317     | Some x -> x in
318     let out = Filename.concat out_dir (cu ^ ".cdo") in
319     let id = Types.CompUnit.mk (U.mk_latin1 cu) in
320 abate 926 Librarian.compile !verbose id src;
321 abate 723 Librarian.save id out;
322 abate 1019 check_ml cu id out_dir out;
323 abate 713 exit 0
324 abate 698 with exn -> catch_exn Format.err_formatter exn; exit 1
325    
326     let compile_run src argv =
327 abate 713 try
328 abate 723 if not (Filename.check_suffix src ".cd")
329     then raise (InvalidInputFilename src);
330     let cu = Filename.chop_suffix (Filename.basename src) ".cd" in
331     let id = Types.CompUnit.mk (U.mk_latin1 cu) in
332 abate 926 Librarian.compile !verbose id src;
333 abate 713 Librarian.run argv id
334     with exn -> catch_exn Format.err_formatter exn; exit 1
335 abate 698
336     let run obj argv =
337 abate 713 try
338 abate 724 if not (Filename.check_suffix obj ".cdo") || (Filename.basename obj <> obj)
339 abate 723 then raise (InvalidObjectFilename obj);
340     let cu = Filename.chop_suffix (Filename.basename obj) ".cdo" in
341     let id = Types.CompUnit.mk (U.mk_latin1 cu) in
342 abate 713 Librarian.import id;
343     Librarian.run argv id
344     with exn -> catch_exn Format.err_formatter exn; exit 1
345 abate 698
346 abate 926
347     let dump_env ppf = dump_env ppf !typing_env !compile_env

CVS Admin">CVS Admin
ViewVC Help
Powered by ViewVC 1.1.5