-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patheval.c
575 lines (518 loc) · 12.1 KB
/
eval.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
* Mathematical expression evaluator.
*
* See eval.h for more info
*
* This is free and unencumbered software released into the public domain.
* http://unlicense.org/
*/
/*
* You can use the following awk script to obtain the syntax of the parser in
* [my own] EBNF form:
*
* awk '/\*#/{a=index($0,"#")+2;print(substr($0,a))}' eval.c
*
* The following awk script will print a list of all the functions and constants
* defined in the script from the comments.
*
* awk '/\*\$/{a=index($0,"$")+2;b=index($0,"* /");print(substr($0,a,b-a))}' eval.c
*
* NOTE: there's a space in the 'index($0,"* /")' above for the sake of this comment
*/
#include <stdlib.h>
#include <math.h> /* remember to compile with -lm */
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#include <assert.h>
#include "eval.h"
/* Special tokens used by the lexer function lex()
* they've been chosen as non-printable characters
* so that printable characters can be used for other
* purposes
*/
#define TOK_END 0 /* end of text */
#define TOK_INI 1 /* Initial state */
#define TOK_ID 2 /* identifier */
#define TOK_NUM 3 /* number */
/* Types of errors */
#define ERR_MEMORY 1
#define ERR_LEXER 2
#define ERR_LONGID 3
#define ERR_VALUE 4
#define ERR_BRACKET 5
#define ERR_FUNC 6
#define ERR_ARGS 7
#define ERR_CONST 8
/* Other definitions */
#define MAX_ID_LEN 11 /* Max length of an identifier */
#define OPERATORS "+-*/%(),^" /* Valid operators */
#define EVAL_PI 3.141592654
#define EVAL_E 2.718281828
/* Internal structure for the parser/evaluator */
struct eval {
jmp_buf j; /* For error handling */
const char *p; /* Position in the text being parsed */
double *st; /* Stack */
int st_size; /* Stack size */
int sp; /* Stack pointer */
/* The current and next tokens identified by the lexer */
struct {
int type; /* Type of the token */
double n_val; /* Numeric value of the previous lexed token */
char s_val[MAX_ID_LEN]; /* String (identifier) value of the previous lexed token */
} token[2];
int cur_tok; /* Current token, either 0 or 1 (see the comments of lex()) */
};
/* Prototypes */
static double pop(struct eval *ev);
static void push(struct eval *ev, double d);
static int lex(struct eval *ev);
/* Prototypes for the recursive descent parser */
static void expr(struct eval *ev);
static void add_expr(struct eval *ev);
static void mul_expr(struct eval *ev);
static void pow_expr(struct eval *ev);
static void uni_expr(struct eval *ev);
static void bra_expr(struct eval *ev);
static void id_expr(struct eval *ev);
static void num_expr(struct eval *ev);
/*
* Evaluates a mathemeatical expression
*/
double eval(const char *exp, int *ep)
{
struct eval ev;
double ans = 0.0;
assert(ep != NULL);
/* Allocate a stack */
ev.st_size = 10;
ev.st = calloc(ev.st_size, sizeof *ev.st);
if(!ev.st)
{
*ep = ERR_MEMORY;
return 0.0;
}
ev.sp = 0;
/* Manage errors */
*ep = setjmp(ev.j);
if(*ep != 0)
{
free(ev.st);
return 0.0;
}
/* Initialize the lexer */
ev.token[0].type = TOK_INI;
ev.token[0].s_val[0] = '\0';
ev.token[1].type = TOK_INI;
ev.token[1].s_val[0] = '\0';
ev.cur_tok = 0;
/* Initialize the parser */
ev.p = exp;
/* lex once to initialize the lexer */
if(lex(&ev) != TOK_END)
{
expr(&ev);
ans = pop(&ev);
}
free(ev.st);
return ans;
}
/*
* Pushes a value onto the stack, increases the stack size if necessary
*/
static void push(struct eval *ev, double d)
{
if(ev->sp == ev->st_size)
{
/* Resize the stack by 1.5 */
double *old = ev->st;
int new_size = ev->st_size + (ev->st_size >> 1);
ev->st = realloc(ev->st, new_size);
if(!ev->st)
{
ev->st = old;
longjmp(ev->j, ERR_MEMORY);
}
ev->st_size = new_size;
}
ev->st[ev->sp++] = d;
}
/*
* Pops a value from the top of the stack
*/
static double pop(struct eval *ev)
{
assert(ev->sp > 0);
return ev->st[--ev->sp];
}
/* stricmp() is common, but not standard, so I provide my own */
static int istrcmp(const char *p, const char *q)
{
for(; tolower(p[0]) == tolower(q[0]) && p[0]; p++, q++);
return tolower(p[0]) - tolower(q[0]);
}
/*
* Returns a string describing a specific error code
*/
const char *eval_error(int err)
{
switch(err)
{
case 0: return "no error";
case ERR_MEMORY: return "out of memory";
case ERR_LEXER: return "unknown token";
case ERR_LONGID: return "identifier too long";
case ERR_VALUE: return "value expected";
case ERR_BRACKET: return "missing ')'";
case ERR_FUNC: return "unknown function";
case ERR_ARGS: return "wrong number of arguments";
case ERR_CONST: return "unknown constant";
}
return "unknown error";
}
/*
* Lexical analyzer function
*
* In order to implement LL(1), struct eval has an array of two token structures,
* and its cur_tok member is used to point to the _current_ token, while the other
* element contains the _next_ token. This implements a 2 element ring buffer where
* the lexer always writes to the _next_ token so that the recursive descent parser can
* _peek_ at the next token.
*/
static int lex(struct eval *ev)
{
int next_tok;
start:
/* Cycle the tokens */
next_tok = ev->cur_tok;
ev->cur_tok = ev->cur_tok?0:1;
while(isspace(ev->p[0])) ev->p++;
if(!ev->p[0])
{
/* End of the expression */
ev->token[next_tok].type = TOK_END;
goto end;
}
else if(isdigit(ev->p[0]) || ev->p[0] == '.')
{
/* Number */
char *endp;
ev->token[next_tok].type = TOK_NUM;
ev->token[next_tok].n_val = strtod(ev->p, &endp);
ev->p = endp;
goto end;
}
else if(isalpha(ev->p[0]))
{
/* Identifier */
int i;
for(i = 0; isalnum(ev->p[0]) && i < MAX_ID_LEN - 1; i++, ev->p++)
ev->token[next_tok].s_val[i] = ev->p[0];
if(isalpha(ev->p[0])) longjmp(ev->j, ERR_LONGID);
ev->token[next_tok].s_val[i] = '\0';
ev->token[next_tok].type = TOK_ID;
goto end;
}
else if(strchr(OPERATORS, ev->p[0]))
{
/* Operator */
ev->token[next_tok].type = ev->p[0];
ev->p++;
goto end;
}
else /* Unknown token */
longjmp(ev->j, ERR_LEXER);
end:
/* If this was the first call, cycle the tokens again */
if(ev->token[ev->cur_tok].type == TOK_INI)
goto start;
return ev->token[ev->cur_tok].type;
}
#define THIS(e) (e->token[e->cur_tok].type)
#define PEEK(e) (e->token[e->cur_tok?0:1].type)
#define GOBBLE(e) lex(e)
#define ERROR(c) longjmp(ev->j, (c))
/*# expr ::= add_expr
*/
static void expr(struct eval *ev)
{
add_expr(ev);
}
/*# add_expr ::= mul_expr [('+'|'-') mul_expr]*
*/
static void add_expr(struct eval *ev)
{
int t;
mul_expr(ev);
while((t =THIS(ev)) == '+' || t == '-')
{
double a,b;
GOBBLE(ev);
mul_expr(ev);
b = pop(ev);
a = pop(ev);
if(t == '+')
push(ev, a + b);
else
push(ev, a - b);
}
}
/*# mul_expr ::= pow_expr [('*'|'/'|'%') pow_expr]*
*/
static void mul_expr(struct eval *ev)
{
int t;
pow_expr(ev);
while((t = THIS(ev)) == '*' || t == '/' || t == '%')
{
double a,b;
GOBBLE(ev);
pow_expr(ev);
b = pop(ev);
a = pop(ev);
if(t == '*')
push(ev, a * b);
else if(t == '/')
push(ev, a / b);
else
push(ev, fmod(a, b));
}
}
/*# pow_expr ::= uni_expr ['^' pow_expr]
*/
static void pow_expr(struct eval *ev)
{
/* Note that exponentiation is right associative:
2^3^4 is 2^(3^4), not (2^3)^4 */
uni_expr(ev);
if(THIS(ev) == '^')
{
double a,b;
GOBBLE(ev);
pow_expr(ev);
b = pop(ev);
a = pop(ev);
push(ev, pow(a,b));
}
}
/*# uni_expr ::= ['+'|'-'] bra_expr
*/
static void uni_expr(struct eval *ev)
{
int t = '+';
if(THIS(ev) == '-' || THIS(ev) == '+')
{
t = THIS(ev);
GOBBLE(ev);
}
bra_expr(ev);
if(t == '-')
{
double a = pop(ev);
push(ev, -a);
}
}
/*# bra_expr ::= '(' add_expr ')'
*# | id_expr
*/
static void bra_expr(struct eval *ev)
{
if(THIS(ev) == '(')
{
GOBBLE(ev);
add_expr(ev);
if(THIS(ev) != ')')
ERROR(ERR_BRACKET);
GOBBLE(ev);
}
else
id_expr(ev);
}
/*# id_expr ::= ID '(' add_expr [',' add_expr]* ')'
*# | ID
*# | num_expr
*/
static void id_expr(struct eval *ev)
{
if(THIS(ev) == TOK_ID)
{
char id[MAX_ID_LEN];
strcpy(id, ev->token[ev->cur_tok].s_val);
GOBBLE(ev);
if(THIS(ev) == '(')
{
int nargs = 0;
GOBBLE(ev);
while(THIS(ev) != ')')
{
add_expr(ev);
nargs++;
if(THIS(ev) == ')') break;
if(THIS(ev) != ',')
ERROR(ERR_BRACKET);
GOBBLE(ev);
}
GOBBLE(ev);
/*$ abs(x) - absolute value of x */
if(!istrcmp(id, "abs"))
{
if(nargs != 1) ERROR(ERR_ARGS);
push(ev, fabs(pop(ev)));
}
/*$ ceil(x) - smallest integer greater than x */
else if(!istrcmp(id, "ceil"))
{
if(nargs != 1) ERROR(ERR_ARGS);
push(ev, ceil(pop(ev)));
}
/*$ floor(x) - largest integer smaller than x */
else if(!istrcmp(id, "floor"))
{
if(nargs != 1) ERROR(ERR_ARGS);
push(ev, floor(pop(ev)));
}
/*$ sin(x) - sine of x, in radians */
else if(!istrcmp(id, "sin"))
{
if(nargs != 1) ERROR(ERR_ARGS);
push(ev, sin(pop(ev)));
}
/*$ asin(x) - arcsine of x, in radians */
else if(!istrcmp(id, "asin"))
{
if(nargs != 1) ERROR(ERR_ARGS);
push(ev, asin(pop(ev)));
}
/*$ cos(x) - cosine of x, in radians */
else if(!istrcmp(id, "cos"))
{
if(nargs != 1) ERROR(ERR_ARGS);
push(ev, cos(pop(ev)));
}
/*$ acos(x) - arccosine of x, in radians */
else if(!istrcmp(id, "acos"))
{
if(nargs != 1) ERROR(ERR_ARGS);
push(ev, acos(pop(ev)));
}
/*$ tan(x) - tangent of x, in radians */
else if(!istrcmp(id, "tan"))
{
if(nargs != 1) ERROR(ERR_ARGS);
push(ev, tan(pop(ev)));
}
/*$ atan(x) - arctangent of x, in radians */
else if(!istrcmp(id, "atan"))
{
if(nargs != 1) ERROR(ERR_ARGS);
push(ev, atan(pop(ev)));
}
/*$ atan(y,x) - arctangent of y/x, in radians. */
else if(!istrcmp(id, "atan2"))
{
double a, b;
if(nargs != 2) ERROR(ERR_ARGS);
b = pop(ev);
a = pop(ev);
push(ev, atan2(a,b));
}
/*$ sinh(x) - hyperbolic sine of x, in radians */
else if(!istrcmp(id, "sinh"))
{
if(nargs != 1) ERROR(ERR_ARGS);
push(ev, sinh(pop(ev)));
}
/*$ cosh(x) - hyperbolic cosine of x, in radians */
else if(!istrcmp(id, "cosh"))
{
if(nargs != 1) ERROR(ERR_ARGS);
push(ev, cosh(pop(ev)));
}
/*$ tanh(x) - hyperbolic tangent of x, in radians */
else if(!istrcmp(id, "tanh"))
{
if(nargs != 1) ERROR(ERR_ARGS);
push(ev, tanh(pop(ev)));
}
/*$ log(x) - natural logarithm of x */
else if(!istrcmp(id, "log"))
{
if(nargs != 1) ERROR(ERR_ARGS);
push(ev, log(pop(ev)));
}
/*$ log10(x) - logarithm of x, base-10 */
else if(!istrcmp(id, "log10"))
{
if(nargs != 1) ERROR(ERR_ARGS);
push(ev, log10(pop(ev)));
}
/*$ exp(x) - computes e^x */
else if(!istrcmp(id, "exp"))
{
if(nargs != 1) ERROR(ERR_ARGS);
push(ev, exp(pop(ev)));
}
/*$ sqrt(x) - square root of x */
else if(!istrcmp(id, "sqrt"))
{
if(nargs != 1) ERROR(ERR_ARGS);
push(ev, sqrt(pop(ev)));
}
/*$ rad(x) - converts x from degrees to radians */
else if(!istrcmp(id, "rad"))
{
if(nargs != 1) ERROR(ERR_ARGS);
push(ev, pop(ev)*EVAL_PI/180);
}
/*$ deg(x) - converts x from radians to degrees */
else if(!istrcmp(id, "deg"))
{
if(nargs != 1) ERROR(ERR_ARGS);
push(ev, pop(ev)*180/EVAL_PI);
}
/*$ pow(x,y) - computes x^y */
else if(!istrcmp(id, "pow"))
{
double a, b;
if(nargs != 2) ERROR(ERR_ARGS);
b = pop(ev);
a = pop(ev);
push(ev, pow(a,b));
}
/*$ hypot(x,y) - computes sqrt(x*x + y*y) */
else if(!istrcmp(id, "hypot"))
{
double a, b;
if(nargs != 2) ERROR(ERR_ARGS);
b = pop(ev);
a = pop(ev);
push(ev, sqrt(a*a + b*b));
}
else
ERROR(ERR_FUNC);
}
else
{
/*$ pi - 3.141592654 */
if(!istrcmp(id, "pi"))
push(ev, EVAL_PI);
/*$ e - base of natural logarithms, 2.718281828 */
else if(!istrcmp(id, "e"))
push(ev, EVAL_E);
else
ERROR(ERR_CONST);
}
}
else
num_expr(ev);
}
/*# num_expr ::= NUMBER
*/
static void num_expr(struct eval *ev)
{
if(THIS(ev) != TOK_NUM)
ERROR(ERR_VALUE);
push(ev, ev->token[ev->cur_tok].n_val);
GOBBLE(ev);
}