/[svn]/runtime/value.ml
ViewVC logotype

Contents of /runtime/value.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1615 - (hide annotations)
Tue Jul 10 19:07:45 2007 UTC (5 years, 10 months ago) by abate
File size: 23182 byte(s)
[r2005-03-31 21:19:11 by afrisch] Empty log message

Original author: afrisch
Date: 2005-03-31 21:19:11+00:00
1 abate 233 open Ident
2 abate 310 open Encodings
3 abate 233
4 abate 48 type t =
5     | Pair of t * t
6 abate 405 | Xml of t * t * t
7 abate 1560 | XmlNs of t * t * t * Ns.table
8 abate 233 | Record of t label_map
9 abate 656 | Atom of Atoms.V.t
10     | Integer of Intervals.V.t
11     | Char of Chars.V.t
12 abate 1497 | Abstraction of (Types.descr * Types.descr) list option * (t -> t)
13 abate 691 | Abstraction2 of t array * (Types.t * Types.t) list * Lambda.branches
14 abate 956 | Abstract of Types.Abstract.V.t
15 abate 310 | String_latin1 of int * int * string * t
16 abate 374 | String_utf8 of Utf8.uindex * Utf8.uindex * Utf8.t * t
17 abate 695 | Concat of t * t
18 abate 229 | Absent
19 abate 48
20 abate 1103 (*
21     The only representation of the empty sequence is nil.
22     In particular, in String_latin1 and String_utf8, the string cannot be empty.
23     *)
24 abate 431
25 abate 1103 let dump_forward = ref (fun _ _ -> assert false)
26    
27 abate 64 exception CDuceExn of t
28    
29 abate 66 let nil = Atom Sequence.nil_atom
30 abate 1103 let string_latin1 s =
31     if String.length s = 0 then nil
32     else String_latin1 (0,String.length s, s, nil)
33     let string_utf8 s =
34     if String.length (Utf8.get_str s) = 0 then nil
35     else String_utf8 (Utf8.start_index s,Utf8.end_index s, s, nil)
36     let substring_utf8 i j s q =
37     if Utf8.equal_index i j then q
38     else String_utf8 (i,j,s,q)
39 abate 656 let vtrue = Atom (Atoms.V.mk_ascii "true")
40     let vfalse = Atom (Atoms.V.mk_ascii "false")
41 abate 237 let vbool x = if x then vtrue else vfalse
42 abate 66
43 abate 501 let vrecord l =
44 abate 811 let l = List.map (fun (qname,v) -> LabelPool.mk qname, v) l in
45 abate 623 Record (LabelMap.from_list_disj l)
46 abate 501
47     let get_fields = function
48 abate 811 | Record map -> LabelMap.mapi_to_list (fun k v -> LabelPool.value k, v) map
49 abate 883 | _ -> raise (Invalid_argument "Value.get_fields")
50 abate 501
51 abate 368 let rec sequence = function
52     | [] -> nil
53     | h::t -> Pair (h, sequence t)
54 abate 237
55 abate 1146 let rec sequence_rev accu = function
56     | [] -> accu
57     | h::t -> sequence_rev (Pair (h,accu)) t
58    
59     let sequence_rev l = sequence_rev nil l
60    
61 abate 1613 let sequence_of_array a =
62     let rec aux accu i =
63     if (i = 0) then accu
64     else let i = pred i in aux (Pair (a.(i), accu)) i in
65     aux nil (Array.length a)
66    
67 abate 695 let concat v1 v2 =
68     match (v1,v2) with
69     | (Atom _, v) | (v, Atom _) -> v
70     | (v1,v2) -> Concat (v1,v2)
71 abate 421
72 abate 1467 let append v1 v2 =
73     concat v1 (Pair (v2,nil))
74    
75 abate 1228 let raise' v = raise (CDuceExn v)
76 abate 1527 let failwith' s = raise' (string_latin1 s)
77 abate 421
78 abate 1228
79 abate 672 let rec const = function
80 abate 70 | Types.Integer i -> Integer i
81     | Types.Atom a -> Atom a
82     | Types.Char c -> Char c
83 abate 672 | Types.Pair (x,y) -> Pair (const x, const y)
84     | Types.Xml (x, Types.Pair (y, z)) -> Xml (const x, const y, const z)
85     | Types.Xml (_,_) -> assert false
86     | Types.Record x -> Record (LabelMap.map const x)
87     | Types.String (i,j,s,c) -> String_utf8 (i,j,s, const c)
88 abate 70
89 abate 754 let rec inv_const = function
90     | Pair (x, y) -> Types.Pair (inv_const x, inv_const y)
91 abate 1560 | Xml (x, y, z) | XmlNs (x,y,z,_) ->
92 abate 754 Types.Pair (inv_const x, Types.Pair (inv_const y, inv_const z))
93     | Record x -> Types.Record (LabelMap.map inv_const x)
94     | Atom a -> Types.Atom a
95     | Integer i -> Types.Integer i
96     | Char c -> Types.Char c
97     | String_latin1 (_, _, s, v) ->
98     let s = Utf8.mk s in
99     Types.String (Utf8.start_index s, Utf8.end_index s, s, inv_const v)
100     | String_utf8 (i, j, s, v) -> Types.String (i, j, s, inv_const v)
101     | Concat (x, y) as v ->
102     let rec children = function
103     | Concat (x, y) -> children x @ children y
104     | x -> [x]
105     in
106     inv_const (sequence (children v))
107     | _ -> failwith "inv_const"
108 abate 672
109 abate 695 let normalize_string_latin1 i j s q =
110     if i = j then q else
111     Pair (Char (Chars.V.mk_char (String.unsafe_get s i)), String_latin1 (succ i,j,s,q))
112    
113     let normalize_string_utf8 i j s q =
114     if Utf8.equal_index i j then q
115     else
116     let (c,i) = Utf8.next s i in
117     Pair (Char (Chars.V.mk_int c), String_utf8 (i,j,s,q))
118    
119    
120    
121     (***** The dirty things **********)
122    
123     type pair = { dummy : t; mutable pair_tl : t }
124     type str = { dummy1 : t; dummy2 : t; dummy3 : t; mutable str_tl : t }
125    
126     (* Could optimize this function by changing the order of the fields
127     in String_latin1, String_utf8 *)
128    
129     let set_cdr cell tl =
130     match cell with
131     | Pair (_,_) -> (Obj.magic cell).pair_tl <- tl
132     | String_latin1 (_,_,_,_)
133     | String_utf8(_,_,_,_)-> (Obj.magic cell).str_tl <- tl
134     | _ -> assert false
135    
136     let rec append_cdr cell tl =
137     match tl with
138     | Concat (x,y) ->
139     append_cdr (append_cdr cell x) y
140     | Pair (x,tl) ->
141     let cell' = Pair (x,Absent) in
142     set_cdr cell cell';
143     append_cdr cell' tl
144     | String_latin1 (s,i,j,tl) ->
145     let cell' = String_latin1 (s,i,j,Absent) in
146     set_cdr cell cell';
147     append_cdr cell' tl
148     | String_utf8 (s,i,j,tl) ->
149     let cell' = String_utf8 (s,i,j,Absent) in
150     set_cdr cell cell';
151     append_cdr cell' tl
152     | _ -> cell
153    
154    
155     let rec flatten = function
156     | Pair (x,y) -> concat x (flatten y)
157     | Concat (x,y) -> concat (flatten x) (flatten y)
158     | q -> q
159    
160     let eval_lazy_concat v =
161     let accu = Obj.magic (Pair (nil,Absent)) in
162     let rec aux accu = function
163     | Concat (x,y) -> aux (append_cdr accu x) y
164     | v -> set_cdr accu v
165     in
166     aux accu v;
167     let nv = match snd accu with
168     | Pair (_,_) as nv -> nv
169     | String_latin1 (i,j,s,q) -> normalize_string_latin1 i j s q
170     | String_utf8 (i,j,s,q) -> normalize_string_utf8 i j s q
171     | _ -> assert false in
172     let v = Obj.repr v in
173     let nv = Obj.repr nv in
174     Obj.set_tag v (Obj.tag nv);
175     Obj.set_field v 0 (Obj.field nv 0);
176     Obj.set_field v 1 (Obj.field nv 1)
177    
178    
179    
180     (******************************)
181    
182     let normalize = function
183     | String_latin1 (i,j,s,q) -> normalize_string_latin1 i j s q
184     | String_utf8 (i,j,s,q) -> normalize_string_utf8 i j s q
185     | Concat (_,_) as v -> eval_lazy_concat v; v
186 abate 1167 | v -> v
187 abate 695
188    
189    
190    
191 abate 310 let buf = Buffer.create 100
192 abate 71
193 abate 310 let rec add_buf_utf8_to_latin1 src i j =
194     if Utf8.equal_index i j then ()
195     else
196     let (c,i) = Utf8.next src i in
197 abate 1228 if (c > 255) then failwith' "get_string_latin1";
198 abate 310 Buffer.add_char buf (Char.chr c);
199     add_buf_utf8_to_latin1 src i j
200    
201 abate 386 let rec add_buf_latin1_to_utf8 src i j =
202     for k = i to j - 1 do
203     Utf8.store buf (Char.code src.[k])
204 abate 310 done
205    
206     let get_string_latin1 e =
207     let rec aux = function
208 abate 656 | Pair (Char x,y) -> Buffer.add_char buf (Chars.V.to_char x); aux y
209 abate 310 | String_latin1 (i,j,src,y) -> Buffer.add_substring buf src i (j - i); aux y
210     | String_utf8 (i,j,src,y) -> add_buf_utf8_to_latin1 src i j; aux y
211 abate 695 | Concat (_,_) as v -> eval_lazy_concat v; aux v
212 abate 310 | _ -> () in
213 abate 1228 Buffer.clear buf;
214 abate 310 aux e;
215     let s = Buffer.contents buf in
216     Buffer.clear buf;
217     s
218    
219     let get_string_utf8 e =
220     let rec aux = function
221 abate 656 | Pair (Char x,y) -> Utf8.store buf (Chars.V.to_int x); aux y
222 abate 386 | String_latin1 (i,j,src,y) -> add_buf_latin1_to_utf8 src i j; aux y
223 abate 310 | String_utf8 (i,j,src,y) -> Utf8.copy buf src i j; aux y
224 abate 695 | Concat (_,_) as v -> eval_lazy_concat v; aux v
225 abate 374 | q -> q in
226     let q = aux e in
227 abate 310 let s = Buffer.contents buf in
228     Buffer.clear buf;
229 abate 374 (Utf8.mk s, q)
230 abate 310
231 abate 501 let get_int = function
232 abate 656 | Integer i when Intervals.V.is_int i -> Intervals.V.get_int i
233 abate 501 | _ -> raise (Invalid_argument "Value.get_int")
234 abate 310
235 abate 1257 let get_integer = function
236     | Integer i -> i
237     | _ -> assert false
238    
239 abate 52 let rec is_seq = function
240     | Pair (_, y) when is_seq y -> true
241     | Atom a when a = Sequence.nil_atom -> true
242 abate 310 | String_latin1 (_,_,_,y) | String_utf8 (_,_,_,y) when is_seq y -> true
243 abate 695 | Concat (_,_) as v -> eval_lazy_concat v; is_seq v
244 abate 52 | _ -> false
245 abate 48
246 abate 52 let rec is_str = function
247 abate 76 | Pair (Char _, y) -> is_str y
248 abate 52 | Atom a when a = Sequence.nil_atom -> true
249 abate 310 | String_latin1 (_,_,_,q) | String_utf8(_,_,_,q) -> is_str q
250 abate 695 | Concat (_,_) as v -> eval_lazy_concat v; is_str v
251 abate 52 | _ -> false
252    
253     let rec print ppf v =
254 abate 374 if is_str v then
255     (Format.fprintf ppf "\"";
256     print_quoted_str ppf v;
257     Format.fprintf ppf "\"")
258     else if is_seq v then Format.fprintf ppf "[ @[<hv>%a@]]" print_seq v
259     else match v with
260 abate 52 | Pair (x,y) -> Format.fprintf ppf "(%a,%a)" print x print y
261 abate 1560 | Xml (x,y,z) | XmlNs (x,y,z,_) -> print_xml ppf x y z
262 abate 233 | Record l -> Format.fprintf ppf "{%a }" print_record (LabelMap.get l)
263 abate 656 | Atom a -> Atoms.V.print_quote ppf a
264     | Integer i -> Intervals.V.print ppf i
265     | Char c -> Chars.V.print ppf c
266 abate 69 | Abstraction _ -> Format.fprintf ppf "<fun>"
267 abate 691 | Abstraction2 _ -> Format.fprintf ppf "<fun>"
268 abate 310 | String_latin1 (i,j,s,q) ->
269     Format.fprintf ppf "<string_latin1:%i-%i,%S,%a>" i j s print q
270     | String_utf8 (i,j,s,q) ->
271     Format.fprintf ppf "<string_utf8:%i-%i,%S,%a>"
272     (Utf8.get_idx i) (Utf8.get_idx j) (Utf8.get_str s) print q
273 abate 695 | Concat (x,y) ->
274     Format.fprintf ppf "<concat:%a;%a>" print x print y
275 abate 1490 | Abstract ("float",o) ->
276     Format.fprintf ppf "%f" (Obj.magic o : float)
277 abate 1151 | Abstract (s,_) ->
278     Format.fprintf ppf "<abstract=%s>" s
279 abate 229 | Absent ->
280     Format.fprintf ppf "<[absent]>"
281 abate 52 and print_quoted_str ppf = function
282 abate 76 | Pair (Char c, q) ->
283 abate 656 Chars.V.print_in_string ppf c;
284 abate 76 print_quoted_str ppf q
285 abate 310 | String_latin1 (i,j,s, q) ->
286 abate 312 for k = i to j - 1 do
287 abate 656 Chars.V.print_in_string ppf (Chars.V.mk_char s.[k])
288 abate 311 done;
289 abate 76 print_quoted_str ppf q
290 abate 310 | String_utf8 (i,j,s, q) ->
291 abate 374 (* Format.fprintf ppf "UTF8:{"; *)
292     let rec aux i =
293     if Utf8.equal_index i j then q
294     else
295     let (c,i) =Utf8.next s i in
296 abate 656 Chars.V.print_in_string ppf (Chars.V.mk_int c);
297 abate 374 aux i
298     in
299     let q = aux i in
300     (* Format.fprintf ppf "}"; *)
301     print_quoted_str ppf q
302     | q -> q
303 abate 52 and print_seq ppf = function
304 abate 374 | (Pair(Char _, _)|String_latin1 (_,_,_,_)|String_utf8 (_,_,_,_)) as s ->
305 abate 76 Format.fprintf ppf "'";
306 abate 374 let q = print_quoted_str ppf s in
307     Format.fprintf ppf "'@ ";
308     print_seq ppf q
309     | Pair (x,y) ->
310     Format.fprintf ppf "@[%a@]@ " print x;
311     print_seq ppf y
312 abate 52 | _ -> ()
313    
314 abate 405 and print_xml ppf tag attr content =
315 abate 501 if is_seq content then
316     Format.fprintf ppf "@[<hv2><%a%a>[@ %a@]]"
317     print_tag tag
318     print_attr attr
319     print_seq content
320     else
321     Format.fprintf ppf "@[<hv2><%a%a>@ %a@]"
322     print_tag tag
323     print_attr attr
324     print content
325 abate 392 and print_tag ppf = function
326 abate 656 | Atom tag -> Atoms.V.print ppf tag
327 abate 392 | tag -> Format.fprintf ppf "(%a)" print tag
328     and print_attr ppf = function
329     | Record attr -> print_record ppf (LabelMap.get attr)
330     | attr -> Format.fprintf ppf "(%a)" print attr
331 abate 52
332 abate 48 and print_record ppf = function
333     | [] -> ()
334 abate 1519 | f :: rem -> Format.fprintf ppf " %a" print_field f; print_record ppf rem
335 abate 48
336     and print_field ppf (l,v) =
337 abate 542 Format.fprintf ppf "%a=%a" Label.print (LabelPool.value l) print v
338 abate 48
339 abate 754 let dump_xml ppf v =
340     let rec aux ppf = function
341     | Pair (x, y) ->
342     Format.fprintf ppf "@[<hv1>";
343     Format.fprintf ppf "<pair>@,%a@,%a@,</pair>@]" aux x aux y
344 abate 1560 | Xml (x, y, z) | XmlNs (x,y,z,_) ->
345 abate 754 Format.fprintf ppf "@[<hv1>";
346     Format.fprintf ppf "<xml>@,%a@,%a@,%a@,</xml>@]" aux x aux y aux z
347     | Record x ->
348     Format.fprintf ppf "@[<hv1>";
349     Format.fprintf ppf "<record>@,%a@,</record>@]"
350     (fun ppf x -> print_record ppf (LabelMap.get x)) x
351     | Atom a ->
352     Format.fprintf ppf "@[<hv1>";
353 abate 783 Format.fprintf ppf "<atom>@,%a@,</atom>@]"
354 abate 754 (fun ppf x -> Atoms.V.print ppf x) a
355     | Integer i ->
356     Format.fprintf ppf "@[<hv1>";
357     Format.fprintf ppf "<integer>@,%a@,</integer>@]"
358     (fun ppf x -> Intervals.V.print ppf x) i
359     | Char c ->
360     Format.fprintf ppf "@[<hv1>";
361     Format.fprintf ppf "<char>@,%a@,</char>@]"
362     (fun ppf x -> Chars.V.print ppf x) c
363     | Abstraction _ ->
364     Format.fprintf ppf "@[<hv1>";
365     Format.fprintf ppf "<abstraction />@]"
366     | Abstraction2 _ ->
367     Format.fprintf ppf "@[<hv1>";
368     Format.fprintf ppf "<abstraction2 />@]"
369 abate 1151 | Abstract (s,_) ->
370     Format.fprintf ppf "<abstract>%s</abstract>" s
371 abate 754 | String_latin1 (_, _, s, v) ->
372     Format.fprintf ppf "@[<hv1>";
373 abate 783 Format.fprintf ppf "<string_latin1>@,%s@,</string_latin1>@," s;
374 abate 754 Format.fprintf ppf "@[<hv1>";
375     Format.fprintf ppf "<follow>@,%a@,</follow>@]</string_latin1>@]" aux v
376     | String_utf8 (_, _, s, v) ->
377     Format.fprintf ppf "@[<hv1>";
378 abate 783 Format.fprintf ppf "<string_utf8>@,%s@,</string_utf8>@,"
379     (Utf8.get_str s);
380 abate 754 Format.fprintf ppf "@[<hv1>";
381     Format.fprintf ppf "<follow>@,%a@,</follow>@]</string_utf8>@]" aux v
382     | Concat (x, y) ->
383     Format.fprintf ppf "@[<hv1>";
384     Format.fprintf ppf "<concat>@,%a@,%a@,</concat>@]" aux x aux y
385     | Absent ->
386     Format.fprintf ppf "@[<hv1>";
387     Format.fprintf ppf "<absent />@]"
388     in
389     Format.fprintf ppf "@[<hv1>";
390     Format.fprintf ppf "<value>@,%a@,</value>@]" aux v
391    
392 abate 237 let rec compare x y =
393     if (x == y) then 0
394     else
395     match (x,y) with
396 abate 405 | Pair (x1,x2), Pair (y1,y2) ->
397 abate 237 let c = compare x1 y1 in if c <> 0 then c
398     else compare x2 y2
399 abate 1560 | (Xml (x1,x2,x3) | XmlNs (x1,x2,x3,_)),
400     (Xml (y1,y2,y3) | XmlNs(y1,y2,y3,_)) ->
401 abate 405 let c = compare x1 y1 in if c <> 0 then c
402 abate 512 else let c = compare x2 y2 in if c <> 0 then c
403 abate 405 else compare x3 y3
404 abate 237 | Record rx, Record ry -> LabelMap.compare compare rx ry
405 abate 656 | Atom x, Atom y -> Atoms.V.compare x y
406     | Integer x, Integer y -> Intervals.V.compare x y
407     | Char x, Char y -> Chars.V.compare x y
408 abate 424 | Abstraction (_,_), _
409 abate 1151 | _, Abstraction (_,_)
410 abate 691 | Abstraction2 (_,_,_), _
411     | _, Abstraction2 (_,_,_) ->
412     raise (CDuceExn (string_latin1 "comparing functional values"))
413 abate 1151 | Abstract (s1,v1), Abstract (s2,v2) ->
414     let c = Types.Abstract.T.compare s1 s2 in if c <> 0 then c
415 abate 956 else raise (CDuceExn (string_latin1 "comparing abstract values"))
416 abate 1372 | Absent,_ | _,Absent ->
417     Format.fprintf Format.std_formatter
418     "ERR: Compare %a %a@." print x print y;
419     assert false
420 abate 695 | Concat (_,_) as x, y -> eval_lazy_concat x; compare x y
421     | x, (Concat (_,_) as y) -> eval_lazy_concat y; compare x y
422 abate 310 | String_latin1 (ix,jx,sx,qx), String_latin1 (iy,jy,sy,qy) ->
423 abate 237 if (sx == sy) && (ix = iy) && (jx = jy) then compare qx qy
424     else
425     (* Note: we would like to compare first jx-ix and jy-iy,
426     but this is not compatible with the equivalence of values *)
427     let rec aux ix iy =
428     if (ix = jx) then
429     if (iy = jy) then compare qx qy
430 abate 310 else compare qx (normalize_string_latin1 iy jy sy qy)
431 abate 237 else
432 abate 310 if (iy = jy) then compare (normalize_string_latin1 ix jx sx qx) qy
433 abate 237 else
434     let c1 = String.unsafe_get sx ix
435     and c2 = String.unsafe_get sy iy in
436     if c1 < c2 then -1 else
437     if c1 > c2 then 1 else aux (ix + 1) (iy + 1)
438     in
439     aux ix iy
440 abate 310 | String_utf8 (ix,jx,sx,qx), String_utf8 (iy,jy,sy,qy) ->
441     if (sx == sy) && (Utf8.equal_index ix iy) && (Utf8.equal_index jx jy) then compare qx qy
442     else
443     let rec aux ix iy =
444     if (Utf8.equal_index ix jx) then
445     if (Utf8.equal_index iy jy) then compare qx qy
446     else compare qx (normalize_string_utf8 iy jy sy qy)
447     else
448     if (Utf8.equal_index iy jy) then compare (normalize_string_utf8 ix jx sx qx) qy
449     else
450     let (c1,ix) = Utf8.next sx ix in
451     let (c2,iy) = Utf8.next sy iy in
452     if c1 < c2 then -1 else
453     if c1 > c2 then 1 else aux ix iy
454     in
455     aux ix iy
456     | String_latin1 (i,j,s,q), _ -> compare (normalize_string_latin1 i j s q) y
457     | _, String_latin1 (i,j,s,q) -> compare x (normalize_string_latin1 i j s q)
458     | String_utf8 (i,j,s,q), _ -> compare (normalize_string_utf8 i j s q) y
459     | _, String_utf8 (i,j,s,q) -> compare x (normalize_string_utf8 i j s q)
460 abate 237
461 abate 424 | Pair (_,_), _ -> -1 | _, Pair(_,_) -> 1
462 abate 1560 | (Xml (_,_,_) | XmlNs (_,_,_,_)),_ -> -1
463     | _, (Xml(_,_,_) | XmlNs(_,_,_,_)) -> 1
464 abate 424 | Record _,_ -> -1 | _, Record _ -> 1
465     | Atom _,_ -> -1 | _, Atom _ -> 1
466     | Integer _,_ -> -1 | _, Integer _ -> 1
467 abate 956 | Abstract _, _ -> -1 | _, Abstract _ -> 1
468 abate 237
469 abate 506 let iter_xml pcdata_callback other_callback =
470     let rec aux = function
471     | v when compare v nil = 0 -> ()
472 abate 513 | Pair (Char c, tl) ->
473 abate 656 pcdata_callback (U.mk_char (Chars.V.to_int c));
474 abate 506 aux tl
475 abate 513 | String_latin1 (i,j,s,tl) ->
476     pcdata_callback (U.mk_latin1 (String.sub s i j));
477 abate 506 aux tl
478 abate 513 | String_utf8 (i,j,s,tl) ->
479 abate 506 pcdata_callback (U.mk (U.get_substr s i j));
480     aux tl
481     | Pair (hd, tl) ->
482     other_callback hd;
483     aux tl
484 abate 695 | Concat (_,_) as v -> eval_lazy_concat v; aux v
485 abate 513 | v -> raise (Invalid_argument "Value.iter_xml")
486 abate 506 in
487     function
488 abate 1560 | Xml (_,_,cont) | XmlNs (_,_,cont,_) -> aux cont
489 abate 506 | _ -> raise (Invalid_argument "Value.iter_xml")
490 abate 424
491 abate 1560 (*
492 abate 783 let map_xml map_pcdata map_other =
493     let patch_string_utf8 cont = function
494     | String_utf8 (i, j, u, v) when compare v nil = 0 ->
495     String_utf8 (i, j, u, cont)
496     | _ -> assert false
497     in
498 abate 811 let rec aux v =
499     match v with
500     | Pair (Char _, _) | String_latin1 _ | String_utf8 _ ->
501     let (u, rest) = get_string_utf8 v in
502     patch_string_utf8 (aux rest) (string_utf8 (map_pcdata u))
503 abate 783 | Pair (hd, tl) -> Pair (map_other hd, aux tl)
504     | Concat (_,_) as v -> eval_lazy_concat v; aux v
505     | v when compare v nil = 0 -> v
506     | v -> raise (Invalid_argument "Value.map_xml")
507     in
508     function
509     | Xml (tag,attrs,cont) -> Xml (tag, attrs, aux cont)
510     | _ -> raise (Invalid_argument "Value.map_xml")
511 abate 1560 *)
512 abate 518
513 abate 1399 let tagged_tuple tag vl =
514     let ct = sequence vl in
515     let at = Record LabelMap.empty in
516     let tag = Atom (Atoms.V.mk_ascii tag) in
517     Xml (tag, at, ct)
518    
519 abate 754 (** set of values *)
520 abate 518
521 abate 754 type tmp = t
522     module OrderedValue =
523     struct
524     type t = tmp
525     let compare = compare
526     end
527     module ValueSet = Set.Make(OrderedValue)
528    
529     let ( |<| ) x y = compare x y < 0
530     let ( |>| ) x y = compare x y > 0
531     let ( |<=| ) x y = let c = compare x y in c < 0 || c = 0
532     let ( |>=| ) x y = let c = compare x y in c > 0 || c = 0
533     let ( |=| ) x y = compare x y = 0
534 abate 839 let equal = ( |=| )
535 abate 754 let ( |<>| ) x y = compare x y <> 0
536    
537 abate 695 (*
538     let rec concat l1 l2 = match l1 with
539     | Pair (x,y) -> Pair (x, concat y l2)
540     | String_latin1 (s,i,j,q) -> String_latin1 (s,i,j, concat q l2)
541     | String_utf8 (s,i,j,q) -> String_utf8 (s,i,j, concat q l2)
542     | q -> l2
543 abate 518
544 abate 695 let rec flatten = function
545     | Pair (x,y) -> concat x (flatten y)
546     | q -> q
547 abate 518
548 abate 695 *)
549 abate 783
550    
551 abate 1103 let () = dump_forward := dump_xml
552 abate 1146
553     let get_pair v =
554     match normalize v with
555     | Pair (x,y) -> (x,y)
556     | _ -> assert false
557    
558     (* TODO: tail-rec version of get_sequence *)
559    
560     let rec get_sequence v =
561     match normalize v with
562     | Pair (x,y) -> x :: (get_sequence y)
563     | _ -> []
564    
565     let rec get_sequence_rev accu v =
566     match normalize v with
567     | Pair (x,y) -> get_sequence_rev (x::accu) y
568     | _ -> accu
569    
570     let get_sequence_rev v = get_sequence_rev [] v
571    
572 abate 1257 let rec fold_sequence f accu v =
573     match normalize v with
574     | Pair (x,y) -> fold_sequence f (f accu x) y
575     | _ -> accu
576    
577 abate 1146 let atom_ascii s =
578     Atom (Atoms.V.mk_ascii s)
579    
580     let get_variant = function
581     | Atom a -> Atoms.V.get_ascii a, None
582     | v -> match normalize v with
583     | Pair (Atom a,x) -> Atoms.V.get_ascii a, Some x
584     | _ -> assert false
585    
586     let label_ascii s =
587     LabelPool.mk (Ns.empty, U.mk s)
588    
589     let record l =
590     Record (LabelMap.from_list_disj l)
591    
592 abate 1404 let record_ascii l =
593     record (List.map (fun (l,v) -> (label_ascii l, v)) l)
594 abate 1146
595 abate 1404
596 abate 1146 let get_field v l =
597     match v with
598     | Record fields -> LabelMap.assoc l fields
599     | _ -> raise Not_found
600 abate 1148
601 abate 1404 let get_field_ascii v l = get_field v (label_ascii l)
602    
603 abate 1151 let abstract a v =
604     Abstract (a,Obj.repr v)
605    
606     let get_abstract = function
607     | Abstract (_,v) -> Obj.magic v
608     | _ -> assert false
609    
610 abate 1154
611 abate 1568 let get_label = LabelPool.mk (Ns.empty, U.mk "get")
612     let set_label = LabelPool.mk (Ns.empty, U.mk "set")
613     let mk_rf ~get ~set =
614     LabelMap.from_list_disj [ get_label, get; set_label, set ]
615    
616 abate 1154 let mk_ref t v =
617     let r = ref v in
618 abate 1497 let get = Abstraction (Some [Sequence.nil_type, t], fun _ -> !r)
619     and set = Abstraction (Some [t, Sequence.nil_type], fun x -> r := x; nil) in
620 abate 1568 Record (mk_rf ~get ~set)
621 abate 1154
622    
623     let mk_ext_ref t get set =
624 abate 1497 let get = Abstraction (
625     (match t with Some t -> Some [Sequence.nil_type, t] | None -> None),
626     fun _ -> get ())
627     and set = Abstraction (
628     (match t with Some t -> Some [t, Sequence.nil_type] | None -> None),
629     fun v -> set v; nil) in
630 abate 1568 Record (mk_rf ~get ~set)
631 abate 1154
632 abate 1165
633     let ocaml2cduce_int i =
634     Integer (Intervals.V.from_int i)
635    
636     let cduce2ocaml_int = function
637     | Integer i -> Intervals.V.get_int i
638     | _ -> assert false
639    
640     let ocaml2cduce_string = string_latin1
641    
642     let cduce2ocaml_string = get_string_latin1
643    
644 abate 1215 let ocaml2cduce_string_utf8 = string_utf8
645    
646     let cduce2ocaml_string_utf8 s = fst (get_string_utf8 s)
647    
648 abate 1165 let ocaml2cduce_char c =
649     Char (Chars.V.mk_char c)
650    
651 abate 1568 let ocaml2cduce_wchar c =
652     Char (Chars.V.mk_int c)
653    
654 abate 1165 let cduce2ocaml_char = function
655     | Char c -> Chars.V.to_char c
656     | _ -> assert false
657 abate 1215
658 abate 1568 (*
659 abate 1217 let ocaml2cduce_bigint i =
660     Integer (Intervals.V.from_bigint i)
661 abate 1215
662 abate 1217 let cduce2ocaml_bigint = function
663     | Integer i -> Intervals.V.get_bigint i
664     | _ -> assert false
665 abate 1568 *)
666 abate 1217
667 abate 1568 let ocaml2cduce_atom ns l =
668     Atom (Atoms.V.mk (Ns.mk ns) l)
669    
670 abate 1215 let print_utf8 v =
671     print_string (U.get_str v);
672     flush stdout
673 abate 1241
674 abate 1490
675     let float n =
676 abate 1568 Abstract ("float", Obj.repr n)
677 abate 1509
678     let cduce2ocaml_option f v =
679     match normalize v with
680     | Pair (x,y) -> Some (f x)
681     | _ -> None
682    
683    
684     let ocaml2cduce_option f = function
685     | Some x -> Pair (f x, nil)
686     | None -> nil
687    
688    
689    
690 abate 1568 let add v1 v2 = match (v1,v2) with
691     | (Integer x, Integer y) -> Integer (Intervals.V.add x y)
692     | (Record r1, Record r2) -> Record (LabelMap.merge (fun x y -> y) r1 r2)
693     | _ -> assert false
694    
695 abate 1582 let sub v1 v2 = match (v1,v2) with
696     | (Integer x, Integer y) -> Integer (Intervals.V.sub x y)
697     | _ -> assert false
698    
699 abate 1611 let mul v1 v2 = match (v1,v2) with
700     | (Integer x, Integer y) -> Integer (Intervals.V.mult x y)
701     | _ -> assert false
702 abate 1582
703 abate 1611 let div v1 v2 = match (v1,v2) with
704     | (Integer x, Integer y) -> Integer (Intervals.V.div x y)
705     | _ -> assert false
706    
707 abate 1613 let modulo v1 v2 = match (v1,v2) with
708     | (Integer x, Integer y) -> Integer (Intervals.V.modulo x y)
709     | _ -> assert false
710 abate 1611
711 abate 1613
712 abate 1568 let pair v1 v2 = Pair (v1,v2)
713     let xml v1 v2 v3 = Xml (v1,v2,v3)
714    
715     let mk_record labels fields =
716     let l = ref [] in
717     assert (Array.length labels == Array.length fields);
718     for i = 0 to Array.length labels - 1 do
719 abate 1583 l := (labels.(i),fields.(i)) :: !l;
720 abate 1568 done;
721     Record (LabelMap.from_list_disj !l)
722    
723 abate 1575
724 abate 1576 (* TODO: optimize cases
725     - (f x = [])
726     - all chars copied or deleted *)
727    
728 abate 1575 let rec transform_aux f accu = function
729     | Pair (x,y) -> let accu = Concat (accu, f x) in transform_aux f accu y
730     | Atom _ -> accu
731     | v -> transform_aux f accu (normalize v)
732    
733     let transform f v = transform_aux f nil v
734 abate 1576
735 abate 1577
736     let rec xtransform_aux f accu = function
737     | Pair (x,y) ->
738     let accu = match f x with
739     | Absent ->
740     let x = match x with
741     | Xml (tag, attr, child) ->
742     let child = xtransform_aux f nil child in
743     Xml (tag, attr, child)
744     | XmlNs (tag, attr, child, ns) ->
745     let child = xtransform_aux f nil child in
746     XmlNs (tag, attr, child, ns)
747     | x -> x in
748     Concat (accu, Pair (x,nil))
749     | x -> Concat (accu, x)
750     in
751     xtransform_aux f accu y
752     | Atom _ -> accu
753     | v -> xtransform_aux f accu (normalize v)
754    
755     let xtransform f v = xtransform_aux f nil v
756    
757 abate 1580 let remove_field l = function
758     | Record r -> Record (LabelMap.remove l r)
759     | _ -> assert false
760 abate 1613
761     let rec ocaml2cduce_list f = function
762     | [] -> nil
763     | hd::tl -> Pair (f hd, ocaml2cduce_list f tl)
764    
765 abate 1614 let rec cduce2ocaml_list f v =
766     match normalize v with
767 abate 1615 | Pair (x,y) -> f x :: (cduce2ocaml_list f y)
768 abate 1614 | _ -> []
769    
770 abate 1613 let no_attr = Record LabelMap.empty
771    
772     let ocaml2cduce_constr tag va = Xml (tag, no_attr, sequence_of_array va)

CVS Admin">CVS Admin
ViewVC Help
Powered by ViewVC 1.1.5