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

Contents of /driver/cduce.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 156 - (show annotations)
Tue Jul 10 17:11:11 2007 UTC (5 years, 11 months ago) by abate
File size: 8057 byte(s)
[r2002-11-25 23:29:46 by cvscast] Empty log message

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

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