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
24 * simple arithmetic expression evaluator.
26 * see http://joe.hotchkiss.com/programming/eval/eval.html
30 #include "mpegvideo.h"
42 #define M_PI 3.14159265358979323846
45 #define STACK_SIZE 100
47 typedef struct Parser{
48 double stack[STACK_SIZE];
52 const char **const_name; // NULL terminated
53 double (**func1)(void *, double a); // NULL terminated
54 const char **func1_name; // NULL terminated
55 double (**func2)(void *, double a, double b); // NULL terminated
56 char **func2_name; // NULL terminated
60 static void evalExpression(Parser *p);
62 static void push(Parser *p, double d){
63 if(p->stack_index+1>= STACK_SIZE){
64 av_log(NULL, AV_LOG_ERROR, "stack overflow in the parser\n");
67 p->stack[ p->stack_index++ ]= d;
68 //printf("push %f\n", d); fflush(stdout);
71 static double pop(Parser *p){
72 if(p->stack_index<=0){
73 av_log(NULL, AV_LOG_ERROR, "stack underflow in the parser\n");
76 //printf("pop\n"); fflush(stdout);
77 return p->stack[ --p->stack_index ];
80 static int strmatch(const char *s, const char *prefix){
82 for(i=0; prefix[i]; i++){
83 if(prefix[i] != s[i]) return 0;
88 static void evalPrimary(Parser *p){
94 d= strtod(p->s, &next);
101 /* named constants */
102 for(i=0; p->const_name[i]; i++){
103 if(strmatch(p->s, p->const_name[i])){
104 push(p, p->const_value[i]);
105 p->s+= strlen(p->const_name[i]);
110 p->s= strchr(p->s, '(');
112 av_log(NULL, AV_LOG_ERROR, "Parser: missing ( in \"%s\"\n", next);
124 av_log(NULL, AV_LOG_ERROR, "Parser: missing ) in \"%s\"\n", next);
129 if( strmatch(next, "sinh" ) ) d= sinh(d);
130 else if( strmatch(next, "cosh" ) ) d= cosh(d);
131 else if( strmatch(next, "tanh" ) ) d= tanh(d);
132 else if( strmatch(next, "sin" ) ) d= sin(d);
133 else if( strmatch(next, "cos" ) ) d= cos(d);
134 else if( strmatch(next, "tan" ) ) d= tan(d);
135 else if( strmatch(next, "exp" ) ) d= exp(d);
136 else if( strmatch(next, "log" ) ) d= log(d);
137 else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
138 else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
139 else if( strmatch(next, "abs" ) ) d= fabs(d);
140 else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2;
141 else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2;
142 else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0;
143 else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0;
144 else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0;
145 else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0;
146 else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0;
147 // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1);
148 // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
151 for(i=0; p->func1_name && p->func1_name[i]; i++){
152 if(strmatch(next, p->func1_name[i])){
153 d= p->func1[i](p->opaque, d);
159 for(i=0; p->func2_name && p->func2_name[i]; i++){
160 if(strmatch(next, p->func2_name[i])){
161 d= p->func2[i](p->opaque, d, d2);
168 av_log(NULL, AV_LOG_ERROR, "Parser: unknown function in \"%s\"\n", next);
176 static void evalPow(Parser *p){
178 if(p->s[0]=='+') p->s++;
190 av_log(NULL, AV_LOG_ERROR, "Parser: missing )\n");
196 if(neg) push(p, -pop(p));
199 static void evalFactor(Parser *p){
207 push(p, pow(pop(p), d));
211 static void evalTerm(Parser *p){
213 while(p->s[0]=='*' || p->s[0]=='/'){
214 int inv= p->s[0]=='/';
225 static void evalExpression(Parser *p){
227 while(p->s[0]=='+' || p->s[0]=='-'){
228 int sign= p->s[0]=='-';
239 double ff_eval(char *s, double *const_value, const char **const_name,
240 double (**func1)(void *, double), const char **func1_name,
241 double (**func2)(void *, double, double), char **func2_name,
247 p.const_value= const_value;
248 p.const_name = const_name;
250 p.func1_name = func1_name;
252 p.func2_name = func2_name;