builtin.ml 7.73 KB
Newer Older
1
open Builtin_defs
2

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

5
6
(* Types *)

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

25
26
let env =
  List.fold_left
27
    (fun accu (n,t) -> 
28
29
       let n = (Ns.empty, Ident.U.mk n) in
       Types.Print.register_global Types.CompUnit.pervasives n t;
30
31
       Typer.enter_type (Ident.ident n) t accu
    )
32
    Typer.empty_env
33
34
35
36
    types

(* Operators *)

37
38
open Operators

39
let binary_op_gen = register_binary
40

41
let unary_op_gen = register_unary
42
43
44
45
46


let binary_op name t1 t2 f run =
  binary_op_gen
    name
47
    (fun arg1 arg2 constr precise ->
48
49
50
       f (arg1 t1 true) (arg2 t2 true))
    run

51
52
let binary_op_cst = register_op2

53
54
55

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

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

open Ident

let exn_load_file_utf8 = 
  Value.CDuceExn (
    Value.Pair (
78
      Value.Atom (Atoms.V.mk_ascii "load_file_utf8"),
79
80
81
82
83
      Value.string_latin1 "File is not a valid UTF-8 stream"))

let exn_int_of = 
  Value.CDuceExn (
    Value.Pair (
84
      Value.Atom (Atoms.V.mk_ascii "Invalid_argument"),
85
      Value.string_latin1 "int_of"))
86
87
88
89
90
let exn_float_of = 
  Value.CDuceExn (
    Value.Pair (
      Value.Atom (Atoms.V.mk_ascii "Invalid_argument"),
      Value.string_latin1 "float_of"))
91
92
93

let eval_load_file ~utf8 e =
  Location.protect_op "load_file";
94
  let fn = Value.get_string_latin1 e in
95
  let s = Url.load_url fn in
96
  if utf8 then 
97
98
99
    match U.mk_check s with 
      | Some s -> Value.string_utf8 s 
      | None -> raise exn_load_file_utf8
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  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));;
  
123
binary_op_cst ">=" 
124
125
126
127
128
129
130
131
132
133
134
  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 *)
  
135
register_fun "string_of" 
136
137
138
139
140
141
142
143
144
  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)
  );;

145
register_fun "load_xml"
146
  string_latin1 any_xml
147
  (fun v -> Location.protect_op "load_xml"; Load_xml.load_xml (Value.get_string_latin1 v));;
148
  
149
150
register_fun "load_html"
  string_latin1 Sequence.any
151
  (fun v -> Location.protect_op "load_html"; Load_xml.load_html  (Value.get_string_latin1 v));;
152

153
154
register_fun "load_file_utf8" 
  string_latin1 string 
155
156
  (eval_load_file ~utf8:true);;

157
158
register_fun "load_file" 
  string_latin1 string_latin1 
159
  (eval_load_file ~utf8:false);;
160
161


162
163
let argv = ref Value.Absent;;

164

165

166
167
168
169
170
171
172
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);;
173

174

175
176
register_fun "print"
  string_latin1 nil
177
178
179
180
181
182
183
  (fun v ->
     Location.protect_op "print";
     print_string (Value.get_string_latin1 v);
     flush stdout;
     Value.nil
  );;

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

194
195
196
197
unary_op_warning "int_of"
  string intstr int
  (fun v ->
     let (s,_) = Value.get_string_utf8 v in
198
     try Value.Integer (Intervals.V.mk (U.get_str s)) (* UTF-8 is ASCII compatible ! *)
199
200
     with Failure _ -> raise exn_int_of);;

201
register_fun "atom_of"
202
203
204
  string atom
  (fun v ->
     let (s,_) = Value.get_string_utf8 v in (* TODO: check that s is a correct Name wrt XML *)
205
     Value.Atom (Atoms.V.mk Ns.empty s));;
206
207

binary_op_warning2 "dump_to_file"
208
  string_latin1 string string_latin1 nil
209
210
211
212
213
214
215
216
  (fun f v ->
     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;
     Value.nil);;
    
binary_op_cst "dump_to_file_utf8"
217
  string_latin1 string nil
218
219
220
221
222
223
224
225
226
227
228
  (fun f v ->
     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;
     Value.nil);;

(* Integer operators *)

binary_op_gen "+"
229
  (fun arg1 arg2 constr precise ->
230
231
232
233
234
235
236
237
238
239
240
241
     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
     )
242
     else raise (Typer.Error "The first argument mixes integers and records"))
243
  (fun v1 v2 -> match (v1,v2) with
244
     | (Value.Integer x, Value.Integer y) -> Value.Integer (Intervals.V.add x y)
245
246
247
248
249
250
251
252
253
     | (Value.Record r1, Value.Record r2) -> Value.Record (LabelMap.merge (fun x y -> y) r1 r2)
     | _ -> assert false);;
      
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
254
     | (Value.Integer x, Value.Integer y) -> Value.Integer (Intervals.V.sub x y)
255
256
     | _ -> assert false);;

257
258
259
260
261
binary_op "*"
  int int
  (fun t1 t2 ->
     Types.interval 
     (Intervals.mul (Types.Int.get t1) (Types.Int.get t2)))
262
  (fun v1 v2 -> match (v1,v2) with
263
     | (Value.Integer x, Value.Integer y) -> Value.Integer (Intervals.V.mult x y)
264
265
     | _ -> assert false);;

266
267
binary_op_warning2 "/"
  int int non_zero_int int
268
  (fun v1 v2 -> match (v1,v2) with
269
     | (Value.Integer x, Value.Integer y) -> Value.Integer (Intervals.V.div x y)
270
271
     | _ -> assert false);;

272
273
binary_op_warning2 "mod"
  int int non_zero_int int
274
  (fun v1 v2 -> match (v1,v2) with
275
     | (Value.Integer x, Value.Integer y) -> Value.Integer (Intervals.V.modulo x y)
276
277
278
279
     | _ -> assert false);;


binary_op_gen "@"
280
  (fun arg1 arg2 constr precise ->
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
     let constr' = Sequence.star 
		     (Sequence.approx (Types.cap Sequence.any constr)) in
     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;;
  

302
register_fun "raise" any Types.empty
303
  (fun v -> raise (Value.CDuceExn v));;
304

305
306
307
308
309
310
311
312
313


(* 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))
     with Failure _ -> raise exn_float_of);;