annotate eval.c @ 2433:0934621b6453 libavcodec

simplify, null pointer, selftest
author michael
date Sat, 15 Jan 2005 19:05:26 +0000
parents 2152760d08ad
children 24aa9209e8b0
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
1106
1e39f273ecd6 per file doxy
michaelni
parents: 1057
diff changeset
22 /**
1e39f273ecd6 per file doxy
michaelni
parents: 1057
diff changeset
23 * @file eval.c
1e39f273ecd6 per file doxy
michaelni
parents: 1057
diff changeset
24 * simple arithmetic expression evaluator.
1e39f273ecd6 per file doxy
michaelni
parents: 1057
diff changeset
25 *
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
26 * see http://joe.hotchkiss.com/programming/eval/eval.html
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
27 */
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
28
1057
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
29 #include "avcodec.h"
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
30 #include "mpegvideo.h"
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
31
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
32 #include <stdio.h>
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
33 #include <stdlib.h>
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
34 #include <string.h>
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
35 #include <math.h>
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
36
614
b786f15df503 NAN doesnt exist on FreeBSD patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michaelni
parents: 612
diff changeset
37 #ifndef NAN
b786f15df503 NAN doesnt exist on FreeBSD patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michaelni
parents: 612
diff changeset
38 #define NAN 0
b786f15df503 NAN doesnt exist on FreeBSD patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michaelni
parents: 612
diff changeset
39 #endif
b786f15df503 NAN doesnt exist on FreeBSD patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michaelni
parents: 612
diff changeset
40
627
79c43f519d02 undefined M_PI / M_E fix
michaelni
parents: 614
diff changeset
41 #ifndef M_PI
79c43f519d02 undefined M_PI / M_E fix
michaelni
parents: 614
diff changeset
42 #define M_PI 3.14159265358979323846
79c43f519d02 undefined M_PI / M_E fix
michaelni
parents: 614
diff changeset
43 #endif
79c43f519d02 undefined M_PI / M_E fix
michaelni
parents: 614
diff changeset
44
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
45 #define STACK_SIZE 100
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
46
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
47 typedef struct Parser{
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
48 double stack[STACK_SIZE];
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
49 int stack_index;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
50 char *s;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
51 double *const_value;
1057
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
52 const char **const_name; // NULL terminated
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
53 double (**func1)(void *, double a); // NULL terminated
1057
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
54 const char **func1_name; // NULL terminated
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
55 double (**func2)(void *, double a, double b); // NULL terminated
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
56 char **func2_name; // NULL terminated
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
57 void *opaque;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
58 } Parser;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
59
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
60 static void evalExpression(Parser *p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
61
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
62 static void push(Parser *p, double d){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
63 if(p->stack_index+1>= STACK_SIZE){
1598
932d306bf1dc av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents: 1106
diff changeset
64 av_log(NULL, AV_LOG_ERROR, "stack overflow in the parser\n");
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
65 return;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
66 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
67 p->stack[ p->stack_index++ ]= d;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
68 //printf("push %f\n", d); fflush(stdout);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
69 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
70
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
71 static double pop(Parser *p){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
72 if(p->stack_index<=0){
1598
932d306bf1dc av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents: 1106
diff changeset
73 av_log(NULL, AV_LOG_ERROR, "stack underflow in the parser\n");
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
74 return NAN;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
75 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
76 //printf("pop\n"); fflush(stdout);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
77 return p->stack[ --p->stack_index ];
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
78 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
79
1057
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
80 static int strmatch(const char *s, const char *prefix){
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
81 int i;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
82 for(i=0; prefix[i]; i++){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
83 if(prefix[i] != s[i]) return 0;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
84 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
85 return 1;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
86 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
87
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
88 static void evalPrimary(Parser *p){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
89 double d, d2=NAN;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
90 char *next= p->s;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
91 int i;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
92
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
93 /* number */
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
94 d= strtod(p->s, &next);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
95 if(next != p->s){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
96 push(p, d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
97 p->s= next;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
98 return;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
99 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
100
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
101 /* named constants */
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
102 for(i=0; p->const_name && p->const_name[i]; i++){
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
103 if(strmatch(p->s, p->const_name[i])){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
104 push(p, p->const_value[i]);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
105 p->s+= strlen(p->const_name[i]);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
106 return;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
107 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
108 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
109
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
110 p->s= strchr(p->s, '(');
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
111 if(p->s==NULL){
1598
932d306bf1dc av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents: 1106
diff changeset
112 av_log(NULL, AV_LOG_ERROR, "Parser: missing ( in \"%s\"\n", next);
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
113 return;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
114 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
115 p->s++; // "("
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
116 evalExpression(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
117 d= pop(p);
1815
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
118 if(p->s[0]== ','){
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
119 p->s++; // ","
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
120 evalExpression(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
121 d2= pop(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
122 }
1815
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
123 if(p->s[0] != ')'){
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
124 av_log(NULL, AV_LOG_ERROR, "Parser: missing ) in \"%s\"\n", next);
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
125 return;
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
126 }
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
127 p->s++; // ")"
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
128
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
129 if( strmatch(next, "sinh" ) ) d= sinh(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
130 else if( strmatch(next, "cosh" ) ) d= cosh(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
131 else if( strmatch(next, "tanh" ) ) d= tanh(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
132 else if( strmatch(next, "sin" ) ) d= sin(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
133 else if( strmatch(next, "cos" ) ) d= cos(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
134 else if( strmatch(next, "tan" ) ) d= tan(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
135 else if( strmatch(next, "exp" ) ) d= exp(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
136 else if( strmatch(next, "log" ) ) d= log(d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
137 else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
138 else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
1057
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
139 else if( strmatch(next, "abs" ) ) d= fabs(d);
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
140 else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
141 else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
142 else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0;
1815
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
143 else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
144 else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0;
1815
2152760d08ad avoid negative array indices
alex
parents: 1598
diff changeset
145 else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
146 else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
147 // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
148 // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
149 else{
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
150 for(i=0; p->func1_name && p->func1_name[i]; i++){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
151 if(strmatch(next, p->func1_name[i])){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
152 d= p->func1[i](p->opaque, d);
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
153 goto push_ret;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
154 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
155 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
156
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
157 for(i=0; p->func2_name && p->func2_name[i]; i++){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
158 if(strmatch(next, p->func2_name[i])){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
159 d= p->func2[i](p->opaque, d, d2);
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
160 goto push_ret;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
161 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
162 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
163
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
164 av_log(NULL, AV_LOG_ERROR, "Parser: unknown function in \"%s\"\n", next);
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
165 return;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
166 }
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
167
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
168 push_ret:
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
169 push(p, d);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
170 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
171
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
172 static void evalPow(Parser *p){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
173 int neg= 0;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
174 if(p->s[0]=='+') p->s++;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
175
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
176 if(p->s[0]=='-'){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
177 neg= 1;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
178 p->s++;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
179 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
180
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
181 if(p->s[0]=='('){
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
182 p->s++;
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
183 evalExpression(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
184
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
185 if(p->s[0]!=')')
1598
932d306bf1dc av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents: 1106
diff changeset
186 av_log(NULL, AV_LOG_ERROR, "Parser: missing )\n");
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
187 p->s++;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
188 }else{
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
189 evalPrimary(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
190 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
191
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
192 if(neg) push(p, -pop(p));
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
193 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
194
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
195 static void evalFactor(Parser *p){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
196 evalPow(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
197 while(p->s[0]=='^'){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
198 double d;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
199
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
200 p->s++;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
201 evalPow(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
202 d= pop(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
203 push(p, pow(pop(p), d));
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
204 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
205 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
206
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
207 static void evalTerm(Parser *p){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
208 evalFactor(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
209 while(p->s[0]=='*' || p->s[0]=='/'){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
210 int inv= p->s[0]=='/';
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
211 double d;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
212
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
213 p->s++;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
214 evalFactor(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
215 d= pop(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
216 if(inv) d= 1.0/d;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
217 push(p, d * pop(p));
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
218 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
219 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
220
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
221 static void evalExpression(Parser *p){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
222 evalTerm(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
223 while(p->s[0]=='+' || p->s[0]=='-'){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
224 int sign= p->s[0]=='-';
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
225 double d;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
226
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
227 p->s++;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
228 evalTerm(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
229 d= pop(p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
230 if(sign) d= -d;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
231 push(p, d + pop(p));
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
232 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
233 }
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
234
1057
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
235 double ff_eval(char *s, double *const_value, const char **const_name,
bb5de8a59da8 * static,const,compiler warning cleanup
kabi
parents: 627
diff changeset
236 double (**func1)(void *, double), const char **func1_name,
612
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
237 double (**func2)(void *, double, double), char **func2_name,
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
238 void *opaque){
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
239 Parser p;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
240
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
241 p.stack_index=0;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
242 p.s= s;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
243 p.const_value= const_value;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
244 p.const_name = const_name;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
245 p.func1 = func1;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
246 p.func1_name = func1_name;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
247 p.func2 = func2;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
248 p.func2_name = func2_name;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
249 p.opaque = opaque;
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
250
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
251 evalExpression(&p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
252 return pop(&p);
c0005de2be59 new ratecontrol code
michaelni
parents:
diff changeset
253 }
2433
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
254
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
255 #ifdef TEST
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
256 #undef printf
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
257 static double const_values[]={
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
258 M_PI,
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
259 M_E,
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
260 0
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
261 };
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
262 static const char *const_names[]={
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
263 "PI",
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
264 "E",
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
265 0
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
266 };
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
267 main(){
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
268 printf("%f == 12.7\n", ff_eval("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL));
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
269 }
0934621b6453 simplify, null pointer, selftest
michael
parents: 1815
diff changeset
270 #endif