You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
6.4KB

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