annotate src/expr.h @ 315:8550ff69fcc9

Tinkering with autotools
author lost
date Tue, 27 Jan 2009 05:50:02 +0000
parents 48945dac8178
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
298
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
1 /*
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
2 expr.h
300
48945dac8178 Fixed copyright year
lost
parents: 298
diff changeset
3 Copyright © 2009 William Astle
298
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
4
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
5 This file is part of LWLINK.
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
6
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
7 LWLINK is free software: you can redistribute it and/or modify it under the
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
10 version.
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
11
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
15 more details.
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
16
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
19 */
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
20
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
21 /*
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
22 Definitions for expression evaluator
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
23 */
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
24
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
25 #ifndef __expr_h_seen__
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
26 #define __expr_h_seen__
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
27
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
28 #ifndef __expr_c_seen__
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
29 #define __expr_E__ extern
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
30 #else
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
31 #define __expr_E__
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
32 #endif
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
33
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
34 // term types
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
35 #define LW_TERM_NONE 0
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
36 #define LW_TERM_OPER 1 // an operator
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
37 #define LW_TERM_INT 2 // 32 bit signed integer
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
38 #define LW_TERM_SYM 3 // symbol reference
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
39
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
40 // operator types
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
41 #define LW_OPER_NONE 0
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
42 #define LW_OPER_PLUS 1 // +
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
43 #define LW_OPER_MINUS 2 // -
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
44 #define LW_OPER_TIMES 3 // *
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
45 #define LW_OPER_DIVIDE 4 // /
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
46 #define LW_OPER_MOD 5 // %
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
47 #define LW_OPER_INTDIV 6 // \ (don't end line with \)
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
48 #define LW_OPER_BWAND 7 // bitwise AND
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
49 #define LW_OPER_BWOR 8 // bitwise OR
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
50 #define LW_OPER_BWXOR 9 // bitwise XOR
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
51 #define LW_OPER_AND 10 // boolean AND
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
52 #define LW_OPER_OR 11 // boolean OR
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
53 #define LW_OPER_NEG 12 // - unary negation (2's complement)
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
54 #define LW_OPER_COM 13 // ^ unary 1's complement
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
55
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
56
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
57 // term structure
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
58 typedef struct lw_expr_term_s
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
59 {
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
60 int term_type; // type of term (see above)
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
61 char *symbol; // name of a symbol
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
62 int value; // value of the term (int) or operator number (OPER)
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
63 } lw_expr_term_t;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
64
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
65 // type for an expression evaluation stack
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
66 typedef struct lw_expr_stack_node_s lw_expr_stack_node_t;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
67 struct lw_expr_stack_node_s
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
68 {
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
69 lw_expr_term_t *term;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
70 lw_expr_stack_node_t *prev;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
71 lw_expr_stack_node_t *next;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
72 };
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
73
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
74 typedef struct lw_expr_stack_s
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
75 {
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
76 lw_expr_stack_node_t *head;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
77 lw_expr_stack_node_t *tail;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
78 } lw_expr_stack_t;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
79
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
80 __expr_E__ void lw_expr_term_free(lw_expr_term_t *t);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
81 __expr_E__ lw_expr_term_t *lw_expr_term_create_oper(int oper);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
82 __expr_E__ lw_expr_term_t *lw_expr_term_create_sym(char *sym, int symtype);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
83 __expr_E__ lw_expr_term_t *lw_expr_term_create_int(int val);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
84 __expr_E__ lw_expr_term_t *lw_expr_term_dup(lw_expr_term_t *t);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
85
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
86 __expr_E__ void lw_expr_stack_free(lw_expr_stack_t *s);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
87 __expr_E__ lw_expr_stack_t *lw_expr_stack_create(void);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
88
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
89 __expr_E__ void lw_expr_stack_push(lw_expr_stack_t *s, lw_expr_term_t *t);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
90 __expr_E__ lw_expr_term_t *lw_expr_stack_pop(lw_expr_stack_t *s);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
91
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
92 // simplify expression
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
93 __expr_E__ int lw_expr_reval(lw_expr_stack_t *s, lw_expr_stack_t *(*sfunc)(char *sym, int symtype, void *state), void *state);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
94
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
95 // useful macros
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
96 // is the expression "simple" (one term)?
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
97 #define lw_expr_is_simple(s) ((s) -> head == (s) -> tail)
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
98
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
99 // is the expression constant?
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
100 #define lw_expr_is_constant(s) (lw_expr_is_simple(s) && (!((s) -> head) || (s) -> head -> term -> term_type == LW_TERM_INT))
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
101
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
102 // get the constant value of an expression or 0 if not constant
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
103 #define lw_expr_get_value(s) (lw_expr_is_constant(s) ? ((s) -> head ? (s) -> head -> term -> value : 0) : 0)
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
104
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
105 #undef __expr_E__
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
106
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
107 #endif // __expr_h_seen__