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

Contents of /driver/cduce.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 656 - (hide annotations)
Tue Jul 10 17:51:50 2007 UTC (5 years, 10 months ago) by abate
File size: 7995 byte(s)
[r2003-09-16 21:30:42 by cvscast] Cleaning in progress... + no more uppercase/lowercase distinction for
identifiers

Original author: cvscast
Date: 2003-09-16 21:30:45+00:00
1 abate 10 open Location
2 abate 225 open Ident
3 abate 10
4 abate 217 let quiet = ref false
5 abate 446 let toplevel = ref false
6 abate 217
7 abate 421 let typing_env = State.ref "Cduce.typing_env" Env.empty
8 abate 107
9 abate 431 let eval_env = State.ref "Cduce.eval_env" Env.empty
10    
11 abate 368 let enter_global_value x v t =
12 abate 431 eval_env := Env.add x v !eval_env;
13 abate 421 typing_env := Env.add x t !typing_env
14 abate 368
15 abate 332 let rec is_abstraction = function
16     | Ast.Abstraction _ -> true
17     | Ast.LocatedExpr (_,e) -> is_abstraction e
18     | _ -> false
19    
20 abate 29 let print_norm ppf d =
21 abate 92 Location.protect ppf
22 abate 367 (fun ppf -> Types.Print.print ppf ((*Types.normalize*) d))
23 abate 29
24 abate 423 let print_sample ppf s =
25     Location.protect ppf
26     (fun ppf -> Sample.print ppf s)
27    
28 abate 541 let print_protect ppf s =
29     Location.protect ppf (fun ppf -> Format.fprintf ppf "%s" s)
30 abate 423
31 abate 92 let print_value ppf v =
32     Location.protect ppf (fun ppf -> Value.print ppf v)
33    
34 abate 107 let dump_env ppf =
35 abate 553 Format.fprintf ppf "Types:%t@." Typer.dump_global_types;
36 abate 552 Format.fprintf ppf "Namespace prefixes:@\n%t" Typer.dump_global_ns;
37 abate 553 Format.fprintf ppf "Namespace prefixes used for pretty-printing:@.%t"
38 abate 552 Ns.InternalPrinter.dump;
39     Format.fprintf ppf "Values:@\n";
40 abate 421 Env.iter
41 abate 107 (fun x v ->
42 abate 421 let t = Env.find x !typing_env in
43 abate 433 Format.fprintf ppf "@[val %a : @[%a = %a@]@]@."
44     U.print (Id.value x) print_norm t print_value v
45 abate 107 )
46 abate 552 !eval_env
47 abate 107
48 abate 10 let rec print_exn ppf = function
49 abate 522 | Location (loc, w, exn) ->
50     Format.fprintf ppf "Error %a:@." Location.print_loc (loc,w);
51 abate 622 Format.fprintf ppf "%a" Location.html_hilight (loc,w);
52 abate 91 print_exn ppf exn
53 abate 64 | Value.CDuceExn v ->
54 abate 433 Format.fprintf ppf "Uncaught CDuce exception: @[%a@]@."
55 abate 92 print_value v
56 abate 368 | Eval.MultipleDeclaration v ->
57 abate 433 Format.fprintf ppf "Multiple declaration for global value %a@."
58 abate 374 U.print (Id.value v)
59 abate 26 | Typer.WrongLabel (t,l) ->
60 abate 529 Format.fprintf ppf "Wrong record selection; field %a "
61 abate 542 Label.print (LabelPool.value l);
62 abate 529 Format.fprintf ppf "not present in an expression of type:@.%a@."
63 abate 29 print_norm t
64 abate 19 | Typer.ShouldHave (t,msg) ->
65 abate 622 Format.fprintf ppf "This expression should have type:@.%a@.%a@."
66 abate 29 print_norm t
67 abate 622 print_protect msg
68 abate 355 | Typer.ShouldHave2 (t1,msg,t2) ->
69 abate 622 Format.fprintf ppf "This expression should have type:@.%a@.%a %a@."
70 abate 355 print_norm t1
71 abate 622 print_protect msg
72 abate 355 print_norm t2
73 abate 421 | Typer.Error s ->
74 abate 622 Format.fprintf ppf "%a@." print_protect s
75 abate 421 | Typer.Constraint (s,t) ->
76 abate 433 Format.fprintf ppf "This expression should have type:@.%a@."
77 abate 29 print_norm t;
78 abate 433 Format.fprintf ppf "but its inferred type is:@.%a@."
79 abate 29 print_norm s;
80 abate 433 Format.fprintf ppf "which is not a subtype, as shown by the sample:@.%a@."
81 abate 423 print_sample (Sample.get (Types.diff s t))
82 abate 17 | Typer.NonExhaustive t ->
83 abate 433 Format.fprintf ppf "This pattern matching is not exhaustive@.";
84     Format.fprintf ppf "Residual type:@.%a@."
85 abate 29 print_norm t;
86 abate 433 Format.fprintf ppf "Sample:@.%a@." print_sample (Sample.get t)
87 abate 656 | Typer.UnboundId (x,tn) ->
88     Format.fprintf ppf "Unbound identifier %a%s@." U.print (Id.value x)
89     (if tn then " (it is a type name)" else "")
90 abate 81 | Wlexer.Illegal_character c ->
91 abate 622 Format.fprintf ppf "Illegal character (%a)@." print_protect (Char.escaped c)
92 abate 81 | Wlexer.Unterminated_comment ->
93 abate 433 Format.fprintf ppf "Comment not terminated@."
94 abate 81 | Wlexer.Unterminated_string ->
95 abate 433 Format.fprintf ppf "String literal not terminated@."
96 abate 81 | Wlexer.Unterminated_string_in_comment ->
97 abate 433 Format.fprintf ppf "This comment contains an unterminated string literal@."
98 abate 90 | Parser.Error s | Stream.Error s ->
99 abate 622 Format.fprintf ppf "Parsing error: %a@." print_protect s
100 abate 91 | Location.Generic s ->
101 abate 622 Format.fprintf ppf "%a@." print_protect s
102 abate 10 | exn ->
103 abate 403 (* raise exn *)
104 abate 622 Format.fprintf ppf "%a@." print_protect (Printexc.to_string exn)
105 abate 10
106 abate 90 let debug ppf = function
107 abate 224 | `Subtype (t1,t2) ->
108 abate 433 Format.fprintf ppf "[DEBUG:subtype]@.";
109 abate 277 let t1 = Types.descr (Typer.typ t1)
110     and t2 = Types.descr (Typer.typ t2) in
111 abate 541 let s = Types.subtype t1 t2 in
112     Format.fprintf ppf "%a %a %a : %b@." print_norm t1 print_protect "<=" print_norm t2 s
113 abate 407 | `Sample t ->
114 abate 433 Format.fprintf ppf "[DEBUG:sample]@.";
115     (try
116     let t = Types.descr (Typer.typ t) in
117     Format.fprintf ppf "%a@." print_sample (Sample.get t)
118     with Not_found ->
119     Format.fprintf ppf "Empty type : no sample !@.")
120 abate 43 | `Filter (t,p) ->
121 abate 433 Format.fprintf ppf "[DEBUG:filter]@.";
122 abate 277 let t = Typer.typ t
123     and p = Typer.pat p in
124 abate 43 let f = Patterns.filter (Types.descr t) p in
125     List.iter (fun (x,t) ->
126 abate 433 Format.fprintf ppf " %a:%a@." U.print (Id.value x)
127 abate 43 print_norm (Types.descr t)) f
128     | `Accept p ->
129 abate 433 Format.fprintf ppf "[DEBUG:accept]@.";
130 abate 277 let p = Typer.pat p in
131 abate 43 let t = Patterns.accept p in
132 abate 433 Format.fprintf ppf " %a@." Types.Print.print (Types.descr t)
133 abate 43 | `Compile (t,pl) ->
134 abate 433 Format.fprintf ppf "[DEBUG:compile]@.";
135 abate 277 let t = Typer.typ t
136     and pl = List.map Typer.pat pl in
137 abate 149 Patterns.Compile.debug_compile ppf t pl
138 abate 66
139 abate 433 let insert_bindings ppf =
140     List.iter2
141     (fun (x,t) (y,v) ->
142     assert (x = y);
143 abate 431 typing_env := Env.add x t !typing_env;
144     eval_env := Env.add x v !eval_env;
145     if not !quiet then
146 abate 502 Format.fprintf ppf "val %a : @[@[%a@] =@ @[%a@]@]@."
147 abate 433 U.print (Id.value x) print_norm t print_value v)
148 abate 90
149 abate 431 let rec collect_funs ppf accu = function
150     | { descr = Ast.FunDecl e } :: rest ->
151     let (_,e) = Typer.expr e in
152     collect_funs ppf (e::accu) rest
153     | rest ->
154 abate 433 let typs = Typer.type_rec_funs !typing_env accu in
155 abate 431 Typer.report_unused_branches ();
156 abate 433 let vals = Eval.eval_rec_funs !eval_env accu in
157     insert_bindings ppf typs vals;
158 abate 431 rest
159 abate 10
160 abate 431 let rec collect_types ppf accu = function
161     | { descr = Ast.TypeDecl (x,t) } :: rest ->
162     collect_types ppf ((x,t) :: accu) rest
163     | rest ->
164     Typer.register_global_types accu;
165     rest
166 abate 66
167 abate 431 let rec phrases ppf phs = match phs with
168     | { descr = Ast.FunDecl _ } :: _ ->
169     phrases ppf (collect_funs ppf [] phs)
170     | { descr = Ast.TypeDecl (_,_) } :: _ ->
171     phrases ppf (collect_types ppf [] phs)
172 abate 501 | { descr = Ast.SchemaDecl (name, schema) } :: rest ->
173     Typer.register_schema name schema;
174     phrases ppf rest
175 abate 529 | { descr = Ast.Namespace (pr,ns) } :: rest ->
176 abate 552 Typer.register_global_ns pr ns;
177 abate 529 phrases ppf rest
178 abate 431 | { descr = Ast.EvalStatement e } :: rest ->
179     let (fv,e) = Typer.expr e in
180     let t = Typer.type_check !typing_env e Types.any true in
181     Typer.report_unused_branches ();
182     if not !quiet then
183 abate 522 Location.dump_loc ppf (e.Typed.exp_loc,`Full);
184 abate 431 let v = Eval.eval !eval_env e in
185     if not !quiet then
186 abate 502 Format.fprintf ppf "- : @[@[%a@] =@ @[%a@]@]@." print_norm t print_value v;
187 abate 431 phrases ppf rest
188     | { descr = Ast.LetDecl (p,e) } :: rest ->
189     let decl = Typer.let_decl p e in
190 abate 433 let typs = Typer.type_let_decl !typing_env decl in
191 abate 431 Typer.report_unused_branches ();
192 abate 433 let vals = Eval.eval_let_decl !eval_env decl in
193     insert_bindings ppf typs vals;
194 abate 431 phrases ppf rest
195     | { descr = Ast.Debug l } :: rest ->
196 abate 541 debug ppf l;
197 abate 431 phrases ppf rest
198 abate 446 | { descr = Ast.Directive `Quit } :: rest ->
199     if !toplevel then raise End_of_file;
200     phrases ppf rest
201     | { descr = Ast.Directive `Env } :: rest ->
202     dump_env ppf;
203     phrases ppf rest
204 abate 553 | { descr = Ast.Directive `Reinit_ns } :: rest ->
205     Typer.set_ns_table_for_printer ();
206     phrases ppf rest
207 abate 431 | [] -> ()
208 abate 90
209 abate 431 let run rule ppf ppf_err input =
210 abate 637 Typer.clear_unused_branches ();
211 abate 13 try
212 abate 90 let p =
213 abate 431 try rule input
214 abate 90 with
215 abate 431 | Stdpp.Exc_located (_, (Location _ as e)) ->
216 abate 495 Parser.sync (); raise e
217 abate 431 | Stdpp.Exc_located ((i,j), e) ->
218 abate 495 Parser.sync (); raise_loc i j e
219 abate 90 in
220 abate 431 phrases ppf p;
221 abate 95 true
222 abate 28 with
223 abate 446 | (End_of_file | Failure _ | Not_found | Invalid_argument _ | Sys.Break)
224     as e ->
225     raise e
226 abate 431 | exn ->
227     print_exn ppf_err exn;
228     Format.fprintf ppf_err "@.";
229     false
230 abate 21
231 abate 431 let script = run Parser.prog
232 abate 446 let topinput = run Parser.top_phrases

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