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.

226 lines
6.1KB

  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. * @file eval.c
  23. * simple arithmetic expression evaluator.
  24. *
  25. * see http://joe.hotchkiss.com/programming/eval/eval.html
  26. */
  27. #include "avcodec.h"
  28. #include "mpegvideo.h"
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <math.h>
  33. #ifndef NAN
  34. #define NAN 0
  35. #endif
  36. #ifndef M_PI
  37. #define M_PI 3.14159265358979323846
  38. #endif
  39. typedef struct Parser{
  40. int stack_index;
  41. char *s;
  42. double *const_value;
  43. const char **const_name; // NULL terminated
  44. double (**func1)(void *, double a); // NULL terminated
  45. const char **func1_name; // NULL terminated
  46. double (**func2)(void *, double a, double b); // NULL terminated
  47. char **func2_name; // NULL terminated
  48. void *opaque;
  49. } Parser;
  50. static double evalExpression(Parser *p);
  51. static int strmatch(const char *s, const char *prefix){
  52. int i;
  53. for(i=0; prefix[i]; i++){
  54. if(prefix[i] != s[i]) return 0;
  55. }
  56. return 1;
  57. }
  58. static double evalPrimary(Parser *p){
  59. double d, d2=NAN;
  60. char *next= p->s;
  61. int i;
  62. /* number */
  63. d= strtod(p->s, &next);
  64. if(next != p->s){
  65. p->s= next;
  66. return d;
  67. }
  68. /* named constants */
  69. for(i=0; p->const_name && p->const_name[i]; i++){
  70. if(strmatch(p->s, p->const_name[i])){
  71. p->s+= strlen(p->const_name[i]);
  72. return p->const_value[i];
  73. }
  74. }
  75. p->s= strchr(p->s, '(');
  76. if(p->s==NULL){
  77. av_log(NULL, AV_LOG_ERROR, "Parser: missing ( in \"%s\"\n", next);
  78. return NAN;
  79. }
  80. p->s++; // "("
  81. d= evalExpression(p);
  82. if(p->s[0]== ','){
  83. p->s++; // ","
  84. d2= evalExpression(p);
  85. }
  86. if(p->s[0] != ')'){
  87. av_log(NULL, AV_LOG_ERROR, "Parser: missing ) in \"%s\"\n", next);
  88. return NAN;
  89. }
  90. p->s++; // ")"
  91. if( strmatch(next, "sinh" ) ) d= sinh(d);
  92. else if( strmatch(next, "cosh" ) ) d= cosh(d);
  93. else if( strmatch(next, "tanh" ) ) d= tanh(d);
  94. else if( strmatch(next, "sin" ) ) d= sin(d);
  95. else if( strmatch(next, "cos" ) ) d= cos(d);
  96. else if( strmatch(next, "tan" ) ) d= tan(d);
  97. else if( strmatch(next, "exp" ) ) d= exp(d);
  98. else if( strmatch(next, "log" ) ) d= log(d);
  99. else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
  100. else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
  101. else if( strmatch(next, "abs" ) ) d= fabs(d);
  102. else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2;
  103. else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2;
  104. else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0;
  105. else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0;
  106. else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0;
  107. else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0;
  108. else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0;
  109. else if( strmatch(next, "(" ) ) d= d;
  110. // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1);
  111. // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
  112. else{
  113. for(i=0; p->func1_name && p->func1_name[i]; i++){
  114. if(strmatch(next, p->func1_name[i])){
  115. return p->func1[i](p->opaque, d);
  116. }
  117. }
  118. for(i=0; p->func2_name && p->func2_name[i]; i++){
  119. if(strmatch(next, p->func2_name[i])){
  120. return p->func2[i](p->opaque, d, d2);
  121. }
  122. }
  123. av_log(NULL, AV_LOG_ERROR, "Parser: unknown function in \"%s\"\n", next);
  124. return NAN;
  125. }
  126. return d;
  127. }
  128. static double evalPow(Parser *p){
  129. if(p->s[0]=='+') p->s++;
  130. if(p->s[0]=='-'){
  131. p->s++;
  132. return -evalPrimary(p);
  133. }else
  134. return evalPrimary(p);
  135. }
  136. static double evalFactor(Parser *p){
  137. double ret= evalPow(p);
  138. while(p->s[0]=='^'){
  139. p->s++;
  140. ret= pow(ret, evalPow(p));
  141. }
  142. return ret;
  143. }
  144. static double evalTerm(Parser *p){
  145. double ret= evalFactor(p);
  146. while(p->s[0]=='*' || p->s[0]=='/'){
  147. if(*p->s++ == '*') ret*= evalFactor(p);
  148. else ret/= evalFactor(p);
  149. }
  150. return ret;
  151. }
  152. static double evalExpression(Parser *p){
  153. double ret;
  154. if(p->stack_index <= 0) //protect against stack overflows
  155. return NAN;
  156. p->stack_index--;
  157. ret= evalTerm(p);
  158. while(p->s[0]=='+' || p->s[0]=='-'){
  159. if(*p->s++ == '+') ret+= evalTerm(p);
  160. else ret-= evalTerm(p);
  161. }
  162. p->stack_index++;
  163. return ret;
  164. }
  165. double ff_eval(char *s, double *const_value, const char **const_name,
  166. double (**func1)(void *, double), const char **func1_name,
  167. double (**func2)(void *, double, double), char **func2_name,
  168. void *opaque){
  169. Parser p;
  170. p.stack_index=100;
  171. p.s= s;
  172. p.const_value= const_value;
  173. p.const_name = const_name;
  174. p.func1 = func1;
  175. p.func1_name = func1_name;
  176. p.func2 = func2;
  177. p.func2_name = func2_name;
  178. p.opaque = opaque;
  179. return evalExpression(&p);
  180. }
  181. #ifdef TEST
  182. #undef printf
  183. static double const_values[]={
  184. M_PI,
  185. M_E,
  186. 0
  187. };
  188. static const char *const_names[]={
  189. "PI",
  190. "E",
  191. 0
  192. };
  193. main(){
  194. 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));
  195. }
  196. #endif