2 * simple arithmetic expression evaluator
4 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * see http://joe.hotchkiss.com/programming/eval/eval.html
35 #define STACK_SIZE 100
37 typedef struct Parser{
38 double stack[STACK_SIZE];
42 char **const_name; // NULL terminated
43 double (**func1)(void *, double a); // NULL terminated
44 char **func1_name; // NULL terminated
45 double (**func2)(void *, double a, double b); // NULL terminated
46 char **func2_name; // NULL terminated
50 static void evalExpression(Parser *p);
52 static void push(Parser *p, double d){
53 if(p->stack_index+1>= STACK_SIZE){
54 fprintf(stderr, "stack overflow in the parser\n");
57 p->stack[ p->stack_index++ ]= d;
58 //printf("push %f\n", d); fflush(stdout);
61 static double pop(Parser *p){
62 if(p->stack_index<=0){
63 fprintf(stderr, "stack underflow in the parser\n");
66 //printf("pop\n"); fflush(stdout);
67 return p->stack[ --p->stack_index ];
70 static int strmatch(char *s, char *prefix){
72 for(i=0; prefix[i]; i++){
73 if(prefix[i] != s[i]) return 0;
78 static void evalPrimary(Parser *p){
84 d= strtod(p->s, &next);
92 for(i=0; p->const_name[i]; i++){
93 if(strmatch(p->s, p->const_name[i])){
94 push(p, p->const_value[i]);
95 p->s+= strlen(p->const_name[i]);
100 p->s= strchr(p->s, '(');
102 fprintf(stderr, "Parser: missing ( in \"%s\"\n", next);
108 p->s++; // ")" or ","
115 if( strmatch(next, "sinh" ) ) d= sinh(d);
116 else if( strmatch(next, "cosh" ) ) d= cosh(d);
117 else if( strmatch(next, "tanh" ) ) d= tanh(d);
118 else if( strmatch(next, "sin" ) ) d= sin(d);
119 else if( strmatch(next, "cos" ) ) d= cos(d);
120 else if( strmatch(next, "tan" ) ) d= tan(d);
121 else if( strmatch(next, "exp" ) ) d= exp(d);
122 else if( strmatch(next, "log" ) ) d= log(d);
123 else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
124 else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
125 else if( strmatch(next, "abs" ) ) d= abs(d);
126 else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2;
127 else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2;
128 else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0;
129 else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0;
130 else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0;
131 // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1);
132 // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
135 for(i=0; p->func1_name && p->func1_name[i]; i++){
136 if(strmatch(next, p->func1_name[i])){
137 d= p->func1[i](p->opaque, d);
143 for(i=0; p->func2_name && p->func2_name[i]; i++){
144 if(strmatch(next, p->func2_name[i])){
145 d= p->func2[i](p->opaque, d, d2);
152 fprintf(stderr, "Parser: unknown function in \"%s\"\n", next);
158 fprintf(stderr, "Parser: missing ) in \"%s\"\n", next);
164 static void evalPow(Parser *p){
166 if(p->s[0]=='+') p->s++;
178 fprintf(stderr, "Parser: missing )\n");
184 if(neg) push(p, -pop(p));
187 static void evalFactor(Parser *p){
195 push(p, pow(pop(p), d));
199 static void evalTerm(Parser *p){
201 while(p->s[0]=='*' || p->s[0]=='/'){
202 int inv= p->s[0]=='/';
213 static void evalExpression(Parser *p){
215 while(p->s[0]=='+' || p->s[0]=='-'){
216 int sign= p->s[0]=='-';
227 double ff_eval(char *s, double *const_value, char **const_name,
228 double (**func1)(void *, double), char **func1_name,
229 double (**func2)(void *, double, double), char **func2_name,
235 p.const_value= const_value;
236 p.const_name = const_name;
238 p.func1_name = func1_name;
240 p.func2_name = func2_name;