cduce.ml 7.29 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" Env.empty
8

9 10
let eval_env = State.ref "Cduce.eval_env" Env.empty

11
let enter_global_value x v t =
12
  eval_env := Env.add x v !eval_env;
13
  typing_env := Env.add x t !typing_env
14

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 26 27 28
let print_sample ppf s =
  Location.protect ppf
    (fun ppf -> Sample.print ppf s)


29 30
let print_value ppf v =
  Location.protect ppf (fun ppf -> Value.print ppf v)
31

32
let dump_env ppf =
33 34
  Format.fprintf ppf "Global types:";
  Typer.dump_global_types ppf;
35
  Format.fprintf ppf ".@.";
36
  Env.iter 
37
    (fun x v ->
38
       let t = Env.find x !typing_env in
39 40
       Format.fprintf ppf "@[val %a : @[%a = %a@]@]@."
	 U.print (Id.value x) print_norm t print_value v
41
    )
42 43 44 45
    !eval_env;
  Format.fprintf ppf "Namespaces:@.";
  Atoms.Ns.dump_prefix_table ppf

46

47
let rec print_exn ppf = function
48 49 50
  | Location (loc, w, exn) ->
      Format.fprintf ppf "Error %a:@." Location.print_loc (loc,w);
      Format.fprintf ppf "%a" Location.html_hilight (loc,w);
51
      print_exn ppf exn
52
  | Value.CDuceExn v ->
53
      Format.fprintf ppf "Uncaught CDuce exception: @[%a@]@."
54
        print_value v
55
  | Eval.MultipleDeclaration v ->
56
      Format.fprintf ppf "Multiple declaration for global value %a@." 
57
        U.print (Id.value v)
58
  | Typer.WrongLabel (t,l) ->
59
      Format.fprintf ppf "Wrong record selection; field %a " 
60
        U.print (LabelPool.value l);
61
      Format.fprintf ppf "not present in an expression of type:@.%a@."
62
        print_norm t
63
  | Typer.ShouldHave (t,msg) ->
64
      Format.fprintf ppf "This expression should have type:@.%a@.%s@." 
65
        print_norm t
66
        msg
67
  | Typer.ShouldHave2 (t1,msg,t2) ->
68
      Format.fprintf ppf "This expression should have type:@.%a@.%s %a@." 
69 70 71
        print_norm t1
        msg
        print_norm t2
72
  | Typer.Error s ->
73
      Format.fprintf ppf "%s@." s
74
  | Typer.Constraint (s,t) ->
75
      Format.fprintf ppf "This expression should have type:@.%a@." 
76
        print_norm t;
77
      Format.fprintf ppf "but its inferred type is:@.%a@." 
78
        print_norm s;
79
      Format.fprintf ppf "which is not a subtype, as shown by the sample:@.%a@." 
80
	print_sample (Sample.get (Types.diff s t))
81
  | Typer.NonExhaustive t ->
82 83
      Format.fprintf ppf "This pattern matching is not exhaustive@.";
      Format.fprintf ppf "Residual type:@.%a@."
84
	print_norm t;
85
      Format.fprintf ppf "Sample:@.%a@." print_sample (Sample.get t)
86
  | Typer.UnboundId x ->
87
      Format.fprintf ppf "Unbound identifier %a@." U.print (Id.value x)
88
  | Wlexer.Illegal_character c ->
89
      Format.fprintf ppf "Illegal character (%s)@." (Char.escaped c)
90
  | Wlexer.Unterminated_comment ->
91
      Format.fprintf ppf "Comment not terminated@."
92
  | Wlexer.Unterminated_string ->
93
      Format.fprintf ppf "String literal not terminated@."
94
  | Wlexer.Unterminated_string_in_comment ->
95
      Format.fprintf ppf "This comment contains an unterminated string literal@."
96
  | Parser.Error s | Stream.Error s -> 
97
      Format.fprintf ppf "Parsing error: %s@." s
98
  | Location.Generic s ->
99
      Format.fprintf ppf "%s@." s
100
  | exn ->
101
(*      raise exn *)
102
      Format.fprintf ppf "%s@." (Printexc.to_string exn)
103

104
let debug ppf = function
105
  | `Subtype (t1,t2) ->
106
      Format.fprintf ppf "[DEBUG:subtype]@.";
107 108
      let t1 = Types.descr (Typer.typ t1)
      and t2 = Types.descr (Typer.typ t2) in
109
      Format.fprintf ppf "%a <= %a : %b@." print_norm t1 print_norm t2
110
	(Types.subtype t1 t2)
111
  | `Sample t ->
112 113 114 115 116 117
      Format.fprintf ppf "[DEBUG:sample]@.";
      (try
	 let t = Types.descr (Typer.typ t) in
	 Format.fprintf ppf "%a@." print_sample (Sample.get t)
       with Not_found ->
	 Format.fprintf ppf "Empty type : no sample !@.")
118
  | `Filter (t,p) -> 
119
      Format.fprintf ppf "[DEBUG:filter]@.";
120 121
      let t = Typer.typ t
      and p = Typer.pat p in
122 123
      let f = Patterns.filter (Types.descr t) p in
      List.iter (fun (x,t) ->
124
		   Format.fprintf ppf " %a:%a@." U.print (Id.value x)
125 126
		     print_norm (Types.descr t)) f
  | `Accept p ->
127
      Format.fprintf ppf "[DEBUG:accept]@.";
128
      let p = Typer.pat p in
129
      let t = Patterns.accept p in
130
      Format.fprintf ppf " %a@." Types.Print.print (Types.descr t)
131
  | `Compile (t,pl) ->
132
      Format.fprintf ppf "[DEBUG:compile]@.";
133 134
      let t = Typer.typ t
      and pl = List.map Typer.pat pl in
135
      Patterns.Compile.debug_compile ppf t pl
136

137 138 139 140
let insert_bindings ppf =
  List.iter2 
    (fun (x,t) (y,v) ->
       assert (x = y);
141 142 143
       typing_env := Env.add x t !typing_env;
       eval_env := Env.add x v !eval_env;
       if not !quiet then
144
	 Format.fprintf ppf "val %a : @[@[%a@] =@ @[%a@]@]@." 
145
	   U.print (Id.value x) print_norm t print_value v)
146
  
147 148 149 150 151
let rec collect_funs ppf accu = function
  | { descr = Ast.FunDecl e } :: rest -> 
      let (_,e) = Typer.expr e in
      collect_funs ppf (e::accu) rest
  | rest ->
152
      let typs = Typer.type_rec_funs !typing_env accu in
153
      Typer.report_unused_branches ();
154 155
      let vals = Eval.eval_rec_funs !eval_env accu in
      insert_bindings ppf typs vals;
156 157 158 159 160 161 162 163 164 165 166 167 168 169
      rest

let rec collect_types ppf accu = function
  | { descr = Ast.TypeDecl (x,t) } :: rest -> 
      collect_types ppf ((x,t) :: accu) rest
  | rest ->
      Typer.register_global_types accu;
      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)
170 171 172
  | { descr = Ast.SchemaDecl (name, schema) } :: rest ->
      Typer.register_schema name schema;
      phrases ppf rest
173 174 175
  | { descr = Ast.Namespace (pr,ns) } :: rest ->
      Typer.register_ns_prefix pr ns;
      phrases ppf rest
176 177 178 179 180
  | { descr = Ast.EvalStatement e } :: rest ->
      let (fv,e) = Typer.expr e in
      let t = Typer.type_check !typing_env e Types.any true in
      Typer.report_unused_branches ();
      if not !quiet then
181
	Location.dump_loc ppf (e.Typed.exp_loc,`Full);
182 183
      let v = Eval.eval !eval_env e in
      if not !quiet then
184
	Format.fprintf ppf "- : @[@[%a@] =@ @[%a@]@]@." print_norm t print_value v;
185 186 187
      phrases ppf rest
  | { descr = Ast.LetDecl (p,e) } :: rest ->
      let decl = Typer.let_decl p e in
188
      let typs = Typer.type_let_decl !typing_env decl in
189
      Typer.report_unused_branches ();
190 191
      let vals = Eval.eval_let_decl !eval_env decl in
      insert_bindings ppf typs vals;
192 193 194 195
      phrases ppf rest
  | { descr = Ast.Debug l } :: rest -> 
      debug ppf l;
      phrases ppf rest
196 197 198 199 200 201
  | { 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
202 203 204
  | [] -> ()

let run rule ppf ppf_err input =
205
  try 
206
    let p = 
207
      try rule input
208
      with
209
	| Stdpp.Exc_located (_, (Location _ as e)) ->
210
	    Parser.sync (); raise e
211
	| Stdpp.Exc_located ((i,j), e) -> 
212
	    Parser.sync (); raise_loc i j e
213
    in
214
    phrases ppf p;
215
    true
216
  with 
217 218 219
    | (End_of_file | Failure _ | Not_found | Invalid_argument _ | Sys.Break) 
	as e -> 
	raise e
220 221 222 223
    | exn -> 
	print_exn ppf_err exn;
	Format.fprintf ppf_err "@.";
	false
224

225
let script = run Parser.prog
226
let topinput = run Parser.top_phrases