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

Contents of /driver/cduce.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1354 - (hide annotations)
Tue Jul 10 18:42:02 2007 UTC (5 years, 10 months ago) by abate
File size: 12703 byte(s)
[r2004-12-22 01:14:22 by afrisch] Better factorization

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

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