summaryrefslogtreecommitdiff
path: root/parex/eval.c
blob: 7eb9607d5da6d5cba73d414feee1512389b3cad9 (plain)
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/* 
 *  EVAL.C - part of the EGG system.
 *
 *  Helper routine for evaluating arithmetic expressions.
 *
 *  By Shawn Hargreaves.
 */


#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>

#ifndef M_PI
#define M_PI      3.14159265358979323846
#endif

#ifndef M_E
#define M_E       2.7182818284590452354
#endif

#include "egg.h"



/* convert radians <-> degrees */
#define DEG2RAD(x)   ((x)*M_PI/180.0)
#define RAD2DEG(x)   ((x)*180.0/M_PI)



/* internal state information */
static int evaluate_error;

static char operator_stack[256];
static double value_stack[256];

static int stack_depth;

static double current_val;

static int current_valid;



/* operator tokens */
#define OP_OPEN_PAREN      '('
#define OP_CLOSE_PAREN     ')'
#define OP_PLUS            '+'
#define OP_MINUS           '-'
#define OP_MUL             '*'
#define OP_DIV             '/'
#define OP_POWER           '^'
#define OP_NEGATE          '~'
#define OP_SQRT            'q'
#define OP_SIN             's'
#define OP_COS             'c'
#define OP_TAN             't'
#define OP_ASIN            'S'
#define OP_ACOS            'C'
#define OP_ATAN            'T'
#define OP_LOG             'l'
#define OP_LN              'L'
#define OP_CEIL            'e'
#define OP_FLOOR           'f'
#define OP_ROUND           'r'
#define OP_ABS             'a'
#define OP_MOD             '%'
#define OP_EQUALS          '='
#define OP_NOT_EQUALS      '#'
#define OP_LESS            '<'
#define OP_GREATER         '>'
#define OP_LESS_EQUALS     '{'
#define OP_GREATER_EQUALS  '}'
#define OP_OR              '|'
#define OP_AND             '&'
#define OP_NOT             '!'
#define OP_TERMINATOR      ' '



/* precedence:
 *  Returns the precedence of the specified operator.
 */
static int precedence(char op)
{
   switch (op) {

      case OP_OPEN_PAREN:
      case OP_CLOSE_PAREN:
	 return 1;

      case OP_TERMINATOR:
	 return 2;

      case OP_OR:
      case OP_AND:
	 return 3;

      case OP_EQUALS:
      case OP_NOT_EQUALS:
      case OP_LESS:
      case OP_GREATER:
      case OP_LESS_EQUALS:
      case OP_GREATER_EQUALS:
	 return 4;

      case OP_PLUS:
      case OP_MINUS:
	 return 5;

      case OP_MUL:
      case OP_DIV:
      case OP_MOD:
	 return 6;

      case OP_POWER:
	 return 7;

      case OP_NEGATE:
      case OP_NOT:
      case OP_SQRT:
      case OP_SIN:
      case OP_COS:
      case OP_TAN:
      case OP_ASIN:
      case OP_ACOS:
      case OP_ATAN:
      case OP_LOG:
      case OP_LN:
      case OP_CEIL:
      case OP_FLOOR:
      case OP_ROUND:
      case OP_ABS:
	 return 8;

      default:
	 return -1;
   }
}



/* is_unary:
 *  Checks whether an operator is unary.
 */
static int is_unary(char op)
{
   if ((op == OP_NEGATE) || (op == OP_SQRT) || (op == OP_SIN)   ||
       (op == OP_COS)    || (op == OP_TAN)  || (op == OP_ASIN)  ||
       (op == OP_ACOS)   || (op == OP_ATAN) || (op == OP_LOG)   ||
       (op == OP_LN)     || (op == OP_CEIL) || (op == OP_FLOOR) ||
       (op == OP_ROUND)  || (op == OP_ABS)  || (op == OP_NOT))
      return TRUE;

   return FALSE;
}



/* add_value:
 *  Processes a new numeric value from the input string.
 */
static void add_value(double val)
{
   if (current_valid) {
      evaluate_error = TRUE;
      return;
   }

   current_val = val;
   current_valid = TRUE;
}



/* add_operator:
 *  Processes a new operator from the input string.
 */
static void add_operator(char op)
{
   /* bodge for unary negation */
   if ((op == OP_MINUS) && (!current_valid))
      op = OP_NEGATE;

   /* check validity */
   if ((op == OP_PLUS)        || (op == OP_MINUS)          || 
       (op == OP_MUL)         || (op == OP_DIV)            || 
       (op == OP_POWER)       || (op == OP_MOD)            ||
       (op == OP_EQUALS)      || (op == OP_NOT_EQUALS)     ||
       (op == OP_LESS)        || (op == OP_GREATER)        ||
       (op == OP_LESS_EQUALS) || (op == OP_GREATER_EQUALS) ||
       (op == OP_OR)          || (op == OP_AND)) {

      if (!current_valid) {
	 evaluate_error = TRUE;
	 return;
      }
   }

   /* evaluate */
   if (op != OP_OPEN_PAREN) {
      while ((stack_depth > 0) && 
	     ((precedence(op) <= precedence(operator_stack[stack_depth-1]) && (!is_unary(operator_stack[stack_depth-1]))) ||
	      (precedence(op) < precedence(operator_stack[stack_depth-1]) && (is_unary(operator_stack[stack_depth-1]))))) {

	 stack_depth--;

	 switch (operator_stack[stack_depth]) {

	    case OP_PLUS:
	       current_val = value_stack[stack_depth] + current_val;
	       break;

	    case OP_MINUS:
	       current_val = value_stack[stack_depth] - current_val;
	       break;

	    case OP_MUL:
	       current_val = value_stack[stack_depth] * current_val;
	       break;

	    case OP_DIV:
	       if (current_val != 0)
		  current_val = value_stack[stack_depth] / current_val;
	       else
		  current_val = 0;
	       break;

	    case OP_POWER:
	       current_val = pow(value_stack[stack_depth], current_val);
	       break;

	    case OP_NEGATE:
	       current_val = -current_val;
	       break;

	    case OP_SQRT:
	       if (current_val >= 0)
		  current_val = sqrt(current_val);
	       else
		  current_val = 0;
	       break;

	    case OP_SIN:
	       current_val = sin(DEG2RAD(current_val));
	       break;

	    case OP_COS:
	       current_val = cos(DEG2RAD(current_val));
	       break;

	    case OP_TAN:
	       current_val = tan(DEG2RAD(current_val));
	       break;

	    case OP_ASIN:
	       if ((current_val >= -1) && (current_val <= 1))
		  current_val = RAD2DEG(asin(current_val));
	       else
		  current_val = 0;
	       break;

	    case OP_ACOS:
	       if ((current_val >= -1) && (current_val <= 1))
		  current_val = RAD2DEG(acos(current_val));
	       else
		  current_val = 0;
	       break;

	    case OP_ATAN:
	       current_val = RAD2DEG(atan(current_val));
	       break;

	    case OP_LOG:
	       if (current_val > 0)
		  current_val = log10(current_val);
	       else
		  current_val = 0;
	       break;

	    case OP_LN:
	       if (current_val > 0)
		  current_val = log(current_val);
	       else
		  current_val = 0;
	       break;

	    case OP_CEIL:
	       current_val = ceil(current_val);
	       break;

	    case OP_FLOOR:
	       current_val = floor(current_val);
	       break;

	    case OP_ROUND:
	       if (current_val < 0)
		  current_val = (int)(current_val - 0.5);
	       else
		  current_val = (int)(current_val + 0.5);
	       break;

	    case OP_ABS:
	       current_val = fabs(current_val);
	       break;

	    case OP_MOD:
	       if (current_val >= 1)
		  current_val = fmod(value_stack[stack_depth], current_val);
	       else
		  current_val = 0;
	       break;

	    case OP_EQUALS:
	       current_val = (value_stack[stack_depth] == current_val);
	       break;

	    case OP_NOT_EQUALS:
	       current_val = (value_stack[stack_depth] != current_val);
	       break;

	    case OP_LESS:
	       current_val = (value_stack[stack_depth] < current_val);
	       break;

	    case OP_GREATER:
	       current_val = (value_stack[stack_depth] > current_val);
	       break;

	    case OP_LESS_EQUALS:
	       current_val = (value_stack[stack_depth] <= current_val);
	       break;

	    case OP_GREATER_EQUALS:
	       current_val = (value_stack[stack_depth] >= current_val);
	       break;

	    case OP_OR:
	       current_val = ((int)value_stack[stack_depth] || (int)current_val);
	       break;

	    case OP_AND:
	       current_val = ((int)value_stack[stack_depth] && (int)current_val);
	       break;

	    case OP_NOT:
	       current_val = !(int)current_val;
	       break;

	    case OP_OPEN_PAREN:
	       if (op == OP_CLOSE_PAREN)
		  return;
	       break;
	 }
      }
   }

   /* push onto the stack */
   if (op != OP_CLOSE_PAREN) {
      operator_stack[stack_depth] = op;
      value_stack[stack_depth] = current_val;
      stack_depth++;
      current_val = 0;
      current_valid = FALSE;
   }
   else {
      if (stack_depth <= 0)
	 evaluate_error = TRUE;
   }
}



/* evaluate:
 *  Top level evaluation function.
 */
double evaluate(char *equation, int *error, double (*variable)(char *name))
{
   char buf[256];
   double val;
   int i;

   stack_depth = 0;
   current_val = 0;
   current_valid = FALSE;
   evaluate_error = FALSE;

   while ((*equation) && (!evaluate_error)) {
      /* skip whitespace */
      while (isspace(*equation))
	 equation++;

      switch (*equation) {

	 case '+':
	    /* addition */
	    add_operator(OP_PLUS);
	    equation++;
	    break;

	 case '-':
	    /* subtraction */
	    add_operator(OP_MINUS);
	    equation++;
	    break;

	 case '*':
	    /* multiplication */
	    add_operator(OP_MUL);
	    equation++;
	    break;

	 case '/':
	    /* division */
	    add_operator(OP_DIV);
	    equation++;
	    break;

	 case '^':
	    /* rasing to a power (_not_ XOR!) */
	    add_operator(OP_POWER);
	    equation++;
	    break;

	 case '%':
	    /* modulus */
	    add_operator(OP_MOD);
	    equation++;
	    break;

	 case '|':
	    /* logical or */
	    add_operator(OP_OR);
	    equation++;
	    break;

	 case '&':
	    /* logical and */
	    add_operator(OP_AND);
	    equation++;
	    break;

	 case '=':
	    /* equality test (requires dual ==) */
	    if (equation[1] == '=') {
	       add_operator(OP_EQUALS);
	       equation += 2;
	    }
	    else
	       evaluate_error = TRUE;
	    break;

	 case '!':
	    /* could be inequality test or logical not */
	    if (equation[1] == '=') {
	       add_operator(OP_NOT_EQUALS);
	       equation += 2;
	    }
	    else {
	       add_operator(OP_NOT);
	       equation++;
	    }
	    break;

	 case '<':
	    /* could be less than or less/equal test */
	    if (equation[1] == '=') {
	       add_operator(OP_LESS_EQUALS);
	       equation += 2;
	    }
	    else {
	       add_operator(OP_LESS);
	       equation++;
	    }
	    break;

	 case '>':
	    /* could be greater than or greater/equal test */
	    if (equation[1] == '=') {
	       add_operator(OP_GREATER_EQUALS);
	       equation += 2;
	    }
	    else {
	       add_operator(OP_GREATER);
	       equation++;
	    }
	    break;

	 case '(':
	    /* open bracket */
	    add_operator(OP_OPEN_PAREN);
	    equation++;
	    break;

	 case ')':
	    /* close bracket */
	    add_operator(OP_CLOSE_PAREN);
	    equation++;
	    break;

	 case '0':
	    /* special case for hex constants (0x prefix) */
	    if (equation[1] == 'x') {
	       equation += 2;

	       for (i=0; isxdigit(equation[i]); i++)
		  buf[i] = equation[i];

	       buf[i] = 0;
	       equation += i;

	       val = strtol(buf, NULL, 16);
	       add_value(val);
	       break;
	    }
	    /* else fall through */

	 case '1':
	 case '2':
	 case '3':
	 case '4':
	 case '5':
	 case '6':
	 case '7':
	 case '8':
	 case '9':
	    /* floating point constant */
	    for (i=0; isdigit(equation[i]) || (equation[i] == '.'); i++)
	       buf[i] = equation[i];

	    buf[i] = 0;
	    equation += i;

	    val = atof(buf);
	    add_value(val);
	    break;

	 default:
	    /* this is a string, could be a variable or function */
	    for (i=0; (isalpha(equation[i])) || (equation[i] == '_'); i++)
	       buf[i] = tolower(equation[i]);

	    buf[i] = 0;
	    equation += i;

	    if (strcmp(buf, "pi") == 0) {
	       /* pi (built in constant) */
	       add_value(M_PI);
	    }
	    else if (strcmp(buf, "e") == 0) {
	       /* e (built in constant) */
	       add_value(M_E);
	    }
	    else if (strcmp(buf, "sqrt") == 0) {
	       /* square root function */
	       add_operator(OP_SQRT);
	    }
	    else if (strcmp(buf, "sin") == 0) {
	       /* sin function */
	       add_operator(OP_SIN);
	    }
	    else if (strcmp(buf, "cos") == 0) {
	       /* cos function */
	       add_operator(OP_COS);
	    }
	    else if (strcmp(buf, "tan") == 0) {
	       /* tan function */
	       add_operator(OP_TAN);
	    }
	    else if (strcmp(buf, "asin") == 0) {
	       /* inverse sin function */
	       add_operator(OP_ASIN);
	    }
	    else if (strcmp(buf, "acos") == 0) {
	       /* inverse cos function */
	       add_operator(OP_ACOS);
	    }
	    else if (strcmp(buf, "atan") == 0) {
	       /* inverse tan function */
	       add_operator(OP_ATAN);
	    }
	    else if (strcmp(buf, "log") == 0) {
	       /* base 10 logarithm function */
	       add_operator(OP_LOG);
	    }
	    else if (strcmp(buf, "ln") == 0) {
	       /* natural logarithm function */
	       add_operator(OP_LN);
	    }
	    else if (strcmp(buf, "ceil") == 0) {
	       /* round upwards function */
	       add_operator(OP_CEIL);
	    }
	    else if (strcmp(buf, "floor") == 0) {
	       /* round downwards function */
	       add_operator(OP_FLOOR);
	    }
	    else if (strcmp(buf, "round") == 0) {
	       /* round to nearest integer function */
	       add_operator(OP_ROUND);
	    }
	    else if (strcmp(buf, "abs") == 0) {
	       /* absolute value function */
	       add_operator(OP_ABS);
	    }
	    else if (strcmp(buf, "rand") == 0) {
	       /* random number between 0 and 1 */
	       add_value((rand()&32767)/32767.0);
	    }
	    else {
	       /* user-supplied callback for looking up variables */
	       if ((buf[0]) && (variable)) {
		  add_value(variable(buf));
	       }
	       else {
		  if (error)
		     *error = TRUE;

		  return 0;
	       }
	    }
	    break;
      }
   }

   if ((evaluate_error) || (!current_valid)) {
      if (error)
	 *error = TRUE;

      return 0;
   }

   /* force a stack flush */
   add_operator(OP_TERMINATOR);

   if (stack_depth != 1) {
      if (error)
	 *error = TRUE;

      return 0;
   }

   if (error)
      *error = FALSE;

   return value_stack[0];
}