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

Contents of /driver/cduce.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 107 - (hide annotations)
Tue Jul 10 17:06:47 2007 UTC (5 years, 11 months ago) by abate
File size: 7524 byte(s)
[r2002-11-10 16:13:31 by cvscast] Empty log message

Original author: cvscast
Date: 2002-11-10 16:13:32+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     (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     Format.fprintf ppf "%s@\n" (Printexc.to_string exn)
79    
80 abate 90 let debug ppf = function
81 abate 43 | `Filter (t,p) ->
82     Format.fprintf ppf "[DEBUG:filter]@\n";
83 abate 107 let t = Typer.typ !glb_env t
84     and p = Typer.pat !glb_env p in
85 abate 43 let f = Patterns.filter (Types.descr t) p in
86     List.iter (fun (x,t) ->
87 abate 76 Format.fprintf ppf " %s:%a@\n" x
88 abate 43 print_norm (Types.descr t)) f
89     | `Accept p ->
90     Format.fprintf ppf "[DEBUG:accept]@\n";
91 abate 107 let p = Typer.pat !glb_env p in
92 abate 43 let t = Patterns.accept p in
93     Format.fprintf ppf " %a@\n" Types.Print.print t
94     | `Compile (t,pl) ->
95     Format.fprintf ppf "[DEBUG:compile]@\n";
96 abate 107 let t = Typer.typ !glb_env t
97     and pl = List.map (Typer.pat !glb_env) pl in
98 abate 43 let pl = Array.of_list
99     (List.map (fun p -> Patterns.Compile.normal
100     (Patterns.descr p)) pl) in
101     Patterns.Compile.show ppf (Types.descr t) pl
102 abate 75 | `Normal_record t ->
103     Format.fprintf ppf "[DEBUG:normal_record]@\n";
104 abate 107 let t = Types.descr (Typer.typ !glb_env t) in
105 abate 75 let count = ref 0 and seen = ref [] in
106     match Types.Record.first_label t with
107     | `Empty -> Format.fprintf ppf "Empty"
108     | `Any -> Format.fprintf ppf "Any"
109     | `Label l ->
110     let (pr,ab) = Types.Record.normal' t l in
111 abate 78 Format.fprintf ppf "Label (%s,@[" (Types.LabelPool.value l);
112 abate 75 List.iter (fun (d,n) ->
113     Format.fprintf ppf "%a => @[%a@];@\n"
114     Types.Print.print_descr d
115     Types.Print.print_descr n
116     ) pr;
117     Format.fprintf ppf "@] Absent: @[%a@])@\n"
118     Types.Print.print_descr
119     (match ab with Some x -> x | None -> Types.empty)
120     (*
121     | `Normal_record t ->
122     Format.fprintf ppf "[DEBUG:normal_record]@\n";
123 abate 107 let t = Types.descr (Typer.typ !glb_env t) in
124 abate 75 let r = Types.Record.normal t in
125     let count = ref 0 and seen = ref [] in
126     let rec aux ppf x =
127     try
128     let no = List.assq x !seen in
129     Format.fprintf ppf "[[%i]]" no
130     with Not_found ->
131     incr count;
132     seen := (x, !count) :: !seen;
133     Format.fprintf ppf "[[%i]]:" !count;
134     match x with
135     | `Success -> Format.fprintf ppf "Success"
136     | `Fail -> Format.fprintf ppf "Fail"
137     | `Label (l,pr,ab) ->
138     Format.fprintf ppf "Label (%s,@[" (Types.label_name l);
139     List.iter (fun (d,n) ->
140     Format.fprintf ppf "%a => @[%a@];@\n"
141     Types.Print.print_descr d
142     aux n
143     ) pr;
144     Format.fprintf ppf "@] Absent: @[%a@])" aux ab
145     in
146     Format.fprintf ppf "%a@\n" aux r
147     *)
148 abate 66
149    
150 abate 107
151 abate 90 let mk_builtin () =
152 abate 107 let bi = List.map (fun (n,t) -> [n, mk noloc (Ast.Internal t)])
153     Builtin.types in
154     glb_env := List.fold_left Typer.register_global_types !glb_env bi
155 abate 66
156 abate 95 let () = mk_builtin ()
157    
158    
159 abate 90 let run ppf input =
160     let insert_type_bindings =
161     List.iter (fun (x,t) ->
162     typing_env := Typer.Env.add x t !typing_env;
163     Format.fprintf ppf "|- %s : %a@\n@." x print_norm t)
164     in
165    
166     let type_decl decl =
167     insert_type_bindings (Typer.type_let_decl !typing_env decl)
168     in
169 abate 10
170 abate 90 let eval_decl decl =
171 abate 107 let bindings = Eval.eval_let_decl Eval.Env.empty decl in
172 abate 90 List.iter
173     (fun (x,v) ->
174     Eval.enter_global x v;
175 abate 92 Format.fprintf ppf "=> %s : @[%a@]@\n@." x print_value v
176 abate 90 ) bindings
177     in
178 abate 66
179 abate 90 let phrase ph =
180     match ph.descr with
181     | Ast.EvalStatement e ->
182 abate 107 let (fv,e) = Typer.expr !glb_env e in
183 abate 90 let t = Typer.type_check !typing_env e Types.any true in
184 abate 93 Location.dump_loc ppf e.Typed.exp_loc;
185 abate 90 Format.fprintf ppf "|- %a@\n@." print_norm t;
186 abate 107 let v = Eval.eval Eval.Env.empty e in
187 abate 92 Format.fprintf ppf "=> @[%a@]@\n@." print_value v
188 abate 90 | Ast.LetDecl (p,{descr=Ast.Abstraction _}) -> ()
189     | Ast.LetDecl (p,e) ->
190 abate 107 let decl = Typer.let_decl !glb_env p e in
191 abate 90 type_decl decl;
192     eval_decl decl
193     | Ast.TypeDecl _ -> ()
194     | Ast.Debug l -> debug ppf l
195     | _ -> assert false
196     in
197    
198     let do_fun_decls decls =
199 abate 107 let decls = List.map (fun (p,e) -> Typer.let_decl !glb_env p e) decls in
200 abate 90 insert_type_bindings (Typer.type_rec_funs !typing_env decls);
201     List.iter eval_decl decls
202     in
203 abate 13 try
204 abate 90 let p =
205     try Parser.prog input
206     with
207     | Stdpp.Exc_located (_, (Location _ as e)) -> raise e
208     | Stdpp.Exc_located (loc, e) -> raise (Location (loc, e))
209     in
210 abate 66 let (type_decls,fun_decls) =
211 abate 13 List.fold_left
212 abate 66 (fun ((typs,funs) as accu) ph -> match ph.descr with
213     | Ast.TypeDecl (x,t) -> ((x,t) :: typs,funs)
214     | Ast.LetDecl (p,({descr=Ast.Abstraction _} as e)) ->
215     (typs, (p,e)::funs)
216 abate 13 | _ -> accu
217 abate 66 ) ([],[]) p in
218 abate 107 glb_env := Typer.register_global_types !glb_env type_decls;
219 abate 66 do_fun_decls fun_decls;
220 abate 95 List.iter phrase p;
221     true
222 abate 28 with
223 abate 57 | (Failure _ | Not_found | Invalid_argument _) as e ->
224 abate 90 raise e (* To get ocamlrun stack trace *)
225 abate 95 | exn -> print_exn ppf exn; false
226 abate 10
227 abate 21

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