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

Contents of /runtime/run_dispatch.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 70 - (show annotations)
Tue Jul 10 17:03:19 2007 UTC (5 years, 10 months ago) by abate
File size: 3374 byte(s)
[r2002-11-01 12:53:44 by cvscast] Empty log message

Original author: cvscast
Date: 2002-11-01 12:53:45+00:00
1 (* 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 and run_disp_kind actions v = match v with
50 | Pair (v1,v2) -> run_disp_prod v v1 v2 actions.Patterns.Compile.prod
51 | Record r -> run_disp_record r v [] r actions.Patterns.Compile.record
52 | Atom a ->
53 run_disp_basic v (fun t -> Types.Atom.has_atom t a)
54 actions.Patterns.Compile.basic
55 | Char c ->
56 run_disp_basic v (fun t -> Types.Char.has_char t c)
57 actions.Patterns.Compile.basic
58 | Integer i ->
59 run_disp_basic v (fun t -> Types.Int.has_int t i)
60 actions.Patterns.Compile.basic
61 | Abstraction (iface,_) ->
62 run_disp_basic v (fun t -> Types.Arrow.check_iface iface t)
63 actions.Patterns.Compile.basic
64 | v ->
65 run_disp_kind actions (normalize v)
66
67
68 and run_disp_basic v f = function
69 | [(_,r)] -> make_result_basic v r
70 | (t,r)::rem -> if f t then make_result_basic v r else run_disp_basic v f rem
71 | _ -> assert false
72
73
74 and run_disp_prod v v1 v2 = function
75 | `None -> assert false
76 | `TailCall d1 -> run_dispatcher d1 v1
77 | `Ignore d2 -> run_disp_prod2 v1 dummy_r v v2 d2
78 | `Dispatch (d1,b1) ->
79 let (code1,r1) = run_dispatcher d1 v1 in
80 run_disp_prod2 v1 r1 v v2 b1.(code1)
81
82 and run_disp_prod2 v1 r1 v v2 = function
83 | `None -> assert false
84 | `Ignore r -> make_result_prod v1 r1 v2 dummy_r v r
85 | `TailCall d2 -> run_dispatcher d2 v2
86 | `Dispatch (d2,b2) ->
87 let (code2,r2) = run_dispatcher d2 v2 in
88 make_result_prod v1 r1 v2 r2 v b2.(code2)
89
90 and run_disp_record f v bindings fields = function
91 | None -> assert false
92 | Some record -> run_disp_record' f v bindings fields record
93
94 and run_disp_record' f v bindings fields = function
95 | `Result r -> make_result_record f v bindings r
96 | `Label (l, present, absent) ->
97 let rec aux = function
98 | (l1,_) :: rem when l1 < l -> aux rem
99 | (l1,vl) :: rem when l1 = l ->
100 run_disp_field f v bindings rem l vl present
101 | _ -> run_disp_record f v bindings fields absent
102 in
103 aux fields
104
105 and run_disp_field f v bindings fields l vl = function
106 | `None -> assert false
107 | `Ignore r -> run_disp_record' f v bindings fields r
108 | `TailCall d -> run_dispatcher d vl
109 | `Dispatch (dl,bl) ->
110 let (codel,rl) = run_dispatcher dl vl in
111 run_disp_record' f v ((l,rl)::bindings) fields bl.(codel)

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