Mercurial > libavcodec.hg
annotate eval.c @ 614:b786f15df503 libavcodec
NAN doesnt exist on FreeBSD patch by (Rmi Guyomarch <rguyom at pobox dot com>)
author | michaelni |
---|---|
date | Sun, 25 Aug 2002 22:54:43 +0000 |
parents | c0005de2be59 |
children | 79c43f519d02 |
rev | line source |
---|---|
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 | |
614
b786f15df503
NAN doesnt exist on FreeBSD patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michaelni
parents:
612
diff
changeset
|
31 #ifndef NAN |
b786f15df503
NAN doesnt exist on FreeBSD patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michaelni
parents:
612
diff
changeset
|
32 #define NAN 0 |
b786f15df503
NAN doesnt exist on FreeBSD patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michaelni
parents:
612
diff
changeset
|
33 #endif |
b786f15df503
NAN doesnt exist on FreeBSD patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michaelni
parents:
612
diff
changeset
|
34 |
612 | 35 #define STACK_SIZE 100 |
36 | |
37 typedef struct Parser{ | |
38 double stack[STACK_SIZE]; | |
39 int stack_index; | |
40 char *s; | |
41 double *const_value; | |
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 | |
47 void *opaque; | |
48 } Parser; | |
49 | |
50 static void evalExpression(Parser *p); | |
51 | |
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"); | |
55 return; | |
56 } | |
57 p->stack[ p->stack_index++ ]= d; | |
58 //printf("push %f\n", d); fflush(stdout); | |
59 } | |
60 | |
61 static double pop(Parser *p){ | |
62 if(p->stack_index<=0){ | |
63 fprintf(stderr, "stack underflow in the parser\n"); | |
64 return NAN; | |
65 } | |
66 //printf("pop\n"); fflush(stdout); | |
67 return p->stack[ --p->stack_index ]; | |
68 } | |
69 | |
70 static int strmatch(char *s, char *prefix){ | |
71 int i; | |
72 for(i=0; prefix[i]; i++){ | |
73 if(prefix[i] != s[i]) return 0; | |
74 } | |
75 return 1; | |
76 } | |
77 | |
78 static void evalPrimary(Parser *p){ | |
79 double d, d2=NAN; | |
80 char *next= p->s; | |
81 int i; | |
82 | |
83 /* number */ | |
84 d= strtod(p->s, &next); | |
85 if(next != p->s){ | |
86 push(p, d); | |
87 p->s= next; | |
88 return; | |
89 } | |
90 | |
91 /* named constants */ | |
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]); | |
96 return; | |
97 } | |
98 } | |
99 | |
100 p->s= strchr(p->s, '('); | |
101 if(p->s==NULL){ | |
102 fprintf(stderr, "Parser: missing ( in \"%s\"\n", next); | |
103 return; | |
104 } | |
105 p->s++; // "(" | |
106 evalExpression(p); | |
107 d= pop(p); | |
108 p->s++; // ")" or "," | |
109 if(p->s[-1]== ','){ | |
110 evalExpression(p); | |
111 d2= pop(p); | |
112 p->s++; // ")" | |
113 } | |
114 | |
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; | |
133 else{ | |
134 int error=1; | |
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); | |
138 error=0; | |
139 break; | |
140 } | |
141 } | |
142 | |
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); | |
146 error=0; | |
147 break; | |
148 } | |
149 } | |
150 | |
151 if(error){ | |
152 fprintf(stderr, "Parser: unknown function in \"%s\"\n", next); | |
153 return; | |
154 } | |
155 } | |
156 | |
157 if(p->s[-1]!= ')'){ | |
158 fprintf(stderr, "Parser: missing ) in \"%s\"\n", next); | |
159 return; | |
160 } | |
161 push(p, d); | |
162 } | |
163 | |
164 static void evalPow(Parser *p){ | |
165 int neg= 0; | |
166 if(p->s[0]=='+') p->s++; | |
167 | |
168 if(p->s[0]=='-'){ | |
169 neg= 1; | |
170 p->s++; | |
171 } | |
172 | |
173 if(p->s[0]=='('){ | |
174 p->s++;; | |
175 evalExpression(p); | |
176 | |
177 if(p->s[0]!=')') | |
178 fprintf(stderr, "Parser: missing )\n"); | |
179 p->s++; | |
180 }else{ | |
181 evalPrimary(p); | |
182 } | |
183 | |
184 if(neg) push(p, -pop(p)); | |
185 } | |
186 | |
187 static void evalFactor(Parser *p){ | |
188 evalPow(p); | |
189 while(p->s[0]=='^'){ | |
190 double d; | |
191 | |
192 p->s++; | |
193 evalPow(p); | |
194 d= pop(p); | |
195 push(p, pow(pop(p), d)); | |
196 } | |
197 } | |
198 | |
199 static void evalTerm(Parser *p){ | |
200 evalFactor(p); | |
201 while(p->s[0]=='*' || p->s[0]=='/'){ | |
202 int inv= p->s[0]=='/'; | |
203 double d; | |
204 | |
205 p->s++; | |
206 evalFactor(p); | |
207 d= pop(p); | |
208 if(inv) d= 1.0/d; | |
209 push(p, d * pop(p)); | |
210 } | |
211 } | |
212 | |
213 static void evalExpression(Parser *p){ | |
214 evalTerm(p); | |
215 while(p->s[0]=='+' || p->s[0]=='-'){ | |
216 int sign= p->s[0]=='-'; | |
217 double d; | |
218 | |
219 p->s++; | |
220 evalTerm(p); | |
221 d= pop(p); | |
222 if(sign) d= -d; | |
223 push(p, d + pop(p)); | |
224 } | |
225 } | |
226 | |
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, | |
230 void *opaque){ | |
231 Parser p; | |
232 | |
233 p.stack_index=0; | |
234 p.s= s; | |
235 p.const_value= const_value; | |
236 p.const_name = const_name; | |
237 p.func1 = func1; | |
238 p.func1_name = func1_name; | |
239 p.func2 = func2; | |
240 p.func2_name = func2_name; | |
241 p.opaque = opaque; | |
242 | |
243 evalExpression(&p); | |
244 return pop(&p); | |
245 } |