612
|
1 /*
|
|
2 * simple arithmetic expression evaluator
|
|
3 *
|
|
4 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
|
|
5 *
|
|
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.
|
|
10 *
|
|
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.
|
|
15 *
|
|
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
|
|
19 *
|
|
20 */
|
|
21
|
|
22 /*
|
|
23 * see http://joe.hotchkiss.com/programming/eval/eval.html
|
|
24 */
|
|
25
|
|
26 #include <stdio.h>
|
|
27 #include <stdlib.h>
|
|
28 #include <string.h>
|
|
29 #include <math.h>
|
|
30
|
|
31 #define STACK_SIZE 100
|
|
32
|
|
33 typedef struct Parser{
|
|
34 double stack[STACK_SIZE];
|
|
35 int stack_index;
|
|
36 char *s;
|
|
37 double *const_value;
|
|
38 char **const_name; // NULL terminated
|
|
39 double (**func1)(void *, double a); // NULL terminated
|
|
40 char **func1_name; // NULL terminated
|
|
41 double (**func2)(void *, double a, double b); // NULL terminated
|
|
42 char **func2_name; // NULL terminated
|
|
43 void *opaque;
|
|
44 } Parser;
|
|
45
|
|
46 static void evalExpression(Parser *p);
|
|
47
|
|
48 static void push(Parser *p, double d){
|
|
49 if(p->stack_index+1>= STACK_SIZE){
|
|
50 fprintf(stderr, "stack overflow in the parser\n");
|
|
51 return;
|
|
52 }
|
|
53 p->stack[ p->stack_index++ ]= d;
|
|
54 //printf("push %f\n", d); fflush(stdout);
|
|
55 }
|
|
56
|
|
57 static double pop(Parser *p){
|
|
58 if(p->stack_index<=0){
|
|
59 fprintf(stderr, "stack underflow in the parser\n");
|
|
60 return NAN;
|
|
61 }
|
|
62 //printf("pop\n"); fflush(stdout);
|
|
63 return p->stack[ --p->stack_index ];
|
|
64 }
|
|
65
|
|
66 static int strmatch(char *s, char *prefix){
|
|
67 int i;
|
|
68 for(i=0; prefix[i]; i++){
|
|
69 if(prefix[i] != s[i]) return 0;
|
|
70 }
|
|
71 return 1;
|
|
72 }
|
|
73
|
|
74 static void evalPrimary(Parser *p){
|
|
75 double d, d2=NAN;
|
|
76 char *next= p->s;
|
|
77 int i;
|
|
78
|
|
79 /* number */
|
|
80 d= strtod(p->s, &next);
|
|
81 if(next != p->s){
|
|
82 push(p, d);
|
|
83 p->s= next;
|
|
84 return;
|
|
85 }
|
|
86
|
|
87 /* named constants */
|
|
88 for(i=0; p->const_name[i]; i++){
|
|
89 if(strmatch(p->s, p->const_name[i])){
|
|
90 push(p, p->const_value[i]);
|
|
91 p->s+= strlen(p->const_name[i]);
|
|
92 return;
|
|
93 }
|
|
94 }
|
|
95
|
|
96 p->s= strchr(p->s, '(');
|
|
97 if(p->s==NULL){
|
|
98 fprintf(stderr, "Parser: missing ( in \"%s\"\n", next);
|
|
99 return;
|
|
100 }
|
|
101 p->s++; // "("
|
|
102 evalExpression(p);
|
|
103 d= pop(p);
|
|
104 p->s++; // ")" or ","
|
|
105 if(p->s[-1]== ','){
|
|
106 evalExpression(p);
|
|
107 d2= pop(p);
|
|
108 p->s++; // ")"
|
|
109 }
|
|
110
|
|
111 if( strmatch(next, "sinh" ) ) d= sinh(d);
|
|
112 else if( strmatch(next, "cosh" ) ) d= cosh(d);
|
|
113 else if( strmatch(next, "tanh" ) ) d= tanh(d);
|
|
114 else if( strmatch(next, "sin" ) ) d= sin(d);
|
|
115 else if( strmatch(next, "cos" ) ) d= cos(d);
|
|
116 else if( strmatch(next, "tan" ) ) d= tan(d);
|
|
117 else if( strmatch(next, "exp" ) ) d= exp(d);
|
|
118 else if( strmatch(next, "log" ) ) d= log(d);
|
|
119 else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
|
|
120 else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
|
|
121 else if( strmatch(next, "abs" ) ) d= abs(d);
|
|
122 else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2;
|
|
123 else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2;
|
|
124 else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0;
|
|
125 else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0;
|
|
126 else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0;
|
|
127 // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1);
|
|
128 // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
|
|
129 else{
|
|
130 int error=1;
|
|
131 for(i=0; p->func1_name && p->func1_name[i]; i++){
|
|
132 if(strmatch(next, p->func1_name[i])){
|
|
133 d= p->func1[i](p->opaque, d);
|
|
134 error=0;
|
|
135 break;
|
|
136 }
|
|
137 }
|
|
138
|
|
139 for(i=0; p->func2_name && p->func2_name[i]; i++){
|
|
140 if(strmatch(next, p->func2_name[i])){
|
|
141 d= p->func2[i](p->opaque, d, d2);
|
|
142 error=0;
|
|
143 break;
|
|
144 }
|
|
145 }
|
|
146
|
|
147 if(error){
|
|
148 fprintf(stderr, "Parser: unknown function in \"%s\"\n", next);
|
|
149 return;
|
|
150 }
|
|
151 }
|
|
152
|
|
153 if(p->s[-1]!= ')'){
|
|
154 fprintf(stderr, "Parser: missing ) in \"%s\"\n", next);
|
|
155 return;
|
|
156 }
|
|
157 push(p, d);
|
|
158 }
|
|
159
|
|
160 static void evalPow(Parser *p){
|
|
161 int neg= 0;
|
|
162 if(p->s[0]=='+') p->s++;
|
|
163
|
|
164 if(p->s[0]=='-'){
|
|
165 neg= 1;
|
|
166 p->s++;
|
|
167 }
|
|
168
|
|
169 if(p->s[0]=='('){
|
|
170 p->s++;;
|
|
171 evalExpression(p);
|
|
172
|
|
173 if(p->s[0]!=')')
|
|
174 fprintf(stderr, "Parser: missing )\n");
|
|
175 p->s++;
|
|
176 }else{
|
|
177 evalPrimary(p);
|
|
178 }
|
|
179
|
|
180 if(neg) push(p, -pop(p));
|
|
181 }
|
|
182
|
|
183 static void evalFactor(Parser *p){
|
|
184 evalPow(p);
|
|
185 while(p->s[0]=='^'){
|
|
186 double d;
|
|
187
|
|
188 p->s++;
|
|
189 evalPow(p);
|
|
190 d= pop(p);
|
|
191 push(p, pow(pop(p), d));
|
|
192 }
|
|
193 }
|
|
194
|
|
195 static void evalTerm(Parser *p){
|
|
196 evalFactor(p);
|
|
197 while(p->s[0]=='*' || p->s[0]=='/'){
|
|
198 int inv= p->s[0]=='/';
|
|
199 double d;
|
|
200
|
|
201 p->s++;
|
|
202 evalFactor(p);
|
|
203 d= pop(p);
|
|
204 if(inv) d= 1.0/d;
|
|
205 push(p, d * pop(p));
|
|
206 }
|
|
207 }
|
|
208
|
|
209 static void evalExpression(Parser *p){
|
|
210 evalTerm(p);
|
|
211 while(p->s[0]=='+' || p->s[0]=='-'){
|
|
212 int sign= p->s[0]=='-';
|
|
213 double d;
|
|
214
|
|
215 p->s++;
|
|
216 evalTerm(p);
|
|
217 d= pop(p);
|
|
218 if(sign) d= -d;
|
|
219 push(p, d + pop(p));
|
|
220 }
|
|
221 }
|
|
222
|
|
223 double ff_eval(char *s, double *const_value, char **const_name,
|
|
224 double (**func1)(void *, double), char **func1_name,
|
|
225 double (**func2)(void *, double, double), char **func2_name,
|
|
226 void *opaque){
|
|
227 Parser p;
|
|
228
|
|
229 p.stack_index=0;
|
|
230 p.s= s;
|
|
231 p.const_value= const_value;
|
|
232 p.const_name = const_name;
|
|
233 p.func1 = func1;
|
|
234 p.func1_name = func1_name;
|
|
235 p.func2 = func2;
|
|
236 p.func2_name = func2_name;
|
|
237 p.opaque = opaque;
|
|
238
|
|
239 evalExpression(&p);
|
|
240 return pop(&p);
|
|
241 }
|