339
|
1 /*
|
|
2 expr.h
|
|
3 Copyright © 2008 William Astle
|
|
4
|
|
5 This file is part of LWASM.
|
|
6
|
|
7 LWASM is free software: you can redistribute it and/or modify it under the
|
|
8 terms of the GNU General Public License as published by the Free Software
|
|
9 Foundation, either version 3 of the License, or (at your option) any later
|
|
10 version.
|
|
11
|
|
12 This program is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
15 more details.
|
|
16
|
|
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/>.
|
|
19 */
|
|
20
|
|
21 /*
|
|
22 Definitions for expression evaluator
|
|
23 */
|
|
24
|
|
25 #ifndef __expr_h_seen__
|
|
26 #define __expr_h_seen__
|
|
27
|
|
28 #ifndef __expr_c_seen__
|
|
29 #define __expr_E__ extern
|
|
30 #else
|
|
31 #define __expr_E__
|
|
32 #endif
|
|
33
|
|
34 // term types
|
|
35 #define LWASM_TERM_NONE 0
|
|
36 #define LWASM_TERM_OPER 1 // an operator
|
|
37 #define LWASM_TERM_INT 2 // 32 bit signed integer
|
|
38 #define LWASM_TERM_SYM 3 // symbol reference
|
|
39 #define LWASM_TERM_SECBASE 4 // section base reference
|
|
40
|
|
41 // operator types
|
|
42 #define LWASM_OPER_NONE 0
|
|
43 #define LWASM_OPER_PLUS 1 // +
|
|
44 #define LWASM_OPER_MINUS 2 // -
|
|
45 #define LWASM_OPER_TIMES 3 // *
|
|
46 #define LWASM_OPER_DIVIDE 4 // /
|
|
47 #define LWASM_OPER_MOD 5 // %
|
|
48 #define LWASM_OPER_INTDIV 6 // \ (don't end line with \)
|
|
49 #define LWASM_OPER_BWAND 7 // bitwise AND
|
|
50 #define LWASM_OPER_BWOR 8 // bitwise OR
|
|
51 #define LWASM_OPER_BWXOR 9 // bitwise XOR
|
|
52 #define LWASM_OPER_AND 10 // boolean AND
|
|
53 #define LWASM_OPER_OR 11 // boolean OR
|
|
54 #define LWASM_OPER_NEG 12 // - unary negation (2's complement)
|
|
55 #define LWASM_OPER_COM 13 // ^ unary 1's complement
|
|
56
|
|
57
|
|
58 // term structure
|
|
59 typedef struct lwasm_expr_term_s
|
|
60 {
|
|
61 int term_type; // type of term (see above)
|
|
62 char *symbol; // name of a symbol
|
|
63 int value; // value of the term (int) or operator number (OPER)
|
|
64 } lwasm_expr_term_t;
|
|
65
|
|
66 // type for an expression evaluation stack
|
|
67 typedef struct lwasm_expr_stack_node_s lwasm_expr_stack_node_t;
|
|
68 struct lwasm_expr_stack_node_s
|
|
69 {
|
|
70 lwasm_expr_term_t *term;
|
|
71 lwasm_expr_stack_node_t *prev;
|
|
72 lwasm_expr_stack_node_t *next;
|
|
73 };
|
|
74
|
|
75 typedef struct lwasm_expr_stack_s
|
|
76 {
|
|
77 lwasm_expr_stack_node_t *head;
|
|
78 lwasm_expr_stack_node_t *tail;
|
|
79 } lwasm_expr_stack_t;
|
|
80
|
|
81 __expr_E__ void lwasm_expr_term_free(lwasm_expr_term_t *t);
|
|
82 __expr_E__ lwasm_expr_term_t *lwasm_expr_term_create_oper(int oper);
|
|
83 __expr_E__ lwasm_expr_term_t *lwasm_expr_term_create_sym(char *sym);
|
|
84 __expr_E__ lwasm_expr_term_t *lwasm_expr_term_create_int(int val);
|
|
85 __expr_E__ lwasm_expr_term_t *lwasm_expr_term_create_secbase(void);
|
|
86 __expr_E__ lwasm_expr_term_t *lwasm_expr_term_dup(lwasm_expr_term_t *t);
|
|
87
|
|
88 __expr_E__ void lwasm_expr_stack_free(lwasm_expr_stack_t *s);
|
|
89 __expr_E__ lwasm_expr_stack_t *lwasm_expr_stack_create(void);
|
|
90
|
|
91 __expr_E__ void lwasm_expr_stack_push(lwasm_expr_stack_t *s, lwasm_expr_term_t *t);
|
|
92 __expr_E__ lwasm_expr_term_t *lwasm_expr_stack_pop(lwasm_expr_stack_t *s);
|
|
93
|
|
94 /*
|
|
95 Evaluate an expression. The result is an lwasm_expr_stack_t pointer. If the
|
|
96 expression evaluates to a constant result, the stack will contain exactly one
|
|
97 value which will be a constant. Otherwise, the stack will contain the
|
|
98 expression with all operations that can be evaluated completely evaluated.
|
|
99 You must call lwasm_expr_stack_free() on the result when you are finished
|
|
100 with it.
|
|
101 */
|
|
102 __expr_E__ lwasm_expr_stack_t *lwasm_expr_eval(const char *inp, const char **outp, lwasm_expr_stack_t *(*sfunc)(char *sym, void *state), void *state);
|
|
103
|
|
104 // simplify expression
|
|
105 __expr_E__ int lwasm_expr_reval(lwasm_expr_stack_t *s, lwasm_expr_stack_t *(*sfunc)(char *sym, void *state), void *state);
|
|
106
|
|
107 // useful macros
|
|
108 // is the expression "simple" (one term)?
|
|
109 #define lwasm_expr_is_simple(s) ((s) -> head == (s) -> tail)
|
|
110
|
|
111 // is the expression constant?
|
|
112 #define lwasm_expr_is_constant(s) (lwasm_expr_is_simple(s) && (!((s) -> head) || (s) -> head -> term -> term_type == LWASM_TERM_INT))
|
|
113
|
|
114 // get the constant value of an expression or 0 if not constant
|
|
115 #define lwasm_expr_get_value(s) (lwasm_expr_is_constant(s) ? ((s) -> head ? (s) -> head -> term -> value : 0) : 0)
|
|
116
|
|
117 #undef __expr_E__
|
|
118
|
|
119 #endif // __expr_h_seen__
|