cduce.ml 7.35 KB
Newer Older
1
2
open Location

3
4
5
6
let typing_env = State.ref "Cduce.typing_env" Typer.Env.empty
let glb_env = State.ref "Cduce.glb_env" Typer.Env.empty
let eval_env = Eval.global_env

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

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

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

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
35
  | Value.CDuceExn v ->
      Format.fprintf ppf "Uncaught CDuce exception: @[%a@]@\n"
36
        print_value v
37
38
  | Typer.WrongLabel (t,l) ->
      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
43
  | Typer.MultipleLabel l ->
      Format.fprintf ppf "Multiple occurences for the record label %s@\n"
44
        (Types.LabelPool.value l);
45
46
  | Typer.ShouldHave (t,msg) ->
      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
58
59
  | Typer.NonExhaustive t ->
      Format.fprintf ppf "This pattern matching is not exhaustive@\n";
      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
64
  | Typer.UnboundId x ->
      Format.fprintf ppf "Unbound identifier %s@\n" x
65
66
67
68
69
70
71
72
  | 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"
73
  | Parser.Error s | Stream.Error s -> 
74
      Format.fprintf ppf "Parsing error: %s@\n" s
75
76
  | Location.Generic s ->
      Format.fprintf ppf "%s@\n" s
77
78
79
  | exn ->
      Format.fprintf ppf "%s@\n" (Printexc.to_string exn)

80
let debug ppf = function
81
82
  | `Filter (t,p) -> 
      Format.fprintf ppf "[DEBUG:filter]@\n";
83
84
      let t = Typer.typ !glb_env t
      and p = Typer.pat !glb_env p in
85
86
      let f = Patterns.filter (Types.descr t) p in
      List.iter (fun (x,t) ->
87
		   Format.fprintf ppf " %s:%a@\n" x
88
89
90
		     print_norm (Types.descr t)) f
  | `Accept p ->
      Format.fprintf ppf "[DEBUG:accept]@\n";
91
      let p = Typer.pat !glb_env p in
92
93
94
95
      let t = Patterns.accept p in
      Format.fprintf ppf " %a@\n" Types.Print.print t
  | `Compile (t,pl) ->
      Format.fprintf ppf "[DEBUG:compile]@\n";
96
97
      let t = Typer.typ !glb_env t
      and pl = List.map (Typer.pat !glb_env) pl in
98
99
100
101
      let pl = Array.of_list 
		 (List.map (fun p -> Patterns.Compile.normal 
			      (Patterns.descr p)) pl) in
      Patterns.Compile.show ppf (Types.descr t) pl
102
103
  | `Normal_record t ->
      Format.fprintf ppf "[DEBUG:normal_record]@\n";
104
      let t = Types.descr (Typer.typ !glb_env t) in
105
106
107
108
109
110
      let count = ref 0 and seen = ref [] in
      match Types.Record.first_label t with
	    | `Empty -> Format.fprintf ppf "Empty"
	    | `Any -> Format.fprintf ppf "Any"
	    | `Label l ->
		let (pr,ab) = Types.Record.normal' t l in
111
		Format.fprintf ppf "Label (%s,@[" (Types.LabelPool.value l);
112
113
114
115
116
117
118
119
120
121
122
		List.iter (fun (d,n) ->
			     Format.fprintf ppf "%a => @[%a@];@\n"
			     Types.Print.print_descr d
			     Types.Print.print_descr n
			  ) pr;
		Format.fprintf ppf "@] Absent: @[%a@])@\n" 
		  Types.Print.print_descr 
		  (match ab with Some x -> x | None -> Types.empty)
(*
  | `Normal_record t ->
      Format.fprintf ppf "[DEBUG:normal_record]@\n";
123
      let t = Types.descr (Typer.typ !glb_env t) in
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
      let r = Types.Record.normal t in
      let count = ref 0 and seen = ref [] in
      let rec aux ppf x =
	try 
	  let no = List.assq x !seen in
	  Format.fprintf ppf "[[%i]]" no
	with Not_found ->
	  incr count;
	  seen := (x, !count) :: !seen;
	  Format.fprintf ppf "[[%i]]:" !count;
	  match x with
	    | `Success -> Format.fprintf ppf "Success"
	    | `Fail -> Format.fprintf ppf "Fail"
	    | `Label (l,pr,ab) ->
		Format.fprintf ppf "Label (%s,@[" (Types.label_name l);
		List.iter (fun (d,n) ->
			     Format.fprintf ppf "%a => @[%a@];@\n"
			     Types.Print.print_descr d
			     aux n
			  ) pr;
		Format.fprintf ppf "@] Absent: @[%a@])" aux ab
      in
      Format.fprintf ppf "%a@\n" aux r
*)
148
149


150

151
let mk_builtin () =
152
153
154
  let bi = List.map (fun (n,t) -> [n, mk noloc (Ast.Internal t)]) 
	     Builtin.types in
  glb_env := List.fold_left Typer.register_global_types !glb_env bi
155

156
157
158
let () = mk_builtin ()


159
160
161
162
163
164
165
166
167
168
169
170
let run ppf input =
  let insert_type_bindings = 
    List.iter (fun (x,t) ->
		 typing_env := Typer.Env.add x t !typing_env;
		 Format.fprintf ppf "|- %s : %a@\n@." x print_norm t) 
  in
  
  let type_decl decl =
    insert_type_bindings (Typer.type_let_decl !typing_env decl)
  in

  let eval_decl decl =
171
    let bindings = Eval.eval_let_decl Eval.Env.empty decl in
172
173
174
    List.iter 
      (fun (x,v) ->
	 Eval.enter_global x v;
175
	 Format.fprintf ppf "=> %s : @[%a@]@\n@." x print_value v
176
177
178
179
180
181
      ) bindings
  in

  let phrase ph =
    match ph.descr with
      | Ast.EvalStatement e ->
182
	  let (fv,e) = Typer.expr !glb_env e in
183
	  let t = Typer.type_check !typing_env e Types.any true in
184
	  Location.dump_loc ppf e.Typed.exp_loc;
185
	  Format.fprintf ppf "|- %a@\n@." print_norm t;
186
	  let v = Eval.eval Eval.Env.empty e in
187
	  Format.fprintf ppf "=> @[%a@]@\n@." print_value v
188
189
      | Ast.LetDecl (p,{descr=Ast.Abstraction _}) -> ()
      | Ast.LetDecl (p,e) ->
190
	  let decl = Typer.let_decl !glb_env p e in
191
192
193
194
195
196
197
198
	  type_decl decl;
	  eval_decl decl
      | Ast.TypeDecl _ -> ()
      | Ast.Debug l -> debug ppf l
      | _ -> assert false
  in

  let do_fun_decls decls =
199
    let decls = List.map (fun (p,e) -> Typer.let_decl !glb_env p e) decls in
200
201
202
    insert_type_bindings (Typer.type_rec_funs !typing_env decls);
    List.iter eval_decl decls
  in
203
  try 
204
205
206
207
208
209
    let p = 
      try Parser.prog input
      with
	| Stdpp.Exc_located (_, (Location _ as e)) -> raise e
	| Stdpp.Exc_located (loc, e) -> raise (Location (loc, e))
    in
210
    let (type_decls,fun_decls) = 
211
      List.fold_left
212
213
214
215
	(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)
216
	   | _ -> accu
217
	) ([],[]) p in
218
    glb_env := Typer.register_global_types !glb_env type_decls;
219
    do_fun_decls fun_decls;
220
221
    List.iter phrase p;
    true
222
  with 
223
    | (Failure _ | Not_found | Invalid_argument _) as e -> 
224
	raise e  (* To get ocamlrun stack trace *)
225
    | exn -> print_exn ppf exn; false
226
	
227