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

4
let quiet = ref false
5
let toplevel = ref false
6

7
let typing_env = State.ref "Cduce.typing_env" Builtin.env
8
let eval_env = State.ref "Cduce.eval_env" Env.empty
9
10
11
12
13
14
15
16
17
18
19
let compile_env = State.ref "Cduce.compile_env" Compile.empty

let do_compile = ref false

let get_global_value v =
  if !do_compile 
  then Eval.L.eval_var (Compile.find v !compile_env)
  else Env.find v !eval_env

let get_global_type v =
  Typer.find_value v !typing_env
20

21
let enter_global_value x v t =
22
  typing_env := Typer.enter_value x t !typing_env;
23

24
25
26
27
  if !do_compile 
  then (compile_env := Compile.enter_global !compile_env x; Eval.L.push v)
  else eval_env := Env.add x v !eval_env
  
28
29
30
31
32
let rec is_abstraction = function
  | Ast.Abstraction _ -> true
  | Ast.LocatedExpr (_,e) -> is_abstraction e
  | _ -> false

33
let print_norm ppf d = 
34
  Location.protect ppf 
35
    (fun ppf -> Types.Print.print ppf ((*Types.normalize*) d))
36

37
38
39
40
let print_sample ppf s =
  Location.protect ppf
    (fun ppf -> Sample.print ppf s)

41
42
43
let print_protect ppf s =
  Location.protect ppf (fun ppf -> Format.fprintf ppf "%s" s)

44
45
let print_value ppf v =
  Location.protect ppf (fun ppf -> Value.print ppf v)
46

47
let dump_env ppf =
48
49
  Format.fprintf ppf "Types:%a@." Typer.dump_types !typing_env;
  Format.fprintf ppf "Namespace prefixes:@\n%a" Typer.dump_ns !typing_env;
50
  Format.fprintf ppf "Namespace prefixes used for pretty-printing:@.%t"
51
    Ns.InternalPrinter.dump;
52
  Format.fprintf ppf "Values:@.";
53
  Env.iter 
54
    (fun x v ->
55
       let t = Typer.find_value x !typing_env in
56
57
       Format.fprintf ppf "@[val %a : @[%a = %a@]@]@."
	 U.print (Id.value x) print_norm t print_value v
58
    )
59
60
    !eval_env;
  Eval.L.dump ppf
61

62
let rec print_exn ppf = function
63
64
  | Location (loc, w, exn) ->
      Format.fprintf ppf "Error %a:@." Location.print_loc (loc,w);
65
      Format.fprintf ppf "%a" Location.html_hilight (loc,w); 
66
      print_exn ppf exn
67
  | Value.CDuceExn v ->
68
      Format.fprintf ppf "Uncaught CDuce exception: @[%a@]@."
69
        print_value v
70
  | Eval.MultipleDeclaration v ->
71
      Format.fprintf ppf "Multiple declaration for global value %a@." 
72
        U.print (Id.value v)
73
  | Typer.WrongLabel (t,l) ->
74
      Format.fprintf ppf "Wrong record selection; field %a " 
75
        Label.print (LabelPool.value l);
76
      Format.fprintf ppf "not present in an expression of type:@.%a@."
77
        print_norm t
78
  | Typer.ShouldHave (t,msg) ->
79
      Format.fprintf ppf "This expression should have type:@.%a@.%a@." 
80
        print_norm t
81
        print_protect msg
82
  | Typer.ShouldHave2 (t1,msg,t2) ->
83
      Format.fprintf ppf "This expression should have type:@.%a@.%a %a@." 
84
        print_norm t1
85
        print_protect msg
86
        print_norm t2
87
  | Typer.Error s ->
88
      Format.fprintf ppf "%a@." print_protect s
89
  | Typer.Constraint (s,t) ->
90
      Format.fprintf ppf "This expression should have type:@.%a@." 
91
        print_norm t;
92
      Format.fprintf ppf "but its inferred type is:@.%a@." 
93
        print_norm s;
94
      Format.fprintf ppf "which is not a subtype, as shown by the sample:@.%a@." 
95
	print_sample (Sample.get (Types.diff s t))
96
  | Typer.NonExhaustive t ->
97
98
      Format.fprintf ppf "This pattern matching is not exhaustive@.";
      Format.fprintf ppf "Residual type:@.%a@."
99
	print_norm t;
100
      Format.fprintf ppf "Sample:@.%a@." print_sample (Sample.get t)
101
102
103
  | Typer.UnboundId (x,tn) ->
      Format.fprintf ppf "Unbound identifier %a%s@." U.print (Id.value x)
        (if tn then " (it is a type name)" else "")
104
105
106
107
  | Ulexer.Error (i,j,s) ->
      let loc = Location.loc_of_pos (i,j), `Full in
      Format.fprintf ppf "Error %a:@." Location.print_loc loc;
      Format.fprintf ppf "%a%s" Location.html_hilight loc s
108
  | Parser.Error s | Stream.Error s -> 
109
      Format.fprintf ppf "Parsing error: %a@." print_protect s
110
  | Location.Generic s ->
111
      Format.fprintf ppf "%a@." print_protect s
112
  | exn ->
113
(*      raise exn *)
114
      Format.fprintf ppf "%a@." print_protect (Printexc.to_string exn)
115

116
let debug ppf = function
117
  | `Subtype (t1,t2) ->
118
      Format.fprintf ppf "[DEBUG:subtype]@.";
119
120
      let t1 = Types.descr (Typer.typ !typing_env t1)
      and t2 = Types.descr (Typer.typ !typing_env t2) in
121
122
      let s = Types.subtype t1 t2 in
      Format.fprintf ppf "%a %a %a : %b@." print_norm t1 print_protect "<=" print_norm t2 s
123
  | `Sample t ->
124
125
      Format.fprintf ppf "[DEBUG:sample]@.";
      (try
126
	 let t = Types.descr (Typer.typ !typing_env t) in
127
128
129
	 Format.fprintf ppf "%a@." print_sample (Sample.get t)
       with Not_found ->
	 Format.fprintf ppf "Empty type : no sample !@.")
130
  | `Filter (t,p) -> 
131
      Format.fprintf ppf "[DEBUG:filter]@.";
132
133
      let t = Typer.typ !typing_env t
      and p = Typer.pat !typing_env p in
134
135
      let f = Patterns.filter (Types.descr t) p in
      List.iter (fun (x,t) ->
136
		   Format.fprintf ppf " %a:%a@." U.print (Id.value x)
137
138
		     print_norm (Types.descr t)) f
  | `Accept p ->
139
      Format.fprintf ppf "[DEBUG:accept]@.";
140
      let p = Typer.pat !typing_env p in
141
      let t = Patterns.accept p in
142
      Format.fprintf ppf " %a@." Types.Print.print (Types.descr t)
143
  | `Compile (t,pl) ->
144
      Format.fprintf ppf "[DEBUG:compile]@.";
145
146
      let t = Typer.typ !typing_env t
      and pl = List.map (Typer.pat !typing_env) pl in
147
      Patterns.Compile.debug_compile ppf t pl
148

149
150
151
152
let insert_bindings ppf =
  List.iter2 
    (fun (x,t) (y,v) ->
       assert (x = y);
153
       typing_env := Typer.enter_value x t !typing_env;
154
155
       eval_env := Env.add x v !eval_env;
       if not !quiet then
156
	 Format.fprintf ppf "val %a : @[@[%a@] =@ @[%a@]@]@." 
157
	   U.print (Id.value x) print_norm t print_value v)
158
159
160
161
162
163
164
165
166

let display ppf =
  List.iter
    (fun x ->
       let t = get_global_type x in
       let v = get_global_value x in
       if not !quiet then
	 Format.fprintf ppf "val %a : @[@[%a@] =@ @[%a@]@]@." 
	   U.print (Id.value x) print_norm t print_value v)
167
  
168
169
let rec collect_funs ppf accu = function
  | { descr = Ast.FunDecl e } :: rest -> 
170
      let (_,e) = Typer.expr !typing_env e in
171
172
      collect_funs ppf (e::accu) rest
  | rest ->
173
      let typs = Typer.type_rec_funs !typing_env accu in
174
      Typer.report_unused_branches ();
175
176
177
178
179
180
181
182
183
184
185

      if !do_compile then
	let (names,env,funs) = Compile.compile_rec_funs !compile_env accu in
	Eval.L.eval_rec_funs funs;
	typing_env := Typer.enter_values typs !typing_env;
	compile_env := env;
	display ppf names
      else (
	let vals = Eval.eval_rec_funs !eval_env accu in
	insert_bindings ppf typs vals);
	
186
187
188
189
190
191
      rest

let rec collect_types ppf accu = function
  | { descr = Ast.TypeDecl (x,t) } :: rest -> 
      collect_types ppf ((x,t) :: accu) rest
  | rest ->
192
193
      typing_env := 
        Typer.enter_types (Typer.type_defs !typing_env accu) !typing_env;
194
195
196
197
198
199
200
      rest

let rec phrases ppf phs = match phs with
  | { descr = Ast.FunDecl _ } :: _ -> 
      phrases ppf (collect_funs ppf [] phs)
  | { descr = Ast.TypeDecl (_,_) } :: _ ->
      phrases ppf (collect_types ppf [] phs)
201
202
203
  | { descr = Ast.SchemaDecl (name, schema) } :: rest ->
      Typer.register_schema name schema;
      phrases ppf rest
204
  | { descr = Ast.Namespace (pr,ns) } :: rest ->
205
      typing_env := Typer.enter_ns pr ns !typing_env;
206
      phrases ppf rest
207
  | { descr = Ast.EvalStatement e } :: rest ->
208
      let (fv,e) = Typer.expr !typing_env e in
209
210
      let t = Typer.type_check !typing_env e Types.any true in
      Typer.report_unused_branches ();
211

212
      if not !quiet then
213
	Location.dump_loc ppf (e.Typed.exp_loc,`Full);
214
215
216
217
218
219
220
221
222
223
224
225
226

      if !do_compile then
	let e = Compile.compile !compile_env false e in 
	let v = Eval.L.eval e in
	if not !quiet then
	  Format.fprintf ppf "- : @[@[%a@] =@ @[%a@]@]@." 
	    print_norm t print_value v; 
      else
	(let v = Eval.eval !eval_env e in
	if not !quiet then
	  Format.fprintf ppf "- : @[@[%a@] =@ @[%a@]@]@." 
	    print_norm t print_value v );

227
228
      phrases ppf rest
  | { descr = Ast.LetDecl (p,e) } :: rest ->
229
      let decl = Typer.let_decl !typing_env p e in
230
      let typs = Typer.type_let_decl !typing_env decl in
231
      Typer.report_unused_branches ();
232
233
234
235
236
237
238
239
240
241
242

      if !do_compile then
	let (names,env,decl) = Compile.compile_let_decl !compile_env decl in
	Eval.L.eval_let_decl decl;
	typing_env := Typer.enter_values typs !typing_env;
	compile_env := env;
	display ppf names
      else
	(let vals = Eval.eval_let_decl !eval_env decl in
	insert_bindings ppf typs vals);

243
244
      phrases ppf rest
  | { descr = Ast.Debug l } :: rest -> 
245
      debug ppf l; 
246
      phrases ppf rest
247
248
249
250
251
252
  | { descr = Ast.Directive `Quit } :: rest ->
      if !toplevel then raise End_of_file;
      phrases ppf rest
  | { descr = Ast.Directive `Env } :: rest ->
      dump_env ppf;
      phrases ppf rest
253
  | { descr = Ast.Directive `Reinit_ns } :: rest ->
254
      Typer.set_ns_table_for_printer !typing_env;
255
      phrases ppf rest
256
257
258
  | [] -> ()

let run rule ppf ppf_err input =
259
  Typer.clear_unused_branches ();
260
  try 
261
    let p = 
262
      try rule input
263
      with
264
	| Stdpp.Exc_located (_, (Location _ as e)) ->
265
	    Parser.sync (); raise e
266
	| Stdpp.Exc_located ((i,j), e) -> 
267
	    Parser.sync (); raise_loc i j e
268
    in
269
    phrases ppf p;
270
    true
271
  with 
272
273
274
    | (End_of_file | Failure _ | Not_found | Invalid_argument _ | Sys.Break) 
	as e -> 
	raise e
275
276
277
278
    | exn -> 
	print_exn ppf_err exn;
	Format.fprintf ppf_err "@.";
	false
279

280
let script = run Parser.prog
281
let topinput = run Parser.top_phrases
282
283
284
285
286
287

let serialize_typing_env t () =
  Typer.serialize t !typing_env

let deserialize_typing_env t =
  typing_env := Typer.deserialize t