comparison src/expr.h @ 15:1f598d89b9b0

Started creating expression parser
author lost
date Thu, 23 Oct 2008 03:37:48 +0000
parents b28d7cb60779
children df0c4a46af8f
comparison
equal deleted inserted replaced
14:b28d7cb60779 15:1f598d89b9b0
17 You should have received a copy of the GNU General Public License along with 17 You should have received a copy of the GNU General Public License along with
18 this program. If not, see <http://www.gnu.org/licenses/>. 18 this program. If not, see <http://www.gnu.org/licenses/>.
19 */ 19 */
20 20
21 /* 21 /*
22 This file contains definitions associated with the expression evaluator used 22 Definitions for expression evaluator
23 by LWASM.
24
25 The core of the entire expression handler is the opaque type LWVAL, pointers
26 to which are passed around to keep track of values. A value may be a simple
27 integer or it could be a more complex expression linked by operators or it
28 could be a polynomial expression. Simple integers are merely a degenerate
29 case of polynomials.
30
31 The package understands the following operations:
32
33 addition
34 subtraction
35 multiplication
36 division
37 modulus
38 parentheses
39 unary negation
40 unary "positive"
41 bitwise and
42 bitwise or
43 bitwise not (1's complement)
44 bitwise exclusive or
45
46 Infix operators can be expressed as LWVAL <op> LWVAL. Thus, the order of
47 operations is only relevant when initially parsing the expression. The order
48 of evaluation is determined by what appears on either side of the <op> as
49 an LWVAL may be an expression.
50 */ 23 */
51 24
52 #ifndef __expr_h_seen__ 25 #ifndef __expr_h_seen__
53 #define __expr_h_seen__ 26 #define __expr_h_seen__
54 27
55 typedef struct lwval LWVAL; 28 #include "lwval.h"
56
57 union lwval_dt
58 {
59 int lwval_int; // integer type data
60 char *lwval_var; // pointer to variable name
61 };
62
63 enum
64 {
65 LWVAL_TYPE_NAN, // not a number
66 LWVAL_TYPE_INT, // integer
67 LWVAL_TYPE_VAR, // variable (symbol)
68 LWVAL_TYPE_EXPR, // expression
69 };
70
71 struct lwval
72 {
73 int lwval_type; // data type
74 union lwval_dt dt; // type specific stuff
75 };
76 29
77 #ifndef __expr_c_seen__ 30 #ifndef __expr_c_seen__
78 #define EE extern 31 #define __expr_E__ extern
79 #else 32 #else
80 #define EE 33 #define __expr_E__
81 #endif 34 #endif
82 35
83 EE LWVAL *lwval_construct_int(int value); 36 // parse an expression
84 EE void lwval_destroy(LWVAL *value); 37 __expr_E__ LWVAL *lwasm_parse_expr(char **ptr);
85 38
86 // operators - operate on v1 and v2 in order, return in v3 39 // attempt to evaluate/simplify expression
87 // if v3 is NULL, allocate new LWVAL and return it else just 40 __expr_E__ int lwasm_eval_expr(LWVAL *expr);
88 // return v3 with new value in it
89 EE LWVAL *lwval_add(LWVAL *v1, LWVAL *v2, LWVAL *v3);
90 EE LWVAL *lwval_sub(LWVAL *v1, LWVAL *v2, LWVAL *v3);
91 EE LWVAL *lwval_mul(LWVAL *v1, LWVAL *v2, LWVAL *v3);
92 EE LWVAL *lwval_div(LWVAL *v1, LWVAL *v2, LWVAL *v3);
93 EE LWVAL *lwval_mod(LWVAL *v1, LWVAL *v2, LWVAL *v3);
94 41
95 #endif //__expr_h_seen__ 42 #undef __expr_E__
43
44 #endif // __expr_h_seen__