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.
37 #include <ecoli_malloc.h>
38 #include <ecoli_log.h>
39 #include <ecoli_test.h>
40 #include <ecoli_strvec.h>
41 #include <ecoli_node.h>
42 #include <ecoli_parsed.h>
43 #include <ecoli_completed.h>
44 #include <ecoli_node_seq.h>
45 #include <ecoli_node_many.h>
46 #include <ecoli_node_or.h>
47 #include <ecoli_node_weakref.h>
48 #include <ecoli_node_expr.h>
54 struct ec_node *child;
56 /* the configuration nodes */
57 struct ec_node *val_node;
58 struct ec_node **bin_ops;
59 unsigned int bin_ops_len;
60 struct ec_node **pre_ops;
61 unsigned int pre_ops_len;
62 struct ec_node **post_ops;
63 unsigned int post_ops_len;
64 struct ec_node **open_ops;
65 struct ec_node **close_ops;
66 unsigned int paren_len;
69 static struct ec_parsed *ec_node_expr_parse(const struct ec_node *gen_node,
70 const struct ec_strvec *strvec)
72 struct ec_node_expr *node = (struct ec_node_expr *)gen_node;
74 return ec_node_parse_strvec(node->child, strvec);
77 static struct ec_completed *ec_node_expr_complete(const struct ec_node *gen_node,
78 const struct ec_strvec *strvec)
80 struct ec_node_expr *node = (struct ec_node_expr *)gen_node;
82 return ec_node_complete_strvec(node->child, strvec);
85 static void ec_node_expr_free_priv(struct ec_node *gen_node)
87 struct ec_node_expr *node = (struct ec_node_expr *)gen_node;
90 ec_log(EC_LOG_DEBUG, "free %p %p %p\n", node, node->child, node->val_node);
91 ec_node_free(node->val_node);
93 for (i = 0; i < node->bin_ops_len; i++)
94 ec_node_free(node->bin_ops[i]);
95 ec_free(node->bin_ops);
96 for (i = 0; i < node->pre_ops_len; i++)
97 ec_node_free(node->pre_ops[i]);
98 ec_free(node->pre_ops);
99 for (i = 0; i < node->post_ops_len; i++)
100 ec_node_free(node->post_ops[i]);
101 ec_free(node->post_ops);
102 for (i = 0; i < node->paren_len; i++) {
103 ec_node_free(node->open_ops[i]);
104 ec_node_free(node->close_ops[i]);
106 ec_free(node->open_ops);
107 ec_free(node->close_ops);
109 ec_node_free(node->child);
112 static int ec_node_expr_build(struct ec_node *gen_node)
114 struct ec_node_expr *node = (struct ec_node_expr *)gen_node;
115 struct ec_node *term = NULL, *expr = NULL, *next = NULL,
116 *pre_op = NULL, *post_op = NULL,
117 *post = NULL, *weak = NULL;
121 if (node->val_node == NULL)
123 if (node->bin_ops_len == 0 && node->pre_ops_len == 0 &&
124 node->post_ops_len == 0)
127 /* create the object, we will initialize it later: this is
128 * needed because we have a circular dependency */
130 weak = ec_node("weakref", "weak");
134 /* prefix unary operators */
135 pre_op = ec_node("or", "pre-op");
138 for (i = 0; i < node->pre_ops_len; i++) {
139 if (ec_node_or_add(pre_op, ec_node_clone(node->pre_ops[i])) < 0)
143 /* suffix unary operators */
144 post_op = ec_node("or", "post-op");
147 for (i = 0; i < node->post_ops_len; i++) {
148 if (ec_node_or_add(post_op, ec_node_clone(node->post_ops[i])) < 0)
152 post = ec_node("or", "post");
155 if (ec_node_or_add(post, ec_node_clone(node->val_node)) < 0)
157 if (ec_node_or_add(post,
159 ec_node_clone(pre_op),
160 ec_node_clone(weak))) < 0)
162 for (i = 0; i < node->paren_len; i++) {
163 if (ec_node_or_add(post, EC_NODE_SEQ(NULL,
164 ec_node_clone(node->open_ops[i]),
166 ec_node_clone(node->close_ops[i]))) < 0)
169 term = EC_NODE_SEQ("term",
171 ec_node_many(NULL, ec_node_clone(post_op), 0, 0)
176 for (i = 0; i < node->bin_ops_len; i++) {
177 next = EC_NODE_SEQ("next",
181 ec_node_clone(node->bin_ops[i]),
195 /* free the initial references */
196 ec_node_free(pre_op);
198 ec_node_free(post_op);
203 /* no need to clone here, the node is not consumed */
204 if (ec_node_weakref_set(weak, expr) < 0)
216 ec_node_free(pre_op);
217 ec_node_free(post_op);
224 static struct ec_node_type ec_node_expr_type = {
226 .build = ec_node_expr_build,
227 .parse = ec_node_expr_parse,
228 .complete = ec_node_expr_complete,
229 .size = sizeof(struct ec_node_expr),
230 .free_priv = ec_node_expr_free_priv,
233 EC_NODE_TYPE_REGISTER(ec_node_expr_type);
235 int ec_node_expr_set_val_node(struct ec_node *gen_node, struct ec_node *val_node)
237 struct ec_node_expr *node = (struct ec_node_expr *)gen_node;
241 if (val_node == NULL)
244 if (gen_node->flags & EC_NODE_F_BUILT)
247 if (node->val_node != NULL)
250 node->val_node = val_node;
251 gen_node->flags &= ~EC_NODE_F_BUILT;
256 ec_node_free(val_node);
260 /* add a binary operator */
261 int ec_node_expr_add_bin_op(struct ec_node *gen_node, struct ec_node *op)
263 struct ec_node_expr *node = (struct ec_node_expr *)gen_node;
264 struct ec_node **bin_ops;
267 // XXX check node type
270 if (node == NULL || op == NULL)
273 if (gen_node->flags & EC_NODE_F_BUILT)
277 bin_ops = ec_realloc(node->bin_ops,
278 (node->bin_ops_len + 1) * sizeof(*node->bin_ops));
282 node->bin_ops = bin_ops;
283 bin_ops[node->bin_ops_len] = op;
285 gen_node->flags &= ~EC_NODE_F_BUILT;
294 /* add a unary pre-operator */
295 int ec_node_expr_add_pre_op(struct ec_node *gen_node, struct ec_node *op)
297 struct ec_node_expr *node = (struct ec_node_expr *)gen_node;
298 struct ec_node **pre_ops;
301 // XXX check node type
304 if (node == NULL || op == NULL)
307 if (gen_node->flags & EC_NODE_F_BUILT)
311 pre_ops = ec_realloc(node->pre_ops,
312 (node->pre_ops_len + 1) * sizeof(*node->pre_ops));
316 node->pre_ops = pre_ops;
317 pre_ops[node->pre_ops_len] = op;
319 gen_node->flags &= ~EC_NODE_F_BUILT;
328 /* add a unary post-operator */
329 int ec_node_expr_add_post_op(struct ec_node *gen_node, struct ec_node *op)
331 struct ec_node_expr *node = (struct ec_node_expr *)gen_node;
332 struct ec_node **post_ops;
335 // XXX check node type
338 if (node == NULL || op == NULL)
341 if (gen_node->flags & EC_NODE_F_BUILT)
345 post_ops = ec_realloc(node->post_ops,
346 (node->post_ops_len + 1) * sizeof(*node->post_ops));
347 if (post_ops == NULL)
350 node->post_ops = post_ops;
351 post_ops[node->post_ops_len] = op;
352 node->post_ops_len++;
353 gen_node->flags &= ~EC_NODE_F_BUILT;
362 /* add parenthesis symbols */
363 int ec_node_expr_add_parenthesis(struct ec_node *gen_node,
364 struct ec_node *open, struct ec_node *close)
366 struct ec_node_expr *node = (struct ec_node_expr *)gen_node;
367 struct ec_node **open_ops, **close_ops;
370 // XXX check node type
373 if (node == NULL || open == NULL || close == NULL)
376 if (gen_node->flags & EC_NODE_F_BUILT)
380 open_ops = ec_realloc(node->open_ops,
381 (node->paren_len + 1) * sizeof(*node->open_ops));
382 if (open_ops == NULL)
384 close_ops = ec_realloc(node->close_ops,
385 (node->paren_len + 1) * sizeof(*node->close_ops));
386 if (close_ops == NULL)
389 node->open_ops = open_ops;
390 node->close_ops = close_ops;
391 open_ops[node->paren_len] = open;
392 close_ops[node->paren_len] = close;
394 gen_node->flags &= ~EC_NODE_F_BUILT;
404 enum expr_node_type {
413 static enum expr_node_type get_node_type(const struct ec_node *expr_gen_node,
414 const struct ec_node *check)
416 struct ec_node_expr *expr_node = (struct ec_node_expr *)expr_gen_node;
419 if (check == expr_node->val_node)
422 for (i = 0; i < expr_node->bin_ops_len; i++) {
423 if (check == expr_node->bin_ops[i])
426 for (i = 0; i < expr_node->pre_ops_len; i++) {
427 if (check == expr_node->pre_ops[i])
430 for (i = 0; i < expr_node->post_ops_len; i++) {
431 if (check == expr_node->post_ops[i])
435 for (i = 0; i < expr_node->paren_len; i++) {
436 if (check == expr_node->open_ops[i])
439 for (i = 0; i < expr_node->paren_len; i++) {
440 if (check == expr_node->close_ops[i])
450 const struct ec_parsed *op;
451 enum expr_node_type op_type;
454 /* merge x and y results in x */
455 static int merge_results(void *userctx,
456 const struct ec_node_expr_eval_ops *ops,
457 struct result *x, const struct result *y)
461 if (y->has_val == 0 && y->op == NULL)
463 if (x->has_val == 0 && x->op == NULL) {
468 if (x->has_val && x->op == NULL && y->has_val && y->op != NULL) {
469 ret = ops->eval_bin_op(&x->val, userctx, x->val, y->op, y->val);
476 if (x->has_val == 0 && x->op != NULL && y->has_val && y->op == NULL) {
477 if (x->op_type == PRE_OP) {
478 ret = ops->eval_pre_op(&x->val, userctx, y->val, x->op);
485 } else if (x->op_type == BIN_OP) {
492 if (x->has_val && x->op == NULL && y->has_val == 0 && y->op != NULL) {
493 ret = ops->eval_post_op(&x->val, userctx, x->val, y->op);
500 assert(true); /* we should not get here */
504 static int eval_expression(struct result *result,
506 const struct ec_node_expr_eval_ops *ops,
507 const struct ec_node *expr_gen_node,
508 const struct ec_parsed *parsed)
511 struct ec_parsed *open = NULL, *close = NULL;
512 struct result child_result;
513 struct ec_parsed *child;
514 enum expr_node_type type;
517 memset(result, 0, sizeof(*result));
518 memset(&child_result, 0, sizeof(child_result));
520 type = get_node_type(expr_gen_node, parsed->node);
522 ret = ops->eval_var(&result->val, userctx, parsed);
526 } else if (type == PRE_OP || type == POST_OP || type == BIN_OP) {
528 result->op_type = type;
531 TAILQ_FOREACH(child, &parsed->children, next) {
533 type = get_node_type(expr_gen_node, child->node);
534 if (type == PAREN_OPEN) {
537 } else if (type == PAREN_CLOSE) {
542 ret = eval_expression(&child_result, userctx, ops,
543 expr_gen_node, child);
547 ret = merge_results(userctx, ops, result, &child_result);
551 memset(&child_result, 0, sizeof(child_result));
554 if (open != NULL && close != NULL) {
555 ret = ops->eval_parenthesis(&result->val, userctx, open, close,
565 ops->eval_free(result->val, userctx);
566 if (child_result.has_val)
567 ops->eval_free(child_result.val, userctx);
568 memset(result, 0, sizeof(*result));
573 int ec_node_expr_eval(void **user_result, const struct ec_node *node,
574 struct ec_parsed *parsed, const struct ec_node_expr_eval_ops *ops,
577 struct result result;
580 if (ops == NULL || ops->eval_var == NULL || ops->eval_pre_op == NULL ||
581 ops->eval_post_op == NULL || ops->eval_bin_op == NULL ||
582 ops->eval_parenthesis == NULL || ops->eval_free == NULL)
585 if (!ec_parsed_matches(parsed))
588 ec_parsed_dump(stdout, parsed); //XXX
589 ret = eval_expression(&result, userctx, ops, node, parsed);
593 assert(result.has_val);
594 assert(result.op == NULL);
595 *user_result = result.val;
600 /* the test case is in a separate file ecoli_node_expr_test.c */