Mercurial > hg-old > index.cgi
comparison src/expr.h @ 18:218aabbc3b1a
First pass at expression evaluator complete
author | lost |
---|---|
date | Thu, 01 Jan 2009 23:55:18 +0000 |
parents | df0c4a46af8f |
children | ec0bf61a5502 |
comparison
equal
deleted
inserted
replaced
17:df0c4a46af8f | 18:218aabbc3b1a |
---|---|
42 #define LWASM_OPER_PLUS 1 // + | 42 #define LWASM_OPER_PLUS 1 // + |
43 #define LWASM_OPER_MINUS 2 // - | 43 #define LWASM_OPER_MINUS 2 // - |
44 #define LWASM_OPER_TIMES 3 // * | 44 #define LWASM_OPER_TIMES 3 // * |
45 #define LWASM_OPER_DIVIDE 4 // / | 45 #define LWASM_OPER_DIVIDE 4 // / |
46 #define LWASM_OPER_MOD 5 // % | 46 #define LWASM_OPER_MOD 5 // % |
47 #define LWASM_OPER_INTDIV 6 // \ | 47 #define LWASM_OPER_INTDIV 6 // \ (don't end line with \) |
48 #define LWASM_OPER_BWAND 7 // bitwise AND | 48 #define LWASM_OPER_BWAND 7 // bitwise AND |
49 #define LWASM_OPER_BWOR 8 // bitwise OR | 49 #define LWASM_OPER_BWOR 8 // bitwise OR |
50 #define LWASM_OPER_BWXOR 9 // bitwise XOR | 50 #define LWASM_OPER_BWXOR 9 // bitwise XOR |
51 #define LWASM_OPER_AND 10 // boolean AND | 51 #define LWASM_OPER_AND 10 // boolean AND |
52 #define LWASM_OPER_OR 11 // boolean OR | 52 #define LWASM_OPER_OR 11 // boolean OR |
87 __expr_E__ lwasm_expr_stack_t *lwasm_expr_stack_create(void); | 87 __expr_E__ lwasm_expr_stack_t *lwasm_expr_stack_create(void); |
88 | 88 |
89 __expr_E__ void lwasm_expr_stack_push(lwasm_expr_stack_t *s, lwasm_expr_term_t *t); | 89 __expr_E__ void lwasm_expr_stack_push(lwasm_expr_stack_t *s, lwasm_expr_term_t *t); |
90 __expr_E__ lwasm_expr_term_t *lwasm_expr_stack_pop(lwasm_expr_stack_t *s); | 90 __expr_E__ lwasm_expr_term_t *lwasm_expr_stack_pop(lwasm_expr_stack_t *s); |
91 | 91 |
92 /* | |
93 Evaluate an expression. The result is an lwasm_expr_stack_t pointer. If the | |
94 expression evaluates to a constant result, the stack will contain exactly one | |
95 value which will be a constant. Otherwise, the stack will contain the | |
96 expression with all operations that can be evaluated completely evaluated. | |
97 You must call lwasm_expr_stack_free() on the result when you are finished | |
98 with it. | |
99 */ | |
100 __expr_E__ lwasm_expr_stack_t *lwasm_expr_eval(const char *inp, const char **outp); | |
101 | |
102 // simplify expression | |
103 __expr_E__ int lwasm_expr_reval(lwasm_expr_stack_t *s); | |
104 | |
105 // useful macros | |
106 // is the expression "simple" (one term)? | |
107 #define lwasm_expr_is_simple(s) ((s) -> head == (s) -> tail) | |
108 | |
109 // is the expression constant? | |
110 #define lwasm_expr_is_constant(s) (lwasm_expr_is_simple(s) && (s) -> head -> term -> term_type == LWASM_TERM_INT) | |
111 | |
112 // get the constant value of an expression or 0 if not constant | |
113 #define lwasm_expr_get_value(s) (lwasm_expr_is_constant(s) ? (s) -> head -> term -> value : 0) | |
114 | |
92 #undef __expr_E__ | 115 #undef __expr_E__ |
93 | 116 |
94 #endif // __expr_h_seen__ | 117 #endif // __expr_h_seen__ |