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

Contents of /driver/cduce.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 136 - (hide annotations)
Tue Jul 10 17:09:36 2007 UTC (5 years, 10 months ago) by abate
File size: 8218 byte(s)
[r2002-11-16 22:17:14 by cvscast] Empty log message

Original author: cvscast
Date: 2002-11-16 22:17:14+00:00
1 abate 10 open Location
2    
3 abate 107 let typing_env = State.ref "Cduce.typing_env" Typer.Env.empty
4     let glb_env = State.ref "Cduce.glb_env" Typer.Env.empty
5     let eval_env = Eval.global_env
6    
7 abate 29 let print_norm ppf d =
8 abate 92 Location.protect ppf
9 abate 111 (fun ppf -> Types.Print.print_descr ppf ((*Types.normalize*) d))
10 abate 29
11 abate 92 let print_value ppf v =
12     Location.protect ppf (fun ppf -> Value.print ppf v)
13    
14 abate 107 let dump_env ppf =
15     Format.fprintf ppf "Global types:";
16     Typer.Env.iter (fun x _ -> Format.fprintf ppf " %s" x) !glb_env;
17     Format.fprintf ppf ".@\n";
18     Eval.Env.iter
19     (fun x v ->
20     let t = Typer.Env.find x !typing_env in
21     Format.fprintf ppf "@[|- %s : %a@ => %a@]@\n"
22     x
23     print_norm t
24     print_value v
25     )
26     !eval_env
27    
28    
29 abate 10 let rec print_exn ppf = function
30 abate 66 | Location (loc, exn) ->
31 abate 91 Format.fprintf ppf "Error %a:@\n" Location.print_loc loc;
32 abate 92 Format.fprintf ppf "%a" Location.html_hilight loc;
33 abate 91 print_exn ppf exn
34 abate 64 | Value.CDuceExn v ->
35     Format.fprintf ppf "Uncaught CDuce exception: @[%a@]@\n"
36 abate 92 print_value v
37 abate 26 | Typer.WrongLabel (t,l) ->
38     Format.fprintf ppf "Wrong record selection: the label %s@\n"
39 abate 78 (Types.LabelPool.value l);
40 abate 27 Format.fprintf ppf "applied to an expression of type %a@\n"
41 abate 29 print_norm t
42 abate 28 | Typer.MultipleLabel l ->
43     Format.fprintf ppf "Multiple occurences for the record label %s@\n"
44 abate 78 (Types.LabelPool.value l);
45 abate 19 | Typer.ShouldHave (t,msg) ->
46     Format.fprintf ppf "This expression should have type %a@\n%s@\n"
47 abate 29 print_norm t
48 abate 28 msg
49 abate 10 | Typer.Constraint (s,t,msg) ->
50 abate 19 Format.fprintf ppf "This expression should have type %a@\n"
51 abate 29 print_norm t;
52 abate 19 Format.fprintf ppf "but its infered type is: %a@\n"
53 abate 29 print_norm s;
54 abate 19 Format.fprintf ppf "which is not a subtype, as shown by the value %a@\n"
55 abate 71 Types.Sample.print (Types.Sample.get (Types.diff s t));
56 abate 19 Format.fprintf ppf "%s@\n" msg
57 abate 17 | Typer.NonExhaustive t ->
58     Format.fprintf ppf "This pattern matching is not exhaustive@\n";
59     Format.fprintf ppf "Residual type: %a@\n"
60 abate 29 print_norm t;
61 abate 17 Format.fprintf ppf "Sample value: %a@\n"
62 abate 71 Types.Sample.print (Types.Sample.get t)
63 abate 36 | Typer.UnboundId x ->
64     Format.fprintf ppf "Unbound identifier %s@\n" x
65 abate 81 | Wlexer.Illegal_character c ->
66     Format.fprintf ppf "Illegal character (%s)@\n" (Char.escaped c)
67     | Wlexer.Unterminated_comment ->
68     Format.fprintf ppf "Comment not terminated@\n"
69     | Wlexer.Unterminated_string ->
70     Format.fprintf ppf "String literal not terminated@\n"
71     | Wlexer.Unterminated_string_in_comment ->
72     Format.fprintf ppf "This comment contains an unterminated string literal@\n"
73 abate 90 | Parser.Error s | Stream.Error s ->
74 abate 81 Format.fprintf ppf "Parsing error: %s@\n" s
75 abate 91 | Location.Generic s ->
76     Format.fprintf ppf "%s@\n" s
77 abate 10 | exn ->
78 abate 129 raise exn
79     (*
80 abate 10 Format.fprintf ppf "%s@\n" (Printexc.to_string exn)
81 abate 129 *)
82 abate 10
83 abate 90 let debug ppf = function
84 abate 43 | `Filter (t,p) ->
85     Format.fprintf ppf "[DEBUG:filter]@\n";
86 abate 107 let t = Typer.typ !glb_env t
87     and p = Typer.pat !glb_env p in
88 abate 43 let f = Patterns.filter (Types.descr t) p in
89     List.iter (fun (x,t) ->
90 abate 76 Format.fprintf ppf " %s:%a@\n" x
91 abate 43 print_norm (Types.descr t)) f
92 abate 136 | `Restrict (p,t) ->
93     Format.fprintf ppf "[DEBUG:restrict]@\n";
94     let t = Typer.typ !glb_env t
95     and p = Typer.pat !glb_env p in
96     (* let f = Patterns.restrict (Patterns.descr p) (Types.descr t) in
97     (match f with
98     | `Pat q -> Format.fprintf ppf "Pat: %a@\n" Patterns.print q
99     | `Accept -> Format.fprintf ppf "Accept@\n"
100     | `Reject -> Format.fprintf ppf "Reject@\n") *)
101     Patterns.demo ppf (Patterns.descr p) (Types.descr t)
102 abate 43 | `Accept p ->
103     Format.fprintf ppf "[DEBUG:accept]@\n";
104 abate 107 let p = Typer.pat !glb_env p in
105 abate 43 let t = Patterns.accept p in
106     Format.fprintf ppf " %a@\n" Types.Print.print t
107     | `Compile (t,pl) ->
108     Format.fprintf ppf "[DEBUG:compile]@\n";
109 abate 107 let t = Typer.typ !glb_env t
110     and pl = List.map (Typer.pat !glb_env) pl in
111 abate 43 let pl = Array.of_list
112     (List.map (fun p -> Patterns.Compile.normal
113     (Patterns.descr p)) pl) in
114     Patterns.Compile.show ppf (Types.descr t) pl
115 abate 75 | `Normal_record t ->
116     Format.fprintf ppf "[DEBUG:normal_record]@\n";
117 abate 107 let t = Types.descr (Typer.typ !glb_env t) in
118 abate 75 let count = ref 0 and seen = ref [] in
119     match Types.Record.first_label t with
120     | `Empty -> Format.fprintf ppf "Empty"
121     | `Any -> Format.fprintf ppf "Any"
122     | `Label l ->
123     let (pr,ab) = Types.Record.normal' t l in
124 abate 78 Format.fprintf ppf "Label (%s,@[" (Types.LabelPool.value l);
125 abate 75 List.iter (fun (d,n) ->
126     Format.fprintf ppf "%a => @[%a@];@\n"
127     Types.Print.print_descr d
128     Types.Print.print_descr n
129     ) pr;
130     Format.fprintf ppf "@] Absent: @[%a@])@\n"
131     Types.Print.print_descr
132     (match ab with Some x -> x | None -> Types.empty)
133     (*
134     | `Normal_record t ->
135     Format.fprintf ppf "[DEBUG:normal_record]@\n";
136 abate 107 let t = Types.descr (Typer.typ !glb_env t) in
137 abate 75 let r = Types.Record.normal t in
138     let count = ref 0 and seen = ref [] in
139     let rec aux ppf x =
140     try
141     let no = List.assq x !seen in
142     Format.fprintf ppf "[[%i]]" no
143     with Not_found ->
144     incr count;
145     seen := (x, !count) :: !seen;
146     Format.fprintf ppf "[[%i]]:" !count;
147     match x with
148     | `Success -> Format.fprintf ppf "Success"
149     | `Fail -> Format.fprintf ppf "Fail"
150     | `Label (l,pr,ab) ->
151     Format.fprintf ppf "Label (%s,@[" (Types.label_name l);
152     List.iter (fun (d,n) ->
153     Format.fprintf ppf "%a => @[%a@];@\n"
154     Types.Print.print_descr d
155     aux n
156     ) pr;
157     Format.fprintf ppf "@] Absent: @[%a@])" aux ab
158     in
159     Format.fprintf ppf "%a@\n" aux r
160     *)
161 abate 66
162    
163 abate 107
164 abate 90 let mk_builtin () =
165 abate 107 let bi = List.map (fun (n,t) -> [n, mk noloc (Ast.Internal t)])
166     Builtin.types in
167     glb_env := List.fold_left Typer.register_global_types !glb_env bi
168 abate 66
169 abate 95 let () = mk_builtin ()
170    
171    
172 abate 124 let run ppf ppf_err input =
173 abate 90 let insert_type_bindings =
174     List.iter (fun (x,t) ->
175     typing_env := Typer.Env.add x t !typing_env;
176     Format.fprintf ppf "|- %s : %a@\n@." x print_norm t)
177     in
178    
179     let type_decl decl =
180     insert_type_bindings (Typer.type_let_decl !typing_env decl)
181     in
182 abate 10
183 abate 90 let eval_decl decl =
184 abate 107 let bindings = Eval.eval_let_decl Eval.Env.empty decl in
185 abate 90 List.iter
186     (fun (x,v) ->
187     Eval.enter_global x v;
188 abate 92 Format.fprintf ppf "=> %s : @[%a@]@\n@." x print_value v
189 abate 90 ) bindings
190     in
191 abate 66
192 abate 126 let phrase ph =
193 abate 90 match ph.descr with
194     | Ast.EvalStatement e ->
195 abate 107 let (fv,e) = Typer.expr !glb_env e in
196 abate 90 let t = Typer.type_check !typing_env e Types.any true in
197 abate 93 Location.dump_loc ppf e.Typed.exp_loc;
198 abate 90 Format.fprintf ppf "|- %a@\n@." print_norm t;
199 abate 107 let v = Eval.eval Eval.Env.empty e in
200 abate 92 Format.fprintf ppf "=> @[%a@]@\n@." print_value v
201 abate 90 | Ast.LetDecl (p,{descr=Ast.Abstraction _}) -> ()
202     | Ast.LetDecl (p,e) ->
203 abate 107 let decl = Typer.let_decl !glb_env p e in
204 abate 90 type_decl decl;
205     eval_decl decl
206     | Ast.TypeDecl _ -> ()
207     | Ast.Debug l -> debug ppf l
208     | _ -> assert false
209     in
210    
211     let do_fun_decls decls =
212 abate 107 let decls = List.map (fun (p,e) -> Typer.let_decl !glb_env p e) decls in
213 abate 90 insert_type_bindings (Typer.type_rec_funs !typing_env decls);
214     List.iter eval_decl decls
215     in
216 abate 126 let rec phrases funs = function
217     | { descr = Ast.LetDecl (p,({descr=Ast.Abstraction _} as e))} :: phs ->
218     phrases ((p,e)::funs) phs
219     | ph :: phs ->
220     do_fun_decls funs;
221     phrase ph;
222     phrases [] phs
223     | _ ->
224     do_fun_decls funs
225     in
226 abate 13 try
227 abate 90 let p =
228     try Parser.prog input
229     with
230     | Stdpp.Exc_located (_, (Location _ as e)) -> raise e
231     | Stdpp.Exc_located (loc, e) -> raise (Location (loc, e))
232     in
233 abate 66 let (type_decls,fun_decls) =
234 abate 13 List.fold_left
235 abate 66 (fun ((typs,funs) as accu) ph -> match ph.descr with
236     | Ast.TypeDecl (x,t) -> ((x,t) :: typs,funs)
237     | Ast.LetDecl (p,({descr=Ast.Abstraction _} as e)) ->
238     (typs, (p,e)::funs)
239 abate 13 | _ -> accu
240 abate 66 ) ([],[]) p in
241 abate 107 glb_env := Typer.register_global_types !glb_env type_decls;
242 abate 126 phrases [] p;
243 abate 95 true
244 abate 28 with
245 abate 57 | (Failure _ | Not_found | Invalid_argument _) as e ->
246 abate 90 raise e (* To get ocamlrun stack trace *)
247 abate 124 | exn -> print_exn ppf_err exn; false
248 abate 10
249 abate 21

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