|
1 | 1 | (* Copyright (C) 2015-2016 Bloomberg Finance L.P. |
2 | | - * |
| 2 | + * |
3 | 3 | * This program is free software: you can redistribute it and/or modify |
4 | 4 | * it under the terms of the GNU Lesser General Public License as published by |
5 | 5 | * the Free Software Foundation, either version 3 of the License, or |
|
17 | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | 19 | * GNU Lesser General Public License for more details. |
20 | | - * |
| 20 | + * |
21 | 21 | * You should have received a copy of the GNU Lesser General Public License |
22 | 22 | * along with this program; if not, write to the Free Software |
23 | 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) |
24 | 24 |
|
25 | 25 | module E = Js_exp_make |
26 | 26 | module S = Js_stmt_make |
27 | 27 |
|
28 | | -type arg_expression = |
| 28 | +type arg_expression = |
29 | 29 | | Splice0 |
30 | | - | Splice1 of E.t |
| 30 | + | Splice1 of E.t |
31 | 31 | | Splice2 of E.t * E.t |
32 | | - |
| 32 | + |
33 | 33 | (* we need destruct [undefined] when input is optional *) |
34 | | -let eval (arg : J.expression) (dispatches : (int * string) list ) : E.t = |
| 34 | +let eval (arg : J.expression) (dispatches : (int * string) list ) : E.t = |
35 | 35 | if arg == E.undefined then E.undefined else |
36 | 36 | match arg.expression_desc with |
37 | | - | Number (Int {i} | Uint i) -> |
38 | | - E.str (Ext_list.assoc_by_int None (Int32.to_int i) dispatches) |
39 | | - | _ -> |
| 37 | + | Number (Int {i} | Uint i) -> |
| 38 | + E.str (Ext_list.assoc_by_int None (Int32.to_int i) dispatches) |
| 39 | + | _ -> |
40 | 40 | E.of_block |
41 | 41 | [(S.int_switch arg |
42 | | - (Ext_list.map (fun (i,r) -> |
43 | | - {J.case = i ; |
| 42 | + (Ext_list.map (fun (i,r) -> |
| 43 | + {J.case = i ; |
44 | 44 | body = [S.return_stmt (E.str r)], |
45 | 45 | false (* FIXME: if true, still print break*) |
46 | 46 | }) dispatches))] |
47 | 47 |
|
48 | 48 | (** invariant: optional is not allowed in this case *) |
49 | | -let eval_as_event (arg : J.expression) (dispatches : (int * string) list ) = |
| 49 | +let eval_as_event (arg : J.expression) (dispatches : (int * string) list ) = |
50 | 50 | match arg.expression_desc with |
51 | 51 | | Array ([{expression_desc = Number (Int {i} | Uint i)}; cb], _) |
52 | 52 | | Caml_block([{expression_desc = Number (Int {i} | Uint i)}; cb], _, _, _) |
53 | | - -> |
54 | | - let v = Ext_list.assoc_by_int None (Int32.to_int i) dispatches in |
55 | | - Splice2(E.str v , cb ) |
56 | | - | _ -> |
57 | | - let event = Ext_ident.create "action" in |
| 53 | + -> |
| 54 | + let v = Ext_list.assoc_by_int None (Int32.to_int i) dispatches in |
| 55 | + Splice2(E.str v , cb ) |
| 56 | + | _ -> |
58 | 57 | Splice2 |
59 | | - (E.ocaml_fun [event] |
60 | | - [(S.int_switch arg |
61 | | - (Ext_list.map (fun (i,r) -> |
62 | | - {J.case = i ; |
63 | | - body = [S.return_stmt (E.index (E.var event) 0l)], |
| 58 | + (E.of_block |
| 59 | + [(S.int_switch (E.index arg 0l) |
| 60 | + (Ext_list.map (fun (i,r) -> |
| 61 | + {J.case = i ; |
| 62 | + body = [S.return_stmt (E.str r)], |
64 | 63 | false (* FIXME: if true, still print break*) |
65 | 64 | }) dispatches))] |
66 | | - , (* TODO: improve, one dispatch later, |
67 | | - the problem is that we can not create bindings |
68 | | - due to the |
| 65 | + , (* TODO: improve, one dispatch later, |
| 66 | + the problem is that we can not create bindings |
| 67 | + due to the |
69 | 68 | *) |
70 | | - E.ocaml_fun [event] |
71 | | - [(S.int_switch arg |
72 | | - (Ext_list.map (fun (i,r) -> |
73 | | - {J.case = i ; |
74 | | - body = [S.return_stmt (E.index (E.var event) 1l)], |
75 | | - false (* FIXME: if true, still print break*) |
76 | | - }) dispatches))] |
| 69 | + E.index arg 1l |
77 | 70 | ) |
| 71 | + (** FIXME: |
| 72 | + 1. duplicated evaluation of expressions arg |
| 73 | + Solution: calcuate the arg once in the beginning |
| 74 | + 2. avoid block for branches < 3 |
| 75 | + or always? |
| 76 | + a === 444? "a" : a==222? "b" |
| 77 | + *) |
78 | 78 |
|
79 | 79 | (* we need destruct [undefined] when input is optional *) |
80 | | -let eval_as_int (arg : J.expression) (dispatches : (int * int) list ) : E.t = |
81 | | - if arg == E.undefined then E.undefined else |
| 80 | +let eval_as_int (arg : J.expression) (dispatches : (int * int) list ) : E.t = |
| 81 | + if arg == E.undefined then E.undefined else |
82 | 82 | match arg.expression_desc with |
83 | 83 | | Number (Int {i} | Uint i) -> |
84 | | - E.int (Int32.of_int (Ext_list.assoc_by_int None (Int32.to_int i) dispatches)) |
85 | | - | _ -> |
| 84 | + E.int (Int32.of_int (Ext_list.assoc_by_int None (Int32.to_int i) dispatches)) |
| 85 | + | _ -> |
86 | 86 | E.of_block |
87 | 87 | [(S.int_switch arg |
88 | | - (Ext_list.map (fun (i,r) -> |
89 | | - {J.case = i ; |
| 88 | + (Ext_list.map (fun (i,r) -> |
| 89 | + {J.case = i ; |
90 | 90 | body = [S.return_stmt (E.int (Int32.of_int r))], |
91 | 91 | false (* FIXME: if true, still print break*) |
92 | 92 | }) dispatches))] |
|
0 commit comments