builtin.ml 10.7 KB
Newer Older
1
open Builtin_defs
2

3 4
let eval = ref (fun ppf err s -> assert false)

5 6
(* Types *)

7
let stringn = Types.cons string
8
let namespaces = 
9
  Sequence.star (Types.times stringn stringn)
10

11 12 13
let types =
  [ 
    "Empty",   Types.empty;
14 15
    "Any",     any;
    "Int",     int;
16
    "Char",    Types.char Chars.any;
17
    "Byte",    char_latin1;
18
    "Atom",    atom;
19 20 21
    "Pair",    Types.Product.any;
    "Arrow",   Types.Arrow.any;
    "Record",  Types.Record.any;
22
    "String",  string;
23
    "Latin1",  string_latin1;
24 25
    "Bool",    bool;
    "Float",   float;
26
    "AnyXml",  any_xml;
27
    "Namespaces", namespaces;
28 29
  ]

30 31
let env =
  List.fold_left
32
    (fun accu (n,t) -> 
33
       let n = (Ns.empty, Ident.U.mk n) in
34
       Types.Print.register_global "" n t;
35 36
       Typer.enter_type (Ident.ident n) t accu
    )
37
    Typer.empty_env
38 39 40 41
    types

(* Operators *)

42 43
open Operators

44
let binary_op_gen = register_binary
45

46
let unary_op_gen = register_unary
47 48 49 50 51


let binary_op name t1 t2 f run =
  binary_op_gen
    name
52
    (fun arg1 arg2 constr precise ->
53 54 55
       f (arg1 t1 true) (arg2 t2 true))
    run

56 57
let binary_op_cst = register_op2

58 59 60

let binary_op_warning2 name t1 t2 w2 t run =
  binary_op_gen name
61
    (fun arg1 arg2 constr precise ->
62 63 64
       ignore (arg1 t1 false); 
       let r = arg2 t2 true in
       if not (Types.subtype r w2) then
65
	 raise (Typer.Warning ("This operator may fail", t));
66 67 68 69
       t)
    run

let unary_op_warning name targ w t run =
70
  unary_op_gen name
71
    (fun arg constr precise ->
72 73
       let res = arg targ true in
       if not (Types.subtype res w) then
74
	 raise (Typer.Warning ("This operator may fail",t));
75 76
       t)
    run
77 78 79

open Ident

80 81 82
let raise_gen exn =
  raise (Value.CDuceExn (Value.string_latin1 (Printexc.to_string exn)))

83
let exn_load_file_utf8 = lazy (
84 85
  Value.CDuceExn (
    Value.Pair (
86
      Value.Atom (Atoms.V.mk_ascii "load_file_utf8"),
87
      Value.string_latin1 "File is not a valid UTF-8 stream"))
88
)
89

90
let exn_int_of =  lazy (
91 92
  Value.CDuceExn (
    Value.Pair (
93
      Value.Atom (Atoms.V.mk_ascii "Invalid_argument"),
94
      Value.string_latin1 "int_of"))
95
)
96

97
let exn_char_of = lazy (
98 99 100 101
  Value.CDuceExn (
    Value.Pair (
      Value.Atom (Atoms.V.mk_ascii "Invalid_argument"),
      Value.string_latin1 "char_of"))
102
)
103

104
let exn_float_of = lazy (
105 106 107 108
  Value.CDuceExn (
    Value.Pair (
      Value.Atom (Atoms.V.mk_ascii "Invalid_argument"),
      Value.string_latin1 "float_of"))
109 110 111
)

let exn_namespaces = lazy (
112 113 114 115
  Value.CDuceExn (
    Value.Pair (
      Value.Atom (Atoms.V.mk_ascii "Invalid_argument"),
      Value.string_latin1 "namespaces"))
116
)
117

118 119 120

let eval_load_file ~utf8 e =
  Location.protect_op "load_file";
121
  let fn = Value.get_string_latin1 e in
122
  let s = Url.load_url fn in
123
  if utf8 then 
124 125
    match U.mk_check s with 
      | Some s -> Value.string_utf8 s 
126
      | None -> raise (Lazy.force exn_load_file_utf8)
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
  else Value.string_latin1 s


let () = ();;

(* Comparison operators *)

binary_op "=" 
  any any 
  (fun t1 t2 ->
     if Types.is_empty (Types.cap t1 t2) then false_type
     else bool)
  (fun v1 v2 ->
     Value.vbool (Value.compare v1 v2 == 0));;
  
binary_op_cst "<=" 
  any any bool
  (fun v1 v2 -> Value.vbool (Value.compare v1 v2 <= 0));;
  
binary_op_cst "<" 
  any any bool
  (fun v1 v2 -> Value.vbool (Value.compare v1 v2 < 0));;
  
150
binary_op_cst ">=" 
151 152 153 154 155 156 157 158 159 160
  any any bool
  (fun v1 v2 -> 
     Value.vbool (Value.compare v1 v2 >= 0));;
  
binary_op_cst ">" 
  any any bool
  (fun v1 v2 -> 
     Value.vbool (Value.compare v1 v2 > 0));;

(* I/O *)
161 162 163 164 165 166

register_fun "char_of_int"
  int (Types.char Chars.any)
  (function
     | Value.Integer x ->
	 (try Value.Char (Chars.V.mk_int (Intervals.V.get_int x))
167
	 with Failure _ -> raise (Lazy.force exn_int_of))
168 169
     | _ -> assert false);;

170
  
171
register_fun "string_of" 
172 173 174 175 176 177 178 179 180
  any string_latin1
  (fun v -> 
     let b = Buffer.create 16 in
     let ppf = Format.formatter_of_buffer b in
     Value.print ppf v;
     Format.pp_print_flush ppf ();
     Value.string_latin1 (Buffer.contents b)
  );;

181
register_fun "load_xml"
182
  string_latin1 any_xml
183
  (fun v -> Location.protect_op "load_xml"; Load_xml.load_xml (Value.get_string_latin1 v));;
184
  
185 186 187 188 189 190
register_fun "!load_xml"
  string_latin1 any_xml
  (fun v -> Location.protect_op "load_xml"; Load_xml.load_xml ~ns:true
     (Value.get_string_latin1 v));;


191 192
register_fun "load_html"
  string_latin1 Sequence.any
193
  (fun v -> Location.protect_op "load_html"; Load_xml.load_html  (Value.get_string_latin1 v));;
194

195 196
register_fun "load_file_utf8" 
  string_latin1 string 
197 198
  (eval_load_file ~utf8:true);;

199 200
register_fun "load_file" 
  string_latin1 string_latin1 
201
  (eval_load_file ~utf8:false);;
202 203


204 205
let argv = ref Value.Absent;;

206

207

208 209 210 211 212 213 214
register_fun "print_xml" 
  Types.any string_latin1
  (fun v -> Print_xml.print_xml ~utf8:false !Eval.ns_table v);;

register_fun "print_xml_utf8" 
  Types.any string
  (fun v -> Print_xml.print_xml ~utf8:true !Eval.ns_table v);;
215

216

217 218 219 220 221 222 223 224 225
register_fun "dump_xml" 
  Types.any string_latin1
  (fun v -> Print_xml.dump_xml ~utf8:false !Eval.ns_table v);;

register_fun "dump_xml_utf8" 
  Types.any string
  (fun v -> Print_xml.dump_xml ~utf8:true !Eval.ns_table v);;


226 227
register_fun "print"
  string_latin1 nil
228 229 230 231 232 233 234
  (fun v ->
     Location.protect_op "print";
     print_string (Value.get_string_latin1 v);
     flush stdout;
     Value.nil
  );;

235 236 237 238 239 240 241 242 243 244
register_fun "print_utf8"
  string nil
  (fun v ->
     Location.protect_op "print";
     let s = Value.cduce2ocaml_string_utf8 v in
     print_string (U.get_str s);
     flush stdout;
     Value.nil
  );;

245 246 247 248
unary_op_warning "int_of"
  string intstr int
  (fun v ->
     let (s,_) = Value.get_string_utf8 v in
249 250 251 252 253 254 255 256 257 258 259 260 261 262
      let str = U.get_str s in
        try let modifier = str.[(String.index str '0')+1] in
	    if ( modifier = 'x' ||
                 modifier = 'X' ||
                 modifier = 'b' ||
                 modifier = 'B' ||
                 modifier = 'o' ||
                 modifier = 'O') 
             then  
               Value.Integer (Intervals.V.from_int (int_of_string(str)))
             else
	       Value.Integer (Intervals.V.mk (str))
         with _ -> 
                (try Value.Integer (Intervals.V.mk (str))
263
                 with Failure _ -> raise (Lazy.force exn_int_of)));;
264

265 266 267 268
(*  It was like that                                    *)
(*     try Value.Integer (Intervals.V.mk (U.get_str s)) *)
(*                 UTF-8 is ASCII compatible !          *)
(* modified to allow 0x 0b 0o notations                 *)
269

270

271
(*
272
register_fun "atom_of"
273 274
  string atom
  (fun v ->
275
     let (s,_) = Value.get_string_utf8 v in 
276
     Value.Atom (Atoms.V.mk Ns.empty s));;
277 278 279 280 281
*)

register_fun "split_atom"
  atom (Types.times stringn stringn)
  (function 
282 283
     | Value.Atom q ->
	 let (ns,l) = Atoms.V.value q in
284
	 Value.Pair(
285
	   Value.string_utf8 (Ns.Uri.value ns),
286 287 288 289 290 291 292 293 294 295
	   Value.string_utf8 l)
     | _ -> assert false);;

register_fun "make_atom"
  (Types.times stringn stringn) atom
  (fun v ->
     let v1,v2 =Value.get_pair v in
     let ns,_ = Value.get_string_utf8 v1 in
     let l,_ = Value.get_string_utf8 v2 in
     (* TODO: check that l is a correct Name wrt XML *)
296
     Value.Atom (Atoms.V.mk (Ns.Uri.mk ns, l)));;
297 298 299


   
300 301

binary_op_warning2 "dump_to_file"
302
  string_latin1 string string_latin1 nil
303
  (fun f v -> try
304 305 306 307
     Location.protect_op "dump_to_file";
     let oc = open_out (Value.get_string_latin1 f) in
     output_string oc (Value.get_string_latin1 v);
     close_out oc;
308 309
     Value.nil
   with exn -> raise_gen exn);;
310 311
    
binary_op_cst "dump_to_file_utf8"
312
  string_latin1 string nil
313
  (fun f v -> try
314 315 316 317 318
     Location.protect_op "dump_to_file_utf8";
     let oc = open_out (Value.get_string_latin1 f) in
     let (v,_) = Value.get_string_utf8 v in
     output_string oc (U.get_str v);
     close_out oc;
319 320
     Value.nil
   with exn -> raise_gen exn);;
321 322 323 324

(* Integer operators *)

binary_op_gen "+"
325
  (fun arg1 arg2 constr precise ->
326 327 328 329 330 331 332 333 334 335 336 337
     let t1 = arg1 (Types.cup int Types.Record.any) true in
     if Types.subtype t1 int 
     then (
       let t2 = arg2 int true in
       Types.interval
	 (Intervals.add (Types.Int.get t1) (Types.Int.get t2))
     )
     else if Types.subtype t1 Types.Record.any 
     then (
       let t2 = arg2 Types.Record.any true in 
       Types.Record.merge t1 t2
     )
338
     else raise (Typer.Error "The first argument mixes integers and records"))
339
  Value.add;;
340 341 342 343 344 345 346
      
binary_op "-"
  int int
  (fun t1 t2 ->
     Types.interval 
     (Intervals.sub (Types.Int.get t1) (Types.Int.get t2)))
  (fun v1 v2 -> match (v1,v2) with
347
     | (Value.Integer x, Value.Integer y) -> Value.Integer (Intervals.V.sub x y)
348 349
     | _ -> assert false);;

350 351 352 353 354
binary_op "*"
  int int
  (fun t1 t2 ->
     Types.interval 
     (Intervals.mul (Types.Int.get t1) (Types.Int.get t2)))
355
  (fun v1 v2 -> match (v1,v2) with
356
     | (Value.Integer x, Value.Integer y) -> Value.Integer (Intervals.V.mult x y)
357 358
     | _ -> assert false);;

359 360
binary_op_warning2 "/"
  int int non_zero_int int
361
  (fun v1 v2 -> match (v1,v2) with
362
     | (Value.Integer x, Value.Integer y) -> Value.Integer (Intervals.V.div x y)
363 364
     | _ -> assert false);;

365 366
binary_op_warning2 "mod"
  int int non_zero_int int
367
  (fun v1 v2 -> match (v1,v2) with
368
     | (Value.Integer x, Value.Integer y) -> Value.Integer (Intervals.V.modulo x y)
369 370 371 372
     | _ -> assert false);;


binary_op_gen "@"
373
  (fun arg1 arg2 constr precise ->
374
     let constr' = Sequence.ub_concat constr in
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
     let exact = Types.subtype constr' constr in
     if exact then
       let t1 = arg1 constr' precise
       and t2 = arg2 constr' precise in
       if precise then Sequence.concat t1 t2 else constr
     else
       (* Note:
	  the knownledge of t1 may makes it useless to
	  check t2 with 'precise' ... *)
       let t1 = arg1 constr' true
       and t2 = arg2 constr' true in
       Sequence.concat t1 t2)
  Value.concat;;

unary_op_gen "flatten"
  Typer.flatten
  Value.flatten;;
  

394
register_fun "raise" any Types.empty
395
  (fun v -> raise (Value.CDuceExn v));;
396

397 398 399 400 401 402 403 404
register_fun "namespaces" any_xml 
  namespaces
  (function 
       Value.XmlNs (_,_,_,ns) ->
	 Value.sequence_rev 
	   (List.map 
	      (fun (pr,ns) ->
		 Value.Pair (Value.string_utf8 pr,
405
			     Value.string_utf8 (Ns.Uri.value ns)))
406
	      (Ns.get_table ns))
407
     | Value.Xml _ -> raise (Lazy.force exn_namespaces)
408
     | _ -> assert false);;
409

410 411 412 413 414 415 416 417 418
register_fun2 "set_namespaces"
  namespaces any_xml any_xml
  (fun ns -> function
     | Value.XmlNs(v1,v2,v3,_) | Value.Xml (v1,v2,v3) ->
	 let ns = Value.get_sequence_rev ns in
	 let ns = List.map (fun v ->
			      let (pr,ns) = Value.get_pair v in
			      let pr,_ = Value.get_string_utf8 pr in
			      let ns,_ = Value.get_string_utf8 ns in
419
			      (pr,Ns.Uri.mk ns)) ns in
420 421 422
	 Value.XmlNs(v1,v2,v3,Ns.mk_table  ns)
     | _ -> assert false);;

423 424 425 426 427 428
(* Float *)

register_fun "float_of" string float
  (fun v ->
     let (s,_) = Value.get_string_utf8 v in
     try Value.float (float_of_string (U.get_str s))
429
     with Failure _ -> raise (Lazy.force exn_float_of));;