save
[protos/libecoli.git] / lib / ecoli_tk_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_TK_EXPR_
29 #define ECOLI_TK_EXPR_
30
31 #include <ecoli_tk.h>
32
33 /* XXX remove the _new for all other tokens */
34
35 /* XXX explain the free policy in case of error or success */
36 struct ec_tk_expr_eval_result {
37         int code; /* 0 on success, negative on error */
38         void *parsed;
39 };
40
41 typedef struct ec_tk_expr_eval_result (*ec_tk_expr_eval_var_t)(
42         void *userctx, const struct ec_parsed_tk *var);
43
44 typedef struct ec_tk_expr_eval_result (*ec_tk_expr_eval_pre_op_t)(
45         void *userctx, struct ec_tk_expr_eval_result operand,
46         const struct ec_parsed_tk *operator);
47
48 typedef struct ec_tk_expr_eval_result (*ec_tk_expr_eval_post_op_t)(
49         void *userctx, struct ec_tk_expr_eval_result operand,
50         const struct ec_parsed_tk *operator);
51
52 typedef struct ec_tk_expr_eval_result (*ec_tk_expr_eval_bin_op_t)(
53         void *userctx, struct ec_tk_expr_eval_result operand1,
54         const struct ec_parsed_tk *operator,
55         struct ec_tk_expr_eval_result operand2);
56
57 typedef struct ec_tk_expr_eval_result (*ec_tk_expr_eval_parenthesis_t)(
58         void *userctx, const struct ec_parsed_tk *operator_str,
59         const struct ec_parsed_tk *operand_str,
60         void *operand);
61
62
63 struct ec_tk *ec_tk_expr(const char *id);
64 int ec_tk_expr_set_val_tk(struct ec_tk *gen_tk, struct ec_tk *val_tk);
65 int ec_tk_expr_add_bin_op(struct ec_tk *gen_tk, struct ec_tk *op);
66 int ec_tk_expr_add_pre_op(struct ec_tk *gen_tk, struct ec_tk *op);
67 int ec_tk_expr_add_post_op(struct ec_tk *gen_tk, struct ec_tk *op);
68 int ec_tk_expr_add_parenthesis(struct ec_tk *gen_tk,
69         struct ec_tk *open, struct ec_tk *close);
70
71 struct ec_tk_expr_eval_ops {
72         ec_tk_expr_eval_var_t eval_var;
73         ec_tk_expr_eval_pre_op_t eval_pre_op;
74         ec_tk_expr_eval_post_op_t eval_post_op;
75         ec_tk_expr_eval_bin_op_t eval_bin_op;
76         ec_tk_expr_eval_parenthesis_t eval_parenthesis;
77 };
78
79 struct ec_tk_expr_eval_result ec_tk_expr_eval(const struct ec_tk *tk,
80         struct ec_parsed_tk *parsed, const struct ec_tk_expr_eval_ops *ops,
81         void *userctx);
82
83 #endif