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

Contents of /driver/cduce.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 798 - (hide annotations)
Tue Jul 10 18:03:55 2007 UTC (5 years, 10 months ago) by abate
File size: 13325 byte(s)
[r2003-11-21 09:27:59 by afrisch] Opt

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

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