Debian Codegraph @

lib/applier.c

raw symbols SCIP
1 /*
2 Copyright (C) 2024 Sutou Kouhei <[email protected]>
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 as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "grn_applier.h"
20 #include "grn_expr.h"
21
22 grn_obj *
23 grn_applier_data_get_applier(grn_ctx *ctx, grn_applier_data *data)
24 {
25 return data->applier;
26 }
27
28 grn_obj *
29 grn_applier_data_get_table(grn_ctx *ctx, grn_applier_data *data)
30 {
31 return data->table;
32 }
33
34 grn_obj *
35 grn_applier_data_get_output_column(grn_ctx *ctx, grn_applier_data *data)
36 {
37 return data->output_column;
38 }
39
40 grn_obj **
41 grn_applier_data_get_args(grn_ctx *ctx, grn_applier_data *data, size_t *n_args)
42 {
43 *n_args = GRN_PTR_VECTOR_SIZE(&(data->args));
44 return (grn_obj **)GRN_BULK_HEAD(&(data->args));
45 }
46
47 void
48 grn_applier_data_init(grn_ctx *ctx,
49 grn_applier_data *data,
50 grn_obj *table,
51 grn_obj *output_column)
52 {
53 data->table = table;
54 data->output_column = output_column;
55 data->applier = NULL;
56 GRN_PTR_INIT(&(data->args), GRN_OBJ_VECTOR, GRN_ID_NIL);
57 }
58
59 void
60 grn_applier_data_fin(grn_ctx *ctx, grn_applier_data *data)
61 {
62 GRN_OBJ_FIN(ctx, &(data->args));
63 }
64
65 bool
66 grn_applier_data_extract(grn_ctx *ctx, grn_applier_data *data, grn_obj *expr)
67 {
68 grn_expr *e = (grn_expr *)expr;
69 grn_expr_code *codes = e->codes;
70 uint32_t codes_curr = e->codes_curr;
71 if (codes_curr < 3) {
72 return false;
73 }
74 if (e->codes[0].op != GRN_OP_PUSH) {
75 return false;
76 }
77 if (!grn_obj_is_applier_proc(ctx, e->codes[0].value)) {
78 return false;
79 }
80 data->applier = e->codes[0].value;
81 for (uint32_t i = 1; i < e->codes_curr - 1; i++) {
82 switch (e->codes[i].op) {
83 case GRN_OP_GET_VALUE:
84 case GRN_OP_PUSH:
85 if (e->codes[i].modify == 0) {
86 GRN_PTR_PUT(ctx, &(data->args), e->codes[i].value);
87 } else {
88 uint32_t call_i = i + e->codes[i].modify;
89 if (e->codes[call_i].op != GRN_OP_CALL) {
90 return false;
91 }
92 if (!grn_obj_is_function_proc(ctx, e->codes[i].value)) {
93 return false;
94 }
95 /* Literal arguments are only supported for now. Nested
96 * function call isn't supported yet. */
97 for (uint32_t j = i + 1; j < call_i; j++) {
98 if (e->codes[j].op != GRN_OP_PUSH) {
99 return false;
100 }
101 grn_obj *arg = e->codes[j].value;
102 if (!arg) {
103 return false;
104 }
105 switch (arg->header.type) {
106 case GRN_BULK:
107 case GRN_VECTOR:
108 case GRN_UVECTOR:
109 case GRN_PVECTOR:
110 /* supported */
111 break;
112 default:
113 return false;
114 }
115 }
116 e->codes += i;
117 e->codes_curr = call_i - i + 1;
118 grn_expr_executor executor;
119 grn_expr_executor_init(ctx, &executor, expr);
120 grn_obj *arg = grn_expr_executor_exec(ctx, &executor, GRN_ID_NIL);
121 GRN_PTR_PUT(ctx, &(data->args), arg);
122 grn_expr_executor_fin(ctx, &executor);
123 e->codes = codes;
124 e->codes_curr = codes_curr;
125 i += e->codes[i].modify;
126 }
127 break;
128 default:
129 return false;
130 }
131 }
132 if (e->codes[e->codes_curr - 1].op != GRN_OP_CALL) {
133 return false;
134 }
135 return true;
136 }
137
138 grn_rc
139 grn_applier_data_run(grn_ctx *ctx, grn_applier_data *data)
140 {
141 grn_proc *proc = (grn_proc *)(data->applier);
142 return proc->callbacks.function.applier(ctx, data);
143 }