annotate src/expr.h @ 37:538e15927776

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