-/*
- * Copyright (c) 2016-2017, Olivier MATZ <zer0@droids-corp.org>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of the University of California, Berkeley nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
*/
#include <errno.h>
#include <ecoli_malloc.h>
#include <ecoli_strvec.h>
#include <ecoli_test.h>
+#include <ecoli_node.h>
+#include <ecoli_parse.h>
#include <ecoli_node_int.h>
#include <ecoli_node_str.h>
#include <ecoli_node_re_lex.h>
#include <ecoli_node_expr.h>
+EC_LOG_TYPE_REGISTER(node_expr);
+
struct my_eval_result {
int val;
};
static int
ec_node_expr_test_eval_var(void **result, void *userctx,
- const struct ec_parsed *var)
+ const struct ec_parse *var)
{
const struct ec_strvec *vec;
- struct my_eval_result *eval;
+ const struct ec_node *node;
+ struct my_eval_result *eval = NULL;
+ int64_t val;
(void)userctx;
/* get parsed string vector, it should contain only one str */
- vec = ec_parsed_strvec(var);
- if (ec_strvec_len(vec) != 1)
- return -EINVAL;
+ vec = ec_parse_strvec(var);
+ if (ec_strvec_len(vec) != 1) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ node = ec_parse_get_node(var);
+ if (ec_node_int_getval(node, ec_strvec_val(vec, 0), &val) < 0)
+ return -1;
eval = ec_malloc(sizeof(*eval));
if (eval == NULL)
- return -ENOMEM;
+ return -1;
- eval->val = atoi(ec_strvec_val(vec, 0)); // XXX use strtol
- printf("eval var %d\n", eval->val);
+ eval->val = val;
+ EC_LOG(EC_LOG_DEBUG, "eval var %d\n", eval->val);
*result = eval;
return 0;
static int
ec_node_expr_test_eval_pre_op(void **result, void *userctx, void *operand,
- const struct ec_parsed *operator)
+ const struct ec_parse *operator)
{
const struct ec_strvec *vec;
struct my_eval_result *eval = operand;;
(void)userctx;
/* get parsed string vector, it should contain only one str */
- vec = ec_parsed_strvec(operator);
- if (ec_strvec_len(vec) != 1)
- return -EINVAL;
+ vec = ec_parse_strvec(operator);
+ if (ec_strvec_len(vec) != 1) {
+ errno = EINVAL;
+ return -1;
+ }
- if (!strcmp(ec_strvec_val(vec, 0), "!"))
+ if (!strcmp(ec_strvec_val(vec, 0), "!")) {
eval->val = !eval->val;
- else
- return -EINVAL;
+ } else {
+ errno = EINVAL;
+ return -1;
+ }
- printf("eval pre_op %d\n", eval->val);
+
+ EC_LOG(EC_LOG_DEBUG, "eval pre_op %d\n", eval->val);
*result = eval;
return 0;
static int
ec_node_expr_test_eval_post_op(void **result, void *userctx, void *operand,
- const struct ec_parsed *operator)
+ const struct ec_parse *operator)
{
const struct ec_strvec *vec;
struct my_eval_result *eval = operand;;
(void)userctx;
/* get parsed string vector, it should contain only one str */
- vec = ec_parsed_strvec(operator);
- if (ec_strvec_len(vec) != 1)
- return -EINVAL;
+ vec = ec_parse_strvec(operator);
+ if (ec_strvec_len(vec) != 1) {
+ errno = EINVAL;
+ return -1;
+ }
- if (!strcmp(ec_strvec_val(vec, 0), "^"))
+ if (!strcmp(ec_strvec_val(vec, 0), "^")) {
eval->val = eval->val * eval->val;
- else
- return -EINVAL;
+ } else {
+ errno = EINVAL;
+ return -1;
+ }
- printf("eval post_op %d\n", eval->val);
+ EC_LOG(EC_LOG_DEBUG, "eval post_op %d\n", eval->val);
*result = eval;
return 0;
static int
ec_node_expr_test_eval_bin_op(void **result, void *userctx, void *operand1,
- const struct ec_parsed *operator, void *operand2)
+ const struct ec_parse *operator, void *operand2)
{
const struct ec_strvec *vec;
(void)userctx;
/* get parsed string vector, it should contain only one str */
- vec = ec_parsed_strvec(operator);
- if (ec_strvec_len(vec) != 1)
- return -EINVAL;
+ vec = ec_parse_strvec(operator);
+ if (ec_strvec_len(vec) != 1) {
+ errno = EINVAL;
+ return -1;
+ }
- if (!strcmp(ec_strvec_val(vec, 0), "+"))
+ if (!strcmp(ec_strvec_val(vec, 0), "+")) {
eval1->val = eval1->val + eval2->val;
- else if (!strcmp(ec_strvec_val(vec, 0), "*"))
+ } else if (!strcmp(ec_strvec_val(vec, 0), "*")) {
eval1->val = eval1->val * eval2->val;
- else
- return -EINVAL;
+ } else {
+ errno = EINVAL;
+ return -1;
+ }
- printf("eval bin_op %d\n", eval1->val);
+ EC_LOG(EC_LOG_DEBUG, "eval bin_op %d\n", eval1->val);
ec_free(eval2);
*result = eval1;
static int
ec_node_expr_test_eval_parenthesis(void **result, void *userctx,
- const struct ec_parsed *open_paren,
- const struct ec_parsed *close_paren,
+ const struct ec_parse *open_paren,
+ const struct ec_parse *close_paren,
void *value)
{
(void)userctx;
(void)open_paren;
(void)close_paren;
- printf("eval paren\n");
+ EC_LOG(EC_LOG_DEBUG, "eval paren\n");
*result = value;
return 0;
const struct ec_node *expr_node,
const char *str, int val)
{
- struct ec_parsed *p;
+ struct ec_parse *p;
void *result;
struct my_eval_result *eval;
int ret;
- /* XXX check node type (again and again) */
-
p = ec_node_parse(lex_node, str);
if (p == NULL)
return -1;
ret = ec_node_expr_eval(&result, expr_node, p, &test_ops, NULL);
- ec_parsed_free(p);
+ ec_parse_free(p);
if (ret < 0)
return -1;
- /* the parsed value is an integer */
+ /* the parse value is an integer */
eval = result;
assert(eval != NULL);
- printf("result: %d (expected %d)\n", eval->val, val);
+ EC_LOG(EC_LOG_DEBUG, "result: %d (expected %d)\n", eval->val, val);
if (eval->val == val)
ret = 0;
else
return ret;
}
+/* LCOV_EXCL_START */
static int ec_node_expr_testcase(void)
{
struct ec_node *node = NULL, *lex_node = NULL;
- int ret = 0;
+ int testres = 0;
- node = ec_node_new("expr", "my_expr");
+ node = ec_node("expr", "my_expr");
if (node == NULL)
return -1;
- ec_node_expr_set_val_node(node, ec_node_int(NULL, 0, UCHAR_MAX, 0));
- ec_node_expr_add_bin_op(node, ec_node_str(NULL, "+"));
- ec_node_expr_add_bin_op(node, ec_node_str(NULL, "*"));
- ec_node_expr_add_pre_op(node, ec_node_str(NULL, "!")); /* not */
- ec_node_expr_add_post_op(node, ec_node_str(NULL, "^")); /* square */
- ec_node_expr_add_parenthesis(node, ec_node_str(NULL, "("),
- ec_node_str(NULL, ")"));
- ret |= EC_TEST_CHECK_PARSE(node, 1, "1");
- ret |= EC_TEST_CHECK_PARSE(node, 1, "1", "1");
- ret |= EC_TEST_CHECK_PARSE(node, 1, "1", "*");
- ret |= EC_TEST_CHECK_PARSE(node, 3, "1", "*", "1");
- ret |= EC_TEST_CHECK_PARSE(node, 3, "1", "*", "1", "*");
- ret |= EC_TEST_CHECK_PARSE(node, 4, "1", "+", "!", "1");
- ret |= EC_TEST_CHECK_PARSE(node, 4, "1", "^", "+", "1");
- ret |= EC_TEST_CHECK_PARSE(node, 5, "1", "*", "1", "*", "1");
- ret |= EC_TEST_CHECK_PARSE(node, 5, "1", "*", "1", "+", "1");
- ret |= EC_TEST_CHECK_PARSE(node, 7, "1", "*", "1", "*", "1", "*", "1");
- ret |= EC_TEST_CHECK_PARSE(
+ ec_node_expr_set_val_node(node, ec_node_int(EC_NO_ID, 0, UCHAR_MAX, 0));
+ ec_node_expr_add_bin_op(node, ec_node_str(EC_NO_ID, "+"));
+ ec_node_expr_add_bin_op(node, ec_node_str(EC_NO_ID, "*"));
+ ec_node_expr_add_pre_op(node, ec_node_str(EC_NO_ID, "!")); /* not */
+ ec_node_expr_add_post_op(node, ec_node_str(EC_NO_ID, "^")); /* square */
+ ec_node_expr_add_parenthesis(node, ec_node_str(EC_NO_ID, "("),
+ ec_node_str(EC_NO_ID, ")"));
+ testres |= EC_TEST_CHECK_PARSE(node, 1, "1");
+ testres |= EC_TEST_CHECK_PARSE(node, 1, "1", "1");
+ testres |= EC_TEST_CHECK_PARSE(node, 1, "1", "*");
+ testres |= EC_TEST_CHECK_PARSE(node, 3, "1", "*", "1");
+ testres |= EC_TEST_CHECK_PARSE(node, 3, "1", "*", "1", "*");
+ testres |= EC_TEST_CHECK_PARSE(node, 4, "1", "+", "!", "1");
+ testres |= EC_TEST_CHECK_PARSE(node, 4, "1", "^", "+", "1");
+ testres |= EC_TEST_CHECK_PARSE(node, 5, "1", "*", "1", "*", "1");
+ testres |= EC_TEST_CHECK_PARSE(node, 5, "1", "*", "1", "+", "1");
+ testres |= EC_TEST_CHECK_PARSE(node, 7, "1", "*", "1", "*", "1", "*",
+ "1");
+ testres |= EC_TEST_CHECK_PARSE(
node, 10, "!", "(", "1", "*", "(", "1", "+", "1", ")", ")");
- ret |= EC_TEST_CHECK_PARSE(node, 5, "1", "+", "!", "1", "^");
+ testres |= EC_TEST_CHECK_PARSE(node, 5, "1", "+", "!", "1", "^");
/* prepend a lexer to the expression node */
- lex_node = ec_node_re_lex(NULL, ec_node_clone(node));
+ lex_node = ec_node_re_lex(EC_NO_ID, ec_node_clone(node));
if (lex_node == NULL)
goto fail;
- ret |= ec_node_re_lex_add(lex_node, "[0-9]+", 1); /* vars */
- ret |= ec_node_re_lex_add(lex_node, "[+*!^()]", 1); /* operators */
- ret |= ec_node_re_lex_add(lex_node, "[ ]+", 0); /* spaces */
+ testres |= ec_node_re_lex_add(lex_node, "[0-9]+", 1); /* vars */
+ testres |= ec_node_re_lex_add(lex_node, "[+*!^()]", 1); /* operators */
+ testres |= ec_node_re_lex_add(lex_node, "[ ]+", 0); /* spaces */
/* valid expressions */
- ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "!1");
- ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "1^");
- ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "1^ + 1");
- ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "1 + 4 * (2 + 3^)^");
- ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "(1)");
- ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "3*!3+!3*(2+ 2)");
- ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "!!(!1)^ + !(4 + (2*3))");
- ret |= EC_TEST_CHECK_PARSE(lex_node, 1, "(1 + 1)^ * 1^");
+ testres |= EC_TEST_CHECK_PARSE(lex_node, 1, "!1");
+ testres |= EC_TEST_CHECK_PARSE(lex_node, 1, "1^");
+ testres |= EC_TEST_CHECK_PARSE(lex_node, 1, "1^ + 1");
+ testres |= EC_TEST_CHECK_PARSE(lex_node, 1, "1 + 4 * (2 + 3^)^");
+ testres |= EC_TEST_CHECK_PARSE(lex_node, 1, "(1)");
+ testres |= EC_TEST_CHECK_PARSE(lex_node, 1, "3*!3+!3*(2+ 2)");
+ testres |= EC_TEST_CHECK_PARSE(lex_node, 1, "!!(!1)^ + !(4 + (2*3))");
+ testres |= EC_TEST_CHECK_PARSE(lex_node, 1, "(1 + 1)^ * 1^");
/* invalid expressions */
- ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "");
- ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "()");
- ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "(");
- ret |= EC_TEST_CHECK_PARSE(lex_node, -1, ")");
- ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "+1");
- ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+");
- ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+*1");
- ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+(1*1");
- ret |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+!1!1)");
-
- ret |= ec_node_expr_test_eval(lex_node, node, "1^", 1);
- ret |= ec_node_expr_test_eval(lex_node, node, "2^", 4);
- ret |= ec_node_expr_test_eval(lex_node, node, "!1", 0);
- ret |= ec_node_expr_test_eval(lex_node, node, "!0", 1);
-
- ret |= ec_node_expr_test_eval(lex_node, node, "1+1", 2);
- ret |= ec_node_expr_test_eval(lex_node, node, "1+1*2", 4);
- ret |= ec_node_expr_test_eval(lex_node, node, "2 * 2^", 8);
- ret |= ec_node_expr_test_eval(lex_node, node, "(1 + !0)^ * !0^", 4);
- ret |= ec_node_expr_test_eval(lex_node, node, "(1 + !1) * 3", 3);
+ testres |= EC_TEST_CHECK_PARSE(lex_node, -1, "");
+ testres |= EC_TEST_CHECK_PARSE(lex_node, -1, "()");
+ testres |= EC_TEST_CHECK_PARSE(lex_node, -1, "(");
+ testres |= EC_TEST_CHECK_PARSE(lex_node, -1, ")");
+ testres |= EC_TEST_CHECK_PARSE(lex_node, -1, "+1");
+ testres |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+");
+ testres |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+*1");
+ testres |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+(1*1");
+ testres |= EC_TEST_CHECK_PARSE(lex_node, -1, "1+!1!1)");
+
+ testres |= ec_node_expr_test_eval(lex_node, node, "1^", 1);
+ testres |= ec_node_expr_test_eval(lex_node, node, "2^", 4);
+ testres |= ec_node_expr_test_eval(lex_node, node, "!1", 0);
+ testres |= ec_node_expr_test_eval(lex_node, node, "!0", 1);
+
+ testres |= ec_node_expr_test_eval(lex_node, node, "1+1", 2);
+ testres |= ec_node_expr_test_eval(lex_node, node, "1+2+3", 6);
+ testres |= ec_node_expr_test_eval(lex_node, node, "1+1*2", 4);
+ testres |= ec_node_expr_test_eval(lex_node, node, "2 * 2^", 8);
+ testres |= ec_node_expr_test_eval(lex_node, node, "(1 + !0)^ * !0^", 4);
+ testres |= ec_node_expr_test_eval(lex_node, node, "(1 + !1) * 3", 3);
ec_node_free(node);
ec_node_free(lex_node);
- return ret;
+ return testres;
fail:
ec_node_free(lex_node);
ec_node_free(node);
return -1;
}
+/* LCOV_EXCL_STOP */
static struct ec_test ec_node_expr_test = {
- .name = "expr",
+ .name = "node_expr",
.test = ec_node_expr_testcase,
};