1 | /* -*- c-basic-offset: 2 -*- */ |
2 | /* Copyright(C) 2014-2015 Brazil |
3 | |
4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License version 2.1 as published by the Free Software Foundation. |
7 | |
8 | This library is distributed in the hope that it will be useful, |
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | Lesser General Public License for more details. |
12 | |
13 | You should have received a copy of the GNU Lesser General Public |
14 | License along with this library; if not, write to the Free Software |
15 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
16 | */ |
17 | |
18 | #include "grn_expr_code.h" |
19 | |
20 | unsigned int |
21 | grn_expr_code_n_used_codes(grn_ctx *ctx, |
22 | grn_expr_code *start, |
23 | grn_expr_code *target) |
24 | { |
25 | unsigned int n_codes; |
26 | int i, n_args; |
27 | grn_expr_code *sub_code; |
28 | |
29 | if (start == target) { |
30 | return 0; |
31 | } |
32 | |
33 | n_args = target->nargs; |
34 | if (target->value) { |
35 | n_args--; |
36 | if (n_args == 0) { |
37 | return 1; |
38 | } |
39 | } |
40 | |
41 | n_codes = 1; |
42 | sub_code = target - 1; |
43 | for (i = 0; i < n_args; i++) { |
44 | int sub_n_codes; |
45 | sub_n_codes = grn_expr_code_n_used_codes(ctx, start, sub_code); |
46 | n_codes += sub_n_codes; |
47 | sub_code -= sub_n_codes; |
48 | if (sub_code < start) { |
49 | /* TODO: report error */ |
50 | return 0; |
51 | } |
52 | } |
53 | |
54 | return n_codes; |
55 | } |
56 | |