cduce.ml 6.47 KB
Newer Older
1
open Location
2
open Ident
3

4
5
let quiet = ref false

6
7
8
let typing_env = State.ref "Cduce.typing_env" Typer.Env.empty
let eval_env = Eval.global_env

9
let print_norm ppf d = 
10
  Location.protect ppf 
11
    (fun ppf -> Types.Print.print_descr ppf ((*Types.normalize*) d))
12
13
14

let print_value ppf v =
  Location.protect ppf (fun ppf -> Value.print ppf v)
15

16
let dump_env ppf =
17
18
19
(*  Format.fprintf ppf "Global types:";
  List.iter (fun x _ -> Format.fprintf ppf " %s" x) (Typer.global_types ()); 
  Format.fprintf ppf ".@\n"; *)
20
21
22
23
  Eval.Env.iter 
    (fun x v ->
       let t = Typer.Env.find x !typing_env in
       Format.fprintf ppf "@[|- %s : %a@ => %a@]@\n"
24
	 (Id.value x)
25
26
27
28
29
30
	 print_norm t
	 print_value v
    )
    !eval_env
  

31
let rec print_exn ppf = function
32
  | Location (loc, exn) ->
33
      Format.fprintf ppf "Error %a:@\n" Location.print_loc loc;
34
      Format.fprintf ppf "%a" Location.html_hilight loc;
35
      print_exn ppf exn
36
37
  | Value.CDuceExn v ->
      Format.fprintf ppf "Uncaught CDuce exception: @[%a@]@\n"
38
        print_value v
39
40
  | Typer.WrongLabel (t,l) ->
      Format.fprintf ppf "Wrong record selection: the label %s@\n" 
41
        (LabelPool.value l);
42
      Format.fprintf ppf "applied to an expression of type %a@\n"
43
        print_norm t
44
45
  | Typer.ShouldHave (t,msg) ->
      Format.fprintf ppf "This expression should have type %a@\n%s@\n" 
46
        print_norm t
47
        msg
48
  | Typer.Constraint (s,t,msg) ->
49
      Format.fprintf ppf "This expression should have type %a@\n" 
50
        print_norm t;
51
      Format.fprintf ppf "but its infered type is: %a@\n" 
52
        print_norm s;
53
      Format.fprintf ppf "which is not a subtype, as shown by the value %a@\n" 
54
	Types.Sample.print (Types.Sample.get (Types.diff s t));
55
      Format.fprintf ppf "%s@\n" msg
56
57
58
  | Typer.NonExhaustive t ->
      Format.fprintf ppf "This pattern matching is not exhaustive@\n";
      Format.fprintf ppf "Residual type: %a@\n"
59
	print_norm t;
60
      Format.fprintf ppf "Sample value: %a@\n" 
61
	Types.Sample.print (Types.Sample.get t)
62
63
  | Typer.UnboundId x ->
      Format.fprintf ppf "Unbound identifier %s@\n" x
64
65
66
67
68
69
70
71
  | Wlexer.Illegal_character c ->
      Format.fprintf ppf "Illegal character (%s)@\n" (Char.escaped c)
  | Wlexer.Unterminated_comment ->
      Format.fprintf ppf "Comment not terminated@\n"
  | Wlexer.Unterminated_string ->
      Format.fprintf ppf "String literal not terminated@\n"
  | Wlexer.Unterminated_string_in_comment ->
      Format.fprintf ppf "This comment contains an unterminated string literal@\n"
72
  | Parser.Error s | Stream.Error s -> 
73
      Format.fprintf ppf "Parsing error: %s@\n" s
74
75
  | Location.Generic s ->
      Format.fprintf ppf "%s@\n" s
76
  | exn ->
77
78
      raise exn
(*
79
      Format.fprintf ppf "%s@\n" (Printexc.to_string exn)
80
*)
81

82
let debug ppf = function
83
84
  | `Subtype (t1,t2) ->
      Format.fprintf ppf "[DEBUG:subtype]@\n";
85
86
      let t1 = Types.descr (Typer.typ t1)
      and t2 = Types.descr (Typer.typ t2) in
87
88
      Format.fprintf ppf "%a <= %a : %b@\n" print_norm t1 print_norm t2
	(Types.subtype t1 t2)
89
90
  | `Filter (t,p) -> 
      Format.fprintf ppf "[DEBUG:filter]@\n";
91
92
      let t = Typer.typ t
      and p = Typer.pat p in
93
94
      let f = Patterns.filter (Types.descr t) p in
      List.iter (fun (x,t) ->
95
		   Format.fprintf ppf " %s:%a@\n" (Id.value x)
96
		     print_norm (Types.descr t)) f
97
98
  | `Compile2 (t,pl) -> 
      Format.fprintf ppf "[DEBUG:compile2]@\n";
99
(*      let t = Types.descr (Typer.typ t) in
100
      let pl = List.map (fun p -> 
101
			   let p = Typer.pat p in
102
103
104
105
106
			   let a = Types.descr (Patterns.accept p) in
			   (Some p, Types.cap a t)) pl in
      let d = Patterns.Compiler.make_dispatcher t pl in
      Patterns.Compiler.print_disp ppf d *)
      ()
107

108
109
  | `Accept p ->
      Format.fprintf ppf "[DEBUG:accept]@\n";
110
      let p = Typer.pat p in
111
112
113
114
      let t = Patterns.accept p in
      Format.fprintf ppf " %a@\n" Types.Print.print t
  | `Compile (t,pl) ->
      Format.fprintf ppf "[DEBUG:compile]@\n";
115
116
      let t = Typer.typ t
      and pl = List.map Typer.pat pl in
117
      Patterns.Compile.debug_compile ppf t pl
118
  | `Normal_record p -> assert false
119
120


121

122
let mk_builtin () =
123
  let bi = List.map (fun (n,t) -> [n, mknoloc (Ast.Internal t)]) 
124
	     Builtin.types in
125
  List.iter Typer.register_global_types bi
126

127
128
129
let () = mk_builtin ()


130
let run ppf ppf_err input =
131
132
133
  let insert_type_bindings = 
    List.iter (fun (x,t) ->
		 typing_env := Typer.Env.add x t !typing_env;
134
		 if not !quiet then
135
		   Format.fprintf ppf "|- %s : %a@\n@." (Id.value x) print_norm t) 
136
137
138
139
140
141
142
  in
  
  let type_decl decl =
    insert_type_bindings (Typer.type_let_decl !typing_env decl)
  in

  let eval_decl decl =
143
    let bindings = Eval.eval_let_decl Eval.Env.empty decl in
144
145
146
    List.iter 
      (fun (x,v) ->
	 Eval.enter_global x v;
147
	 if not !quiet then
148
	   Format.fprintf ppf "=> %s : @[%a@]@\n@." (Id.value x) print_value v
149
150
151
      ) bindings
  in

152
  let phrase ph = 
153
154
    match ph.descr with
      | Ast.EvalStatement e ->
155
	  let (fv,e) = Typer.expr e in
156
	  let t = Typer.type_check !typing_env e Types.any true in
157
	  Location.dump_loc ppf e.Typed.exp_loc;
158
159
	  if not !quiet then
	    Format.fprintf ppf "|- %a@\n@." print_norm t;
160
	  let v = Eval.eval Eval.Env.empty e in
161
162
	  if not !quiet then
	    Format.fprintf ppf "=> @[%a@]@\n@." print_value v
163
164
      | Ast.LetDecl (p,{descr=Ast.Abstraction _}) -> ()
      | Ast.LetDecl (p,e) ->
165
	  let decl = Typer.let_decl p e in
166
167
168
169
170
171
172
173
	  type_decl decl;
	  eval_decl decl
      | Ast.TypeDecl _ -> ()
      | Ast.Debug l -> debug ppf l
      | _ -> assert false
  in

  let do_fun_decls decls =
174
    let decls = List.map (fun (p,e) -> Typer.let_decl p e) decls in
175
176
177
    insert_type_bindings (Typer.type_rec_funs !typing_env decls);
    List.iter eval_decl decls
  in
178
179
180
181
182
183
184
185
186
187
  let rec phrases funs = function
    | { descr = Ast.LetDecl (p,({descr=Ast.Abstraction _} as e))} :: phs -> 
	phrases ((p,e)::funs) phs
    | ph :: phs ->
	do_fun_decls funs;
	phrase ph;
	phrases [] phs
    | _ ->
	do_fun_decls funs
  in
188
  try 
189
190
191
192
    let p = 
      try Parser.prog input
      with
	| Stdpp.Exc_located (_, (Location _ as e)) -> raise e
193
	| Stdpp.Exc_located ((i,j), e) -> raise_loc i j e
194
    in
195
    let (type_decls,fun_decls) = 
196
      List.fold_left
197
198
199
200
	(fun ((typs,funs) as accu) ph -> match ph.descr with
	   | Ast.TypeDecl (x,t) -> ((x,t) :: typs,funs)
	   | Ast.LetDecl (p,({descr=Ast.Abstraction _} as e)) -> 
	       (typs, (p,e)::funs)
201
	   | _ -> accu
202
	) ([],[]) p in
203
    Typer.register_global_types type_decls;
204
    phrases [] p;
205
    true
206
  with 
207
    | (Failure _ | Not_found | Invalid_argument _) as e -> 
208
	raise e  (* To get ocamlrun stack trace *)
209
    | exn -> print_exn ppf_err exn; false
210
	
211