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.

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