| 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" Eval.empty
|
| 9 |
let compile_env = State.ref "Cduce.compile_env" Compile.empty
|
| 10 |
|
| 11 |
let do_compile = ref false
|
| 12 |
|
| 13 |
let get_global_value v =
|
| 14 |
if !do_compile
|
| 15 |
then Eval.L.var (Compile.find v !compile_env)
|
| 16 |
else Eval.find_value v !eval_env
|
| 17 |
|
| 18 |
let get_global_type v =
|
| 19 |
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 |
if !do_compile
|
| 25 |
then (compile_env := Compile.enter_global !compile_env x; Eval.L.push v)
|
| 26 |
else eval_env := Eval.enter_value x v !eval_env
|
| 27 |
|
| 28 |
let rec is_abstraction = function
|
| 29 |
| Ast.Abstraction _ -> true
|
| 30 |
| Ast.LocatedExpr (_,e) -> is_abstraction e
|
| 31 |
| _ -> false
|
| 32 |
|
| 33 |
let print_norm ppf d =
|
| 34 |
Location.protect ppf
|
| 35 |
(fun ppf -> Types.Print.print ppf ((*Types.normalize*) d))
|
| 36 |
|
| 37 |
let print_sample ppf s =
|
| 38 |
Location.protect ppf
|
| 39 |
(fun ppf -> Sample.print ppf s)
|
| 40 |
|
| 41 |
let print_protect ppf s =
|
| 42 |
Location.protect ppf (fun ppf -> Format.fprintf ppf "%s" s)
|
| 43 |
|
| 44 |
let print_value ppf v =
|
| 45 |
Location.protect ppf (fun ppf -> Value.print ppf v)
|
| 46 |
|
| 47 |
let dump_value ppf x t v =
|
| 48 |
Format.fprintf ppf "@[val %a : @[%a = %a@]@]@."
|
| 49 |
U.print (Id.value x) print_norm t print_value v
|
| 50 |
|
| 51 |
let dump_env ppf =
|
| 52 |
Format.fprintf ppf "Types:%a@." Typer.dump_types !typing_env;
|
| 53 |
Format.fprintf ppf "Namespace prefixes:@\n%a" Typer.dump_ns !typing_env;
|
| 54 |
Format.fprintf ppf "Namespace prefixes used for pretty-printing:@.%t"
|
| 55 |
Ns.InternalPrinter.dump;
|
| 56 |
Format.fprintf ppf "Values:@.";
|
| 57 |
Typer.iter_values !typing_env
|
| 58 |
(fun x t -> dump_value ppf x t (get_global_value x))
|
| 59 |
|
| 60 |
let directive_help ppf =
|
| 61 |
Format.fprintf ppf
|
| 62 |
"Toplevel directives:
|
| 63 |
#quit;; quit the interpreter
|
| 64 |
#env;; dump current environment
|
| 65 |
#reinit_ns;; reinitialize namespace processing
|
| 66 |
#help;; shows this help message
|
| 67 |
"
|
| 68 |
|
| 69 |
let rec print_exn ppf = function
|
| 70 |
| Location (loc, w, exn) ->
|
| 71 |
Format.fprintf ppf "Error %a:@." Location.print_loc (loc,w);
|
| 72 |
Format.fprintf ppf "%a" Location.html_hilight (loc,w);
|
| 73 |
print_exn ppf exn
|
| 74 |
| Value.CDuceExn v ->
|
| 75 |
Format.fprintf ppf "Uncaught CDuce exception: @[%a@]@."
|
| 76 |
print_value v
|
| 77 |
| Eval.MultipleDeclaration v ->
|
| 78 |
Format.fprintf ppf "Multiple declaration for global value %a@."
|
| 79 |
U.print (Id.value v)
|
| 80 |
| Typer.WrongLabel (t,l) ->
|
| 81 |
Format.fprintf ppf "Wrong record selection; field %a "
|
| 82 |
Label.print (LabelPool.value l);
|
| 83 |
Format.fprintf ppf "not present in an expression of type:@.%a@."
|
| 84 |
print_norm t
|
| 85 |
| Typer.ShouldHave (t,msg) ->
|
| 86 |
Format.fprintf ppf "This expression should have type:@.%a@.%a@."
|
| 87 |
print_norm t
|
| 88 |
print_protect msg
|
| 89 |
| Typer.ShouldHave2 (t1,msg,t2) ->
|
| 90 |
Format.fprintf ppf "This expression should have type:@.%a@.%a %a@."
|
| 91 |
print_norm t1
|
| 92 |
print_protect msg
|
| 93 |
print_norm t2
|
| 94 |
| Typer.Error s ->
|
| 95 |
Format.fprintf ppf "%a@." print_protect s
|
| 96 |
| Typer.Constraint (s,t) ->
|
| 97 |
Format.fprintf ppf "This expression should have type:@.%a@."
|
| 98 |
print_norm t;
|
| 99 |
Format.fprintf ppf "but its inferred type is:@.%a@."
|
| 100 |
print_norm s;
|
| 101 |
Format.fprintf ppf "which is not a subtype, as shown by the sample:@.%a@."
|
| 102 |
print_sample (Sample.get (Types.diff s t))
|
| 103 |
| Typer.NonExhaustive t ->
|
| 104 |
Format.fprintf ppf "This pattern matching is not exhaustive@.";
|
| 105 |
Format.fprintf ppf "Residual type:@.%a@."
|
| 106 |
print_norm t;
|
| 107 |
Format.fprintf ppf "Sample:@.%a@." print_sample (Sample.get t)
|
| 108 |
| Typer.UnboundId (x,tn) ->
|
| 109 |
Format.fprintf ppf "Unbound identifier %a%s@." U.print (Id.value x)
|
| 110 |
(if tn then " (it is a type name)" else "")
|
| 111 |
| Ulexer.Error (i,j,s) ->
|
| 112 |
let loc = Location.loc_of_pos (i,j), `Full in
|
| 113 |
Format.fprintf ppf "Error %a:@." Location.print_loc loc;
|
| 114 |
Format.fprintf ppf "%a%s" Location.html_hilight loc s
|
| 115 |
| Parser.Error s | Stream.Error s ->
|
| 116 |
Format.fprintf ppf "Parsing error: %a@." print_protect s
|
| 117 |
| Location.Generic s ->
|
| 118 |
Format.fprintf ppf "%a@." print_protect s
|
| 119 |
| exn ->
|
| 120 |
(* raise exn *)
|
| 121 |
Format.fprintf ppf "%a@." print_protect (Printexc.to_string exn)
|
| 122 |
|
| 123 |
|
| 124 |
let display ppf l =
|
| 125 |
if not !quiet then
|
| 126 |
List.iter
|
| 127 |
(fun (x,t) -> dump_value ppf x t (get_global_value x))
|
| 128 |
l
|
| 129 |
|
| 130 |
let eval ppf e =
|
| 131 |
let (e,t) = Typer.type_expr !typing_env e in
|
| 132 |
|
| 133 |
if not !quiet then
|
| 134 |
Location.dump_loc ppf (e.Typed.exp_loc,`Full);
|
| 135 |
|
| 136 |
let v =
|
| 137 |
if !do_compile then
|
| 138 |
let e = Compile.compile_eval !compile_env e in
|
| 139 |
Eval.L.expr e
|
| 140 |
else
|
| 141 |
Eval.eval !eval_env e
|
| 142 |
in
|
| 143 |
if not !quiet then
|
| 144 |
Format.fprintf ppf "- : @[@[%a@] =@ @[%a@]@]@."
|
| 145 |
print_norm t print_value v;
|
| 146 |
v
|
| 147 |
|
| 148 |
let let_decl ppf p e =
|
| 149 |
let (tenv,decl,typs) = Typer.type_let_decl !typing_env p e in
|
| 150 |
|
| 151 |
let () =
|
| 152 |
if !do_compile then
|
| 153 |
let (env,decl) = Compile.compile_let_decl !compile_env decl in
|
| 154 |
Eval.L.eval decl;
|
| 155 |
compile_env := env
|
| 156 |
else
|
| 157 |
eval_env := Eval.eval_let_decl !eval_env decl
|
| 158 |
in
|
| 159 |
typing_env := tenv;
|
| 160 |
display ppf typs
|
| 161 |
|
| 162 |
|
| 163 |
let let_funs ppf funs =
|
| 164 |
let (tenv,funs,typs) = Typer.type_let_funs !typing_env funs in
|
| 165 |
|
| 166 |
let () =
|
| 167 |
if !do_compile then
|
| 168 |
let (env,funs) = Compile.compile_rec_funs !compile_env funs in
|
| 169 |
Eval.L.eval funs;
|
| 170 |
compile_env := env;
|
| 171 |
else
|
| 172 |
eval_env := Eval.eval_rec_funs !eval_env funs
|
| 173 |
in
|
| 174 |
typing_env := tenv;
|
| 175 |
display ppf typs
|
| 176 |
|
| 177 |
|
| 178 |
let debug ppf = function
|
| 179 |
| `Subtype (t1,t2) ->
|
| 180 |
Format.fprintf ppf "[DEBUG:subtype]@.";
|
| 181 |
let t1 = Types.descr (Typer.typ !typing_env t1)
|
| 182 |
and t2 = Types.descr (Typer.typ !typing_env t2) in
|
| 183 |
let s = Types.subtype t1 t2 in
|
| 184 |
Format.fprintf ppf "%a %a %a : %b@." print_norm t1 print_protect "<=" print_norm t2 s
|
| 185 |
| `Sample t ->
|
| 186 |
Format.fprintf ppf "[DEBUG:sample]@.";
|
| 187 |
(try
|
| 188 |
let t = Types.descr (Typer.typ !typing_env t) in
|
| 189 |
Format.fprintf ppf "%a@." print_sample (Sample.get t)
|
| 190 |
with Not_found ->
|
| 191 |
Format.fprintf ppf "Empty type : no sample !@.")
|
| 192 |
| `Filter (t,p) ->
|
| 193 |
Format.fprintf ppf "[DEBUG:filter]@.";
|
| 194 |
let t = Typer.typ !typing_env t
|
| 195 |
and p = Typer.pat !typing_env p in
|
| 196 |
let f = Patterns.filter (Types.descr t) p in
|
| 197 |
List.iter (fun (x,t) ->
|
| 198 |
Format.fprintf ppf " %a:%a@." U.print (Id.value x)
|
| 199 |
print_norm (Types.descr t)) f
|
| 200 |
| `Accept p ->
|
| 201 |
Format.fprintf ppf "[DEBUG:accept]@.";
|
| 202 |
let p = Typer.pat !typing_env p in
|
| 203 |
let t = Patterns.accept p in
|
| 204 |
Format.fprintf ppf " %a@." Types.Print.print (Types.descr t)
|
| 205 |
| `Compile (t,pl) ->
|
| 206 |
Format.fprintf ppf "[DEBUG:compile]@.";
|
| 207 |
let t = Typer.typ !typing_env t
|
| 208 |
and pl = List.map (Typer.pat !typing_env) pl in
|
| 209 |
Patterns.Compile.debug_compile ppf t pl
|
| 210 |
| `Explain (t,e) ->
|
| 211 |
Format.fprintf ppf "[DEBUG:explain]@.";
|
| 212 |
let t = Typer.typ !typing_env t in
|
| 213 |
(match Explain.explain (Types.descr t) (eval ppf e) with
|
| 214 |
| Some p ->
|
| 215 |
Format.fprintf ppf "Explanation: @[%a@]@."
|
| 216 |
Explain.print_path p
|
| 217 |
| None ->
|
| 218 |
Format.fprintf ppf "Explanation: value has given type@.")
|
| 219 |
|
| 220 |
|
| 221 |
let rec collect_funs ppf accu = function
|
| 222 |
| { descr = Ast.FunDecl e } :: rest -> collect_funs ppf (e::accu) rest
|
| 223 |
| rest -> let_funs ppf accu; rest
|
| 224 |
|
| 225 |
let rec collect_types ppf accu = function
|
| 226 |
| { descr = Ast.TypeDecl (x,t) } :: rest ->
|
| 227 |
collect_types ppf ((x,t) :: accu) rest
|
| 228 |
| rest ->
|
| 229 |
typing_env :=
|
| 230 |
Typer.enter_types (Typer.type_defs !typing_env accu) !typing_env;
|
| 231 |
rest
|
| 232 |
|
| 233 |
|
| 234 |
let rec phrases ppf phs = match phs with
|
| 235 |
| { descr = Ast.FunDecl _ } :: _ ->
|
| 236 |
phrases ppf (collect_funs ppf [] phs)
|
| 237 |
| { descr = Ast.TypeDecl (_,_) } :: _ ->
|
| 238 |
phrases ppf (collect_types ppf [] phs)
|
| 239 |
| { descr = Ast.SchemaDecl (name, schema) } :: rest ->
|
| 240 |
Typer.register_schema name schema;
|
| 241 |
phrases ppf rest
|
| 242 |
| { descr = Ast.Namespace (pr,ns) } :: rest ->
|
| 243 |
typing_env := Typer.enter_ns pr ns !typing_env;
|
| 244 |
phrases ppf rest
|
| 245 |
| { descr = Ast.EvalStatement e } :: rest ->
|
| 246 |
ignore (eval ppf e);
|
| 247 |
phrases ppf rest
|
| 248 |
| { descr = Ast.LetDecl (p,e) } :: rest ->
|
| 249 |
let_decl ppf p e;
|
| 250 |
phrases ppf rest
|
| 251 |
| { descr = Ast.Debug l } :: rest ->
|
| 252 |
debug ppf l;
|
| 253 |
phrases ppf rest
|
| 254 |
| { descr = Ast.Directive `Quit } :: rest ->
|
| 255 |
if !toplevel then raise End_of_file;
|
| 256 |
phrases ppf rest
|
| 257 |
| { descr = Ast.Directive `Env } :: rest ->
|
| 258 |
dump_env ppf;
|
| 259 |
phrases ppf rest
|
| 260 |
| { descr = Ast.Directive `Reinit_ns } :: rest ->
|
| 261 |
Typer.set_ns_table_for_printer !typing_env;
|
| 262 |
phrases ppf rest
|
| 263 |
| { descr = Ast.Directive `Help } :: rest ->
|
| 264 |
directive_help ppf;
|
| 265 |
phrases ppf rest
|
| 266 |
| [] -> ()
|
| 267 |
|
| 268 |
let catch_exn ppf_err = function
|
| 269 |
| (End_of_file | Failure _ | Not_found | Invalid_argument _ | Sys.Break)
|
| 270 |
as e ->
|
| 271 |
raise e
|
| 272 |
| exn ->
|
| 273 |
print_exn ppf_err exn;
|
| 274 |
Format.fprintf ppf_err "@."
|
| 275 |
|
| 276 |
let parse rule input =
|
| 277 |
try Some (rule input)
|
| 278 |
with
|
| 279 |
| Stdpp.Exc_located (_, (Location _ as e)) ->
|
| 280 |
Parser.sync (); raise e
|
| 281 |
| Stdpp.Exc_located ((i,j), e) ->
|
| 282 |
Parser.sync (); raise_loc i j e
|
| 283 |
|
| 284 |
let run rule ppf ppf_err input =
|
| 285 |
try match parse rule input with
|
| 286 |
| Some phs -> phrases ppf phs; true
|
| 287 |
| None -> false
|
| 288 |
with exn -> catch_exn ppf_err exn; false
|
| 289 |
|
| 290 |
let script = run Parser.prog
|
| 291 |
let topinput = run Parser.top_phrases
|
| 292 |
|
| 293 |
let comp_unit src =
|
| 294 |
try
|
| 295 |
let ic = open_in src in
|
| 296 |
Location.push_source (`File src);
|
| 297 |
let input = Stream.of_channel ic in
|
| 298 |
match parse Parser.prog input with
|
| 299 |
| Some p ->
|
| 300 |
close_in ic;
|
| 301 |
let argv = ident (U.mk "argv") in
|
| 302 |
let (tenv,cenv,codes) =
|
| 303 |
Compile.comp_unit
|
| 304 |
(Typer.enter_value argv (Sequence.star Sequence.string)
|
| 305 |
Builtin.env)
|
| 306 |
(Compile.enter_global Compile.empty argv)
|
| 307 |
p in
|
| 308 |
codes
|
| 309 |
| None -> exit 1
|
| 310 |
with exn -> catch_exn Format.err_formatter exn; exit 1
|
| 311 |
|
| 312 |
let run_code argv codes =
|
| 313 |
try
|
| 314 |
Eval.L.push argv;
|
| 315 |
List.iter Eval.L.eval codes
|
| 316 |
with exn -> catch_exn Format.err_formatter exn; exit 1
|
| 317 |
|
| 318 |
|
| 319 |
let compile src =
|
| 320 |
let codes = comp_unit src in
|
| 321 |
let oc = open_out (src ^ ".out") in
|
| 322 |
let codes_s = Serialize.Put.run Lambda.Put.compunit codes in
|
| 323 |
output_string oc codes_s;
|
| 324 |
close_out oc;
|
| 325 |
exit 0
|
| 326 |
|
| 327 |
let compile_run src argv =
|
| 328 |
run_code argv (comp_unit src)
|
| 329 |
|
| 330 |
let run obj argv =
|
| 331 |
let ic = open_in obj in
|
| 332 |
let len = in_channel_length ic in
|
| 333 |
let codes = String.create len in
|
| 334 |
really_input ic codes 0 len;
|
| 335 |
close_in ic;
|
| 336 |
let codes = Serialize.Get.run Lambda.Get.compunit codes in
|
| 337 |
run_code argv codes
|
| 338 |
|
| 339 |
|
| 340 |
let serialize_typing_env t () =
|
| 341 |
Typer.serialize t !typing_env
|
| 342 |
|
| 343 |
let deserialize_typing_env t =
|
| 344 |
typing_env := Typer.deserialize t
|