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

Contents of /driver/cduce.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 926 - (hide annotations)
Tue Jul 10 18:12:01 2007 UTC (5 years, 10 months ago) by abate
File size: 10871 byte(s)
[r2003-12-13 13:51:19 by afrisch] Factorization + --verbose

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

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