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

4
5
let quiet = ref false

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

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

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

17
18
let dump_env ppf =
  Format.fprintf ppf "Global types:";
19
  Typer.TypeEnv.iter (fun x _ -> Format.fprintf ppf " %s" x) !glb_env;
20
21
22
23
24
  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"
25
	 (Id.value x)
26
27
28
29
30
31
	 print_norm t
	 print_value v
    )
    !eval_env
  

32
let rec print_exn ppf = function
33
  | Location (loc, exn) ->
34
      Format.fprintf ppf "Error %a:@\n" Location.print_loc loc;
35
      Format.fprintf ppf "%a" Location.html_hilight loc;
36
      print_exn ppf exn
37
38
  | Value.CDuceExn v ->
      Format.fprintf ppf "Uncaught CDuce exception: @[%a@]@\n"
39
        print_value v
40
41
  | Typer.WrongLabel (t,l) ->
      Format.fprintf ppf "Wrong record selection: the label %s@\n" 
42
        (LabelPool.value l);
43
      Format.fprintf ppf "applied to an expression of type %a@\n"
44
        print_norm t
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
  | exn ->
78
79
      raise exn
(*
80
      Format.fprintf ppf "%s@\n" (Printexc.to_string exn)
81
*)
82

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

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


122

123
let mk_builtin () =
124
125
126
  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
127

128
129
130
let () = mk_builtin ()


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

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

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

  let do_fun_decls decls =
175
    let decls = List.map (fun (p,e) -> Typer.let_decl !glb_env p e) decls in
176
177
178
    insert_type_bindings (Typer.type_rec_funs !typing_env decls);
    List.iter eval_decl decls
  in
179
180
181
182
183
184
185
186
187
188
  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
189
  try 
190
191
192
193
194
195
    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
196
    let (type_decls,fun_decls) = 
197
      List.fold_left
198
199
200
201
	(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)
202
	   | _ -> accu
203
	) ([],[]) p in
204
    glb_env := Typer.register_global_types !glb_env type_decls;
205
    phrases [] p;
206
    true
207
  with 
208
    | (Failure _ | Not_found | Invalid_argument _) as e -> 
209
	raise e  (* To get ocamlrun stack trace *)
210
    | exn -> print_exn ppf_err exn; false
211
	
212