builtin.ml 10.9 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
    "Caml_int", caml_int;
29
30
  ]

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

(* Operators *)

43
44
open Operators

45
let binary_op_gen = register_binary
46

47
let unary_op_gen = register_unary
48
49
50
51
52


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

57
58
let binary_op_cst = register_op2

59
60
61

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

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

open Ident

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

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

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

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

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

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

119
120
121

let eval_load_file ~utf8 e =
  Location.protect_op "load_file";
122
  let fn = Value.get_string_latin1 e in
123
  let s = Url.load_url fn in
124
  if utf8 then 
125
126
    match U.mk_check s with 
      | Some s -> Value.string_utf8 s 
127
      | None -> raise (Lazy.force exn_load_file_utf8)
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  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));;
  
151
binary_op_cst ">=" 
152
153
154
155
156
157
158
159
160
161
  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 *)
162
163
164
165
166
167

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))
168
	 with Failure _ -> raise (Lazy.force exn_int_of))
169
170
     | _ -> assert false);;

171
172
173
174
175
176
177
register_fun "int_of_char"
  (Types.char Chars.any) int
  (function
     | Value.Char x ->
	 Value.Integer (Intervals.V.from_int (Chars.V.to_int x))
     | _ -> assert false);;

178
  
179
register_fun "string_of" 
180
181
182
183
184
185
186
187
188
  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)
  );;

189
register_fun "load_xml"
190
  string_latin1 any_xml
191
  (fun v -> Location.protect_op "load_xml"; Load_xml.load_xml (Value.get_string_latin1 v));;
192
  
193
194
195
196
197
198
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));;


199
200
register_fun "load_html"
  string_latin1 Sequence.any
201
  (fun v -> Location.protect_op "load_html"; Load_xml.load_html  (Value.get_string_latin1 v));;
202

203
204
register_fun "load_file_utf8" 
  string_latin1 string 
205
206
  (eval_load_file ~utf8:true);;

207
208
register_fun "load_file" 
  string_latin1 string_latin1 
209
  (eval_load_file ~utf8:false);;
210
211


212
213
let argv = ref Value.Absent;;

214

215

216
217
218
219
220
221
222
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);;
223

224

225
register_fun "dump_xml" 
226
227
228
229
  Types.any nil
  (fun v ->
     Location.protect_op "print"; 
     Print_xml.dump_xml ~utf8:false !Eval.ns_table v);;
230
231

register_fun "dump_xml_utf8" 
232
233
234
235
  Types.any nil
  (fun v -> 
     Location.protect_op "print";
     Print_xml.dump_xml ~utf8:true !Eval.ns_table v);;
236
237


238
239
register_fun "print"
  string_latin1 nil
240
241
242
243
244
245
246
  (fun v ->
     Location.protect_op "print";
     print_string (Value.get_string_latin1 v);
     flush stdout;
     Value.nil
  );;

247
248
249
250
251
252
253
254
255
256
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
  );;

257
258
259
260
unary_op_warning "int_of"
  string intstr int
  (fun v ->
     let (s,_) = Value.get_string_utf8 v in
261
262
263
264
265
266
267
268
269
270
271
272
273
274
      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))
275
                 with Failure _ -> raise (Lazy.force exn_int_of)));;
276

277
278
279
280
(*  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                 *)
281

282

283
(*
284
register_fun "atom_of"
285
286
  string atom
  (fun v ->
287
     let (s,_) = Value.get_string_utf8 v in 
288
     Value.Atom (Atoms.V.mk Ns.empty s));;
289
290
291
292
293
*)

register_fun "split_atom"
  atom (Types.times stringn stringn)
  (function 
294
295
     | Value.Atom q ->
	 let (ns,l) = Atoms.V.value q in
296
	 Value.Pair(
297
	   Value.string_utf8 (Ns.Uri.value ns),
298
299
300
301
302
303
304
305
306
307
	   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 *)
308
     Value.Atom (Atoms.V.mk (Ns.Uri.mk ns, l)));;
309
310
311


   
312
313

binary_op_warning2 "dump_to_file"
314
  string_latin1 string string_latin1 nil
315
  (fun f v -> try
316
317
318
319
     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;
320
321
     Value.nil
   with exn -> raise_gen exn);;
322
323
    
binary_op_cst "dump_to_file_utf8"
324
  string_latin1 string nil
325
  (fun f v -> try
326
327
328
329
330
     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;
331
332
     Value.nil
   with exn -> raise_gen exn);;
333
334
335
336

(* Integer operators *)

binary_op_gen "+"
337
  (fun arg1 arg2 constr precise ->
338
339
340
341
342
343
344
345
346
347
348
349
     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
     )
350
     else raise (Typer.Error "The first argument mixes integers and records"))
351
  Value.add;;
352
353
354
355
356
357
358
      
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
359
     | (Value.Integer x, Value.Integer y) -> Value.Integer (Intervals.V.sub x y)
360
361
     | _ -> assert false);;

362
363
364
365
366
binary_op "*"
  int int
  (fun t1 t2 ->
     Types.interval 
     (Intervals.mul (Types.Int.get t1) (Types.Int.get t2)))
367
  (fun v1 v2 -> match (v1,v2) with
368
     | (Value.Integer x, Value.Integer y) -> Value.Integer (Intervals.V.mult x y)
369
370
     | _ -> assert false);;

371
372
binary_op_warning2 "/"
  int int non_zero_int int
373
  (fun v1 v2 -> match (v1,v2) with
374
     | (Value.Integer x, Value.Integer y) -> Value.Integer (Intervals.V.div x y)
375
376
     | _ -> assert false);;

377
378
binary_op_warning2 "mod"
  int int non_zero_int int
379
  (fun v1 v2 -> match (v1,v2) with
380
     | (Value.Integer x, Value.Integer y) -> Value.Integer (Intervals.V.modulo x y)
381
382
383
384
     | _ -> assert false);;


binary_op_gen "@"
385
  (fun arg1 arg2 constr precise ->
386
     let constr' = Sequence.ub_concat constr in
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
     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;;
  

406
register_fun "raise" any Types.empty
407
  (fun v -> raise (Value.CDuceExn v));;
408

409
410
411
412
413
414
415
416
register_fun "namespaces" any_xml 
  namespaces
  (function 
       Value.XmlNs (_,_,_,ns) ->
	 Value.sequence_rev 
	   (List.map 
	      (fun (pr,ns) ->
		 Value.Pair (Value.string_utf8 pr,
417
			     Value.string_utf8 (Ns.Uri.value ns)))
418
	      (Ns.get_table ns))
419
     | Value.Xml _ -> raise (Lazy.force exn_namespaces)
420
     | _ -> assert false);;
421

422
423
424
425
426
427
428
429
430
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
431
			      (pr,Ns.Uri.mk ns)) ns in
432
433
434
	 Value.XmlNs(v1,v2,v3,Ns.mk_table  ns)
     | _ -> assert false);;

435
436
437
438
439
440
(* 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))
441
     with Failure _ -> raise (Lazy.force exn_float_of));;