annotate src/expr.c @ 23:ec0bf61a5502

Added ^ (bitwise complement)
author lost
date Fri, 02 Jan 2009 01:32:07 +0000
parents 218aabbc3b1a
children 538e15927776
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.c
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 /*
18
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
22 This file contains the actual expression evaluator
13
05d4115b4860 Started work on new expression evaluator system and major code re-work for next release
lost
parents:
diff changeset
23 */
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
24
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
25 #define __expr_c_seen__
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
26
18
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
27 #include <ctype.h>
17
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
28 #include <stdio.h>
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
29 #include <stdlib.h>
18
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
30 #include <string.h>
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
31
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
32 #include "expr.h"
17
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
33 #include "util.h"
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
34
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
35 lwasm_expr_stack_t *lwasm_expr_stack_create(void)
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
36 {
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
37 lwasm_expr_stack_t *s;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
38
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
39 s = lwasm_alloc(sizeof(lwasm_expr_stack_t));
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
40 s -> head = NULL;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
41 s -> tail = NULL;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
42 return s;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
43 }
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
44
17
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
45 void lwasm_expr_stack_free(lwasm_expr_stack_t *s)
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
46 {
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
47 while (s -> head)
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
48 {
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
49 s -> tail = s -> head;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
50 s -> head = s -> head -> next;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
51 lwasm_expr_term_free(s -> tail -> term);
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
52 lwasm_free(s -> tail);
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
53 }
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
54 lwasm_free(s);
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 void lwasm_expr_term_free(lwasm_expr_term_t *t)
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
58 {
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
59 if (t)
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
60 {
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
61 if (t -> symbol)
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
62 lwasm_free(t -> symbol);
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
63 lwasm_free(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 }
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
66
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
67 lwasm_expr_term_t *lwasm_expr_term_create_oper(int oper)
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 *t;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
70
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
71 t = lwasm_alloc(sizeof(lwasm_expr_term_t));
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
72 t -> term_type = LWASM_TERM_OPER;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
73 t -> value = oper;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
74 return t;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
75 }
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
76
17
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
77 lwasm_expr_term_t *lwasm_expr_term_create_int(int val)
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
78 {
17
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
79 lwasm_expr_term_t *t;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
80
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
81 t = lwasm_alloc(sizeof(lwasm_expr_term_t));
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
82 t -> term_type = LWASM_TERM_INT;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
83 t -> value = val;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
84 return 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
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
87 lwasm_expr_term_t *lwasm_expr_term_create_sym(char *sym)
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
88 {
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
89 lwasm_expr_term_t *t;
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
90
17
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
91 t = lwasm_alloc(sizeof(lwasm_expr_term_t));
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
92 t -> term_type = LWASM_TERM_SYM;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
93 t -> symbol = lwasm_strdup(sym);
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
94 return t;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
95 }
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
96
17
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
97 lwasm_expr_term_t *lwasm_expr_term_dup(lwasm_expr_term_t *t)
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
98 {
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
99 switch (t -> term_type)
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
100 {
17
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
101 case LWASM_TERM_INT:
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
102 return lwasm_expr_term_create_int(t -> value);
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
103
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
104 case LWASM_TERM_OPER:
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
105 return lwasm_expr_term_create_oper(t -> value);
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
106
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
107 case LWASM_TERM_SYM:
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
108 return lwasm_expr_term_create_sym(t -> symbol);
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
109
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
110 default:
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
111 fprintf(stderr, "lwasm_expr_term_dup(): invalid term type %d\n", t -> term_type);
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
112 exit(1);
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
113 }
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
114 // can't get here
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
115 }
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
116
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
117 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
118 {
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
119 lwasm_expr_stack_node_t *n;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
120
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
121 if (!s)
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
122 {
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
123 fprintf(stderr, "lwasm_expr_stack_push(): invalid stack pointer\n");
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
124 exit(1);
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
125 }
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
126
17
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
127 n = lwasm_alloc(sizeof(lwasm_expr_stack_node_t));
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
128 n -> next = NULL;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
129 n -> prev = s -> tail;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
130 n -> term = lwasm_expr_term_dup(t);
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
131
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
132 if (s -> head)
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
133 {
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
134 s -> tail -> next = n;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
135 s -> tail = n;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
136 }
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
137 else
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
138 {
17
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
139 s -> head = n;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
140 s -> tail = n;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
141 }
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
142 }
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
143
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
144 lwasm_expr_term_t *lwasm_expr_stack_pop(lwasm_expr_stack_t *s)
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
145 {
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
146 lwasm_expr_term_t *t;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
147 lwasm_expr_stack_node_t *n;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
148
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
149 if (!(s -> tail))
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
150 return NULL;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
151
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
152 n = s -> tail;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
153 s -> tail = n -> prev;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
154 if (!(n -> prev))
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
155 {
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
156 s -> head = NULL;
15
1f598d89b9b0 Started creating expression parser
lost
parents: 14
diff changeset
157 }
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
158
17
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
159 t = n -> term;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
160 n -> term = NULL;
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
161
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
162 lwasm_free(n);
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
163
df0c4a46af8f Started adding expression handling infrastructure
lost
parents: 15
diff changeset
164 return t;
14
b28d7cb60779 checkpoint
lost
parents: 13
diff changeset
165 }
18
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
166
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
167 // the following two functions are co-routines which actually parse
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
168 // an infix expression onto the expression stack, each returns -1
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
169 // if an error is encountered
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
170
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
171 /*
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
172 parse a term and push it onto the stack
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
173
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
174 this function handles unary prefix operators (-, +, .not., .com.)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
175 as well as ()
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
176 */
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
177 int lwasm_expr_parse_term(lwasm_expr_stack_t *s, const char **p)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
178 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
179 lwasm_expr_term_t *t;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
180
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
181 eval_next:
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
182 if (**p == '(')
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
183 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
184 (*p)++;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
185 lwasm_expr_parse_expr(s, p, 0);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
186 if (**p != ')')
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
187 return -1;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
188 (*p)++;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
189 return 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
190 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
191
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
192 if (**p == '+')
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
193 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
194 (*p)++;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
195 goto eval_next;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
196 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
197
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
198 if (**p == '-')
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
199 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
200 // parse expression following "-"
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
201 (*p)++;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
202 if (lwasm_expr_parse_expr(s, p, 200) < 0)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
203 return -1;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
204 t = lwasm_expr_term_create_oper(LWASM_OPER_NEG);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
205 lwasm_expr_stack_push(s, t);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
206 lwasm_expr_term_free(t);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
207 return 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
208 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
209
23
ec0bf61a5502 Added ^ (bitwise complement)
lost
parents: 18
diff changeset
210 if (**p == '^')
ec0bf61a5502 Added ^ (bitwise complement)
lost
parents: 18
diff changeset
211 {
ec0bf61a5502 Added ^ (bitwise complement)
lost
parents: 18
diff changeset
212 // parse expression following "^"
ec0bf61a5502 Added ^ (bitwise complement)
lost
parents: 18
diff changeset
213 (*p)++;
ec0bf61a5502 Added ^ (bitwise complement)
lost
parents: 18
diff changeset
214 if (lwasm_expr_parse_expr(s, p, 200) < 0)
ec0bf61a5502 Added ^ (bitwise complement)
lost
parents: 18
diff changeset
215 return -1;
ec0bf61a5502 Added ^ (bitwise complement)
lost
parents: 18
diff changeset
216 t = lwasm_expr_term_create_oper(LWASM_OPER_COM);
ec0bf61a5502 Added ^ (bitwise complement)
lost
parents: 18
diff changeset
217 lwasm_expr_stack_push(s, t);
ec0bf61a5502 Added ^ (bitwise complement)
lost
parents: 18
diff changeset
218 lwasm_expr_term_free(t);
ec0bf61a5502 Added ^ (bitwise complement)
lost
parents: 18
diff changeset
219 return 0;
ec0bf61a5502 Added ^ (bitwise complement)
lost
parents: 18
diff changeset
220 }
ec0bf61a5502 Added ^ (bitwise complement)
lost
parents: 18
diff changeset
221
18
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
222 /*
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
223 we have an actual term here so evaluate it
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
224
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
225 it could be one of the following:
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
226
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
227 1. a decimal constant
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
228 2. a hexadecimal constant
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
229 3. an octal constant
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
230 4. a binary constant
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
231 5. a symbol reference
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
232 6. the "current" instruction address (*)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
233 7. the "current" data address (.)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
234 8. a "back reference" (<)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
235 9. a "forward reference" (>)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
236
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
237 items 6 through 9 are stored as symbol references
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
238
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
239 (a . followed by a . or a alpha char or number is a symbol)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
240 */
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
241 if (**p == '*'
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
242 || (
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
243 **p == '.'
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
244 && (*p)[1] != '.'
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
245 && !((*p)[1] >= 'A' && (*p)[1] <= 'Z')
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
246 && !((*p)[1] >= 'a' && (*p)[1] <= 'z')
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
247 && !((*p)[1] >= '0' && (*p)[1] <= '9')
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
248 )
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
249 || **p == '<'
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
250 || **p == '>')
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
251 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
252 char tstr[2];
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
253 tstr[0] = **p;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
254 tstr[1] = '\0';
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
255 t = lwasm_expr_term_create_sym(tstr);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
256 lwasm_expr_stack_push(s, t);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
257 lwasm_expr_term_free(t);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
258 (*p)++;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
259 return 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
260 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
261
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
262 /*
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
263 - a symbol will be a string of characters introduced by a letter, ".",
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
264 "_" but NOT a number
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
265 - a decimal constant will consist of only digits, optionally prefixed
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
266 with "&"
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
267 - a binary constant will consist of only 0s and 1s either prefixed with %
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
268 or suffixed with "B"
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
269 - a hex constant will consist of 0-9A-F either prefixed with $ or
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
270 suffixed with "H"; a hex number starting with A-F must be prefixed
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
271 with $ or start with 0 and end with H
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
272 - an octal constant will consist of 0-7 either prefixed with @ or
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
273 suffixed with "O" or "Q"
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
274 - an ascii constant will be a single character prefixed with a '
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
275 - a double ascii constant will be two characters prefixed with a "
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
276
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
277 */
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
278 if (**p == '"')
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
279 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
280 // double ascii constant
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
281 int val;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
282 (*p)++;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
283 if (!**p)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
284 return -1;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
285 if (!*((*p)+1))
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
286 return -1;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
287 val = **p << 8 | *((*p) + 1);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
288 (*p) += 2;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
289 t = lwasm_expr_term_create_int(val);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
290 lwasm_expr_stack_push(s, t);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
291 lwasm_expr_term_free(t);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
292 return 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
293 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
294 else if (**p == '\'')
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
295 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
296 // single ascii constant
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
297 int val;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
298 (*p)++;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
299 if (!**p)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
300 return -1;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
301 val = **p;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
302 (*p)++;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
303 t = lwasm_expr_term_create_int(val);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
304 lwasm_expr_stack_push(s, t);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
305 lwasm_expr_term_free(t);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
306 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
307 else if (**p == '&')
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
308 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
309 // decimal constant
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
310 int val = 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
311
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
312 (*p)++;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
313 while (strchr("0123456789", **p))
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
314 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
315 val = val * 10 + (**p - '0');
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
316 (*p)++;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
317 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
318 t = lwasm_expr_term_create_int(val);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
319 lwasm_expr_stack_push(s, t);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
320 lwasm_expr_term_free(t);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
321 return 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
322 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
323 else if (**p == '%')
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
324 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
325 // binary constant
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
326 int val = 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
327
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
328 (*p)++;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
329 while (**p == '0' || **p == '1')
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
330 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
331 val = val * 2 + (**p - '0');
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
332 (*p)++;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
333 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
334 t = lwasm_expr_term_create_int(val);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
335 lwasm_expr_stack_push(s, t);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
336 lwasm_expr_term_free(t);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
337 return 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
338 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
339 else if (**p == '$')
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
340 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
341 // hexadecimal constant
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
342 int val = 0, val2;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
343
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
344 (*p)++;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
345 while (strchr("0123456789ABCDEFabcdef", **p))
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
346 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
347 val2 = toupper(**p) - '0';
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
348 if (val2 > 9)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
349 val2 -= 7;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
350 val = val * 16 + val2;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
351 (*p)++;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
352 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
353 t = lwasm_expr_term_create_int(val);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
354 lwasm_expr_stack_push(s, t);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
355 lwasm_expr_term_free(t);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
356 return 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
357 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
358 else if (**p == '@')
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
359 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
360 // octal constant
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
361 int val = 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
362
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
363 (*p)++;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
364 while (strchr("01234567", **p))
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
365 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
366 val = val * 8 + (**p - '0');
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
367 (*p)++;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
368 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
369 t = lwasm_expr_term_create_int(val);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
370 lwasm_expr_stack_push(s, t);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
371 lwasm_expr_term_free(t);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
372 return 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
373 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
374
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
375 // symbol or bare decimal or suffix identified constant here
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
376 // all numbers will start with a digit at this point
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
377 if (**p < '0' || **p > '9')
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
378 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
379 int l = 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
380 char *sb;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
381
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
382 // evaluate a symbol here
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
383 static const char *symchars = "_.$@\\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
384 while (strchr(symchars, (*p)[l]))
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
385 l++;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
386
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
387 if (l == 0)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
388 return -1;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
389
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
390 sb = lwasm_alloc(l + 1);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
391 sb[l] = '\0';
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
392 memcpy(sb, *p, l);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
393 t = lwasm_expr_term_create_sym(sb);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
394 lwasm_expr_stack_push(s, t);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
395 lwasm_expr_term_free(t);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
396 lwasm_free(sb);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
397 (*p) += l;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
398 return 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
399 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
400
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
401 if (!**p)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
402 return -1;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
403
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
404 // evaluate a suffix based constant
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
405 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
406 int decval = 0, binval = 0, hexval = 0, octval = 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
407 int valtype = 15; // 1 = bin, 2 = oct, 4 = dec, 8 = hex
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
408 int bindone = 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
409 int val;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
410 int dval;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
411
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
412 while (1)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
413 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
414 if (!**p || !strchr("0123456789ABCDEFabcdefqhoQHO", **p))
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
415 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
416 // we can legally have bin or decimal here
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
417 if (bindone)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
418 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
419 // we just finished a binary value
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
420 val = binval;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
421 break;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
422 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
423 else if (valtype & 4)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
424 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
425 // otherwise we must be decimal (if we're still allowed one)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
426 val = decval;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
427 break;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
428 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
429 else
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
430 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
431 // bad value
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
432 return -1;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
433 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
434 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
435
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
436 dval = toupper(**p);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
437 (*p)++;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
438
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
439 if (bindone)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
440 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
441 // any characters past "B" means it is not binary
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
442 bindone = 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
443 valtype &= 14;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
444 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
445
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
446 switch (dval)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
447 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
448 case 'Q':
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
449 case 'O':
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
450 if (valtype & 2)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
451 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
452 val = octval;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
453 valtype = -1;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
454 break;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
455 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
456 else
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
457 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
458 // not a valid octal value
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
459 return -1;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
460 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
461 /* can't get here */
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
462
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
463 case 'H':
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
464 if (valtype & 8)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
465 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
466 val = hexval;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
467 valtype = -1;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
468 break;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
469 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
470 else
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
471 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
472 // not a valid hex number
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
473 return -1;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
474 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
475 /* can't get here */
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
476
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
477 case 'B':
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
478 // this is a bit of a sticky one since B is a legit hex
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
479 // digit so this may or may not be the end of the number
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
480 // so we fall through to the digit case
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
481
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
482 if (valtype & 1)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
483 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
484 // could still be binary
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
485 bindone = 1;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
486 valtype = 9; // hex and binary
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
487 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
488 /* fall through intentional */
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
489
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
490 default:
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
491 // digit
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
492 dval -= '0';
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
493 if (dval > 9)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
494 dval -= 7;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
495
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
496 if (valtype & 8)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
497 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
498 hexval = hexval * 16 + dval;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
499 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
500 if (valtype & 4)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
501 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
502 if (dval > 9)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
503 valtype &= 11;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
504 else
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
505 decval = decval * 10 + dval;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
506 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
507 if (valtype & 2)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
508 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
509 if (dval > 7)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
510 valtype &= 13;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
511 else
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
512 octval = octval * 8 + dval;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
513 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
514 if (valtype & 1)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
515 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
516 if (dval > 1)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
517 valtype &= 14;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
518 else
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
519 binval = binval * 2 + dval;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
520 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
521 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
522 // break out if we have a return value
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
523 if (valtype = -1)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
524 break;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
525 // return if no more valid possibilities!
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
526 if (valtype == 0)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
527 return -1;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
528 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
529
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
530 // we get here when we have a value to return
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
531 t = lwasm_expr_term_create_int(val);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
532 lwasm_expr_stack_push(s, t);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
533 lwasm_expr_term_free(t);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
534 return 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
535 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
536 /* can't get here */
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
537 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
538
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
539 // parse an expression and push the result onto the stack
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
540 // if an operator of lower precedence than the value of "prec" is found,
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
541 int lwasm_expr_parse_expr(lwasm_expr_stack_t *s, const char **p, int prec)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
542 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
543 static const struct operinfo
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
544 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
545 int opernum;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
546 char *operstr;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
547 int operprec;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
548 } operators[] =
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
549 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
550 { LWASM_OPER_PLUS, "+", 100 },
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
551 { LWASM_OPER_MINUS, "-", 100 },
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
552 { LWASM_OPER_TIMES, "*", 150 },
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
553 { LWASM_OPER_DIVIDE, "/", 150 },
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
554 { LWASM_OPER_MOD, "%", 150 },
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
555 { LWASM_OPER_INTDIV, "\\", 150 },
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
556
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
557 { LWASM_OPER_NONE, "", 0 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
558 };
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
559 int opern, i;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
560 lwasm_expr_term_t *operterm;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
561
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
562 // return if we are at the end of the expression or a subexpression
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
563 if (!**p || isspace(**p) || **p == ')')
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
564 return 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
565
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
566 eval_next:
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
567 if (lwasm_expr_parse_term(s, p) < 0)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
568 return -1;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
569
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
570 // expecting an operator here
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
571 for (opern = 0; operators[opern].opernum != LWASM_OPER_NONE; opern++)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
572 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
573 for (i = 0; (*p)[i] && operators[opern].operstr[i] && (*p[i] == operators[opern].operstr[i]); i++)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
574 /* do nothing */ ;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
575 if (operators[opern].operstr[i] == '\0')
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
576 break;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
577 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
578 if (operators[opern].opernum == LWASM_OPER_NONE)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
579 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
580 // unrecognized operator
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
581 return -1;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
582 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
583
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
584 // the operator number in question is in opern; i is the length of the
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
585 // operator string
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
586
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
587 // logic:
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
588 // if the precedence of this operation is <= to the "prec" flag,
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
589 // we simply return without advancing the input pointer; the operator
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
590 // will be evaluated again in the enclosing function call
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
591 if (operators[opern].operprec <= prec)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
592 return 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
593
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
594 // logic:
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
595 // we have a higher precedence operator here so we will advance the
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
596 // input pointer to the next term and let the expression evaluator
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
597 // loose on it after which time we will push our operator onto the
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
598 // stack and then go on with the expression evaluation
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
599 (*p) += i; // advance input pointer
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
600
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
601 // evaluate next expression(s) of higher precedence
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
602 if (lwasm_expr_parse_expr(s, p, operators[opern].operprec) < 0)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
603 return -1;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
604
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
605 operterm = lwasm_expr_term_create_oper(operators[opern].opernum);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
606 lwasm_expr_stack_push(s, operterm);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
607 lwasm_expr_term_free(operterm);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
608
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
609 // return if we are at the end of the expression or a subexpression
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
610 if (!**p || isspace(**p) || **p == ')')
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
611 return 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
612
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
613 // continue evaluating
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
614 goto eval_next;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
615 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
616
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
617 /*
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
618 actually evaluate an expression
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
619
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
620 This happens in two stages. The first stage merely parses the expression into
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
621 a lwasm_expr_stack_t * which is then evaluated as much as possible before the
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
622 result is returned.
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
623
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
624 Returns NULL on a parse error or otherwise invalid expression. *outp will
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
625 contain the pointer to the next character after the expression if and only
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
626 if there is no error. In the case of an error, *outp is undefined.
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
627 */
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
628 lwasm_expr_stack_t *lwasm_expr_eval(const char *inp, const char **outp)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
629 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
630 lwasm_expr_stack_t *s;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
631 const char *p;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
632
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
633 // actually parse the expression
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
634 p = inp;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
635 s = lwasm_expr_stack_create();
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
636 if (lwasm_expr_parse_expr(s, &p, 0) < 0)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
637 goto cleanup_error;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
638
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
639 // save end of expression
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
640 if (outp)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
641 (*outp) = p;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
642
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
643 // return potentially partial expression
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
644 if (lwasm_expr_reval(s) < 0)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
645 goto cleanup_error;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
646
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
647 return s;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
648
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
649 cleanup_error:
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
650 lwasm_expr_stack_free(s);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
651 return NULL;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
652 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
653
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
654 /*
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
655 take an expression stack s and scan for operations that can be completed
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
656
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
657 return -1 on error, 0 on no error
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
658
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
659 possible errors are: division by zero or unknown operator
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
660
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
661 theory of operation:
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
662
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
663 scan the stack for an operator which has two constants preceding it (binary)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
664 or 1 constant preceding it (unary) and if found, perform the calculation
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
665 and replace the operator and its operands with the result
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
666
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
667 repeat the scan until no futher simplications are found or if there are no
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
668 further operators or only a single term remains
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
669
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
670 */
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
671 int lwasm_expr_reval(lwasm_expr_stack_t *s)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
672 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
673 lwasm_expr_stack_node_t *n;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
674
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
675 next_iter:
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
676 // a single term
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
677 if (s -> head == s -> tail)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
678 return 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
679
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
680 // search for an operator
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
681 for (n = s -> head; n; n = n -> next)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
682 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
683 if (n -> term -> term_type == LWASM_TERM_OPER)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
684 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
685 if (n -> term -> value == LWASM_OPER_NEG
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
686 || n -> term -> value == LWASM_OPER_COM
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
687 )
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
688 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
689 // unary operator
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
690 if (n -> prev && n -> prev -> term -> term_type == LWASM_TERM_INT)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
691 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
692 // a unary operator we can resolve
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
693 // we do the op then remove the term "n" is pointing at
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
694 if (n -> term -> value == LWASM_OPER_NEG)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
695 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
696 n -> prev -> term -> value = -(n -> prev -> term -> value);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
697 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
698 else if (n -> term -> value == LWASM_OPER_COM)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
699 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
700 n -> prev -> term -> value = ~(n -> prev -> term -> value);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
701 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
702 n -> prev -> next = n -> next;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
703 if (n -> next)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
704 n -> next -> prev = n -> prev;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
705 else
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
706 s -> tail = n -> prev;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
707
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
708 lwasm_expr_term_free(n -> term);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
709 lwasm_free(n);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
710 break;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
711 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
712 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
713 else
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
714 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
715 // binary operator
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
716 if (n -> prev && n -> prev -> prev && n -> prev -> term -> term_type == LWASM_TERM_INT && n -> prev -> prev -> term -> term_type == LWASM_TERM_INT)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
717 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
718 // a binary operator we can resolve
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
719 switch (n -> term -> value)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
720 {
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
721 case LWASM_OPER_PLUS:
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
722 n -> prev -> prev -> term -> value += n -> prev -> term -> value;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
723 break;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
724
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
725 case LWASM_OPER_MINUS:
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
726 n -> prev -> prev -> term -> value -= n -> prev -> term -> value;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
727 break;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
728
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
729 case LWASM_OPER_TIMES:
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
730 n -> prev -> prev -> term -> value *= n -> prev -> term -> value;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
731 break;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
732
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
733 case LWASM_OPER_DIVIDE:
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
734 if (n -> prev -> term -> value == 0)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
735 return -1;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
736 n -> prev -> prev -> term -> value /= n -> prev -> term -> value;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
737 break;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
738
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
739 case LWASM_OPER_MOD:
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
740 if (n -> prev -> term -> value == 0)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
741 return -1;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
742 n -> prev -> prev -> term -> value %= n -> prev -> term -> value;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
743 break;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
744
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
745 case LWASM_OPER_INTDIV:
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
746 if (n -> prev -> term -> value == 0)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
747 return -1;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
748 n -> prev -> prev -> term -> value /= n -> prev -> term -> value;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
749 break;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
750
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
751 case LWASM_OPER_BWAND:
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
752 n -> prev -> prev -> term -> value &= n -> prev -> term -> value;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
753 break;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
754
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
755 case LWASM_OPER_BWOR:
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
756 n -> prev -> prev -> term -> value |= n -> prev -> term -> value;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
757 break;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
758
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
759 case LWASM_OPER_BWXOR:
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
760 n -> prev -> prev -> term -> value ^= n -> prev -> term -> value;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
761 break;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
762
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
763 case LWASM_OPER_AND:
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
764 n -> prev -> prev -> term -> value = (n -> prev -> term -> value && n -> prev -> prev -> term -> value) ? 1 : 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
765 break;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
766
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
767 case LWASM_OPER_OR:
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
768 n -> prev -> prev -> term -> value = (n -> prev -> term -> value || n -> prev -> prev -> term -> value) ? 1 : 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
769 break;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
770
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
771 default:
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
772 // return error if unknown operator!
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
773 return -1;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
774 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
775
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
776 // now remove the two unneeded entries from the stack
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
777 n -> prev -> prev -> next = n -> next;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
778 if (n -> next)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
779 n -> next -> prev = n -> prev -> prev;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
780 else
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
781 s -> tail = n -> prev -> prev;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
782
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
783 lwasm_expr_term_free(n -> term);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
784 lwasm_expr_term_free(n -> prev -> term);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
785 lwasm_free(n -> prev);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
786 lwasm_free(n);
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
787 break;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
788 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
789 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
790 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
791 }
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
792 // note for the terminally confused about dynamic memory and pointers:
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
793 // n will not be NULL even after the lwasm_free calls above so
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
794 // this test will still work (n will be a dangling pointer)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
795 // (n will only be NULL if we didn't find any operators to simplify)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
796 if (n)
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
797 goto next_iter;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
798
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
799 return 0;
218aabbc3b1a First pass at expression evaluator complete
lost
parents: 17
diff changeset
800 }