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

Contents of /driver/cduce.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 95 - (hide annotations)
Tue Jul 10 17:05:54 2007 UTC (5 years, 10 months ago) by abate
File size: 6976 byte(s)
[r2002-11-10 02:21:45 by cvscast] Saving/restoring global state

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

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