| 1 |
abate |
70 |
(* Running dispatchers *)
|
| 2 |
|
|
|
| 3 |
|
|
open Value
|
| 4 |
|
|
|
| 5 |
|
|
|
| 6 |
|
|
let make_result_prod v1 r1 v2 r2 v (code,r) =
|
| 7 |
|
|
let ret = Array.map
|
| 8 |
|
|
(function
|
| 9 |
|
|
| `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 |
|
|
Pair ((if (i < 0) then v1 else r1.(i)),
|
| 15 |
|
|
(if (j < 0) then v2 else r2.(j)))
|
| 16 |
|
|
| _ -> assert false
|
| 17 |
|
|
) r in
|
| 18 |
|
|
(code,ret)
|
| 19 |
|
|
|
| 20 |
|
|
let make_result_record fields v bindings (code,r) =
|
| 21 |
|
|
let ret = Array.map
|
| 22 |
|
|
(function
|
| 23 |
|
|
| `Catch -> v
|
| 24 |
|
|
| `Const c -> const c
|
| 25 |
|
|
| `Field (l,i) ->
|
| 26 |
|
|
if (i < 0) then List.assoc l fields
|
| 27 |
|
|
else (List.assoc l bindings).(i)
|
| 28 |
|
|
| _ -> assert false
|
| 29 |
|
|
) r in
|
| 30 |
|
|
(code,ret)
|
| 31 |
|
|
|
| 32 |
|
|
let make_result_basic v (code,r) =
|
| 33 |
|
|
let ret = Array.map
|
| 34 |
|
|
(function
|
| 35 |
|
|
| `Catch -> v
|
| 36 |
|
|
| `Const c -> const c
|
| 37 |
|
|
| _ -> assert false
|
| 38 |
|
|
) r in
|
| 39 |
|
|
(code,ret)
|
| 40 |
|
|
|
| 41 |
|
|
let dummy_r = [||]
|
| 42 |
|
|
|
| 43 |
|
|
let rec run_dispatcher d v =
|
| 44 |
|
|
let actions = Patterns.Compile.actions d in
|
| 45 |
|
|
match actions with
|
| 46 |
|
|
| `Ignore r -> make_result_basic v r
|
| 47 |
|
|
| `Kind k -> run_disp_kind k v
|
| 48 |
|
|
|
| 49 |
abate |
71 |
and run_disp_kind actions v =
|
| 50 |
|
|
match v with
|
| 51 |
abate |
70 |
| Pair (v1,v2) -> run_disp_prod v v1 v2 actions.Patterns.Compile.prod
|
| 52 |
|
|
| Record r -> run_disp_record r v [] r actions.Patterns.Compile.record
|
| 53 |
|
|
| Atom a ->
|
| 54 |
|
|
run_disp_basic v (fun t -> Types.Atom.has_atom t a)
|
| 55 |
|
|
actions.Patterns.Compile.basic
|
| 56 |
|
|
| Char c ->
|
| 57 |
|
|
run_disp_basic v (fun t -> Types.Char.has_char t c)
|
| 58 |
|
|
actions.Patterns.Compile.basic
|
| 59 |
|
|
| Integer i ->
|
| 60 |
|
|
run_disp_basic v (fun t -> Types.Int.has_int t i)
|
| 61 |
|
|
actions.Patterns.Compile.basic
|
| 62 |
|
|
| Abstraction (iface,_) ->
|
| 63 |
|
|
run_disp_basic v (fun t -> Types.Arrow.check_iface iface t)
|
| 64 |
|
|
actions.Patterns.Compile.basic
|
| 65 |
|
|
| v ->
|
| 66 |
|
|
run_disp_kind actions (normalize v)
|
| 67 |
|
|
|
| 68 |
|
|
|
| 69 |
abate |
71 |
and run_disp_basic v f x =
|
| 70 |
|
|
match x with
|
| 71 |
abate |
70 |
| [(_,r)] -> make_result_basic v r
|
| 72 |
|
|
| (t,r)::rem -> if f t then make_result_basic v r else run_disp_basic v f rem
|
| 73 |
|
|
| _ -> assert false
|
| 74 |
|
|
|
| 75 |
|
|
|
| 76 |
abate |
71 |
and run_disp_prod v v1 v2 x =
|
| 77 |
|
|
match x with
|
| 78 |
abate |
70 |
| `None -> assert false
|
| 79 |
|
|
| `TailCall d1 -> run_dispatcher d1 v1
|
| 80 |
|
|
| `Ignore d2 -> run_disp_prod2 v1 dummy_r v v2 d2
|
| 81 |
|
|
| `Dispatch (d1,b1) ->
|
| 82 |
|
|
let (code1,r1) = run_dispatcher d1 v1 in
|
| 83 |
|
|
run_disp_prod2 v1 r1 v v2 b1.(code1)
|
| 84 |
|
|
|
| 85 |
abate |
71 |
and run_disp_prod2 v1 r1 v v2 x =
|
| 86 |
|
|
match x with
|
| 87 |
abate |
70 |
| `None -> assert false
|
| 88 |
|
|
| `Ignore r -> make_result_prod v1 r1 v2 dummy_r v r
|
| 89 |
|
|
| `TailCall d2 -> run_dispatcher d2 v2
|
| 90 |
|
|
| `Dispatch (d2,b2) ->
|
| 91 |
|
|
let (code2,r2) = run_dispatcher d2 v2 in
|
| 92 |
|
|
make_result_prod v1 r1 v2 r2 v b2.(code2)
|
| 93 |
|
|
|
| 94 |
|
|
and run_disp_record f v bindings fields = function
|
| 95 |
|
|
| None -> assert false
|
| 96 |
abate |
75 |
| Some record -> run_disp_record' f v bindings None fields record
|
| 97 |
abate |
70 |
|
| 98 |
abate |
75 |
and run_disp_record' f v bindings abs fields = function
|
| 99 |
abate |
70 |
| `Result r -> make_result_record f v bindings r
|
| 100 |
abate |
75 |
| `Absent -> run_disp_record f v bindings fields abs
|
| 101 |
abate |
70 |
| `Label (l, present, absent) ->
|
| 102 |
|
|
let rec aux = function
|
| 103 |
|
|
| (l1,_) :: rem when l1 < l -> aux rem
|
| 104 |
|
|
| (l1,vl) :: rem when l1 = l ->
|
| 105 |
abate |
75 |
run_disp_field f v bindings abs rem l vl present
|
| 106 |
abate |
70 |
| _ -> run_disp_record f v bindings fields absent
|
| 107 |
|
|
in
|
| 108 |
|
|
aux fields
|
| 109 |
|
|
|
| 110 |
abate |
75 |
and run_disp_field f v bindings abs fields l vl = function
|
| 111 |
abate |
70 |
| `None -> assert false
|
| 112 |
abate |
75 |
| `Ignore r -> run_disp_record' f v bindings abs fields r
|
| 113 |
abate |
70 |
| `TailCall d -> run_dispatcher d vl
|
| 114 |
|
|
| `Dispatch (dl,bl) ->
|
| 115 |
|
|
let (codel,rl) = run_dispatcher dl vl in
|
| 116 |
abate |
75 |
run_disp_record' f v ((l,rl)::bindings) abs fields bl.(codel)
|