| 1 |
abate |
70 |
(* Running dispatchers *)
|
| 2 |
|
|
|
| 3 |
|
|
open Value
|
| 4 |
abate |
172 |
open Patterns.Compile
|
| 5 |
abate |
70 |
|
| 6 |
|
|
let make_result_prod v1 r1 v2 r2 v (code,r) =
|
| 7 |
|
|
let ret = Array.map
|
| 8 |
|
|
(function
|
| 9 |
abate |
172 |
| Catch -> v
|
| 10 |
|
|
| Const c -> const c
|
| 11 |
|
|
| Left i -> if (i < 0) then v1 else r1.(i)
|
| 12 |
|
|
| Right j -> if (j < 0) then v2 else r2.(j)
|
| 13 |
|
|
| Recompose (i,j) ->
|
| 14 |
abate |
70 |
Pair ((if (i < 0) then v1 else r1.(i)),
|
| 15 |
|
|
(if (j < 0) then v2 else r2.(j)))
|
| 16 |
|
|
) r in
|
| 17 |
|
|
(code,ret)
|
| 18 |
|
|
|
| 19 |
|
|
let make_result_basic v (code,r) =
|
| 20 |
|
|
let ret = Array.map
|
| 21 |
|
|
(function
|
| 22 |
abate |
172 |
| Catch -> v
|
| 23 |
|
|
| Const c -> const c
|
| 24 |
abate |
70 |
| _ -> assert false
|
| 25 |
|
|
) r in
|
| 26 |
|
|
(code,ret)
|
| 27 |
|
|
|
| 28 |
|
|
let dummy_r = [||]
|
| 29 |
|
|
|
| 30 |
|
|
let rec run_dispatcher d v =
|
| 31 |
abate |
229 |
(*
|
| 32 |
|
|
Format.fprintf Format.std_formatter "Matching (%a) with:@\n" Value.print v;
|
| 33 |
|
|
Patterns.Compile.print_dispatcher Format.std_formatter d;
|
| 34 |
|
|
*)
|
| 35 |
abate |
172 |
match actions d with
|
| 36 |
|
|
| AIgnore r -> make_result_basic v r
|
| 37 |
|
|
| AKind k -> run_disp_kind k v
|
| 38 |
abate |
70 |
|
| 39 |
abate |
71 |
and run_disp_kind actions v =
|
| 40 |
|
|
match v with
|
| 41 |
abate |
172 |
| Pair (v1,v2) -> run_disp_prod v v1 v2 actions.prod
|
| 42 |
|
|
| Xml (v1,v2) -> run_disp_prod v v1 v2 actions.xml
|
| 43 |
abate |
229 |
| Record r -> run_disp_record false v r actions.record
|
| 44 |
abate |
70 |
| Atom a ->
|
| 45 |
abate |
172 |
run_disp_basic v (fun t -> Types.Atom.has_atom t a) actions.basic
|
| 46 |
abate |
70 |
| Char c ->
|
| 47 |
abate |
172 |
run_disp_basic v (fun t -> Types.Char.has_char t c) actions.basic
|
| 48 |
abate |
70 |
| Integer i ->
|
| 49 |
abate |
172 |
run_disp_basic v (fun t -> Types.Int.has_int t i) actions.basic
|
| 50 |
abate |
70 |
| Abstraction (iface,_) ->
|
| 51 |
|
|
run_disp_basic v (fun t -> Types.Arrow.check_iface iface t)
|
| 52 |
abate |
172 |
actions.basic
|
| 53 |
abate |
229 |
| Absent ->
|
| 54 |
|
|
run_disp_basic v (fun t -> Types.Record.has_absent t) actions.basic
|
| 55 |
abate |
70 |
| v ->
|
| 56 |
|
|
run_disp_kind actions (normalize v)
|
| 57 |
|
|
|
| 58 |
|
|
|
| 59 |
abate |
229 |
and run_disp_basic v f = function
|
| 60 |
|
|
(* | [(_,r)] -> make_result_basic v r *)
|
| 61 |
abate |
70 |
| (t,r)::rem -> if f t then make_result_basic v r else run_disp_basic v f rem
|
| 62 |
abate |
229 |
| _ ->
|
| 63 |
|
|
assert false
|
| 64 |
abate |
70 |
|
| 65 |
|
|
|
| 66 |
abate |
229 |
and run_disp_prod v v1 v2 = function
|
| 67 |
abate |
172 |
| Impossible -> assert false
|
| 68 |
|
|
| TailCall d1 -> run_dispatcher d1 v1
|
| 69 |
|
|
| Ignore d2 -> run_disp_prod2 v1 dummy_r v v2 d2
|
| 70 |
|
|
| Dispatch (d1,b1) ->
|
| 71 |
abate |
70 |
let (code1,r1) = run_dispatcher d1 v1 in
|
| 72 |
|
|
run_disp_prod2 v1 r1 v v2 b1.(code1)
|
| 73 |
|
|
|
| 74 |
abate |
229 |
and run_disp_prod2 v1 r1 v v2 = function
|
| 75 |
abate |
172 |
| Impossible -> assert false
|
| 76 |
|
|
| Ignore r -> make_result_prod v1 r1 v2 dummy_r v r
|
| 77 |
|
|
| TailCall d2 -> run_dispatcher d2 v2
|
| 78 |
|
|
| Dispatch (d2,b2) ->
|
| 79 |
abate |
70 |
let (code2,r2) = run_dispatcher d2 v2 in
|
| 80 |
|
|
make_result_prod v1 r1 v2 r2 v b2.(code2)
|
| 81 |
|
|
|
| 82 |
abate |
229 |
and run_disp_record other v fields = function
|
| 83 |
abate |
70 |
| None -> assert false
|
| 84 |
abate |
229 |
| Some (`Label (l,d)) ->
|
| 85 |
abate |
165 |
let rec aux other = function
|
| 86 |
|
|
| (l1,_) :: rem when l1 < l -> aux true rem
|
| 87 |
abate |
229 |
| (l1,vl) :: rem when l1 = l ->
|
| 88 |
|
|
run_disp_record1 other vl rem d
|
| 89 |
|
|
| rem ->
|
| 90 |
|
|
run_disp_record1 other Absent rem d
|
| 91 |
abate |
70 |
in
|
| 92 |
abate |
165 |
aux other fields
|
| 93 |
abate |
229 |
| Some (`Nolabel (some,none)) ->
|
| 94 |
|
|
let r = if other then some else none in
|
| 95 |
|
|
match r with
|
| 96 |
|
|
| Some r -> make_result_basic v r
|
| 97 |
|
|
| None -> assert false
|
| 98 |
|
|
|
| 99 |
|
|
and run_disp_record1 other v1 rem = function
|
| 100 |
|
|
| Impossible -> assert false
|
| 101 |
|
|
| TailCall d1 -> run_dispatcher d1 v1
|
| 102 |
|
|
| Ignore d2 -> run_disp_record2 other v1 dummy_r rem d2
|
| 103 |
|
|
| Dispatch (d1,b1) ->
|
| 104 |
|
|
let (code1,r1) = run_dispatcher d1 v1 in
|
| 105 |
|
|
run_disp_record2 other v1 r1 rem b1.(code1)
|
| 106 |
abate |
70 |
|
| 107 |
abate |
229 |
and run_disp_record2 other v1 r1 rem = function
|
| 108 |
abate |
172 |
| Impossible -> assert false
|
| 109 |
abate |
229 |
| Ignore r -> make_result_prod v1 r1 Absent dummy_r Absent r
|
| 110 |
|
|
| TailCall d2 -> run_disp_record_loop other rem d2
|
| 111 |
|
|
| Dispatch (d2,b2) ->
|
| 112 |
|
|
let (code2,r2) = run_disp_record_loop other rem d2 in
|
| 113 |
|
|
make_result_prod v1 r1 Absent r2 Absent b2.(code2)
|
| 114 |
|
|
|
| 115 |
|
|
and run_disp_record_loop other rem d =
|
| 116 |
|
|
match actions d with
|
| 117 |
|
|
| AIgnore r -> make_result_basic Absent r
|
| 118 |
|
|
| AKind k -> run_disp_record other (Pair(Absent,Absent)) rem k.record
|
| 119 |
|
|
|