aboutsummaryrefslogtreecommitdiffstats
path: root/evaluator.c
blob: 2b84dd65de31fe76c5c28a2b186d518dbc28d3d3 (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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
/* $Id: evaluator.c,v 1.7 2004/01/07 10:15:41 reinelt Exp $
 *
 * expression evaluation
 *
 * based on EE (Expression Evaluator) which is 
 * (c) 1992 Mark Morley <morley@Camosun.BC.CA>
 * 
 * heavily modified 2003 by Michael Reinelt <reinelt@eunet.at>
 *
 * FIXME: GPL or not GPL????
 *
 * $Log: evaluator.c,v $
 * Revision 1.7  2004/01/07 10:15:41  reinelt
 * small glitch in evaluator fixed
 * made config table sorted and access with bsearch(),
 * which should be much faster
 *
 * Revision 1.6  2004/01/06 23:01:37  reinelt
 * more copyright issues
 *
 * Revision 1.5  2004/01/06 17:33:45  reinelt
 * Evaluator: functions with variable argument lists
 * Evaluator: plugin_sample.c and README.Plugins added
 *
 * Revision 1.4  2004/01/06 15:19:12  reinelt
 * Evaluator rearrangements...
 *
 * Revision 1.3  2003/10/11 06:01:52  reinelt
 * renamed expression.{c,h} to client.{c,h}
 * added config file client
 * new functions 'AddNumericVariable()' and 'AddStringVariable()'
 * new parameter '-i' for interactive mode
 *
 * Revision 1.2  2003/10/06 05:47:27  reinelt
 * operators: ==, \!=, <=, >=
 *
 * Revision 1.1  2003/10/06 04:34:06  reinelt
 * expression evaluator added
 *
 */


/***************************************************************************
 **                                                                       **
 ** EE.C         Expression Evaluator                                     **
 **                                                                       **
 ** AUTHOR:      Mark Morley                                              **
 ** COPYRIGHT:   (c) 1992 by Mark Morley                                  **
 ** DATE:        December 1991                                            **
 ** HISTORY:     Jan 1992 - Made it squash all command line arguments     **
 **                         into one big long string.                     **
 **                       - It now can set/get VMS symbols as if they     **
 **                         were variables.                               **
 **                       - Changed max variable name length from 5 to 15 **
 **              Jun 1992 - Updated comments and docs                     **
 **                                                                       **
 ** You are free to incorporate this code into your own works, even if it **
 ** is a commercial application.  However, you may not charge anyone else **
 ** for the use of this code!  If you intend to distribute your code,     **
 ** I'd appreciate it if you left this message intact.  I'd like to       **
 ** receive credit wherever it is appropriate.  Thanks!                   **
 **                                                                       **
 ** I don't promise that this code does what you think it does...         **
 **                                                                       **
 ** Please mail any bug reports/fixes/enhancments to me at:               **
 **      morley@camosun.bc.ca                                             **
 ** or                                                                    **
 **      Mark Morley                                                      **
 **      3889 Mildred Street                                              **
 **      Victoria, BC  Canada                                             **
 **      V8Z 7G1                                                          **
 **      (604) 479-7861                                                   **
 **                                                                       **
 ***************************************************************************/


/* 
 * exported functions:
 *
 * int AddNumericVariable(char *name, double value)
 *   adds a numerical variable to the evaluator
 *
 * int AddStringVariable(char *name, char *value)
 *   adds a numerical variable to the evaluator
 *
 * int AddFunction (char *name, int args, void (*func)())
 *   adds a function to the evaluator
 *
 * RESULT* SetResult (RESULT **result, int type, void *value)
 *   initializes a result
 *
 * double R2N (RESULT *result)
 *   converts a result into a number
 *
 * char* R2S (RESULT *result)
 *   converts a result into a string
 *
 * int Eval (char* expression, RESULT *result)
 *   evaluates an expression
 *
 */



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

#include "debug.h"
#include "evaluator.h"


// Token types
#define T_DELIMITER 1
#define T_NUMBER    2
#define T_STRING    3
#define T_NAME      4


#define is_blank(c)  (c==' ' || c=='\t')
#define is_number(c) (isdigit(c) || c=='.')
#define is_name(c)   (isalnum(c) || c=='_')
#define is_delim(c)  (strchr("+-*/%^().,;=<>!&|", c)!=NULL)


typedef struct {
  char   *name;
  RESULT *value;
} VARIABLE;

static VARIABLE *Variable=NULL;
static int      nVariable=0;


typedef struct {
  char *name;
  int   args;
  void (*func)();
} FUNCTION;

static FUNCTION *Function=NULL;
static int      nFunction=0;
static int       FunctionSorted=0;

static char *Expression=NULL;
static char *Token=NULL;
static int   Type=0;


// error handling
#define ERROR(n) longjmp(jb,n)
jmp_buf jb;

char* ErrMsg[] = {
  "",
  "Syntax error",
  "Unbalanced parenthesis",
  "Division by zero",
  "Unknown variable",
  "Unrecognized function",
  "Wrong number of arguments",
  "Missing an argument",
  "Empty expression"
};



static void DelResult (RESULT *result)
{
  result->type=0;
  result->number=0.0;
  if (result->string) {
    free (result->string);
    result->string=NULL;
  }
}


static void FreeResult (RESULT *result)
{
  if (result!=NULL) {
    DelResult(result);
    free (result);
  }
}


static RESULT* NewResult (void)
{
  RESULT *result = malloc(sizeof(RESULT));
  if (result==NULL) {
    error ("cannot allocate result: out of memory!");
    return NULL;
  }
  result->type=0;
  result->number=0.0;
  result->string=NULL;
  
  return result;
}


static RESULT* DupResult (RESULT *result)
{
  RESULT *result2;
  
  if ((result2=NewResult())==NULL) 
    return NULL;
  
  result2->type=result->type;
  result2->number=result->number;
  if (result->string!=NULL)
    result2->string=strdup(result->string);
  else    
    result2->string=NULL;
  
  return result2;
}


RESULT* SetResult (RESULT **result, int type, void *value)
{
  if (*result) {
    DelResult(*result);
  } else {
    if ((*result=NewResult())==NULL) 
      return NULL;
  }
  
  if (type==R_NUMBER) {
    (*result)->type=R_NUMBER;
    (*result)->number=*(double*)value;
    (*result)->string=NULL;
  } else if (type==R_STRING) {
    (*result)->type=R_STRING;
    (*result)->string=strdup(value);
  } else {
    error ("internal error: invalid result type %d", type); 
    return NULL;
  }
  
  return *result;
}


double R2N (RESULT *result)
{
  if (result==NULL) {
    error ("internal error: NULL result");
    return 0.0;
  }

  if (result->type & R_NUMBER) {
    return result->number;
  }
  
  if (result->type & R_STRING) {
    result->type |= R_NUMBER;
    result->number=atof(result->string);
    return result->number;
  }
  
  error ("internal error: invalid result type %d", result->type); 
  return 0.0;
}


char* R2S (RESULT *result)
{
  char buffer[16];
  
  if (result==NULL) {
    error ("internal error: NULL result");
    return NULL;
  }

  if (result->type & R_STRING) {
    return result->string;
  }
  
  if (result->type & R_NUMBER) {
    sprintf(buffer, "%g", result->number);
    result->type |= R_STRING;
    result->string=strdup(buffer);
    return result->string;
  }
  
  error ("internal error: invalid result type %d", result->type); 
  return NULL;
  
}


// bsearch compare function for variables 
static int v_lookup (const void *a, const void *b)
{
  char *name=(char*)a;
  VARIABLE *v=(VARIABLE*)b;

  return strcmp(name, v->name);
}


// qsort compare function for variables
static int v_sort (const void *a, const void *b)
{
  VARIABLE *va=(VARIABLE*)a;
  VARIABLE *vb=(VARIABLE*)b;

  return strcmp(va->name, vb->name);
}


static int DelVariable (char *name)
{
  VARIABLE *V;

  V=bsearch(name, Variable, nVariable, sizeof(VARIABLE), v_lookup);
  if (V!=NULL) {
    FreeResult (V->value);
    memmove (V, V+1, (nVariable-1)*sizeof(VARIABLE)-(V-Variable));
    nVariable--;
    Variable=realloc(Variable, nVariable*sizeof(VARIABLE));
    return 1;
  }
  return 0;
}


static int GetVariable (char *name, RESULT *value)
{
  VARIABLE *V;

  V=bsearch(name, Variable, nVariable, sizeof(VARIABLE), v_lookup);
  if (V!=NULL) {
    value->type=V->value->type;
    value->number=V->value->number;
    if (V->value->string!=NULL) value->string=strdup(V->value->string);
    else value->string=NULL;
    return 1;
  }
  
  DelResult (value);
  return 0;
}


static int SetVariable (char *name, RESULT *value)
{
  VARIABLE *V;

  V=bsearch(name, Variable, nVariable, sizeof(VARIABLE), v_lookup);
  if (V!=NULL) {
    FreeResult (V->value);
    V->value=DupResult(value);
    return 1;
  }
  
  // we append the var at the end and re-sort
  // the whole array. As every SetVariable call
  // implies a bsearch(), this is ok and cannot
  // be optimized.
  nVariable++;
  Variable=realloc(Variable, nVariable*sizeof(VARIABLE));
  Variable[nVariable-1].name=strdup(name);
  Variable[nVariable-1].value=DupResult(value);
  qsort(Variable, nVariable, sizeof(VARIABLE), v_sort);

  return 0;
}


int AddNumericVariable (char *name, double value)
{
  RESULT result;
  
  result.type=R_NUMBER;
  result.number=value;
  result.string=NULL;
  
  return SetVariable (name, &result);
}


int AddStringVariable (char *name, char *value)
{
  RESULT result;
  
  result.type=R_STRING;
  result.number=0.0;
  result.string=strdup(value);
  
  return SetVariable (name, &result);
}


// bsearch compare function for functions 
static int f_lookup (const void *a, const void *b)
{
  char *name=(char*)a;
  FUNCTION *f=(FUNCTION*)b;
  return strcmp(name, f->name);
}


// qsort compare function for functions
static int f_sort (const void *a, const void *b)
{
  FUNCTION *va=(FUNCTION*)a;
  FUNCTION *vb=(FUNCTION*)b;
  return strcmp(va->name, vb->name);
}


static FUNCTION* GetFunction (char *name)
{
  FUNCTION *F;
  
  if (!FunctionSorted) {
    FunctionSorted=1;
    qsort(Function, nFunction, sizeof(FUNCTION), f_sort);
  }
  
  F=bsearch(name, Function, nFunction, sizeof(FUNCTION), f_lookup);
  
  return F;
}


int AddFunction (char *name, int args, void (*func)())
{
  // we append the func at the end, and flag
  // the function table as unsorted. It will be
  // sorted again on the next GetFunction call.
  FunctionSorted=0;
  nFunction++;
  Function=realloc(Function, nFunction*sizeof(FUNCTION));
  Function[nFunction-1].name=strdup(name);
  Function[nFunction-1].args=args;
  Function[nFunction-1].func=func;
  
  return 0;
}



// Prototypes
static void Level01 (RESULT *result);
static void Level02 (RESULT *result);
static void Level03 (RESULT *result);
static void Level04 (RESULT *result);
static void Level05 (RESULT *result);
static void Level06 (RESULT *result);
static void Level07 (RESULT *result);
static void Level08 (RESULT *result);
static void Level09 (RESULT *result);
static void Level10 (RESULT *result);
static void Level11 (RESULT *result);



static void Parse (void)
{
  char *start;
  
  Type=0;
  if (Token) {
    free (Token);
    Token=NULL;
  }
  
  while (is_blank(*Expression)) Expression++;
  
  if (is_delim(*Expression)) {
    Type=T_DELIMITER;
    // special case for <=, >=, ==, !=
    if (strchr("<>=!", *Expression)!=NULL && *(Expression+1)=='=') {
      Token=strndup(Expression, 2);
      Expression+=2;
    } else {
      Token=strndup(Expression, 1);
      Expression++;
    }
  }
  
  else if (isdigit(*Expression)) {
    Type=T_NUMBER;
    start=Expression; 
    while (is_number(*Expression)) Expression++;
    Token=strndup(start, Expression-start);
  }
  
  else if (is_name(*Expression)) {
    Type=T_NAME;
    start=Expression;
    while (is_name(*Expression)) Expression++;
    Token=strndup(start, Expression-start);
  }
  
  else if (*Expression=='\'') {
    Type=T_STRING;
    start=++Expression;
    while (*Expression && *Expression!='\'') Expression++;
    Token=strndup(start, Expression-start);
    if (*Expression=='\'') Expression++;
  }
  
  else if (*Expression) {
    ERROR(E_SYNTAX);
  }
  
  while(is_blank(*Expression)) Expression++;

  // empty token
  if (Token==NULL) Token=strdup("");
}



// expression lists
static void Level01 (RESULT *result)
{
  do {
    while (*Token==';') Parse();
    Level02(result);
  } while (*Token==';');
}


// variable assignments
static void Level02 (RESULT *result)
{
  char *name;
  
  if (Type==T_NAME) {
    if (Expression[0]=='=' && Expression[1]!='=') {
      name=strdup(Token);
      Parse();
      Parse();
      if (*Token && *Token!=';') {
	Level03(result);
	SetVariable(name, result);
      } else {
	DelVariable(name);
      }
      free (name);
      return;
    }
  }
  Level03(result);
}


// logical 'or'
static void Level03 (RESULT *result)
{
  RESULT operand;
  double value;
  
  Level04(result);
  
  while(*Token=='|') {
    Parse();
    Level04 (&operand);
    value = (R2N(result)!=0.0) || (R2N(&operand)!=0.0);
    SetResult(&result, R_NUMBER, &value); 
  }
}


// logical 'and'
static void Level04 (RESULT *result)
{
  RESULT operand;
  double value;
  
  Level05(result);
  
  while(*Token=='&') {
    Parse();
    Level05 (&operand);
    value = (R2N(result)!=0.0) && (R2N(&operand)!=0.0);
    SetResult(&result, R_NUMBER, &value); 
  }
}


// equal, not equal
static void Level05 (RESULT *result)
{
  char operator;
  RESULT operand = {0, 0.0, NULL};
  double value;
  
  Level06 (result);
  
  if (((operator=Token[0])=='=' || operator=='!') && Token[1]=='=') {
    Parse();
    Level06 (&operand);
    if (operator=='=')
      value = (R2N(result) == R2N(&operand));
    else
      value = (R2N(result) != R2N(&operand));
    SetResult(&result, R_NUMBER, &value); 
  }
}


// relational operators
static void Level06 (RESULT *result)
{
  char operator[2];
  RESULT operand = {0, 0.0, NULL};
  double value;
  
  Level07 (result);
  
  if (*Token=='<' || *Token=='>') {
    operator[0]=Token[0];
    operator[1]=Token[1];
    Parse();
    Level07 (&operand);
    if (operator[0]=='<')
      if (operator[1]=='=')
	value = (R2N(result) <= R2N(&operand));
      else
	value = (R2N(result) <  R2N(&operand));
    else
      if (operator[1]=='=')
	value = (R2N(result) >= R2N(&operand));
      else
	value = (R2N(result) >  R2N(&operand));
    SetResult(&result, R_NUMBER, &value); 
  }
}


// addition, subtraction, concatenation
static void Level07 (RESULT *result)
{
  char operator;
  RESULT operand = {0, 0.0, NULL};
  double value;
  
  Level08(result);
  
  while((operator=*Token)=='+' || operator=='-' || operator=='.') {
    Parse();
    Level08 (&operand);
    if (operator=='+') {
      value = (R2N(result) + R2N(&operand));
      SetResult(&result, R_NUMBER, &value); 
    } else if (operator=='-') {
      value = (R2N(result) - R2N(&operand));
      SetResult(&result, R_NUMBER, &value); 
    } else {
      char *s1=R2S(result);
      char *s2=R2S(&operand);
      char *s3=malloc(strlen(s1)+strlen(s2)+1);
      strcpy (s3, s1);
      strcat (s3, s2);
      SetResult (&result, R_STRING, s3);
      free (s3);
    }
  }
}


// multiplication, division, modulo
static void Level08 (RESULT *result)
{
  char operator;
  RESULT operand = {0, 0.0, NULL};
  double value;
  
  Level09 (result);
  
  while((operator=*Token)=='*' || operator=='/' || operator=='%') {
    Parse();
    Level09(&operand);
    if (operator == '*') {
      value = (R2N(result) * R2N(&operand));
    } else if (operator == '/') {
      if (R2N(&operand) == 0.0) ERROR (E_DIVZERO);
      value = (R2N(result) / R2N(&operand));
    } else {
      if (R2N(&operand) == 0.0) ERROR (E_DIVZERO);
      value = fmod(R2N(result), R2N(&operand));
    }
    SetResult(&result, R_NUMBER, &value); 
  }
}


// x^y
static void Level09 (RESULT *result)
{
  RESULT exponent = {0, 0.0, NULL};
  double value;

  Level10 (result);
  
  if (*Token == '^') {
    Parse();
    Level10 (&exponent);
    value = pow(R2N(result), R2N(&exponent));
    SetResult(&result, R_NUMBER, &value); 
  }
}


// unary + or - signs or logical 'not'
static void Level10 (RESULT *result)
{
  char sign=0;
  double value;
  
  if (*Token=='+' || *Token=='-' || *Token=='!') {
    sign=*Token;
    Parse();
  }

  Level11 (result);
  
  if (sign == '-') {
    value = -R2N(result);
    SetResult(&result, R_NUMBER, &value); 
  }
  else if (sign == '!') {
    value = (R2N(result)==0.0);
    SetResult(&result, R_NUMBER, &value); 
  }    
}


// literal numbers, variables, functions
static void Level11 (RESULT *result)
{
  RESULT *param[10];
  
  if (*Token == '(') {
    
    Parse();
    if (*Token == ')') ERROR (E_NOARG);
    Level01(result);
    if (*Token != ')') ERROR (E_UNBALAN);
    Parse();
    
  } else {
    
    if (Type == T_NUMBER) {
      double value=atof(Token);
      SetResult(&result, R_NUMBER, &value);
      Parse();
      
    } else if (Type == T_STRING) {
      SetResult(&result, R_STRING, Token);
      Parse();
      
    } else if (Type == T_NAME) {

      if (*Expression == '(') {
	FUNCTION *F=GetFunction(Token);
	if (F!=NULL) {
	  int n=0;
	  Parse(); // read opening brace
	  do { 
	    Parse(); // read argument
	    if (*Token == ',') ERROR (E_NOARG);
	    if (*Token == ')') {
	      // immediately closed when no args
	      if (F->args>0 || n>0) ERROR (E_NOARG);
	    } else {
	      param[n]=NewResult();
	      Level01(param[n]);
	      n++;
	    }
	  } while (n < 10 && *Token == ',');
	  Parse(); // read closing brace
	  if (F->args<0) {
	    // Function with variable argument list: 
	    // pass number of arguments as first parameter
	    F->func(result, n, &param); 
	  } else {
	    if (n != F->args) ERROR (E_NUMARGS);
	    F->func(result, 
		    param[0], param[1], param[2], param[3], param[4], 
		    param[5], param[6], param[7], param[8], param[9]);
	  }
	  // free parameter list
	  while(n-->0) {
	    FreeResult(param[n]);
	  }
	  
	  return;
	  
	} else {
	  ERROR(E_BADFUNC);
	}
	
      } else {
	if (!GetVariable(Token, result)) 
	  ERROR(E_UNKNOWN);
      }
      Parse();
      
    } else {
      ERROR(E_SYNTAX);
    }
  }
}


int Eval (char* expression, RESULT *result)
{
  int i, err;
  
  if ((err=setjmp(jb))) {
    error ("Error: %s in expression <%s>", ErrMsg[err], expression);
    return -1;
  }
  
  // maybe sort function table
  if (!FunctionSorted) {
    FunctionSorted=1;
    qsort(Function, nFunction, sizeof(FUNCTION), f_sort);
    // sanity check: two functions with the same name?
    for (i=1; i<nFunction; i++) {
      if (strcmp(Function[i].name, Function[i-1].name)==0) {
	error ("internal error: Function '%s' defined twice!", Function[i].name);
      }
    }
  }
  
  Expression=expression;
  DelResult (result);
  Parse();
  if (*Token=='\0') ERROR (E_EMPTY);
  Level01(result);
  if (*Token!='\0') ERROR (E_SYNTAX);
  return 0;
}