-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexpr.c
283 lines (262 loc) · 7.9 KB
/
expr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* A simple recursive descent expression evaluator.
*
* Compile the test program like so:
* $ gcc -o expr -Wall -Werror -pedantic -O3 -DTEST expr.c
*
* This is free and unencumbered software released into the public domain.
* http://unlicense.org/
*/
#include <stdlib.h>
#include <ctype.h>
#include <setjmp.h>
#include <assert.h>
#include <stdint.h>
/*
* For integer based math, define FIZ_INTEGER_EXPR symbol in the whole project or
* use add MATH=int flag when running make. If not defined, double based math is used.
*
* Double based math supports numbers in range of +/- 1000000000000000000
* And up to 8 decimals precision in printing and comparasion
*/
#ifdef FIZ_INTEGER_EXPR
typedef int number;
inline static number operator_modulo(const number a, const number b) { return a % b; }
inline static char operator_equals(const number a, const number b) { return a == b; }
inline static char operator_gt(const number a, const number b) { return a == b; }
#else
#include <math.h>
typedef double number;
inline static number operator_modulo(const number a, const number b) { return fmod(a,b); }
inline static char operator_equals(const number a, const number b)
{
const double epsilon = 0.00000001;
const number fabsa = fabs(a);
const number fabsb = fabs(b);
number largest = 1.0;
if(fabsa > largest) largest = fabsa;
if(fabsb > largest) largest = fabsb;
return fabs(a - b) <= epsilon * largest;
}
inline static char operator_gt(const number a, const number b)
{
return a > b && !operator_equals(a, b);
}
#endif
struct expr_handler {
const char *str, *prev;
jmp_buf on_err;
const char **err;
};
static void do_error(struct expr_handler *eh, const char *msg) {
*(eh->err) = msg;
longjmp(eh->on_err, 1);
}
static char getnext(struct expr_handler *h) {
char c;
while(isspace((int)h->str[0])) h->str++;
h->prev = h->str;
c = h->str[0];
h->str++;
return c;
}
/* Macros that hopefully makes my intention clearer */
#define peeknext(h) (h->str[0])
#define gobblenext(h) (h->str++)
static void reset(struct expr_handler *h) {
/* We're only looking ahead one token, so don't
* use it in situations where this may happen: */
assert(h->prev != NULL);
h->str = h->prev;
h->prev = NULL;
}
/* Prototypes for the recursive descent parser's functions defined below */
static number or(struct expr_handler *h);
static number and(struct expr_handler *h);
static number not(struct expr_handler *h);
static number comp(struct expr_handler *h);
static number term(struct expr_handler *h);
static number factor(struct expr_handler *h);
static number unary(struct expr_handler *h);
static number atom(struct expr_handler *h);
/* Entry point for the expression evaluator.
* - 'str' contains the expression to be parsed.
* - 'err' should be a pointer to a (const char *). When the
* function returns, if the pointer pointed to by 'err' is NULL
* the expression was parsed successfully, otherwise *err will
* point to a string containing an error message.
*/
number expr(const char *str, const char **err) {
number n;
const char *dummy;
struct expr_handler eh;
if(err) {
*err = NULL;
eh.err = err;
} else
eh.err = &dummy;
eh.str = str;
eh.prev = NULL;
if(setjmp(eh.on_err))
return 0;
n = or(&eh);
if(getnext(&eh) != '\0')
do_error(&eh, "end of expression expected");
return n;
}
/*
* Recursive descent starts here.
*/
static number or(struct expr_handler *h) {
number n = and(h), r;
while(getnext(h) == '|' && peeknext(h) == '|') {
gobblenext(h);
/* Short-circuit caught me here:
* If you try to write this as n = n || and(h);
* and n is "true", then the and(h) won't get called,
* meaning the pointer h->str won't be moved along, causing
* problems down the road.
* I just hope a smart optimiser doesn't cause problems
* (so far I've checked GCC, and it doesn't), so test with
* something like "1||0" or "0&&1" to check for problems
*/
r = and(h);
n = n || r;
}
reset(h);
return n;
}
static number and(struct expr_handler *h) {
number n = not(h), r;
while(getnext(h) == '&' && peeknext(h) == '&') {
gobblenext(h);
/* potential short-circuit problem; see or() above */
r = not(h);
n = n && r;
}
reset(h);
return n;
}
static number not(struct expr_handler *h) {
if(getnext(h) == '!')
return !comp(h);
reset(h);
return comp(h);
}
static number comp(struct expr_handler *h) {
number n = term(h);
char c = getnext(h);
if(c == '=' || c == '>' || c == '<' || (c == '!' && peeknext(h) == '=')) {
if(h->str[0] == '=') {
gobblenext(h);
switch(c) {
case '=' : return operator_equals(n, term(h));
case '>' : return !operator_gt(term(h), n); //n >= term(h);
case '<' : return !operator_gt(n, term(h)); //n <= term(h);
case '!' : return !operator_equals(n, term(h));
}
} else {
switch(c) {
case '=' : return operator_equals(n, term(h));
case '>' : return operator_gt(n, term(h)); // n > term(h);
case '<' : return operator_gt(term(h), n); //n < term(h);
}
}
} else
reset(h);
return n;
}
static number term(struct expr_handler *h) {
number n = factor(h), c;
while((c=getnext(h)) == '+' || c == '-') {
if(c == '+')
n += factor(h);
else
n -= factor(h);
}
reset(h);
return n;
}
static number factor(struct expr_handler *h) {
number n = unary(h), c;
while((c=getnext(h)) == '*' || c == '/' || c == '%') {
number x = unary(h);
if(c == '*')
n *= x;
else {
if(x == 0) do_error(h, "divide by zero");
if(c == '/')
n /= x;
else
n = operator_modulo(n, x);
}
}
reset(h);
return n;
}
static number unary(struct expr_handler *h) {
number c = getnext(h);
if(c == '-' || c == '+') {
if(c == '-')
return -atom(h);
return atom(h);
}
reset(h);
return atom(h);
}
static number atom(struct expr_handler *h) {
if(getnext(h) == '(') {
number n = or(h);
if(getnext(h) != ')')
do_error(h, "missing ')'");
return n;
}
reset(h);
while(isspace((int)h->str[0])) h->str++;
if(!isdigit((int)h->str[0]))
do_error(h, "number expected");
uint64_t n = 0;
while(isdigit((int)h->str[0])) {
if(n >= 1000000000000000000L)
do_error(h, "number too large");
n = n * 10 + (h->str[0] - '0');
h->str++;
}
#ifndef FIZ_INTEGER_EXPR
if (peeknext(h) == '.')
{
h->str++;
if(!isdigit((int)h->str[0]))
do_error(h, "floating point part expected");
uint64_t m = 0, mbase = 1;
while(isdigit((int)h->str[0])) {
if(mbase >= 1000000000000000000L)
do_error(h, "number fractional part too large");
m = m * 10 + (h->str[0] - '0');
mbase *= 10;
h->str++;
}
return (number)n + (number)m / (number)mbase;
}
#endif
return (number)n;
}
/**********************************************************************
* Test program
**********************************************************************/
#ifdef TEST
#include <stdio.h>
int main(int argc, char *argv[]) {
int i;
number r;
const char *err;
for(i = 1; i < argc; i++) {
r = expr(argv[i], &err);
if(!err)
printf("%s = %d\n", argv[i], r);
else
fprintf(stderr, "error: %s: %s\n", argv[i], err);
}
return 0;
}
#endif /* TEST */