1 | /* -*- c-basic-offset: 2 -*- */ |
2 | /* |
3 | Copyright(C) 2014-2015 Brazil |
4 | |
5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License version 2.1 as published by the Free Software Foundation. |
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_ctx_impl.h" |
20 | |
21 | #ifdef GRN_WITH_MRUBY |
22 | #include <mruby.h> |
23 | |
24 | #include "mrb_operator.h" |
25 | |
26 | mrb_value |
27 | grn_mrb_value_from_operator(mrb_state *mrb, grn_operator op) |
28 | { |
29 | grn_ctx *ctx = (grn_ctx *)(mrb->ud); |
30 | grn_mrb_data *data = &(ctx->impl->mrb); |
31 | mrb_value mrb_op_raw; |
32 | mrb_value mrb_op; |
33 | |
34 | mrb_op_raw = mrb_fixnum_value(op); |
35 | mrb_op = mrb_funcall(mrb, mrb_obj_value(data->groonga.operator_class), |
36 | "find" , 1, mrb_op_raw); |
37 | if (mrb_nil_p(mrb_op)) { |
38 | return mrb_op_raw; |
39 | } else { |
40 | return mrb_op; |
41 | } |
42 | } |
43 | |
44 | grn_operator |
45 | grn_mrb_value_to_operator(mrb_state *mrb, mrb_value mrb_op) |
46 | { |
47 | if (!mrb_fixnum_p(mrb_op)) { |
48 | mrb_op = mrb_funcall(mrb, mrb_op, "value" , 0); |
49 | } |
50 | |
51 | return mrb_fixnum(mrb_op); |
52 | } |
53 | |
54 | void |
55 | grn_mrb_operator_init(grn_ctx *ctx) |
56 | { |
57 | grn_mrb_data *data = &(ctx->impl->mrb); |
58 | mrb_state *mrb = data->state; |
59 | struct RClass *module = ctx->impl->mrb.module; |
60 | struct RClass *klass; |
61 | mrb_value klass_obj; |
62 | |
63 | klass = mrb_class_get_under(mrb, module, "Operator" ); |
64 | data->groonga.operator_class = klass; |
65 | |
66 | klass_obj = mrb_obj_value(klass); |
67 | #define DEFINE_OPERATOR(name) \ |
68 | mrb_funcall(mrb, klass_obj, "register", 1, \ |
69 | mrb_funcall(mrb, klass_obj, "new", 2, \ |
70 | mrb_str_new_lit(mrb, #name), \ |
71 | mrb_fixnum_value(GRN_OP_ ## name))) |
72 | |
73 | DEFINE_OPERATOR(PUSH); |
74 | DEFINE_OPERATOR(POP); |
75 | DEFINE_OPERATOR(NOP); |
76 | DEFINE_OPERATOR(CALL); |
77 | DEFINE_OPERATOR(INTERN); |
78 | DEFINE_OPERATOR(GET_REF); |
79 | DEFINE_OPERATOR(GET_VALUE); |
80 | DEFINE_OPERATOR(AND); |
81 | DEFINE_OPERATOR(AND_NOT); |
82 | DEFINE_OPERATOR(OR); |
83 | DEFINE_OPERATOR(ASSIGN); |
84 | DEFINE_OPERATOR(STAR_ASSIGN); |
85 | DEFINE_OPERATOR(SLASH_ASSIGN); |
86 | DEFINE_OPERATOR(MOD_ASSIGN); |
87 | DEFINE_OPERATOR(PLUS_ASSIGN); |
88 | DEFINE_OPERATOR(MINUS_ASSIGN); |
89 | DEFINE_OPERATOR(SHIFTL_ASSIGN); |
90 | DEFINE_OPERATOR(SHIFTR_ASSIGN); |
91 | DEFINE_OPERATOR(SHIFTRR_ASSIGN); |
92 | DEFINE_OPERATOR(AND_ASSIGN); |
93 | DEFINE_OPERATOR(XOR_ASSIGN); |
94 | DEFINE_OPERATOR(OR_ASSIGN); |
95 | DEFINE_OPERATOR(JUMP); |
96 | DEFINE_OPERATOR(CJUMP); |
97 | DEFINE_OPERATOR(COMMA); |
98 | DEFINE_OPERATOR(BITWISE_OR); |
99 | DEFINE_OPERATOR(BITWISE_XOR); |
100 | DEFINE_OPERATOR(BITWISE_AND); |
101 | DEFINE_OPERATOR(BITWISE_NOT); |
102 | DEFINE_OPERATOR(EQUAL); |
103 | DEFINE_OPERATOR(NOT_EQUAL); |
104 | DEFINE_OPERATOR(LESS); |
105 | DEFINE_OPERATOR(GREATER); |
106 | DEFINE_OPERATOR(LESS_EQUAL); |
107 | DEFINE_OPERATOR(GREATER_EQUAL); |
108 | DEFINE_OPERATOR(IN); |
109 | DEFINE_OPERATOR(MATCH); |
110 | DEFINE_OPERATOR(NEAR); |
111 | DEFINE_OPERATOR(NEAR2); |
112 | DEFINE_OPERATOR(SIMILAR); |
113 | DEFINE_OPERATOR(TERM_EXTRACT); |
114 | DEFINE_OPERATOR(SHIFTL); |
115 | DEFINE_OPERATOR(SHIFTR); |
116 | DEFINE_OPERATOR(SHIFTRR); |
117 | DEFINE_OPERATOR(PLUS); |
118 | DEFINE_OPERATOR(MINUS); |
119 | DEFINE_OPERATOR(STAR); |
120 | DEFINE_OPERATOR(SLASH); |
121 | DEFINE_OPERATOR(MOD); |
122 | DEFINE_OPERATOR(DELETE); |
123 | DEFINE_OPERATOR(INCR); |
124 | DEFINE_OPERATOR(DECR); |
125 | DEFINE_OPERATOR(INCR_POST); |
126 | DEFINE_OPERATOR(DECR_POST); |
127 | DEFINE_OPERATOR(NOT); |
128 | DEFINE_OPERATOR(ADJUST); |
129 | DEFINE_OPERATOR(EXACT); |
130 | DEFINE_OPERATOR(LCP); |
131 | DEFINE_OPERATOR(PARTIAL); |
132 | DEFINE_OPERATOR(UNSPLIT); |
133 | DEFINE_OPERATOR(PREFIX); |
134 | DEFINE_OPERATOR(SUFFIX); |
135 | DEFINE_OPERATOR(GEO_DISTANCE1); |
136 | DEFINE_OPERATOR(GEO_DISTANCE2); |
137 | DEFINE_OPERATOR(GEO_DISTANCE3); |
138 | DEFINE_OPERATOR(GEO_DISTANCE4); |
139 | DEFINE_OPERATOR(GEO_WITHINP5); |
140 | DEFINE_OPERATOR(GEO_WITHINP6); |
141 | DEFINE_OPERATOR(GEO_WITHINP8); |
142 | DEFINE_OPERATOR(OBJ_SEARCH); |
143 | DEFINE_OPERATOR(EXPR_GET_VAR); |
144 | DEFINE_OPERATOR(TABLE_CREATE); |
145 | DEFINE_OPERATOR(TABLE_SELECT); |
146 | DEFINE_OPERATOR(TABLE_SORT); |
147 | DEFINE_OPERATOR(TABLE_GROUP); |
148 | DEFINE_OPERATOR(JSON_PUT); |
149 | DEFINE_OPERATOR(GET_MEMBER); |
150 | DEFINE_OPERATOR(REGEXP); |
151 | DEFINE_OPERATOR(FUZZY); |
152 | |
153 | #undef DEFINE_OPERATOR |
154 | } |
155 | #endif |
156 | |