builtin.ml 11.5 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 (Types.BoolChars.atom (`Atm ( 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

Pietro Abate's avatar
Pietro Abate committed
119
120
121
122
123
124
let exn_cdata_of = lazy (
  Value.CDuceExn (
    Value.Pair (
      Value.Atom (Atoms.V.mk_ascii "Invalid_argument"),
      Value.string_latin1 "cdata_of"))
)
125
126

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

register_fun "char_of_int"
170
  int (Types.char (Types.BoolChars.atom (`Atm ( Chars.any ) )))
171
172
173
  (function
     | Value.Integer x ->
	 (try Value.Char (Chars.V.mk_int (Intervals.V.get_int x))
174
	 with Failure _ -> raise (Lazy.force exn_int_of))
175
176
     | _ -> assert false);;

177
register_fun "int_of_char"
178
  (Types.char (Types.BoolChars.atom (`Atm ( Chars.any) ))) int
179
180
181
182
183
  (function
     | Value.Char x ->
	 Value.Integer (Intervals.V.from_int (Chars.V.to_int x))
     | _ -> assert false);;

184
  
185
register_fun "string_of" 
186
187
188
189
190
191
192
193
194
  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)
  );;

195
register_fun "load_xml"
196
  string_latin1 any_xml
197
  (fun v -> Cduce_loc.protect_op "load_xml"; Load_xml.load_xml (Value.get_string_latin1 v));;
198
  
199
200
register_fun "!load_xml"
  string_latin1 any_xml
201
  (fun v -> Cduce_loc.protect_op "load_xml"; Load_xml.load_xml ~ns:true
202
203
204
     (Value.get_string_latin1 v));;


205
206
register_fun "load_html"
  string_latin1 Sequence.any
207
  (fun v -> Cduce_loc.protect_op "load_html"; Load_xml.load_html  (Value.get_string_latin1 v));;
208

209
210
register_fun "load_file_utf8" 
  string_latin1 string 
211
212
  (eval_load_file ~utf8:true);;

213
214
register_fun "load_file" 
  string_latin1 string_latin1 
215
  (eval_load_file ~utf8:false);;
216
217


218
219
let argv = ref Value.Absent;;

220

221

222
223
224
225
226
227
228
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);;
229

230

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

register_fun "dump_xml_utf8" 
238
239
  Types.any nil
  (fun v -> 
240
     Cduce_loc.protect_op "print";
241
     Print_xml.dump_xml ~utf8:true !Eval.ns_table v);;
242
243


244
245
register_fun "print"
  string_latin1 nil
246
  (fun v ->
247
     Cduce_loc.protect_op "print";
248
249
250
251
252
     print_string (Value.get_string_latin1 v);
     flush stdout;
     Value.nil
  );;

253
254
255
register_fun "print_utf8"
  string nil
  (fun v ->
256
     Cduce_loc.protect_op "print";
257
258
259
260
261
262
     let s = Value.cduce2ocaml_string_utf8 v in
     print_string (U.get_str s);
     flush stdout;
     Value.nil
  );;

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

283
284
285
286
(*  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                 *)
287

288

289
(*
290
register_fun "atom_of"
291
292
  string atom
  (fun v ->
293
     let (s,_) = Value.get_string_utf8 v in 
294
     Value.Atom (Atoms.V.mk Ns.empty s));;
295
296
297
298
299
*)

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


   
318
319

binary_op_warning2 "dump_to_file"
320
  string_latin1 string string_latin1 nil
321
  (fun f v -> try
322
     Cduce_loc.protect_op "dump_to_file";
323
324
325
     let oc = open_out (Value.get_string_latin1 f) in
     output_string oc (Value.get_string_latin1 v);
     close_out oc;
326
327
     Value.nil
   with exn -> raise_gen exn);;
328
329
    
binary_op_cst "dump_to_file_utf8"
330
  string_latin1 string nil
331
  (fun f v -> try
332
     Cduce_loc.protect_op "dump_to_file_utf8";
333
334
335
336
     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;
337
338
     Value.nil
   with exn -> raise_gen exn);;
339
340
341

(* Integer operators *)

342
343
344
let intop f x y =
  let s = Types.BoolIntervals.get x in
  let t = Types.BoolIntervals.get y in
345
  Types.BoolIntervals.atom (`Atm (f s t))
346
347
;;

348
binary_op_gen "+"
349
  (fun arg1 arg2 constr precise ->
350
351
352
353
     let t1 = arg1 (Types.cup int Types.Record.any) true in
     if Types.subtype t1 int 
     then (
       let t2 = arg2 int true in
354
355
       Types.interval 
	 (intop Intervals.add (Types.Int.get t1) (Types.Int.get t2))
356
357
358
359
360
361
     )
     else if Types.subtype t1 Types.Record.any 
     then (
       let t2 = arg2 Types.Record.any true in 
       Types.Record.merge t1 t2
     )
362
     else raise (Typer.Error "The first argument mixes integers and records"))
363
  Value.add;;
364
365
366
367
368
      
binary_op "-"
  int int
  (fun t1 t2 ->
     Types.interval 
369
     (intop Intervals.sub (Types.Int.get t1) (Types.Int.get t2)))
370
  (fun v1 v2 -> match (v1,v2) with
371
     | (Value.Integer x, Value.Integer y) -> Value.Integer (Intervals.V.sub x y)
372
373
     | _ -> assert false);;

374
375
376
377
binary_op "*"
  int int
  (fun t1 t2 ->
     Types.interval 
378
     (intop Intervals.mul (Types.Int.get t1) (Types.Int.get t2)))
379
  (fun v1 v2 -> match (v1,v2) with
380
     | (Value.Integer x, Value.Integer y) -> Value.Integer (Intervals.V.mult x y)
381
382
     | _ -> assert false);;

383
384
binary_op_warning2 "/"
  int int non_zero_int int
385
  (fun v1 v2 -> match (v1,v2) with
386
     | (Value.Integer x, Value.Integer y) -> Value.Integer (Intervals.V.div x y)
387
388
     | _ -> assert false);;

389
390
binary_op_warning2 "mod"
  int int non_zero_int int
391
  (fun v1 v2 -> match (v1,v2) with
392
     | (Value.Integer x, Value.Integer y) -> Value.Integer (Intervals.V.modulo x y)
393
394
395
396
     | _ -> assert false);;


binary_op_gen "@"
397
  (fun arg1 arg2 constr precise ->
398
     let constr' = Sequence.ub_concat constr in
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
     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;;
  

418
register_fun "raise" any Types.empty
419
  (fun v -> raise (Value.CDuceExn v));;
420

421
422
423
424
425
426
427
428
register_fun "namespaces" any_xml 
  namespaces
  (function 
       Value.XmlNs (_,_,_,ns) ->
	 Value.sequence_rev 
	   (List.map 
	      (fun (pr,ns) ->
		 Value.Pair (Value.string_utf8 pr,
429
			     Value.string_utf8 (Ns.Uri.value ns)))
430
	      (Ns.get_table ns))
431
     | Value.Xml _ -> raise (Lazy.force exn_namespaces)
432
     | _ -> assert false);;
433

434
435
436
437
438
439
440
441
442
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
443
			      (pr,Ns.Uri.mk ns)) ns in
444
445
446
	 Value.XmlNs(v1,v2,v3,Ns.mk_table  ns)
     | _ -> assert false);;

447
448
449
450
451
452
(* 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))
453
     with Failure _ -> raise (Lazy.force exn_float_of));;
Pietro Abate's avatar
Pietro Abate committed
454
455
456
457
458
459
460
461

(* cdata *)

register_fun "cdata_of" string string
  (fun v ->
     let (s,_) = Value.get_string_utf8 v in
     try Value.cdata (U.get_str s)
     with Failure _ -> raise (Lazy.force exn_cdata_of));;