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

4
let version = "0.0.9 (alpha)"
5

6
7
let quiet = ref false

8
9
let typing_env = State.ref "Cduce.typing_env" Typer.Env.empty

10
11
12
13
14
let enter_global_value x v t =
  let x = Ident.ident x in
  Eval.enter_global x v;
  typing_env := Typer.Env.add x t !typing_env

15
16
17
18
19
let rec is_abstraction = function
  | Ast.Abstraction _ -> true
  | Ast.LocatedExpr (_,e) -> is_abstraction e
  | _ -> false

20
let print_norm ppf d = 
21
  Location.protect ppf 
22
    (fun ppf -> Types.Print.print ppf ((*Types.normalize*) d))
23
24
25

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

27
let dump_env ppf =
28
29
30
  Format.fprintf ppf "Global types:";
  Typer.dump_global_types ppf;
  Format.fprintf ppf ".@\n";
31
32
33
34
  Eval.Env.iter 
    (fun x v ->
       let t = Typer.Env.find x !typing_env in
       Format.fprintf ppf "@[|- %s : %a@ => %a@]@\n"
35
	 (Id.value x)
36
37
38
	 print_norm t
	 print_value v
    )
39
    !Eval.global_env
40
41
  

42
let rec print_exn ppf = function
43
  | Location (loc, exn) ->
44
      Format.fprintf ppf "Error %a:@\n" Location.print_loc loc;
45
      Format.fprintf ppf "%a" Location.html_hilight loc;
46
      print_exn ppf exn
47
48
  | Value.CDuceExn v ->
      Format.fprintf ppf "Uncaught CDuce exception: @[%a@]@\n"
49
        print_value v
50
51
  | Eval.MultipleDeclaration v ->
      Format.fprintf ppf "Multiple declaration for global value %s@\n" v
52
53
  | Typer.WrongLabel (t,l) ->
      Format.fprintf ppf "Wrong record selection: the label %s@\n" 
54
        (LabelPool.value l);
55
      Format.fprintf ppf "applied to an expression of type:@\n%a@\n"
56
        print_norm t
57
  | Typer.ShouldHave (t,msg) ->
58
      Format.fprintf ppf "This expression should have type:@\n%a@\n%s@\n" 
59
        print_norm t
60
        msg
61
  | Typer.ShouldHave2 (t1,msg,t2) ->
62
      Format.fprintf ppf "This expression should have type:@\n%a@\n%s %a@\n" 
63
64
65
        print_norm t1
        msg
        print_norm t2
66
  | Typer.Constraint (s,t,msg) ->
67
      Format.fprintf ppf "This expression should have type:@\n%a@\n" 
68
        print_norm t;
69
      Format.fprintf ppf "but its infered type is:@\n%a@\n" 
70
        print_norm s;
71
72
73
74
75
      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
76
77
  | Typer.NonExhaustive t ->
      Format.fprintf ppf "This pattern matching is not exhaustive@\n";
78
      Format.fprintf ppf "Residual type:@\n%a@\n"
79
	print_norm t;
80
      Format.fprintf ppf "Sample value:@\n%a@\n" 
81
	Types.Sample.print (Types.Sample.get t)
82
83
  | Typer.UnboundId x ->
      Format.fprintf ppf "Unbound identifier %s@\n" x
84
85
86
87
88
89
90
91
  | 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"
92
  | Parser.Error s | Stream.Error s -> 
93
      Format.fprintf ppf "Parsing error: %s@\n" s
94
95
  | Location.Generic s ->
      Format.fprintf ppf "%s@\n" s
96
  | exn ->
97
98
      raise exn
(*
99
      Format.fprintf ppf "%s@\n" (Printexc.to_string exn)
100
*)
101

102
let debug ppf = function
103
104
  | `Subtype (t1,t2) ->
      Format.fprintf ppf "[DEBUG:subtype]@\n";
105
106
      let t1 = Types.descr (Typer.typ t1)
      and t2 = Types.descr (Typer.typ t2) in
107
108
      Format.fprintf ppf "%a <= %a : %b@\n" print_norm t1 print_norm t2
	(Types.subtype t1 t2)
109
110
  | `Filter (t,p) -> 
      Format.fprintf ppf "[DEBUG:filter]@\n";
111
112
      let t = Typer.typ t
      and p = Typer.pat p in
113
114
      let f = Patterns.filter (Types.descr t) p in
      List.iter (fun (x,t) ->
115
		   Format.fprintf ppf " %s:%a@\n" (Id.value x)
116
		     print_norm (Types.descr t)) f
117
118
  | `Compile2 (t,pl) -> 
      Format.fprintf ppf "[DEBUG:compile2]@\n";
119
(*      let t = Types.descr (Typer.typ t) in
120
      let pl = List.map (fun p -> 
121
			   let p = Typer.pat p in
122
123
124
125
126
			   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 *)
      ()
127

128
129
  | `Accept p ->
      Format.fprintf ppf "[DEBUG:accept]@\n";
130
      let p = Typer.pat p in
131
      let t = Patterns.accept p in
132
      Format.fprintf ppf " %a@\n" Types.Print.print (Types.descr t)
133
134
  | `Compile (t,pl) ->
      Format.fprintf ppf "[DEBUG:compile]@\n";
135
136
      let t = Typer.typ t
      and pl = List.map Typer.pat pl in
137
      Patterns.Compile.debug_compile ppf t pl
138
  | `Normal_record p -> assert false
139
140


141

142
let mk_builtin () =
143
  let bi = List.map (fun (n,t) -> [n, mknoloc (Ast.Internal t)]) 
144
	     Builtin.types in
145
  List.iter Typer.register_global_types bi
146

147
148
149
let () = mk_builtin ()


150
let run ppf ppf_err input =
151
152
153
  let insert_type_bindings = 
    List.iter (fun (x,t) ->
		 typing_env := Typer.Env.add x t !typing_env;
154
		 if not !quiet then
155
		   Format.fprintf ppf "|- %s : %a@\n@." (Id.value x) print_norm t) 
156
157
158
  in
  
  let type_decl decl =
159
160
    insert_type_bindings (Typer.type_let_decl !typing_env decl);
    Typer.report_unused_branches ()
161
162
163
  in

  let eval_decl decl =
164
    let bindings = Eval.eval_let_decl Eval.Env.empty decl in
165
166
167
    List.iter 
      (fun (x,v) ->
	 Eval.enter_global x v;
168
	 if not !quiet then
169
	   Format.fprintf ppf "=> %s : @[%a@]@\n@." (Id.value x) print_value v
170
171
172
      ) bindings
  in

173
  let phrase ph = 
174
175
    match ph.descr with
      | Ast.EvalStatement e ->
176
	  let (fv,e) = Typer.expr e in
177
	  let t = Typer.type_check !typing_env e Types.any true in
178
	  Typer.report_unused_branches ();
179
	  Location.dump_loc ppf e.Typed.exp_loc;
180
181
	  if not !quiet then
	    Format.fprintf ppf "|- %a@\n@." print_norm t;
182
	  let v = Eval.eval Eval.Env.empty e in
183
184
	  if not !quiet then
	    Format.fprintf ppf "=> @[%a@]@\n@." print_value v
185
      | Ast.LetDecl (p,e) when is_abstraction e -> ()
186
      | Ast.LetDecl (p,e) ->
187
	  let decl = Typer.let_decl p e in
188
	  type_decl decl;
189
	  Typer.report_unused_branches ();
190
191
192
193
194
195
196
	  eval_decl decl
      | Ast.TypeDecl _ -> ()
      | Ast.Debug l -> debug ppf l
      | _ -> assert false
  in

  let do_fun_decls decls =
197
    let decls = List.map (fun (p,e) -> Typer.let_decl p e) decls in
198
    insert_type_bindings (Typer.type_rec_funs !typing_env decls);
199
    Typer.report_unused_branches ();
200
201
    List.iter eval_decl decls
  in
202
  let rec phrases funs = function
203
    | { descr = Ast.LetDecl (p,e) } :: phs when is_abstraction e -> 
204
205
206
207
208
209
210
211
	phrases ((p,e)::funs) phs
    | ph :: phs ->
	do_fun_decls funs;
	phrase ph;
	phrases [] phs
    | _ ->
	do_fun_decls funs
  in
212
  try 
213
214
215
216
    let p = 
      try Parser.prog input
      with
	| Stdpp.Exc_located (_, (Location _ as e)) -> raise e
217
	| Stdpp.Exc_located ((i,j), e) -> raise_loc i j e
218
    in
219
    let (type_decls,fun_decls) = 
220
      List.fold_left
221
222
	(fun ((typs,funs) as accu) ph -> match ph.descr with
	   | Ast.TypeDecl (x,t) -> ((x,t) :: typs,funs)
223
	   | Ast.LetDecl (p,e) when is_abstraction e -> 
224
	       (typs, (p,e)::funs)
225
	   | _ -> accu
226
	) ([],[]) p in
227
    Typer.register_global_types type_decls;
228
    phrases [] p;
229
    true
230
  with 
231
    | (Failure _ | Not_found | Invalid_argument _) as e -> 
232
	raise e  (* To get ocamlrun stack trace *)
233
    | exn -> print_exn ppf_err exn; false
234
	
235