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

Contents of /driver/cduce.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 446 - (hide annotations)
Tue Jul 10 17:35:30 2007 UTC (5 years, 10 months ago) by abate
File size: 7141 byte(s)
[r2003-05-26 19:54:58 by cvscast] toplevel: Ctrl-D, Ctrl-C, #quit, #env

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

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