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.

250 lines
6.3KB

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