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

4 5
let quiet = ref false

6 7
let typing_env = State.ref "Cduce.typing_env" Typer.Env.empty

8 9 10 11 12
let rec is_abstraction = function
  | Ast.Abstraction _ -> true
  | Ast.LocatedExpr (_,e) -> is_abstraction e
  | _ -> false

13
let print_norm ppf d = 
14
  Location.protect ppf 
15
    (fun ppf -> Types.Print.print_descr ppf ((*Types.normalize*) d))
16 17 18

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

20
let dump_env ppf =
21 22 23
  Format.fprintf ppf "Global types:";
  Typer.dump_global_types ppf;
  Format.fprintf ppf ".@\n";
24 25 26 27
  Eval.Env.iter 
    (fun x v ->
       let t = Typer.Env.find x !typing_env in
       Format.fprintf ppf "@[|- %s : %a@ => %a@]@\n"
28
	 (Id.value x)
29 30 31
	 print_norm t
	 print_value v
    )
32
    !Eval.global_env
33 34
  

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

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

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


127

128
let mk_builtin () =
129
  let bi = List.map (fun (n,t) -> [n, mknoloc (Ast.Internal t)]) 
130
	     Builtin.types in
131
  List.iter Typer.register_global_types bi
132

133 134 135
let () = mk_builtin ()


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

  let eval_decl decl =
150
    let bindings = Eval.eval_let_decl Eval.Env.empty decl in
151 152 153
    List.iter 
      (fun (x,v) ->
	 Eval.enter_global x v;
154
	 if not !quiet then
155
	   Format.fprintf ppf "=> %s : @[%a@]@\n@." (Id.value x) print_value v
156 157 158
      ) bindings
  in

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

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