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
    "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
register_fun "string_of" 
173
174
175
176
177
178
179
180
181
  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)
  );;

182
register_fun "load_xml"
183
  string_latin1 any_xml
184
  (fun v -> Location.protect_op "load_xml"; Load_xml.load_xml (Value.get_string_latin1 v));;
185
  
186
187
188
189
190
191
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));;


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

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

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


205
206
let argv = ref Value.Absent;;

207

208

209
210
211
212
213
214
215
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);;
216

217

218
219
220
221
222
223
224
225
226
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);;


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

236
237
238
239
240
241
242
243
244
245
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
  );;

246
247
248
249
unary_op_warning "int_of"
  string intstr int
  (fun v ->
     let (s,_) = Value.get_string_utf8 v in
250
251
252
253
254
255
256
257
258
259
260
261
262
263
      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))
264
                 with Failure _ -> raise (Lazy.force exn_int_of)));;
265

266
267
268
269
(*  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                 *)
270

271

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

register_fun "split_atom"
  atom (Types.times stringn stringn)
  (function 
283
284
     | Value.Atom q ->
	 let (ns,l) = Atoms.V.value q in
285
	 Value.Pair(
286
	   Value.string_utf8 (Ns.Uri.value ns),
287
288
289
290
291
292
293
294
295
296
	   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 *)
297
     Value.Atom (Atoms.V.mk (Ns.Uri.mk ns, l)));;
298
299
300


   
301
302

binary_op_warning2 "dump_to_file"
303
  string_latin1 string string_latin1 nil
304
  (fun f v -> try
305
306
307
308
     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;
309
310
     Value.nil
   with exn -> raise_gen exn);;
311
312
    
binary_op_cst "dump_to_file_utf8"
313
  string_latin1 string nil
314
  (fun f v -> try
315
316
317
318
319
     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;
320
321
     Value.nil
   with exn -> raise_gen exn);;
322
323
324
325

(* Integer operators *)

binary_op_gen "+"
326
  (fun arg1 arg2 constr precise ->
327
328
329
330
331
332
333
334
335
336
337
338
     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
     )
339
     else raise (Typer.Error "The first argument mixes integers and records"))
340
  Value.add;;
341
342
343
344
345
346
347
      
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
348
     | (Value.Integer x, Value.Integer y) -> Value.Integer (Intervals.V.sub x y)
349
350
     | _ -> assert false);;

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

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

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


binary_op_gen "@"
374
  (fun arg1 arg2 constr precise ->
375
     let constr' = Sequence.ub_concat constr in
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
     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;;
  

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

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

411
412
413
414
415
416
417
418
419
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
420
			      (pr,Ns.Uri.mk ns)) ns in
421
422
423
	 Value.XmlNs(v1,v2,v3,Ns.mk_table  ns)
     | _ -> assert false);;

424
425
426
427
428
429
(* 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))
430
     with Failure _ -> raise (Lazy.force exn_float_of));;