comparison lwlib/lw_expr.h @ 0:2c24602be78f

Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
author lost@l-w.ca
date Wed, 19 Jan 2011 22:27:17 -0700
parents
children 6b9991fb39b6
comparison
equal deleted inserted replaced
-1:000000000000 0:2c24602be78f
1 /*
2 lwexpr.h
3
4 Copyright © 2010 William Astle
5
6 This file is part of LWTOOLS.
7
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation, either version 3 of the License, or (at your option) any later
11 version.
12
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 more details.
17
18 You should have received a copy of the GNU General Public License along with
19 this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #ifndef ___lw_expr_h_seen___
23 #define ___lw_expr_h_seen___
24
25 #include <stdio.h>
26
27 enum
28 {
29 lw_expr_type_oper, // operator term
30 lw_expr_type_int, // integer
31 lw_expr_type_var, // a "variable" (string for the name)
32 lw_expr_type_special // a "special" reference (user defined)
33 };
34
35 enum
36 {
37 lw_expr_oper_plus = 1, // addition
38 lw_expr_oper_minus, // subtraction
39 lw_expr_oper_times, // multiplication
40 lw_expr_oper_divide, // division
41 lw_expr_oper_mod, // modulus
42 lw_expr_oper_intdiv, // integer division
43 lw_expr_oper_bwand, // bitwise and
44 lw_expr_oper_bwor, // bitwise or
45 lw_expr_oper_bwxor, // bitwise xor
46 lw_expr_oper_and, // boolean and
47 lw_expr_oper_or, // boolean or
48 lw_expr_oper_neg, // unary negation, 2's complement
49 lw_expr_oper_com, // unary 1's complement
50 lw_expr_oper_none = 0
51 };
52
53 #ifdef ___lw_expr_c_seen___
54
55 typedef struct lw_expr_priv * lw_expr_t;
56
57 struct lw_expr_opers
58 {
59 lw_expr_t p;
60 struct lw_expr_opers *next;
61 };
62
63 struct lw_expr_priv
64 {
65 int type; // type of term
66 int value; // integer value
67 void *value2; // misc pointer value
68 struct lw_expr_opers *operands; // ptr to list of operands (for operators)
69 };
70
71 typedef lw_expr_t lw_expr_fn_t(int t, void *ptr, void *priv);
72 typedef lw_expr_t lw_expr_fn2_t(char *var, void *priv);
73 typedef lw_expr_t lw_expr_fn3_t(char **p, void *priv);
74 typedef int lw_expr_testfn_t(lw_expr_t e, void *priv);
75
76 #else /* def ___lw_expr_c_seen___ */
77
78 typedef void * lw_expr_t;
79
80 extern lw_expr_t lwexpr_create(void);
81 extern void lw_expr_destroy(lw_expr_t E);
82 extern lw_expr_t lw_expr_copy(lw_expr_t E);
83 extern void lw_expr_add_operand(lw_expr_t E, lw_expr_t O);
84 extern lw_expr_t lw_expr_build(int exprtype, ...);
85 extern char *lw_expr_print(lw_expr_t E);
86 extern int lw_expr_compare(lw_expr_t E1, lw_expr_t E2);
87 extern void lw_expr_simplify(lw_expr_t E, void *priv);
88
89 typedef lw_expr_t lw_expr_fn_t(int t, void *ptr, void *priv);
90 typedef lw_expr_t lw_expr_fn2_t(char *var, void *priv);
91 typedef lw_expr_t lw_expr_fn3_t(char **p, void *priv);
92
93 extern void lw_expr_set_special_handler(lw_expr_fn_t *fn);
94 extern void lw_expr_set_var_handler(lw_expr_fn2_t *fn);
95 extern void lw_expr_set_term_parser(lw_expr_fn3_t *fn);
96
97 extern lw_expr_t lw_expr_parse(char **p, void *priv);
98 extern int lw_expr_istype(lw_expr_t e, int t);
99 extern int lw_expr_intval(lw_expr_t e);
100 extern int lw_expr_specint(lw_expr_t e);
101 extern void *lw_expr_specptr(lw_expr_t e);
102 extern int lw_expr_whichop(lw_expr_t e);
103
104 extern int lw_expr_type(lw_expr_t e);
105
106 typedef int lw_expr_testfn_t(lw_expr_t e, void *priv);
107
108 // run a function on all terms in an expression; if the function
109 // returns non-zero for any term, return non-zero, else return
110 // zero
111 extern int lw_expr_testterms(lw_expr_t e, lw_expr_testfn_t *fn, void *priv);
112
113 #endif /* def ___lw_expr_c_seen___ */
114
115 #endif /* ___lw_expr_h_seen___ */