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.

242 lines
6.2KB

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