| 1 |
abate |
70 |
(* Running dispatchers *)
|
| 2 |
|
|
|
| 3 |
abate |
245 |
(* Possible simple optimizations:
|
| 4 |
|
|
- in make_result_prod, see if buffer can be simply overwritten
|
| 5 |
|
|
(precompute this ...)
|
| 6 |
abate |
405 |
- optimize for Xml elements (don't build the Pair (attr,content))
|
| 7 |
abate |
245 |
*)
|
| 8 |
|
|
|
| 9 |
abate |
332 |
(*
|
| 10 |
|
|
let (<) : int -> int -> bool = (<);;
|
| 11 |
|
|
*)
|
| 12 |
|
|
|
| 13 |
abate |
70 |
open Value
|
| 14 |
abate |
233 |
open Ident
|
| 15 |
abate |
172 |
open Patterns.Compile
|
| 16 |
abate |
310 |
open Encodings
|
| 17 |
abate |
70 |
|
| 18 |
abate |
237 |
|
| 19 |
abate |
245 |
let buffer = ref (Array.create 127 Absent)
|
| 20 |
|
|
let cursor = ref 0
|
| 21 |
|
|
|
| 22 |
|
|
let blit a1 ofs1 a2 ofs2 len =
|
| 23 |
|
|
for i = 0 to len - 1 do
|
| 24 |
|
|
Array.unsafe_set a2 (ofs2 + i) (Array.unsafe_get a1 (ofs1 + i))
|
| 25 |
|
|
done
|
| 26 |
abate |
248 |
(* important to do this in the increasing order ... *)
|
| 27 |
abate |
245 |
|
| 28 |
|
|
|
| 29 |
|
|
let ensure_room n =
|
| 30 |
|
|
let l = Array.length !buffer in
|
| 31 |
|
|
if !cursor + n > l then
|
| 32 |
|
|
let buffer' = Array.create (l * 2 + n) Absent in
|
| 33 |
|
|
blit !buffer 0 buffer' 0 !cursor;
|
| 34 |
|
|
buffer := buffer'
|
| 35 |
abate |
1369 |
|
| 36 |
|
|
let push v =
|
| 37 |
|
|
ensure_room 1;
|
| 38 |
|
|
!buffer.(!cursor) <- v;
|
| 39 |
|
|
incr cursor
|
| 40 |
|
|
|
| 41 |
|
|
|
| 42 |
|
|
(* Old dispatchers *)
|
| 43 |
abate |
245 |
|
| 44 |
abate |
70 |
let make_result_prod v1 r1 v2 r2 v (code,r) =
|
| 45 |
abate |
245 |
let n = Array.length r in
|
| 46 |
abate |
332 |
if n == 0 then code else (
|
| 47 |
abate |
245 |
ensure_room n;
|
| 48 |
|
|
let buf = !buffer in
|
| 49 |
abate |
248 |
let c = !cursor in
|
| 50 |
abate |
245 |
for a = 0 to n - 1 do
|
| 51 |
|
|
let x = match Array.unsafe_get r a with
|
| 52 |
|
|
| Catch -> v
|
| 53 |
|
|
| Const c -> const c
|
| 54 |
abate |
1415 |
| Left i -> (match i with (-1) -> v1 | (-2) -> nil | _ -> buf.(r1+i))
|
| 55 |
|
|
| Right j -> (match j with (-1) -> v2 | (-2) -> nil | _ -> buf.(r2+j))
|
| 56 |
abate |
245 |
| Recompose (i,j) ->
|
| 57 |
abate |
1415 |
Pair (
|
| 58 |
|
|
(match i with (-1) -> v1 | (-2) -> nil | _ -> buf.(r1+i)),
|
| 59 |
|
|
(match j with (-1) -> v2 | (-2) -> nil | _ -> buf.(r2+j))
|
| 60 |
|
|
)
|
| 61 |
abate |
245 |
in
|
| 62 |
abate |
248 |
buf.(c + a) <- x
|
| 63 |
abate |
245 |
done;
|
| 64 |
abate |
332 |
if r1 != c then blit buf c buf r1 n;
|
| 65 |
abate |
245 |
cursor := r1 + n; (* clean space for GC ? *)
|
| 66 |
|
|
code )
|
| 67 |
abate |
70 |
|
| 68 |
|
|
let make_result_basic v (code,r) =
|
| 69 |
abate |
245 |
let n = Array.length r in
|
| 70 |
abate |
332 |
if n == 0 then code else (
|
| 71 |
abate |
245 |
ensure_room n;
|
| 72 |
|
|
let buf = !buffer in
|
| 73 |
|
|
for a = 0 to n - 1 do
|
| 74 |
|
|
let x = match Array.unsafe_get r a with
|
| 75 |
|
|
| Catch -> v
|
| 76 |
|
|
| Const c -> const c
|
| 77 |
|
|
| _ -> assert false
|
| 78 |
|
|
in
|
| 79 |
abate |
484 |
buf.(!cursor) <- x;
|
| 80 |
|
|
incr cursor
|
| 81 |
abate |
245 |
done;
|
| 82 |
|
|
code )
|
| 83 |
abate |
70 |
|
| 84 |
abate |
245 |
|
| 85 |
abate |
231 |
let make_result_char ch (code,r) =
|
| 86 |
abate |
245 |
let n = Array.length r in
|
| 87 |
abate |
332 |
if n == 0 then code else (
|
| 88 |
abate |
245 |
ensure_room n;
|
| 89 |
|
|
let buf = !buffer in
|
| 90 |
|
|
for a = 0 to n - 1 do
|
| 91 |
|
|
let x = match Array.unsafe_get r a with
|
| 92 |
|
|
| Catch -> Char ch
|
| 93 |
|
|
| Const c -> const c
|
| 94 |
|
|
| _ -> assert false
|
| 95 |
|
|
in
|
| 96 |
|
|
buf.(!cursor + a) <- x
|
| 97 |
|
|
done;
|
| 98 |
abate |
246 |
cursor := !cursor + n;
|
| 99 |
abate |
245 |
code )
|
| 100 |
abate |
231 |
|
| 101 |
abate |
310 |
let tail_string_latin1 i j s q =
|
| 102 |
abate |
332 |
if i + 1 == j then q else String_latin1 (i + 1,j,s,q)
|
| 103 |
abate |
243 |
|
| 104 |
abate |
310 |
let make_result_string_latin1 i j s q r1 r2 (code,r) =
|
| 105 |
abate |
245 |
let n = Array.length r in
|
| 106 |
abate |
332 |
if n == 0 then code else (
|
| 107 |
abate |
245 |
ensure_room n;
|
| 108 |
|
|
let buf = !buffer in
|
| 109 |
|
|
for a = 0 to n - 1 do
|
| 110 |
|
|
let x = match Array.unsafe_get r a with
|
| 111 |
abate |
310 |
| Catch -> String_latin1 (i,j,s,q)
|
| 112 |
abate |
245 |
| Const c -> const c
|
| 113 |
abate |
1415 |
| Left n -> (match n with
|
| 114 |
|
|
| (-1) -> Char (Chars.V.mk_char s.[i])
|
| 115 |
|
|
| (-2) -> nil
|
| 116 |
|
|
| _ -> buf.(r1+n))
|
| 117 |
|
|
| Right m -> (match m with
|
| 118 |
|
|
| (-1) -> tail_string_latin1 i j s q
|
| 119 |
|
|
| (-2) -> nil
|
| 120 |
|
|
| _ -> buf.(r2+m))
|
| 121 |
abate |
245 |
| Recompose (n,m) ->
|
| 122 |
abate |
1415 |
Pair (
|
| 123 |
|
|
(match n with
|
| 124 |
|
|
| (-1) -> Char (Chars.V.mk_char s.[i])
|
| 125 |
|
|
| (-2) -> nil
|
| 126 |
|
|
| _ -> buf.(r1+n)),
|
| 127 |
|
|
(match m with
|
| 128 |
|
|
| (-1) -> tail_string_latin1 i j s q
|
| 129 |
|
|
| (-2) -> nil
|
| 130 |
|
|
| _ -> buf.(r2+m))
|
| 131 |
|
|
)
|
| 132 |
abate |
245 |
in
|
| 133 |
|
|
buf.(!cursor + a) <- x
|
| 134 |
|
|
done;
|
| 135 |
abate |
332 |
if r1 != !cursor then blit buf !cursor buf r1 n;
|
| 136 |
abate |
245 |
cursor := r1 + n;
|
| 137 |
|
|
code )
|
| 138 |
abate |
232 |
|
| 139 |
abate |
310 |
let tail_string_utf8 i j s q =
|
| 140 |
|
|
let i = Utf8.advance s i in
|
| 141 |
|
|
if Utf8.equal_index i j then q else String_utf8 (i,j,s,q)
|
| 142 |
abate |
232 |
|
| 143 |
abate |
310 |
let make_result_string_utf8 i j s q r1 r2 (code,r) =
|
| 144 |
|
|
let n = Array.length r in
|
| 145 |
abate |
332 |
if n == 0 then code else (
|
| 146 |
abate |
310 |
ensure_room n;
|
| 147 |
|
|
let buf = !buffer in
|
| 148 |
|
|
for a = 0 to n - 1 do
|
| 149 |
|
|
let x = match Array.unsafe_get r a with
|
| 150 |
|
|
| Catch -> String_utf8 (i,j,s,q)
|
| 151 |
|
|
| Const c -> const c
|
| 152 |
abate |
1415 |
| Left n -> (match n with
|
| 153 |
|
|
| (-1) -> Char (Chars.V.mk_int (Utf8.get s i))
|
| 154 |
|
|
| (-2) -> nil
|
| 155 |
|
|
| _ -> buf.(r1+n))
|
| 156 |
|
|
| Right m -> (match m with
|
| 157 |
|
|
| (-1) -> tail_string_utf8 i j s q
|
| 158 |
|
|
| (-2) -> nil
|
| 159 |
|
|
| _ -> buf.(r2+m))
|
| 160 |
abate |
310 |
| Recompose (n,m) ->
|
| 161 |
abate |
1415 |
Pair (
|
| 162 |
|
|
(match n with
|
| 163 |
|
|
| (-1) -> Char (Chars.V.mk_int (Utf8.get s i))
|
| 164 |
|
|
| (-2) -> nil
|
| 165 |
|
|
| _ -> buf.(r1+n)),
|
| 166 |
|
|
(match m with
|
| 167 |
|
|
| (-1) -> tail_string_utf8 i j s q
|
| 168 |
|
|
| (-2) -> nil
|
| 169 |
|
|
| _ -> buf.(r2+m))
|
| 170 |
|
|
)
|
| 171 |
abate |
310 |
in
|
| 172 |
|
|
buf.(!cursor + a) <- x
|
| 173 |
|
|
done;
|
| 174 |
abate |
332 |
if r1 != !cursor then blit buf !cursor buf r1 n;
|
| 175 |
abate |
310 |
cursor := r1 + n;
|
| 176 |
|
|
code )
|
| 177 |
|
|
|
| 178 |
abate |
230 |
let rec run_disp_basic v f = function
|
| 179 |
|
|
| [(_,r)] -> make_result_basic v r
|
| 180 |
|
|
| (t,r)::rem -> if f t then make_result_basic v r else run_disp_basic v f rem
|
| 181 |
abate |
1372 |
| _ ->
|
| 182 |
|
|
Format.fprintf Format.std_formatter "ERR: %a@." Value.print v;
|
| 183 |
|
|
assert false
|
| 184 |
abate |
230 |
|
| 185 |
abate |
70 |
let rec run_dispatcher d v =
|
| 186 |
abate |
1372 |
(* Format.fprintf Format.std_formatter "Matching (%a) with:@." Value.print v; *)
|
| 187 |
|
|
(* Patterns.Compile.print_dispatcher Format.std_formatter d;
|
| 188 |
abate |
229 |
*)
|
| 189 |
abate |
172 |
match actions d with
|
| 190 |
|
|
| AIgnore r -> make_result_basic v r
|
| 191 |
|
|
| AKind k -> run_disp_kind k v
|
| 192 |
abate |
70 |
|
| 193 |
abate |
71 |
and run_disp_kind actions v =
|
| 194 |
|
|
match v with
|
| 195 |
abate |
172 |
| Pair (v1,v2) -> run_disp_prod v v1 v2 actions.prod
|
| 196 |
abate |
405 |
| Xml (v1,v2,v3) -> run_disp_prod v v1 (Pair (v2,v3)) actions.xml
|
| 197 |
abate |
233 |
| Record r -> run_disp_record false v (LabelMap.get r) actions.record
|
| 198 |
abate |
1370 |
| String_latin1 (i,j,s,q) ->
|
| 199 |
abate |
1372 |
(* run_disp_kind actions (Value.normalize v) *)
|
| 200 |
|
|
run_disp_string_latin1 i j s q actions
|
| 201 |
abate |
1370 |
| String_utf8 (i,j,s,q) ->
|
| 202 |
abate |
1372 |
(* run_disp_kind actions (Value.normalize v) *)
|
| 203 |
|
|
run_disp_string_utf8 i j s q actions
|
| 204 |
abate |
243 |
| Atom a -> make_result_basic v (Atoms.get_map a actions.atoms)
|
| 205 |
|
|
| Char c -> make_result_basic v (Chars.get_map c actions.chars)
|
| 206 |
abate |
70 |
| Integer i ->
|
| 207 |
abate |
172 |
run_disp_basic v (fun t -> Types.Int.has_int t i) actions.basic
|
| 208 |
abate |
691 |
| Abstraction (iface,_)
|
| 209 |
|
|
| Abstraction2 (_,iface,_) ->
|
| 210 |
abate |
70 |
run_disp_basic v (fun t -> Types.Arrow.check_iface iface t)
|
| 211 |
abate |
172 |
actions.basic
|
| 212 |
abate |
956 |
| Abstract (abs,_) ->
|
| 213 |
|
|
run_disp_basic v (fun t -> Types.Abstract.contains abs (Types.get_abstract t))
|
| 214 |
|
|
actions.basic
|
| 215 |
abate |
229 |
| Absent ->
|
| 216 |
|
|
run_disp_basic v (fun t -> Types.Record.has_absent t) actions.basic
|
| 217 |
abate |
695 |
| Concat (_,_) as v -> run_disp_kind actions (Value.normalize v)
|
| 218 |
abate |
70 |
|
| 219 |
abate |
229 |
and run_disp_prod v v1 v2 = function
|
| 220 |
abate |
172 |
| Impossible -> assert false
|
| 221 |
|
|
| TailCall d1 -> run_dispatcher d1 v1
|
| 222 |
abate |
246 |
| Ignore d2 -> run_disp_prod2 v1 !cursor v v2 d2
|
| 223 |
abate |
172 |
| Dispatch (d1,b1) ->
|
| 224 |
abate |
245 |
let r1 = !cursor in
|
| 225 |
|
|
let code1 = run_dispatcher d1 v1 in
|
| 226 |
abate |
70 |
run_disp_prod2 v1 r1 v v2 b1.(code1)
|
| 227 |
|
|
|
| 228 |
abate |
229 |
and run_disp_prod2 v1 r1 v v2 = function
|
| 229 |
abate |
172 |
| Impossible -> assert false
|
| 230 |
abate |
246 |
| Ignore r -> make_result_prod v1 r1 v2 !cursor v r
|
| 231 |
abate |
172 |
| TailCall d2 -> run_dispatcher d2 v2
|
| 232 |
|
|
| Dispatch (d2,b2) ->
|
| 233 |
abate |
245 |
let r2 = !cursor in
|
| 234 |
|
|
let code2 = run_dispatcher d2 v2 in
|
| 235 |
abate |
70 |
make_result_prod v1 r1 v2 r2 v b2.(code2)
|
| 236 |
abate |
405 |
|
| 237 |
abate |
229 |
and run_disp_record other v fields = function
|
| 238 |
abate |
70 |
| None -> assert false
|
| 239 |
abate |
230 |
| Some (RecLabel (l,d)) ->
|
| 240 |
abate |
165 |
let rec aux other = function
|
| 241 |
|
|
| (l1,_) :: rem when l1 < l -> aux true rem
|
| 242 |
abate |
332 |
| (l1,vl) :: rem when l1 == l ->
|
| 243 |
abate |
1373 |
run_disp_record1 v other vl rem d
|
| 244 |
abate |
229 |
| rem ->
|
| 245 |
abate |
1373 |
run_disp_record1 v other Absent rem d
|
| 246 |
abate |
70 |
in
|
| 247 |
abate |
165 |
aux other fields
|
| 248 |
abate |
230 |
| Some (RecNolabel (some,none)) ->
|
| 249 |
abate |
593 |
let other = other || (fields != []) in
|
| 250 |
abate |
229 |
let r = if other then some else none in
|
| 251 |
|
|
match r with
|
| 252 |
|
|
| Some r -> make_result_basic v r
|
| 253 |
|
|
| None -> assert false
|
| 254 |
|
|
|
| 255 |
abate |
1373 |
and run_disp_record1 v other v1 rem = function
|
| 256 |
abate |
229 |
| Impossible -> assert false
|
| 257 |
|
|
| TailCall d1 -> run_dispatcher d1 v1
|
| 258 |
abate |
1373 |
| Ignore d2 -> run_disp_record2 v other v1 !cursor rem d2
|
| 259 |
abate |
229 |
| Dispatch (d1,b1) ->
|
| 260 |
abate |
245 |
let r1 = !cursor in
|
| 261 |
|
|
let code1 = run_dispatcher d1 v1 in
|
| 262 |
abate |
1373 |
run_disp_record2 v other v1 r1 rem b1.(code1)
|
| 263 |
abate |
70 |
|
| 264 |
abate |
1373 |
and run_disp_record2 v other v1 r1 rem = function
|
| 265 |
abate |
172 |
| Impossible -> assert false
|
| 266 |
abate |
1373 |
| Ignore r -> make_result_prod v1 r1 Absent 0 v r
|
| 267 |
|
|
| TailCall d2 -> run_disp_record_loop v other rem d2
|
| 268 |
abate |
229 |
| Dispatch (d2,b2) ->
|
| 269 |
abate |
245 |
let r2 = !cursor in
|
| 270 |
abate |
1373 |
let code2 = run_disp_record_loop v other rem d2 in
|
| 271 |
|
|
make_result_prod v1 r1 Absent r2 v b2.(code2)
|
| 272 |
abate |
229 |
|
| 273 |
abate |
1373 |
and run_disp_record_loop v other rem d =
|
| 274 |
abate |
229 |
match actions d with
|
| 275 |
abate |
1373 |
| AIgnore r -> make_result_basic v r
|
| 276 |
|
|
| AKind k -> run_disp_record other v rem k.record
|
| 277 |
abate |
229 |
|
| 278 |
abate |
231 |
|
| 279 |
abate |
310 |
and run_disp_string_latin1 i j s q actions =
|
| 280 |
abate |
332 |
if i == j then run_disp_kind actions q
|
| 281 |
abate |
231 |
else match actions.prod with
|
| 282 |
|
|
| Impossible -> assert false
|
| 283 |
abate |
656 |
| TailCall d1 -> run_disp_string_latin1_char d1 (Chars.V.mk_char s.[i])
|
| 284 |
abate |
310 |
| Ignore d2 -> run_disp_string_latin1_2 !cursor i j s q d2
|
| 285 |
abate |
231 |
| Dispatch (d1,b1) ->
|
| 286 |
abate |
245 |
let r1 = !cursor in
|
| 287 |
abate |
656 |
let code1 = run_disp_string_latin1_char d1 (Chars.V.mk_char s.[i]) in
|
| 288 |
abate |
310 |
run_disp_string_latin1_2 r1 i j s q b1.(code1)
|
| 289 |
|
|
and run_disp_string_latin1_char d ch =
|
| 290 |
abate |
231 |
match actions d with
|
| 291 |
|
|
| AIgnore r -> make_result_char ch r
|
| 292 |
abate |
243 |
| AKind k -> make_result_char ch (Chars.get_map ch k.chars)
|
| 293 |
abate |
310 |
and run_disp_string_latin1_2 r1 i j s q = function
|
| 294 |
abate |
231 |
| Impossible -> assert false
|
| 295 |
|
|
| Ignore r ->
|
| 296 |
abate |
310 |
make_result_string_latin1 i j s q r1 0 r
|
| 297 |
|
|
| TailCall d2 -> run_disp_string_latin1_loop i j s q d2
|
| 298 |
abate |
231 |
| Dispatch (d2,b2) ->
|
| 299 |
abate |
245 |
let r2 = !cursor in
|
| 300 |
abate |
310 |
let code2 = run_disp_string_latin1_loop i j s q d2 in
|
| 301 |
|
|
make_result_string_latin1 i j s q r1 r2 b2.(code2)
|
| 302 |
|
|
and run_disp_string_latin1_loop i j s q d =
|
| 303 |
abate |
1372 |
let i = succ i in
|
| 304 |
|
|
if i == j then run_dispatcher d q else
|
| 305 |
abate |
231 |
match actions d with
|
| 306 |
abate |
1372 |
| AIgnore r -> make_result_basic (Value.String_latin1 (i,j,s,q)) r
|
| 307 |
|
|
| AKind k -> run_disp_string_latin1 i j s q k
|
| 308 |
abate |
245 |
|
| 309 |
abate |
310 |
and run_disp_string_utf8 i j s q actions =
|
| 310 |
abate |
1372 |
if Utf8.equal_index i j then run_disp_kind actions q
|
| 311 |
|
|
else
|
| 312 |
|
|
match actions.prod with
|
| 313 |
abate |
310 |
| Impossible -> assert false
|
| 314 |
abate |
656 |
| TailCall d1 -> run_disp_string_utf8_char d1 (Chars.V.mk_int (Utf8.get s i))
|
| 315 |
abate |
310 |
| Ignore d2 -> run_disp_string_utf8_2 !cursor i j s q d2
|
| 316 |
|
|
| Dispatch (d1,b1) ->
|
| 317 |
|
|
let r1 = !cursor in
|
| 318 |
abate |
656 |
let code1 = run_disp_string_utf8_char d1 (Chars.V.mk_int (Utf8.get s i)) in
|
| 319 |
abate |
310 |
run_disp_string_utf8_2 r1 i j s q b1.(code1)
|
| 320 |
|
|
and run_disp_string_utf8_char d ch =
|
| 321 |
|
|
match actions d with
|
| 322 |
|
|
| AIgnore r -> make_result_char ch r
|
| 323 |
|
|
| AKind k -> make_result_char ch (Chars.get_map ch k.chars)
|
| 324 |
|
|
and run_disp_string_utf8_2 r1 i j s q = function
|
| 325 |
|
|
| Impossible -> assert false
|
| 326 |
|
|
| Ignore r ->
|
| 327 |
|
|
make_result_string_utf8 i j s q r1 0 r
|
| 328 |
|
|
| TailCall d2 -> run_disp_string_utf8_loop i j s q d2
|
| 329 |
|
|
| Dispatch (d2,b2) ->
|
| 330 |
|
|
let r2 = !cursor in
|
| 331 |
|
|
let code2 = run_disp_string_utf8_loop i j s q d2 in
|
| 332 |
|
|
make_result_string_utf8 i j s q r1 r2 b2.(code2)
|
| 333 |
|
|
and run_disp_string_utf8_loop i j s q d =
|
| 334 |
abate |
1372 |
let i = Utf8.advance s i in
|
| 335 |
|
|
if Utf8.equal_index i j then run_dispatcher d q else
|
| 336 |
abate |
310 |
match actions d with
|
| 337 |
abate |
1372 |
| AIgnore r -> make_result_basic (Value.String_utf8 (i,j,s,q)) r
|
| 338 |
|
|
| AKind k -> run_disp_string_utf8 i j s q k
|
| 339 |
abate |
310 |
|
| 340 |
abate |
245 |
let run_dispatcher d v =
|
| 341 |
|
|
let code = run_dispatcher d v in
|
| 342 |
abate |
484 |
cursor := 0;
|
| 343 |
|
|
(code,!buffer)
|
| 344 |
abate |
245 |
|
| 345 |
abate |
1369 |
let old_dispatcher = run_dispatcher
|
| 346 |
|
|
|
| 347 |
abate |
231 |
|
| 348 |
abate |
245 |
(*
|
| 349 |
|
|
let rec check_overwrite_aux r i =
|
| 350 |
|
|
if i < 0 then true
|
| 351 |
|
|
else match r.(i) with
|
| 352 |
|
|
| Right j | Recompose (_,j) ->
|
| 353 |
|
|
if (j < 0) || (j >=i ) then check_overwrite_aux r (i - 1) else false
|
| 354 |
|
|
| _ -> check_overwrite_aux r (i - 1)
|
| 355 |
abate |
231 |
|
| 356 |
abate |
245 |
|
| 357 |
|
|
let check_overwrite r2 r =
|
| 358 |
|
|
(Array.length r2 = Array.length r) && (check_overwrite_aux r (Array.length r - 1))
|
| 359 |
|
|
|
| 360 |
|
|
|
| 361 |
|
|
*)
|
| 362 |
abate |
1369 |
|
| 363 |
|
|
|
| 364 |
|
|
|
| 365 |
|
|
|
| 366 |
|
|
(* New dispatcher *)
|
| 367 |
|
|
|
| 368 |
|
|
open Patterns.Compile2
|
| 369 |
|
|
|
| 370 |
|
|
let make_result_basic v (code,r) =
|
| 371 |
|
|
let n = Array.length r in
|
| 372 |
|
|
if n > 0 then (
|
| 373 |
|
|
ensure_room n;
|
| 374 |
|
|
let buf = !buffer in
|
| 375 |
|
|
for a = 0 to n - 1 do
|
| 376 |
|
|
buf.(!cursor) <- begin match Array.unsafe_get r a with
|
| 377 |
|
|
| SrcCapture -> v
|
| 378 |
|
|
| SrcCst c -> const c
|
| 379 |
|
|
| _ -> assert false
|
| 380 |
|
|
end;
|
| 381 |
|
|
incr cursor
|
| 382 |
|
|
done);
|
| 383 |
|
|
code
|
| 384 |
|
|
|
| 385 |
|
|
|
| 386 |
|
|
let make_result_prod v1 r1 v2 r2 v (code,r) =
|
| 387 |
|
|
let n = Array.length r in
|
| 388 |
|
|
if n > 0 then (
|
| 389 |
|
|
ensure_room n;
|
| 390 |
|
|
let buf = !buffer in
|
| 391 |
|
|
let c = !cursor in
|
| 392 |
|
|
for a = 0 to n - 1 do
|
| 393 |
|
|
buf.(c + a) <- match Array.unsafe_get r a with
|
| 394 |
|
|
| SrcCapture -> v
|
| 395 |
|
|
| SrcLeft -> v1
|
| 396 |
|
|
| SrcRight -> v2
|
| 397 |
|
|
| SrcCst c -> const c
|
| 398 |
|
|
| SrcFetchLeft i -> buf.(r1+i)
|
| 399 |
|
|
| SrcFetchRight i -> buf.(r2+i)
|
| 400 |
|
|
| SrcPair (l,r) ->
|
| 401 |
|
|
Pair (
|
| 402 |
|
|
(match l with
|
| 403 |
|
|
| SrcLeft -> v1 | SrcRight -> v2
|
| 404 |
|
|
| SrcFetchLeft i -> buf.(r1+i)
|
| 405 |
|
|
| SrcFetchRight i -> buf.(r2+i) | _ -> assert false),
|
| 406 |
abate |
1370 |
(match r with
|
| 407 |
abate |
1369 |
| SrcLeft -> v1 | SrcRight -> v2
|
| 408 |
|
|
| SrcFetchLeft i -> buf.(r1+i)
|
| 409 |
|
|
| SrcFetchRight i -> buf.(r2+i) | _ -> assert false))
|
| 410 |
|
|
| _ -> assert false
|
| 411 |
|
|
done;
|
| 412 |
|
|
if r1 != c then blit buf c buf r1 n;
|
| 413 |
|
|
cursor := r1 + n);
|
| 414 |
|
|
code
|
| 415 |
|
|
|
| 416 |
|
|
let make_result_record sp v (code,r) =
|
| 417 |
|
|
let n = Array.length r in
|
| 418 |
|
|
if n > 0 then (
|
| 419 |
|
|
ensure_room n;
|
| 420 |
|
|
let buf = !buffer in
|
| 421 |
|
|
let c = !cursor in
|
| 422 |
|
|
for a = 0 to n - 1 do
|
| 423 |
|
|
buf.(c + a) <- match Array.unsafe_get r a with
|
| 424 |
|
|
| SrcLocal i -> buf.(sp+i)
|
| 425 |
|
|
| _ -> assert false
|
| 426 |
|
|
done;
|
| 427 |
|
|
if sp != c then blit buf c buf sp n;
|
| 428 |
|
|
cursor := sp + n);
|
| 429 |
|
|
code
|
| 430 |
|
|
|
| 431 |
|
|
let rec run_disp_basic v f = function
|
| 432 |
|
|
| [(_,r)] -> make_result_basic v r
|
| 433 |
|
|
| (t,r)::rem -> if f t then make_result_basic v r else run_disp_basic v f rem
|
| 434 |
|
|
| _ -> assert false
|
| 435 |
|
|
|
| 436 |
|
|
let count = ref 0
|
| 437 |
|
|
let rec run_dispatcher d v =
|
| 438 |
|
|
(* Format.fprintf Format.std_formatter "Matching (%a)@." Value.print v; *)
|
| 439 |
|
|
(* Patterns.Compile.print_dispatcher Format.std_formatter d; *)
|
| 440 |
|
|
(* print_string "."; flush stdout; *)
|
| 441 |
abate |
1370 |
(* incr count;
|
| 442 |
abate |
1369 |
print_int !count;
|
| 443 |
|
|
print_string "X"; flush stdout;
|
| 444 |
|
|
if !count = 9685 then
|
| 445 |
|
|
Format.fprintf Format.std_formatter "Matching (%a)@\n with:@\n%a@."
|
| 446 |
|
|
Value.print v
|
| 447 |
abate |
1370 |
Patterns.Compile2.print_dispatcher d;*)
|
| 448 |
abate |
1369 |
|
| 449 |
|
|
let res =
|
| 450 |
|
|
match actions d with
|
| 451 |
|
|
| AResult r -> make_result_basic v r
|
| 452 |
|
|
| AKind k -> run_disp_kind k v
|
| 453 |
|
|
in
|
| 454 |
abate |
1370 |
(* print_string "Y"; flush stdout;*)
|
| 455 |
abate |
1369 |
res
|
| 456 |
|
|
|
| 457 |
|
|
and run_disp_kind actions v =
|
| 458 |
|
|
match v with
|
| 459 |
|
|
| Pair (v1,v2) -> run_disp_prod v v1 v2 actions.prod
|
| 460 |
|
|
| Xml (v1,v2,v3) -> run_disp_prod v v1 (Pair (v2,v3)) actions.xml
|
| 461 |
|
|
| Record r -> run_disp_record !cursor false v (LabelMap.get r) actions.record
|
| 462 |
abate |
1370 |
| String_latin1 (i,j,s,q) ->
|
| 463 |
|
|
run_disp_kind actions (Value.normalize v)
|
| 464 |
abate |
1369 |
(* run_disp_string_latin1 i j s q actions *)
|
| 465 |
|
|
| String_utf8 (i,j,s,q) as v ->
|
| 466 |
|
|
run_disp_kind actions (Value.normalize v)
|
| 467 |
|
|
(* run_disp_string_utf8 i j s q actions *)
|
| 468 |
|
|
| Atom a -> make_result_basic v (Atoms.get_map a actions.atoms)
|
| 469 |
|
|
| Char c -> make_result_basic v (Chars.get_map c actions.chars)
|
| 470 |
|
|
| Integer i ->
|
| 471 |
|
|
run_disp_basic v (fun t -> Types.Int.has_int t i) actions.basic
|
| 472 |
|
|
| Abstraction (iface,_)
|
| 473 |
|
|
| Abstraction2 (_,iface,_) ->
|
| 474 |
|
|
run_disp_basic v (fun t -> Types.Arrow.check_iface iface t)
|
| 475 |
|
|
actions.basic
|
| 476 |
|
|
| Abstract (abs,_) ->
|
| 477 |
|
|
run_disp_basic v (fun t -> Types.Abstract.contains abs (Types.get_abstract t))
|
| 478 |
|
|
actions.basic
|
| 479 |
abate |
1370 |
| Absent ->
|
| 480 |
|
|
run_disp_basic v (fun t -> Types.Record.has_absent t) actions.basic
|
| 481 |
abate |
1369 |
| Concat (_,_) as v -> run_disp_kind actions (Value.normalize v)
|
| 482 |
|
|
|
| 483 |
|
|
|
| 484 |
|
|
and run_disp_prod v v1 v2 = function
|
| 485 |
|
|
| Impossible -> assert false
|
| 486 |
|
|
| LeftRight rdd -> run_disp_prod' v v1 v2 rdd
|
| 487 |
|
|
| RightLeft rdd -> run_disp_prod' v v2 v1 rdd
|
| 488 |
|
|
|
| 489 |
|
|
and run_disp_prod' v v1 v2 = function
|
| 490 |
|
|
| Dispatch (d1,b1) ->
|
| 491 |
|
|
let r1 = !cursor in
|
| 492 |
|
|
let code1 = run_dispatcher d1 v1 in
|
| 493 |
|
|
run_disp_prod2 v1 r1 v v2 b1.(code1)
|
| 494 |
|
|
| TailCall d1 -> run_dispatcher d1 v1
|
| 495 |
|
|
| Ignore d2 -> run_disp_prod2 v1 !cursor v v2 d2
|
| 496 |
|
|
|
| 497 |
|
|
and run_disp_prod2 v1 r1 v v2 = function
|
| 498 |
|
|
| Ignore r -> make_result_prod v1 r1 v2 !cursor v r
|
| 499 |
|
|
| TailCall d2 -> run_dispatcher d2 v2
|
| 500 |
|
|
| Dispatch (d2,b2) ->
|
| 501 |
|
|
let r2 = !cursor in
|
| 502 |
|
|
let code2 = run_dispatcher d2 v2 in
|
| 503 |
|
|
make_result_prod v1 r1 v2 r2 v b2.(code2)
|
| 504 |
|
|
|
| 505 |
|
|
and do_pushes v vl = function
|
| 506 |
|
|
| [] -> ()
|
| 507 |
|
|
| PushConst c :: rem -> push (const c); do_pushes v vl rem
|
| 508 |
|
|
| PushField :: rem -> push vl; do_pushes v vl rem
|
| 509 |
|
|
| PushCapture :: rem -> push v; do_pushes v vl rem
|
| 510 |
|
|
|
| 511 |
abate |
1370 |
and do_record_tr sp other v vl fields tr =
|
| 512 |
|
|
let (pushes,ct) = Lazy.force tr in
|
| 513 |
|
|
(* print_string "*"; flush stdout; *)
|
| 514 |
abate |
1369 |
do_pushes v vl pushes;
|
| 515 |
|
|
run_disp_record sp other v fields ct
|
| 516 |
|
|
|
| 517 |
|
|
and run_disp_record sp other v fields = function
|
| 518 |
abate |
1370 |
| RecordLabel (l,d,cts) ->
|
| 519 |
abate |
1369 |
let rec aux other = function
|
| 520 |
|
|
| (l1,_) :: rem when l1 < l -> aux true rem
|
| 521 |
|
|
| (l1,vl) :: rem when l1 == l ->
|
| 522 |
|
|
do_record_tr sp other v vl rem cts.(run_dispatcher d vl)
|
| 523 |
abate |
1370 |
| rem ->
|
| 524 |
|
|
do_record_tr sp other v Absent rem cts.(run_dispatcher d Absent)
|
| 525 |
abate |
1369 |
in
|
| 526 |
|
|
aux other fields
|
| 527 |
abate |
1370 |
| RecordLabelSkip (l,pr) ->
|
| 528 |
abate |
1369 |
let rec aux other = function
|
| 529 |
|
|
| (l1,_) :: rem when l1 < l -> aux true rem
|
| 530 |
|
|
| (l1,vl) :: rem when l1 == l -> do_record_tr sp other v vl rem pr
|
| 531 |
abate |
1370 |
| rem -> do_record_tr sp other v Absent rem pr
|
| 532 |
abate |
1369 |
in
|
| 533 |
|
|
aux other fields
|
| 534 |
|
|
| RecordResult r ->
|
| 535 |
|
|
make_result_record sp v r
|
| 536 |
|
|
| RecordMore (nomore,more) ->
|
| 537 |
|
|
let other = other || (fields != []) in
|
| 538 |
|
|
make_result_record sp v (if other then more else nomore)
|
| 539 |
|
|
| RecordImpossible -> assert false
|
| 540 |
|
|
|
| 541 |
|
|
(*
|
| 542 |
|
|
|
| 543 |
|
|
and run_disp_string_latin1 i j s q actions =
|
| 544 |
|
|
if i == j then run_disp_kind actions q
|
| 545 |
|
|
else match actions.prod with
|
| 546 |
|
|
| Impossible -> assert false
|
| 547 |
|
|
| TailCall d1 -> run_disp_string_latin1_char d1 (Chars.V.mk_char s.[i])
|
| 548 |
|
|
| Ignore d2 -> run_disp_string_latin1_2 !cursor i j s q d2
|
| 549 |
|
|
| Dispatch (d1,b1) ->
|
| 550 |
|
|
let r1 = !cursor in
|
| 551 |
|
|
let code1 = run_disp_string_latin1_char d1 (Chars.V.mk_char s.[i]) in
|
| 552 |
|
|
run_disp_string_latin1_2 r1 i j s q b1.(code1)
|
| 553 |
|
|
and run_disp_string_latin1_char d ch =
|
| 554 |
|
|
match actions d with
|
| 555 |
|
|
| AIgnore r -> make_result_char ch r
|
| 556 |
|
|
| AKind k -> make_result_char ch (Chars.get_map ch k.chars)
|
| 557 |
|
|
and run_disp_string_latin1_2 r1 i j s q = function
|
| 558 |
|
|
| Impossible -> assert false
|
| 559 |
|
|
| Ignore r ->
|
| 560 |
|
|
make_result_string_latin1 i j s q r1 0 r
|
| 561 |
|
|
| TailCall d2 -> run_disp_string_latin1_loop i j s q d2
|
| 562 |
|
|
| Dispatch (d2,b2) ->
|
| 563 |
|
|
let r2 = !cursor in
|
| 564 |
|
|
let code2 = run_disp_string_latin1_loop i j s q d2 in
|
| 565 |
|
|
make_result_string_latin1 i j s q r1 r2 b2.(code2)
|
| 566 |
|
|
and run_disp_string_latin1_loop i j s q d =
|
| 567 |
|
|
match actions d with
|
| 568 |
|
|
| AIgnore r -> make_result_basic Absent r
|
| 569 |
|
|
| AKind k -> run_disp_string_latin1 (succ i) j s q k
|
| 570 |
|
|
|
| 571 |
|
|
and run_disp_string_utf8 i j s q actions =
|
| 572 |
|
|
if Utf8.equal_index i j then run_disp_kind actions q
|
| 573 |
|
|
else match actions.prod with
|
| 574 |
|
|
| Impossible -> assert false
|
| 575 |
|
|
| TailCall d1 -> run_disp_string_utf8_char d1 (Chars.V.mk_int (Utf8.get s i))
|
| 576 |
|
|
| Ignore d2 -> run_disp_string_utf8_2 !cursor i j s q d2
|
| 577 |
|
|
| Dispatch (d1,b1) ->
|
| 578 |
|
|
let r1 = !cursor in
|
| 579 |
|
|
let code1 = run_disp_string_utf8_char d1 (Chars.V.mk_int (Utf8.get s i)) in
|
| 580 |
|
|
run_disp_string_utf8_2 r1 i j s q b1.(code1)
|
| 581 |
|
|
and run_disp_string_utf8_char d ch =
|
| 582 |
|
|
match actions d with
|
| 583 |
|
|
| AIgnore r -> make_result_char ch r
|
| 584 |
|
|
| AKind k -> make_result_char ch (Chars.get_map ch k.chars)
|
| 585 |
|
|
and run_disp_string_utf8_2 r1 i j s q = function
|
| 586 |
|
|
| Impossible -> assert false
|
| 587 |
|
|
| Ignore r ->
|
| 588 |
|
|
make_result_string_utf8 i j s q r1 0 r
|
| 589 |
|
|
| TailCall d2 -> run_disp_string_utf8_loop i j s q d2
|
| 590 |
|
|
| Dispatch (d2,b2) ->
|
| 591 |
|
|
let r2 = !cursor in
|
| 592 |
|
|
let code2 = run_disp_string_utf8_loop i j s q d2 in
|
| 593 |
|
|
make_result_string_utf8 i j s q r1 r2 b2.(code2)
|
| 594 |
|
|
and run_disp_string_utf8_loop i j s q d =
|
| 595 |
|
|
match actions d with
|
| 596 |
|
|
| AIgnore r -> make_result_basic Absent r
|
| 597 |
|
|
| AKind k -> run_disp_string_utf8 (Utf8.advance s i) j s q k
|
| 598 |
|
|
*)
|
| 599 |
|
|
|
| 600 |
|
|
let run_dispatcher2 d v =
|
| 601 |
abate |
1370 |
(* print_string "+"; flush stdout; *)
|
| 602 |
abate |
1369 |
let code = run_dispatcher d v in
|
| 603 |
|
|
cursor := 0;
|
| 604 |
abate |
1370 |
(* print_string "-\n"; flush stdout; *)
|
| 605 |
abate |
1369 |
(code,!buffer)
|
| 606 |
|
|
|
| 607 |
|
|
|
| 608 |
|
|
|
| 609 |
|
|
let run_dispatcher = old_dispatcher
|