2 * Copyright (c) 2016-2017, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <ecoli_malloc.h>
34 #include <ecoli_strvec.h>
35 #include <ecoli_test.h>
36 #include <ecoli_node_int.h>
37 #include <ecoli_node_str.h>
38 #include <ecoli_node_re_lex.h>
39 #include <ecoli_node_expr.h>
41 struct my_eval_result {
46 ec_node_expr_test_eval_var(void **result, void *userctx,
47 const struct ec_parsed *var)
49 const struct ec_strvec *vec;
50 struct my_eval_result *eval;
54 /* get parsed string vector, it should contain only one str */
55 vec = ec_parsed_strvec(var);
56 if (ec_strvec_len(vec) != 1)
59 eval = ec_malloc(sizeof(*eval));
63 eval->val = atoi(ec_strvec_val(vec, 0)); // XXX use strtol
64 printf("eval var %d\n", eval->val);
71 ec_node_expr_test_eval_pre_op(void **result, void *userctx, void *operand,
72 const struct ec_parsed *operator)
74 const struct ec_strvec *vec;
75 struct my_eval_result *eval = operand;;
79 /* get parsed string vector, it should contain only one str */
80 vec = ec_parsed_strvec(operator);
81 if (ec_strvec_len(vec) != 1)
84 if (!strcmp(ec_strvec_val(vec, 0), "!"))
85 eval->val = !eval->val;
89 printf("eval pre_op %d\n", eval->val);
96 ec_node_expr_test_eval_post_op(void **result, void *userctx, void *operand,
97 const struct ec_parsed *operator)
99 const struct ec_strvec *vec;
100 struct my_eval_result *eval = operand;;
104 /* get parsed string vector, it should contain only one str */
105 vec = ec_parsed_strvec(operator);
106 if (ec_strvec_len(vec) != 1)
109 if (!strcmp(ec_strvec_val(vec, 0), "^"))
110 eval->val = eval->val * eval->val;
114 printf("eval post_op %d\n", eval->val);
121 ec_node_expr_test_eval_bin_op(void **result, void *userctx, void *operand1,
122 const struct ec_parsed *operator, void *operand2)
125 const struct ec_strvec *vec;
126 struct my_eval_result *eval1 = operand1;;
127 struct my_eval_result *eval2 = operand2;;
131 /* get parsed string vector, it should contain only one str */
132 vec = ec_parsed_strvec(operator);
133 if (ec_strvec_len(vec) != 1)
136 if (!strcmp(ec_strvec_val(vec, 0), "+"))
137 eval1->val = eval1->val + eval2->val;
138 else if (!strcmp(ec_strvec_val(vec, 0), "*"))
139 eval1->val = eval1->val * eval2->val;
143 printf("eval bin_op %d\n", eval1->val);
151 ec_node_expr_test_eval_parenthesis(void **result, void *userctx,
152 const struct ec_parsed *open_paren,
153 const struct ec_parsed *close_paren,
160 printf("eval paren\n");
167 ec_node_expr_test_eval_free(void *result, void *userctx)
173 static const struct ec_node_expr_eval_ops test_ops = {
174 .eval_var = ec_node_expr_test_eval_var,
175 .eval_pre_op = ec_node_expr_test_eval_pre_op,
176 .eval_post_op = ec_node_expr_test_eval_post_op,
177 .eval_bin_op = ec_node_expr_test_eval_bin_op,
178 .eval_parenthesis = ec_node_expr_test_eval_parenthesis,
179 .eval_free = ec_node_expr_test_eval_free,
182 static int ec_node_expr_test_eval(struct ec_node *lex_node,
183 const struct ec_node *expr_node,
184 const char *str, int val)
188 struct my_eval_result *eval;
191 /* XXX check node type (again and again) */
193 p = ec_node_parse(lex_node, str);
197 ret = ec_node_expr_eval(&result, expr_node, p, &test_ops, NULL);
202 /* the parsed value is an integer */
204 assert(eval != NULL);
206 printf("result: %d (expected %d)\n", eval->val, val);
207 if (eval->val == val)
217 static int ec_node_expr_testcase(void)
219 struct ec_node *node = NULL, *lex_node = NULL;
222 node = ec_node("expr", "my_expr");
226 ec_node_expr_set_val_node(node, ec_node_int(NULL, 0, UCHAR_MAX, 0));
227 ec_node_expr_add_bin_op(node, ec_node_str(NULL, "+"));
228 ec_node_expr_add_bin_op(node, ec_node_str(NULL, "*"));
229 ec_node_expr_add_pre_op(node, ec_node_str(NULL, "!")); /* not */
230 ec_node_expr_add_post_op(node, ec_node_str(NULL, "^")); /* square */
231 ec_node_expr_add_parenthesis(node, ec_node_str(NULL, "("),
232 ec_node_str(NULL, ")"));
233 ret |= EC_TEST_CHECK_PARSE(node, 1, "1");
234 ret |= EC_TEST_CHECK_PARSE(node, 1, "1", "1");
235 ret |= EC_TEST_CHECK_PARSE(node, 1, "1", "*");
236 ret |= EC_TEST_CHECK_PARSE(node, 3, "1", "*", "1");
237 ret |= EC_TEST_CHECK_PARSE(node, 3, "1", "*", "1", "*");
238 ret |= EC_TEST_CHECK_PARSE(node, 4, "1", "+", "!", "1");
239 ret |= EC_TEST_CHECK_PARSE(node, 4, "1", "^", "+", "1");
240 ret |= EC_TEST_CHECK_PARSE(node, 5, "1", "*", "1", "*", "1");
241 ret |= EC_TEST_CHECK_PARSE(node, 5, "1", "*", "1", "+", "1");
242 ret |= EC_TEST_CHECK_PARSE(node, 7, "1", "*", "1", "*", "1", "*", "1");
243 ret |= EC_TEST_CHECK_PARSE(
244 node, 10, "!", "(", "1", "*", "(", "1", "+", "1", ")", ")");
245 ret |= EC_TEST_CHECK_PARSE(node, 5, "1", "+", "!", "1", "^");
247 /* prepend a lexer to the expression node */
248 lex_node = ec_node_re_lex(NULL, ec_node_clone(node));
249 if (lex_node == NULL)
252 ret |= ec_node_re_lex_add(lex_node, "[0-9]+", 1); /* vars */
253 ret |= ec_node_re_lex_add(lex_node, "[+*!^()]", 1); /* operators */
254 ret |= ec_node_re_lex_add(lex_node, "[ ]+", 0); /* spaces */
256 /* valid expressions */
257 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "!1");
258 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "1^");
259 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "1^ + 1");
260 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "1 + 4 * (2 + 3^)^");
261 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "(1)");
262 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "3*!3+!3*(2+ 2)");
263 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "!!(!1)^ + !(4 + (2*3))");
264 ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "(1 + 1)^ * 1^");
266 /* invalid expressions */
267 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "");
268 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "()");
269 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "(");
270 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, ")");
271 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "+1");
272 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+");
273 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+*1");
274 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+(1*1");
275 ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+!1!1)");
277 ret |= ec_node_expr_test_eval(lex_node, node, "1^", 1);
278 ret |= ec_node_expr_test_eval(lex_node, node, "2^", 4);
279 ret |= ec_node_expr_test_eval(lex_node, node, "!1", 0);
280 ret |= ec_node_expr_test_eval(lex_node, node, "!0", 1);
282 ret |= ec_node_expr_test_eval(lex_node, node, "1+1", 2);
283 ret |= ec_node_expr_test_eval(lex_node, node, "1+1*2", 4);
284 ret |= ec_node_expr_test_eval(lex_node, node, "2 * 2^", 8);
285 ret |= ec_node_expr_test_eval(lex_node, node, "(1 + !0)^ * !0^", 4);
286 ret |= ec_node_expr_test_eval(lex_node, node, "(1 + !1) * 3", 3);
289 ec_node_free(lex_node);
294 ec_node_free(lex_node);
299 static struct ec_test ec_node_expr_test = {
301 .test = ec_node_expr_testcase,
304 EC_TEST_REGISTER(ec_node_expr_test);