| 1 |
abate |
10 |
open Location
|
| 2 |
abate |
225 |
open Ident
|
| 3 |
abate |
10 |
|
| 4 |
abate |
217 |
let quiet = ref false
|
| 5 |
abate |
446 |
let toplevel = ref false
|
| 6 |
abate |
217 |
|
| 7 |
abate |
421 |
let typing_env = State.ref "Cduce.typing_env" Env.empty
|
| 8 |
abate |
107 |
|
| 9 |
abate |
431 |
let eval_env = State.ref "Cduce.eval_env" Env.empty
|
| 10 |
|
|
|
| 11 |
abate |
368 |
let enter_global_value x v t =
|
| 12 |
abate |
431 |
eval_env := Env.add x v !eval_env;
|
| 13 |
abate |
421 |
typing_env := Env.add x t !typing_env
|
| 14 |
abate |
368 |
|
| 15 |
abate |
332 |
let rec is_abstraction = function
|
| 16 |
|
|
| Ast.Abstraction _ -> true
|
| 17 |
|
|
| Ast.LocatedExpr (_,e) -> is_abstraction e
|
| 18 |
|
|
| _ -> false
|
| 19 |
|
|
|
| 20 |
abate |
29 |
let print_norm ppf d =
|
| 21 |
abate |
92 |
Location.protect ppf
|
| 22 |
abate |
367 |
(fun ppf -> Types.Print.print ppf ((*Types.normalize*) d))
|
| 23 |
abate |
29 |
|
| 24 |
abate |
423 |
let print_sample ppf s =
|
| 25 |
|
|
Location.protect ppf
|
| 26 |
|
|
(fun ppf -> Sample.print ppf s)
|
| 27 |
|
|
|
| 28 |
abate |
541 |
let print_protect ppf s =
|
| 29 |
|
|
Location.protect ppf (fun ppf -> Format.fprintf ppf "%s" s)
|
| 30 |
abate |
423 |
|
| 31 |
abate |
92 |
let print_value ppf v =
|
| 32 |
|
|
Location.protect ppf (fun ppf -> Value.print ppf v)
|
| 33 |
|
|
|
| 34 |
abate |
107 |
let dump_env ppf =
|
| 35 |
abate |
553 |
Format.fprintf ppf "Types:%t@." Typer.dump_global_types;
|
| 36 |
abate |
552 |
Format.fprintf ppf "Namespace prefixes:@\n%t" Typer.dump_global_ns;
|
| 37 |
abate |
553 |
Format.fprintf ppf "Namespace prefixes used for pretty-printing:@.%t"
|
| 38 |
abate |
552 |
Ns.InternalPrinter.dump;
|
| 39 |
|
|
Format.fprintf ppf "Values:@\n";
|
| 40 |
abate |
421 |
Env.iter
|
| 41 |
abate |
107 |
(fun x v ->
|
| 42 |
abate |
421 |
let t = Env.find x !typing_env in
|
| 43 |
abate |
433 |
Format.fprintf ppf "@[val %a : @[%a = %a@]@]@."
|
| 44 |
|
|
U.print (Id.value x) print_norm t print_value v
|
| 45 |
abate |
107 |
)
|
| 46 |
abate |
552 |
!eval_env
|
| 47 |
abate |
107 |
|
| 48 |
abate |
10 |
let rec print_exn ppf = function
|
| 49 |
abate |
522 |
| Location (loc, w, exn) ->
|
| 50 |
|
|
Format.fprintf ppf "Error %a:@." Location.print_loc (loc,w);
|
| 51 |
abate |
622 |
Format.fprintf ppf "%a" Location.html_hilight (loc,w);
|
| 52 |
abate |
91 |
print_exn ppf exn
|
| 53 |
abate |
64 |
| Value.CDuceExn v ->
|
| 54 |
abate |
433 |
Format.fprintf ppf "Uncaught CDuce exception: @[%a@]@."
|
| 55 |
abate |
92 |
print_value v
|
| 56 |
abate |
368 |
| Eval.MultipleDeclaration v ->
|
| 57 |
abate |
433 |
Format.fprintf ppf "Multiple declaration for global value %a@."
|
| 58 |
abate |
374 |
U.print (Id.value v)
|
| 59 |
abate |
26 |
| Typer.WrongLabel (t,l) ->
|
| 60 |
abate |
529 |
Format.fprintf ppf "Wrong record selection; field %a "
|
| 61 |
abate |
542 |
Label.print (LabelPool.value l);
|
| 62 |
abate |
529 |
Format.fprintf ppf "not present in an expression of type:@.%a@."
|
| 63 |
abate |
29 |
print_norm t
|
| 64 |
abate |
19 |
| Typer.ShouldHave (t,msg) ->
|
| 65 |
abate |
622 |
Format.fprintf ppf "This expression should have type:@.%a@.%a@."
|
| 66 |
abate |
29 |
print_norm t
|
| 67 |
abate |
622 |
print_protect msg
|
| 68 |
abate |
355 |
| Typer.ShouldHave2 (t1,msg,t2) ->
|
| 69 |
abate |
622 |
Format.fprintf ppf "This expression should have type:@.%a@.%a %a@."
|
| 70 |
abate |
355 |
print_norm t1
|
| 71 |
abate |
622 |
print_protect msg
|
| 72 |
abate |
355 |
print_norm t2
|
| 73 |
abate |
421 |
| Typer.Error s ->
|
| 74 |
abate |
622 |
Format.fprintf ppf "%a@." print_protect s
|
| 75 |
abate |
421 |
| Typer.Constraint (s,t) ->
|
| 76 |
abate |
433 |
Format.fprintf ppf "This expression should have type:@.%a@."
|
| 77 |
abate |
29 |
print_norm t;
|
| 78 |
abate |
433 |
Format.fprintf ppf "but its inferred type is:@.%a@."
|
| 79 |
abate |
29 |
print_norm s;
|
| 80 |
abate |
433 |
Format.fprintf ppf "which is not a subtype, as shown by the sample:@.%a@."
|
| 81 |
abate |
423 |
print_sample (Sample.get (Types.diff s t))
|
| 82 |
abate |
17 |
| Typer.NonExhaustive t ->
|
| 83 |
abate |
433 |
Format.fprintf ppf "This pattern matching is not exhaustive@.";
|
| 84 |
|
|
Format.fprintf ppf "Residual type:@.%a@."
|
| 85 |
abate |
29 |
print_norm t;
|
| 86 |
abate |
433 |
Format.fprintf ppf "Sample:@.%a@." print_sample (Sample.get t)
|
| 87 |
abate |
36 |
| Typer.UnboundId x ->
|
| 88 |
abate |
433 |
Format.fprintf ppf "Unbound identifier %a@." U.print (Id.value x)
|
| 89 |
abate |
81 |
| Wlexer.Illegal_character c ->
|
| 90 |
abate |
622 |
Format.fprintf ppf "Illegal character (%a)@." print_protect (Char.escaped c)
|
| 91 |
abate |
81 |
| Wlexer.Unterminated_comment ->
|
| 92 |
abate |
433 |
Format.fprintf ppf "Comment not terminated@."
|
| 93 |
abate |
81 |
| Wlexer.Unterminated_string ->
|
| 94 |
abate |
433 |
Format.fprintf ppf "String literal not terminated@."
|
| 95 |
abate |
81 |
| Wlexer.Unterminated_string_in_comment ->
|
| 96 |
abate |
433 |
Format.fprintf ppf "This comment contains an unterminated string literal@."
|
| 97 |
abate |
90 |
| Parser.Error s | Stream.Error s ->
|
| 98 |
abate |
622 |
Format.fprintf ppf "Parsing error: %a@." print_protect s
|
| 99 |
abate |
91 |
| Location.Generic s ->
|
| 100 |
abate |
622 |
Format.fprintf ppf "%a@." print_protect s
|
| 101 |
abate |
10 |
| exn ->
|
| 102 |
abate |
403 |
(* raise exn *)
|
| 103 |
abate |
622 |
Format.fprintf ppf "%a@." print_protect (Printexc.to_string exn)
|
| 104 |
abate |
10 |
|
| 105 |
abate |
90 |
let debug ppf = function
|
| 106 |
abate |
224 |
| `Subtype (t1,t2) ->
|
| 107 |
abate |
433 |
Format.fprintf ppf "[DEBUG:subtype]@.";
|
| 108 |
abate |
277 |
let t1 = Types.descr (Typer.typ t1)
|
| 109 |
|
|
and t2 = Types.descr (Typer.typ t2) in
|
| 110 |
abate |
541 |
let s = Types.subtype t1 t2 in
|
| 111 |
|
|
Format.fprintf ppf "%a %a %a : %b@." print_norm t1 print_protect "<=" print_norm t2 s
|
| 112 |
abate |
407 |
| `Sample t ->
|
| 113 |
abate |
433 |
Format.fprintf ppf "[DEBUG:sample]@.";
|
| 114 |
|
|
(try
|
| 115 |
|
|
let t = Types.descr (Typer.typ t) in
|
| 116 |
|
|
Format.fprintf ppf "%a@." print_sample (Sample.get t)
|
| 117 |
|
|
with Not_found ->
|
| 118 |
|
|
Format.fprintf ppf "Empty type : no sample !@.")
|
| 119 |
abate |
43 |
| `Filter (t,p) ->
|
| 120 |
abate |
433 |
Format.fprintf ppf "[DEBUG:filter]@.";
|
| 121 |
abate |
277 |
let t = Typer.typ t
|
| 122 |
|
|
and p = Typer.pat p in
|
| 123 |
abate |
43 |
let f = Patterns.filter (Types.descr t) p in
|
| 124 |
|
|
List.iter (fun (x,t) ->
|
| 125 |
abate |
433 |
Format.fprintf ppf " %a:%a@." U.print (Id.value x)
|
| 126 |
abate |
43 |
print_norm (Types.descr t)) f
|
| 127 |
|
|
| `Accept p ->
|
| 128 |
abate |
433 |
Format.fprintf ppf "[DEBUG:accept]@.";
|
| 129 |
abate |
277 |
let p = Typer.pat p in
|
| 130 |
abate |
43 |
let t = Patterns.accept p in
|
| 131 |
abate |
433 |
Format.fprintf ppf " %a@." Types.Print.print (Types.descr t)
|
| 132 |
abate |
43 |
| `Compile (t,pl) ->
|
| 133 |
abate |
433 |
Format.fprintf ppf "[DEBUG:compile]@.";
|
| 134 |
abate |
277 |
let t = Typer.typ t
|
| 135 |
|
|
and pl = List.map Typer.pat pl in
|
| 136 |
abate |
149 |
Patterns.Compile.debug_compile ppf t pl
|
| 137 |
abate |
66 |
|
| 138 |
abate |
433 |
let insert_bindings ppf =
|
| 139 |
|
|
List.iter2
|
| 140 |
|
|
(fun (x,t) (y,v) ->
|
| 141 |
|
|
assert (x = y);
|
| 142 |
abate |
431 |
typing_env := Env.add x t !typing_env;
|
| 143 |
|
|
eval_env := Env.add x v !eval_env;
|
| 144 |
|
|
if not !quiet then
|
| 145 |
abate |
502 |
Format.fprintf ppf "val %a : @[@[%a@] =@ @[%a@]@]@."
|
| 146 |
abate |
433 |
U.print (Id.value x) print_norm t print_value v)
|
| 147 |
abate |
90 |
|
| 148 |
abate |
431 |
let rec collect_funs ppf accu = function
|
| 149 |
|
|
| { descr = Ast.FunDecl e } :: rest ->
|
| 150 |
|
|
let (_,e) = Typer.expr e in
|
| 151 |
|
|
collect_funs ppf (e::accu) rest
|
| 152 |
|
|
| rest ->
|
| 153 |
abate |
433 |
let typs = Typer.type_rec_funs !typing_env accu in
|
| 154 |
abate |
431 |
Typer.report_unused_branches ();
|
| 155 |
abate |
433 |
let vals = Eval.eval_rec_funs !eval_env accu in
|
| 156 |
|
|
insert_bindings ppf typs vals;
|
| 157 |
abate |
431 |
rest
|
| 158 |
abate |
10 |
|
| 159 |
abate |
431 |
let rec collect_types ppf accu = function
|
| 160 |
|
|
| { descr = Ast.TypeDecl (x,t) } :: rest ->
|
| 161 |
|
|
collect_types ppf ((x,t) :: accu) rest
|
| 162 |
|
|
| rest ->
|
| 163 |
|
|
Typer.register_global_types accu;
|
| 164 |
|
|
rest
|
| 165 |
abate |
66 |
|
| 166 |
abate |
431 |
let rec phrases ppf phs = match phs with
|
| 167 |
|
|
| { descr = Ast.FunDecl _ } :: _ ->
|
| 168 |
|
|
phrases ppf (collect_funs ppf [] phs)
|
| 169 |
|
|
| { descr = Ast.TypeDecl (_,_) } :: _ ->
|
| 170 |
|
|
phrases ppf (collect_types ppf [] phs)
|
| 171 |
abate |
501 |
| { descr = Ast.SchemaDecl (name, schema) } :: rest ->
|
| 172 |
|
|
Typer.register_schema name schema;
|
| 173 |
|
|
phrases ppf rest
|
| 174 |
abate |
529 |
| { descr = Ast.Namespace (pr,ns) } :: rest ->
|
| 175 |
abate |
552 |
Typer.register_global_ns pr ns;
|
| 176 |
abate |
529 |
phrases ppf rest
|
| 177 |
abate |
431 |
| { descr = Ast.EvalStatement e } :: rest ->
|
| 178 |
|
|
let (fv,e) = Typer.expr e in
|
| 179 |
|
|
let t = Typer.type_check !typing_env e Types.any true in
|
| 180 |
|
|
Typer.report_unused_branches ();
|
| 181 |
|
|
if not !quiet then
|
| 182 |
abate |
522 |
Location.dump_loc ppf (e.Typed.exp_loc,`Full);
|
| 183 |
abate |
431 |
let v = Eval.eval !eval_env e in
|
| 184 |
|
|
if not !quiet then
|
| 185 |
abate |
502 |
Format.fprintf ppf "- : @[@[%a@] =@ @[%a@]@]@." print_norm t print_value v;
|
| 186 |
abate |
431 |
phrases ppf rest
|
| 187 |
|
|
| { descr = Ast.LetDecl (p,e) } :: rest ->
|
| 188 |
|
|
let decl = Typer.let_decl p e in
|
| 189 |
abate |
433 |
let typs = Typer.type_let_decl !typing_env decl in
|
| 190 |
abate |
431 |
Typer.report_unused_branches ();
|
| 191 |
abate |
433 |
let vals = Eval.eval_let_decl !eval_env decl in
|
| 192 |
|
|
insert_bindings ppf typs vals;
|
| 193 |
abate |
431 |
phrases ppf rest
|
| 194 |
|
|
| { descr = Ast.Debug l } :: rest ->
|
| 195 |
abate |
541 |
debug ppf l;
|
| 196 |
abate |
431 |
phrases ppf rest
|
| 197 |
abate |
446 |
| { descr = Ast.Directive `Quit } :: rest ->
|
| 198 |
|
|
if !toplevel then raise End_of_file;
|
| 199 |
|
|
phrases ppf rest
|
| 200 |
|
|
| { descr = Ast.Directive `Env } :: rest ->
|
| 201 |
|
|
dump_env ppf;
|
| 202 |
|
|
phrases ppf rest
|
| 203 |
abate |
553 |
| { descr = Ast.Directive `Reinit_ns } :: rest ->
|
| 204 |
|
|
Typer.set_ns_table_for_printer ();
|
| 205 |
|
|
phrases ppf rest
|
| 206 |
abate |
431 |
| [] -> ()
|
| 207 |
abate |
90 |
|
| 208 |
abate |
431 |
let run rule ppf ppf_err input =
|
| 209 |
abate |
637 |
Typer.clear_unused_branches ();
|
| 210 |
abate |
13 |
try
|
| 211 |
abate |
90 |
let p =
|
| 212 |
abate |
431 |
try rule input
|
| 213 |
abate |
90 |
with
|
| 214 |
abate |
431 |
| Stdpp.Exc_located (_, (Location _ as e)) ->
|
| 215 |
abate |
495 |
Parser.sync (); raise e
|
| 216 |
abate |
431 |
| Stdpp.Exc_located ((i,j), e) ->
|
| 217 |
abate |
495 |
Parser.sync (); raise_loc i j e
|
| 218 |
abate |
90 |
in
|
| 219 |
abate |
431 |
phrases ppf p;
|
| 220 |
abate |
95 |
true
|
| 221 |
abate |
28 |
with
|
| 222 |
abate |
446 |
| (End_of_file | Failure _ | Not_found | Invalid_argument _ | Sys.Break)
|
| 223 |
|
|
as e ->
|
| 224 |
|
|
raise e
|
| 225 |
abate |
431 |
| exn ->
|
| 226 |
|
|
print_exn ppf_err exn;
|
| 227 |
|
|
Format.fprintf ppf_err "@.";
|
| 228 |
|
|
false
|
| 229 |
abate |
21 |
|
| 230 |
abate |
431 |
let script = run Parser.prog
|
| 231 |
abate |
446 |
let topinput = run Parser.top_phrases
|