save
[protos/libecoli.git] / lib / ecoli_node_expr.h
1 /*
2  * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
15  *
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.
26  */
27
28 #ifndef ECOLI_NODE_EXPR_
29 #define ECOLI_NODE_EXPR_
30
31 #include <ecoli_node.h>
32
33 /* XXX remove the _new for all other nodes */
34
35 /**
36  * Callback function type for evaluating a variable
37  *
38  * @param result
39  *   On success, this pointer must be set by the user to point
40  *   to a user structure describing the evaluated result.
41  * @param userctx
42  *   A user-defined context passed to all callback functions, which
43  *   can be used to maintain a state or store global information.
44  * @param var
45  *   The parsed result referencing the variable.
46  * @return
47  *   0 on success (*result must be set), or -errno on error (*result
48  *   is undefined).
49  */
50 typedef int (*ec_node_expr_eval_var_t)(
51         void **result, void *userctx,
52         const struct ec_parsed *var);
53
54 /**
55  * Callback function type for evaluating a prefix-operator
56  *
57  * @param result
58  *   On success, this pointer must be set by the user to point
59  *   to a user structure describing the evaluated result.
60  * @param userctx
61  *   A user-defined context passed to all callback functions, which
62  *   can be used to maintain a state or store global information.
63  * @param operand
64  *   The evaluated expression on which the operation should be applied.
65  * @param var
66  *   The parsed result referencing the operator.
67  * @return
68  *   0 on success (*result must be set, operand is freed),
69  *   or -errno on error (*result is undefined, operand is not freed).
70  */
71 typedef int (*ec_node_expr_eval_pre_op_t)(
72         void **result, void *userctx,
73         void *operand,
74         const struct ec_parsed *operator);
75
76 typedef int (*ec_node_expr_eval_post_op_t)(
77         void **result, void *userctx,
78         void *operand,
79         const struct ec_parsed *operator);
80
81 typedef int (*ec_node_expr_eval_bin_op_t)(
82         void **result, void *userctx,
83         void *operand1,
84         const struct ec_parsed *operator,
85         void *operand2);
86
87 typedef int (*ec_node_expr_eval_parenthesis_t)(
88         void **result, void *userctx,
89         const struct ec_parsed *open_paren,
90         const struct ec_parsed *close_paren,
91         void * value);
92
93 typedef void (*ec_node_expr_eval_free_t)(
94         void *result, void *userctx);
95
96
97 struct ec_node *ec_node_expr(const char *id);
98 int ec_node_expr_set_val_node(struct ec_node *gen_node, struct ec_node *val_node);
99 int ec_node_expr_add_bin_op(struct ec_node *gen_node, struct ec_node *op);
100 int ec_node_expr_add_pre_op(struct ec_node *gen_node, struct ec_node *op);
101 int ec_node_expr_add_post_op(struct ec_node *gen_node, struct ec_node *op);
102 int ec_node_expr_add_parenthesis(struct ec_node *gen_node,
103         struct ec_node *open, struct ec_node *close);
104
105 struct ec_node_expr_eval_ops {
106         ec_node_expr_eval_var_t eval_var;
107         ec_node_expr_eval_pre_op_t eval_pre_op;
108         ec_node_expr_eval_post_op_t eval_post_op;
109         ec_node_expr_eval_bin_op_t eval_bin_op;
110         ec_node_expr_eval_parenthesis_t eval_parenthesis;
111         ec_node_expr_eval_free_t eval_free;
112 };
113
114 int ec_node_expr_eval(void **result, const struct ec_node *node,
115         struct ec_parsed *parsed, const struct ec_node_expr_eval_ops *ops,
116         void *userctx);
117
118 #endif