annotate eval.c @ 680:79393a88c6e4 libavcodec

10l (forgot to commit)
author michaelni
date Wed, 18 Sep 2002 18:30:31 +0000
parents 79c43f519d02
children bb5de8a59da8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
1 /*
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
2 * simple arithmetic expression evaluator
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
3 *
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
4 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
5 *
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
6 * This library is free software; you can redistribute it and/or
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
7 * modify it under the terms of the GNU Lesser General Public
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
8 * License as published by the Free Software Foundation; either
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
9 * version 2 of the License, or (at your option) any later version.
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
10 *
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
11 * This library is distributed in the hope that it will be useful,
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
14 * Lesser General Public License for more details.
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
15 *
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
16 * You should have received a copy of the GNU Lesser General Public
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
17 * License along with this library; if not, write to the Free Software
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
19 *
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
20 */
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
21
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
22 /*
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
23 * see http://joe.hotchkiss.com/programming/eval/eval.html
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
24 */
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
25
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
26 #include <stdio.h>
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
27 #include <stdlib.h>
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
28 #include <string.h>
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
29 #include <math.h>
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
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
627
79c43f519d02 undefined M_PI / M_E fix
michaelni
parents: 614
diff changeset
35 #ifndef M_PI
79c43f519d02 undefined M_PI / M_E fix
michaelni
parents: 614
diff changeset
36 #define M_PI 3.14159265358979323846
79c43f519d02 undefined M_PI / M_E fix
michaelni
parents: 614
diff changeset
37 #endif
79c43f519d02 undefined M_PI / M_E fix
michaelni
parents: 614
diff changeset
38
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
39 #define STACK_SIZE 100
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
40
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
41 typedef struct Parser{
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
42 double stack[STACK_SIZE];
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
43 int stack_index;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
44 char *s;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
45 double *const_value;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
46 char **const_name; // NULL terminated
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
47 double (**func1)(void *, double a); // NULL terminated
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
48 char **func1_name; // NULL terminated
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
49 double (**func2)(void *, double a, double b); // NULL terminated
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
50 char **func2_name; // NULL terminated
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
51 void *opaque;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
52 } Parser;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
53
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
54 static void evalExpression(Parser *p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
55
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
56 static void push(Parser *p, double d){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
57 if(p->stack_index+1>= STACK_SIZE){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
58 fprintf(stderr, "stack overflow in the parser\n");
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
59 return;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
60 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
61 p->stack[ p->stack_index++ ]= d;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
62 //printf("push %f\n", d); fflush(stdout);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
63 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
64
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
65 static double pop(Parser *p){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
66 if(p->stack_index<=0){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
67 fprintf(stderr, "stack underflow in the parser\n");
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
68 return NAN;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
69 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
70 //printf("pop\n"); fflush(stdout);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
71 return p->stack[ --p->stack_index ];
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
72 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
73
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
74 static int strmatch(char *s, char *prefix){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
75 int i;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
76 for(i=0; prefix[i]; i++){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
77 if(prefix[i] != s[i]) return 0;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
78 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
79 return 1;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
80 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
81
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
82 static void evalPrimary(Parser *p){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
83 double d, d2=NAN;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
84 char *next= p->s;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
85 int i;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
86
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
87 /* number */
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
88 d= strtod(p->s, &next);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
89 if(next != p->s){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
90 push(p, d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
91 p->s= next;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
92 return;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
93 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
94
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
95 /* named constants */
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
96 for(i=0; p->const_name[i]; i++){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
97 if(strmatch(p->s, p->const_name[i])){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
98 push(p, p->const_value[i]);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
99 p->s+= strlen(p->const_name[i]);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
100 return;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
101 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
102 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
103
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
104 p->s= strchr(p->s, '(');
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
105 if(p->s==NULL){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
106 fprintf(stderr, "Parser: missing ( in \"%s\"\n", next);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
107 return;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
108 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
109 p->s++; // "("
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
110 evalExpression(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
111 d= pop(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
112 p->s++; // ")" or ","
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
113 if(p->s[-1]== ','){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
114 evalExpression(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
115 d2= pop(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
116 p->s++; // ")"
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
117 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
118
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
119 if( strmatch(next, "sinh" ) ) d= sinh(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
120 else if( strmatch(next, "cosh" ) ) d= cosh(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
121 else if( strmatch(next, "tanh" ) ) d= tanh(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
122 else if( strmatch(next, "sin" ) ) d= sin(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
123 else if( strmatch(next, "cos" ) ) d= cos(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
124 else if( strmatch(next, "tan" ) ) d= tan(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
125 else if( strmatch(next, "exp" ) ) d= exp(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
126 else if( strmatch(next, "log" ) ) d= log(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
127 else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
128 else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
129 else if( strmatch(next, "abs" ) ) d= abs(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
130 else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
131 else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
132 else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
133 else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
134 else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
135 // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
136 // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
137 else{
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
138 int error=1;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
139 for(i=0; p->func1_name && p->func1_name[i]; i++){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
140 if(strmatch(next, p->func1_name[i])){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
141 d= p->func1[i](p->opaque, d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
142 error=0;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
143 break;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
144 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
145 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
146
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
147 for(i=0; p->func2_name && p->func2_name[i]; i++){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
148 if(strmatch(next, p->func2_name[i])){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
149 d= p->func2[i](p->opaque, d, d2);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
150 error=0;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
151 break;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
152 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
153 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
154
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
155 if(error){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
156 fprintf(stderr, "Parser: unknown function in \"%s\"\n", next);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
157 return;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
158 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
159 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
160
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
161 if(p->s[-1]!= ')'){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
162 fprintf(stderr, "Parser: missing ) in \"%s\"\n", next);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
163 return;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
164 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
165 push(p, d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
166 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
167
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
168 static void evalPow(Parser *p){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
169 int neg= 0;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
170 if(p->s[0]=='+') p->s++;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
171
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
172 if(p->s[0]=='-'){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
173 neg= 1;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
174 p->s++;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
175 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
176
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
177 if(p->s[0]=='('){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
178 p->s++;;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
179 evalExpression(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
180
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
181 if(p->s[0]!=')')
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
182 fprintf(stderr, "Parser: missing )\n");
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
183 p->s++;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
184 }else{
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
185 evalPrimary(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
186 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
187
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
188 if(neg) push(p, -pop(p));
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
189 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
190
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
191 static void evalFactor(Parser *p){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
192 evalPow(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
193 while(p->s[0]=='^'){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
194 double d;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
195
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
196 p->s++;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
197 evalPow(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
198 d= pop(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
199 push(p, pow(pop(p), d));
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
200 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
201 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
202
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
203 static void evalTerm(Parser *p){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
204 evalFactor(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
205 while(p->s[0]=='*' || p->s[0]=='/'){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
206 int inv= p->s[0]=='/';
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
207 double d;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
208
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
209 p->s++;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
210 evalFactor(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
211 d= pop(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
212 if(inv) d= 1.0/d;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
213 push(p, d * pop(p));
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
214 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
215 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
216
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
217 static void evalExpression(Parser *p){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
218 evalTerm(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
219 while(p->s[0]=='+' || p->s[0]=='-'){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
220 int sign= p->s[0]=='-';
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
221 double d;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
222
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
223 p->s++;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
224 evalTerm(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
225 d= pop(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
226 if(sign) d= -d;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
227 push(p, d + pop(p));
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
228 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
229 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
230
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
231 double ff_eval(char *s, double *const_value, char **const_name,
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
232 double (**func1)(void *, double), char **func1_name,
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
233 double (**func2)(void *, double, double), char **func2_name,
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
234 void *opaque){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
235 Parser p;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
236
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
237 p.stack_index=0;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
238 p.s= s;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
239 p.const_value= const_value;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
240 p.const_name = const_name;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
241 p.func1 = func1;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
242 p.func1_name = func1_name;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
243 p.func2 = func2;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
244 p.func2_name = func2_name;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
245 p.opaque = opaque;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
246
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
247 evalExpression(&p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
248 return pop(&p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
249 }