2
0

tinyexpr.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. // SPDX-License-Identifier: Zlib
  2. /*
  3. * TINYEXPR - Tiny recursive descent parser and evaluation engine in C
  4. *
  5. * Copyright (c) 2015-2020 Lewis Van Winkle
  6. *
  7. * http://CodePlea.com
  8. *
  9. * This software is provided 'as-is', without any express or implied
  10. * warranty. In no event will the authors be held liable for any damages
  11. * arising from the use of this software.
  12. *
  13. * Permission is granted to anyone to use this software for any purpose,
  14. * including commercial applications, and to alter it and redistribute it
  15. * freely, subject to the following restrictions:
  16. *
  17. * 1. The origin of this software must not be misrepresented; you must not
  18. * claim that you wrote the original software. If you use this software
  19. * in a product, an acknowledgement in the product documentation would be
  20. * appreciated but is not required.
  21. * 2. Altered source versions must be plainly marked as such, and must not be
  22. * misrepresented as being the original software.
  23. * 3. This notice may not be removed or altered from any source distribution.
  24. */
  25. /* COMPILE TIME OPTIONS */
  26. /* Exponentiation associativity:
  27. For a^b^c = (a^b)^c and -a^b = (-a)^b do nothing.
  28. For a^b^c = a^(b^c) and -a^b = -(a^b) uncomment the next line.*/
  29. /* #define TE_POW_FROM_RIGHT */
  30. /* Logarithms
  31. For log = base 10 log do nothing
  32. For log = natural log uncomment the next line. */
  33. /* #define TE_NAT_LOG */
  34. #include "tinyexpr.h"
  35. #include <stdlib.h>
  36. #include <math.h>
  37. #include <string.h>
  38. #include <stdio.h>
  39. #include <ctype.h>
  40. #include <limits.h>
  41. #ifndef NAN
  42. #define NAN (0.0/0.0)
  43. #endif
  44. #ifndef INFINITY
  45. #define INFINITY (1.0/0.0)
  46. #endif
  47. typedef double (*te_fun2)(double, double);
  48. enum {
  49. TOK_NULL = TE_CLOSURE7+1, TOK_ERROR, TOK_END, TOK_SEP,
  50. TOK_OPEN, TOK_CLOSE, TOK_NUMBER, TOK_VARIABLE, TOK_INFIX
  51. };
  52. enum {TE_CONSTANT = 1};
  53. typedef struct state {
  54. const char *start;
  55. const char *next;
  56. int type;
  57. union {double value; const double *bound; const void *function;};
  58. void *context;
  59. const te_variable *lookup;
  60. int lookup_len;
  61. } state;
  62. #define TYPE_MASK(TYPE) ((TYPE)&0x0000001F)
  63. #define IS_PURE(TYPE) (((TYPE) & TE_FLAG_PURE) != 0)
  64. #define IS_FUNCTION(TYPE) (((TYPE) & TE_FUNCTION0) != 0)
  65. #define IS_CLOSURE(TYPE) (((TYPE) & TE_CLOSURE0) != 0)
  66. #define ARITY(TYPE) ( ((TYPE) & (TE_FUNCTION0 | TE_CLOSURE0)) ? ((TYPE) & 0x00000007) : 0 )
  67. #define NEW_EXPR(type, ...) new_expr((type), (const te_expr*[]){__VA_ARGS__})
  68. #define CHECK_NULL(ptr, ...) if ((ptr) == NULL) { __VA_ARGS__; return NULL; }
  69. static te_expr *new_expr(const int type, const te_expr *parameters[]) {
  70. const int arity = ARITY(type);
  71. const int psize = sizeof(void*) * arity;
  72. const int size = (sizeof(te_expr) - sizeof(void*)) + psize + (IS_CLOSURE(type) ? sizeof(void*) : 0);
  73. te_expr *ret = malloc(size);
  74. CHECK_NULL(ret);
  75. memset(ret, 0, size);
  76. if (arity && parameters) {
  77. memcpy(ret->parameters, parameters, psize);
  78. }
  79. ret->type = type;
  80. ret->bound = 0;
  81. return ret;
  82. }
  83. void te_free_parameters(te_expr *n) {
  84. if (!n) return;
  85. switch (TYPE_MASK(n->type)) {
  86. case TE_FUNCTION7: case TE_CLOSURE7: te_free(n->parameters[6]); /* Falls through. */
  87. case TE_FUNCTION6: case TE_CLOSURE6: te_free(n->parameters[5]); /* Falls through. */
  88. case TE_FUNCTION5: case TE_CLOSURE5: te_free(n->parameters[4]); /* Falls through. */
  89. case TE_FUNCTION4: case TE_CLOSURE4: te_free(n->parameters[3]); /* Falls through. */
  90. case TE_FUNCTION3: case TE_CLOSURE3: te_free(n->parameters[2]); /* Falls through. */
  91. case TE_FUNCTION2: case TE_CLOSURE2: te_free(n->parameters[1]); /* Falls through. */
  92. case TE_FUNCTION1: case TE_CLOSURE1: te_free(n->parameters[0]);
  93. }
  94. }
  95. void te_free(te_expr *n) {
  96. if (!n) return;
  97. te_free_parameters(n);
  98. free(n);
  99. }
  100. static double pi(void) {return 3.14159265358979323846;}
  101. static double e(void) {return 2.71828182845904523536;}
  102. static double fac(double a) {/* simplest version of fac */
  103. if (a < 0.0)
  104. return NAN;
  105. if (a > UINT_MAX)
  106. return INFINITY;
  107. unsigned int ua = (unsigned int)(a);
  108. unsigned long int result = 1, i;
  109. for (i = 1; i <= ua; i++) {
  110. if (i > ULONG_MAX / result)
  111. return INFINITY;
  112. result *= i;
  113. }
  114. return (double)result;
  115. }
  116. static double ncr(double n, double r) {
  117. if (n < 0.0 || r < 0.0 || n < r) return NAN;
  118. if (n > UINT_MAX || r > UINT_MAX) return INFINITY;
  119. unsigned long int un = (unsigned int)(n), ur = (unsigned int)(r), i;
  120. unsigned long int result = 1;
  121. if (ur > un / 2) ur = un - ur;
  122. for (i = 1; i <= ur; i++) {
  123. if (result > ULONG_MAX / (un - ur + i))
  124. return INFINITY;
  125. result *= un - ur + i;
  126. result /= i;
  127. }
  128. return result;
  129. }
  130. static double npr(double n, double r) {return ncr(n, r) * fac(r);}
  131. #ifdef _MSC_VER
  132. #pragma function (ceil)
  133. #pragma function (floor)
  134. #endif
  135. static const te_variable functions[] = {
  136. /* must be in alphabetical order */
  137. {"abs", fabs, TE_FUNCTION1 | TE_FLAG_PURE, 0},
  138. {"acos", acos, TE_FUNCTION1 | TE_FLAG_PURE, 0},
  139. {"asin", asin, TE_FUNCTION1 | TE_FLAG_PURE, 0},
  140. {"atan", atan, TE_FUNCTION1 | TE_FLAG_PURE, 0},
  141. {"atan2", atan2, TE_FUNCTION2 | TE_FLAG_PURE, 0},
  142. {"ceil", ceil, TE_FUNCTION1 | TE_FLAG_PURE, 0},
  143. {"cos", cos, TE_FUNCTION1 | TE_FLAG_PURE, 0},
  144. {"cosh", cosh, TE_FUNCTION1 | TE_FLAG_PURE, 0},
  145. {"e", e, TE_FUNCTION0 | TE_FLAG_PURE, 0},
  146. {"exp", exp, TE_FUNCTION1 | TE_FLAG_PURE, 0},
  147. {"fac", fac, TE_FUNCTION1 | TE_FLAG_PURE, 0},
  148. {"floor", floor, TE_FUNCTION1 | TE_FLAG_PURE, 0},
  149. {"ln", log, TE_FUNCTION1 | TE_FLAG_PURE, 0},
  150. #ifdef TE_NAT_LOG
  151. {"log", log, TE_FUNCTION1 | TE_FLAG_PURE, 0},
  152. #else
  153. {"log", log10, TE_FUNCTION1 | TE_FLAG_PURE, 0},
  154. #endif
  155. {"log10", log10, TE_FUNCTION1 | TE_FLAG_PURE, 0},
  156. {"ncr", ncr, TE_FUNCTION2 | TE_FLAG_PURE, 0},
  157. {"npr", npr, TE_FUNCTION2 | TE_FLAG_PURE, 0},
  158. {"pi", pi, TE_FUNCTION0 | TE_FLAG_PURE, 0},
  159. {"pow", pow, TE_FUNCTION2 | TE_FLAG_PURE, 0},
  160. {"sin", sin, TE_FUNCTION1 | TE_FLAG_PURE, 0},
  161. {"sinh", sinh, TE_FUNCTION1 | TE_FLAG_PURE, 0},
  162. {"sqrt", sqrt, TE_FUNCTION1 | TE_FLAG_PURE, 0},
  163. {"tan", tan, TE_FUNCTION1 | TE_FLAG_PURE, 0},
  164. {"tanh", tanh, TE_FUNCTION1 | TE_FLAG_PURE, 0},
  165. {0, 0, 0, 0}
  166. };
  167. static const te_variable *find_builtin(const char *name, int len) {
  168. int imin = 0;
  169. int imax = sizeof(functions) / sizeof(te_variable) - 2;
  170. /*Binary search.*/
  171. while (imax >= imin) {
  172. const int i = (imin + ((imax-imin)/2));
  173. int c = strncmp(name, functions[i].name, len);
  174. if (!c) c = '\0' - functions[i].name[len];
  175. if (c == 0) {
  176. return functions + i;
  177. } else if (c > 0) {
  178. imin = i + 1;
  179. } else {
  180. imax = i - 1;
  181. }
  182. }
  183. return 0;
  184. }
  185. static const te_variable *find_lookup(const state *s, const char *name, int len) {
  186. int iters;
  187. const te_variable *var;
  188. if (!s->lookup) return 0;
  189. for (var = s->lookup, iters = s->lookup_len; iters; ++var, --iters) {
  190. if (strncmp(name, var->name, len) == 0 && var->name[len] == '\0') {
  191. return var;
  192. }
  193. }
  194. return 0;
  195. }
  196. static double add(double a, double b) {return a + b;}
  197. static double sub(double a, double b) {return a - b;}
  198. static double mul(double a, double b) {return a * b;}
  199. static double divide(double a, double b) {return a / b;}
  200. static double negate(double a) {return -a;}
  201. static double comma(double a, double b) {(void)a; return b;}
  202. void next_token(state *s) {
  203. s->type = TOK_NULL;
  204. do {
  205. if (!*s->next){
  206. s->type = TOK_END;
  207. return;
  208. }
  209. /* Try reading a number. */
  210. if ((s->next[0] >= '0' && s->next[0] <= '9') || s->next[0] == '.') {
  211. s->value = strtod(s->next, (char**)&s->next);
  212. s->type = TOK_NUMBER;
  213. } else {
  214. /* Look for a variable or builtin function call. */
  215. if (isalpha(s->next[0])) {
  216. const char *start;
  217. start = s->next;
  218. while (isalpha(s->next[0]) || isdigit(s->next[0]) || (s->next[0] == '_')) s->next++;
  219. const te_variable *var = find_lookup(s, start, s->next - start);
  220. if (!var) var = find_builtin(start, s->next - start);
  221. if (!var) {
  222. s->type = TOK_ERROR;
  223. } else {
  224. switch(TYPE_MASK(var->type))
  225. {
  226. case TE_VARIABLE:
  227. s->type = TOK_VARIABLE;
  228. s->bound = var->address;
  229. break;
  230. case TE_CLOSURE0: case TE_CLOSURE1: case TE_CLOSURE2: case TE_CLOSURE3: /* Falls through. */
  231. case TE_CLOSURE4: case TE_CLOSURE5: case TE_CLOSURE6: case TE_CLOSURE7: /* Falls through. */
  232. s->context = var->context; /* Falls through. */
  233. case TE_FUNCTION0: case TE_FUNCTION1: case TE_FUNCTION2: case TE_FUNCTION3: /* Falls through. */
  234. case TE_FUNCTION4: case TE_FUNCTION5: case TE_FUNCTION6: case TE_FUNCTION7: /* Falls through. */
  235. s->type = var->type;
  236. s->function = var->address;
  237. break;
  238. }
  239. }
  240. } else {
  241. /* Look for an operator or special character. */
  242. switch (s->next++[0]) {
  243. case '+': s->type = TOK_INFIX; s->function = add; break;
  244. case '-': s->type = TOK_INFIX; s->function = sub; break;
  245. case '*': s->type = TOK_INFIX; s->function = mul; break;
  246. case '/': s->type = TOK_INFIX; s->function = divide; break;
  247. case '^': s->type = TOK_INFIX; s->function = pow; break;
  248. case '%': s->type = TOK_INFIX; s->function = fmod; break;
  249. case '(': s->type = TOK_OPEN; break;
  250. case ')': s->type = TOK_CLOSE; break;
  251. case ',': s->type = TOK_SEP; break;
  252. case ' ': case '\t': case '\n': case '\r': break;
  253. default: s->type = TOK_ERROR; break;
  254. }
  255. }
  256. }
  257. } while (s->type == TOK_NULL);
  258. }
  259. static te_expr *list(state *s);
  260. static te_expr *expr(state *s);
  261. static te_expr *power(state *s);
  262. static te_expr *base(state *s) {
  263. /* <base> = <constant> | <variable> | <function-0> {"(" ")"} | <function-1> <power> | <function-X> "(" <expr> {"," <expr>} ")" | "(" <list> ")" */
  264. te_expr *ret;
  265. int arity;
  266. switch (TYPE_MASK(s->type)) {
  267. case TOK_NUMBER:
  268. ret = new_expr(TE_CONSTANT, 0);
  269. CHECK_NULL(ret);
  270. ret->value = s->value;
  271. next_token(s);
  272. break;
  273. case TOK_VARIABLE:
  274. ret = new_expr(TE_VARIABLE, 0);
  275. CHECK_NULL(ret);
  276. ret->bound = s->bound;
  277. next_token(s);
  278. break;
  279. case TE_FUNCTION0:
  280. case TE_CLOSURE0:
  281. ret = new_expr(s->type, 0);
  282. CHECK_NULL(ret);
  283. ret->function = s->function;
  284. if (IS_CLOSURE(s->type)) ret->parameters[0] = s->context;
  285. next_token(s);
  286. if (s->type == TOK_OPEN) {
  287. next_token(s);
  288. if (s->type != TOK_CLOSE) {
  289. s->type = TOK_ERROR;
  290. } else {
  291. next_token(s);
  292. }
  293. }
  294. break;
  295. case TE_FUNCTION1:
  296. case TE_CLOSURE1:
  297. ret = new_expr(s->type, 0);
  298. CHECK_NULL(ret);
  299. ret->function = s->function;
  300. if (IS_CLOSURE(s->type)) ret->parameters[1] = s->context;
  301. next_token(s);
  302. ret->parameters[0] = power(s);
  303. CHECK_NULL(ret->parameters[0], te_free(ret));
  304. break;
  305. case TE_FUNCTION2: case TE_FUNCTION3: case TE_FUNCTION4:
  306. case TE_FUNCTION5: case TE_FUNCTION6: case TE_FUNCTION7:
  307. case TE_CLOSURE2: case TE_CLOSURE3: case TE_CLOSURE4:
  308. case TE_CLOSURE5: case TE_CLOSURE6: case TE_CLOSURE7:
  309. arity = ARITY(s->type);
  310. ret = new_expr(s->type, 0);
  311. CHECK_NULL(ret);
  312. ret->function = s->function;
  313. if (IS_CLOSURE(s->type)) ret->parameters[arity] = s->context;
  314. next_token(s);
  315. if (s->type != TOK_OPEN) {
  316. s->type = TOK_ERROR;
  317. } else {
  318. int i;
  319. for(i = 0; i < arity; i++) {
  320. next_token(s);
  321. ret->parameters[i] = expr(s);
  322. CHECK_NULL(ret->parameters[i], te_free(ret));
  323. if(s->type != TOK_SEP) {
  324. break;
  325. }
  326. }
  327. if(s->type != TOK_CLOSE || i != arity - 1) {
  328. s->type = TOK_ERROR;
  329. } else {
  330. next_token(s);
  331. }
  332. }
  333. break;
  334. case TOK_OPEN:
  335. next_token(s);
  336. ret = list(s);
  337. CHECK_NULL(ret);
  338. if (s->type != TOK_CLOSE) {
  339. s->type = TOK_ERROR;
  340. } else {
  341. next_token(s);
  342. }
  343. break;
  344. default:
  345. ret = new_expr(0, 0);
  346. CHECK_NULL(ret);
  347. s->type = TOK_ERROR;
  348. ret->value = NAN;
  349. break;
  350. }
  351. return ret;
  352. }
  353. static te_expr *power(state *s) {
  354. /* <power> = {("-" | "+")} <base> */
  355. int sign = 1;
  356. while (s->type == TOK_INFIX && (s->function == add || s->function == sub)) {
  357. if (s->function == sub) sign = -sign;
  358. next_token(s);
  359. }
  360. te_expr *ret;
  361. if (sign == 1) {
  362. ret = base(s);
  363. } else {
  364. te_expr *b = base(s);
  365. CHECK_NULL(b);
  366. ret = NEW_EXPR(TE_FUNCTION1 | TE_FLAG_PURE, b);
  367. CHECK_NULL(ret, te_free(b));
  368. ret->function = negate;
  369. }
  370. return ret;
  371. }
  372. #ifdef TE_POW_FROM_RIGHT
  373. static te_expr *factor(state *s) {
  374. /* <factor> = <power> {"^" <power>} */
  375. te_expr *ret = power(s);
  376. CHECK_NULL(ret);
  377. int neg = 0;
  378. if (ret->type == (TE_FUNCTION1 | TE_FLAG_PURE) && ret->function == negate) {
  379. te_expr *se = ret->parameters[0];
  380. free(ret);
  381. ret = se;
  382. neg = 1;
  383. }
  384. te_expr *insertion = 0;
  385. while (s->type == TOK_INFIX && (s->function == pow)) {
  386. te_fun2 t = s->function;
  387. next_token(s);
  388. if (insertion) {
  389. /* Make exponentiation go right-to-left. */
  390. te_expr *p = power(s);
  391. CHECK_NULL(p, te_free(ret));
  392. te_expr *insert = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, insertion->parameters[1], p);
  393. CHECK_NULL(insert, te_free(p), te_free(ret));
  394. insert->function = t;
  395. insertion->parameters[1] = insert;
  396. insertion = insert;
  397. } else {
  398. te_expr *p = power(s);
  399. CHECK_NULL(p, te_free(ret));
  400. te_expr *prev = ret;
  401. ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, p);
  402. CHECK_NULL(ret, te_free(p), te_free(prev));
  403. ret->function = t;
  404. insertion = ret;
  405. }
  406. }
  407. if (neg) {
  408. te_expr *prev = ret;
  409. ret = NEW_EXPR(TE_FUNCTION1 | TE_FLAG_PURE, ret);
  410. CHECK_NULL(ret, te_free(prev));
  411. ret->function = negate;
  412. }
  413. return ret;
  414. }
  415. #else
  416. static te_expr *factor(state *s) {
  417. /* <factor> = <power> {"^" <power>} */
  418. te_expr *ret = power(s);
  419. CHECK_NULL(ret);
  420. while (s->type == TOK_INFIX && (s->function == pow)) {
  421. te_fun2 t = s->function;
  422. next_token(s);
  423. te_expr *p = power(s);
  424. CHECK_NULL(p, te_free(ret));
  425. te_expr *prev = ret;
  426. ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, p);
  427. CHECK_NULL(ret, te_free(p), te_free(prev));
  428. ret->function = t;
  429. }
  430. return ret;
  431. }
  432. #endif
  433. static te_expr *term(state *s) {
  434. /* <term> = <factor> {("*" | "/" | "%") <factor>} */
  435. te_expr *ret = factor(s);
  436. CHECK_NULL(ret);
  437. while (s->type == TOK_INFIX && (s->function == mul || s->function == divide || s->function == fmod)) {
  438. te_fun2 t = s->function;
  439. next_token(s);
  440. te_expr *f = factor(s);
  441. CHECK_NULL(f, te_free(ret));
  442. te_expr *prev = ret;
  443. ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, f);
  444. CHECK_NULL(ret, te_free(f), te_free(prev));
  445. ret->function = t;
  446. }
  447. return ret;
  448. }
  449. static te_expr *expr(state *s) {
  450. /* <expr> = <term> {("+" | "-") <term>} */
  451. te_expr *ret = term(s);
  452. CHECK_NULL(ret);
  453. while (s->type == TOK_INFIX && (s->function == add || s->function == sub)) {
  454. te_fun2 t = s->function;
  455. next_token(s);
  456. te_expr *te = term(s);
  457. CHECK_NULL(te, te_free(ret));
  458. te_expr *prev = ret;
  459. ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, te);
  460. CHECK_NULL(ret, te_free(te), te_free(prev));
  461. ret->function = t;
  462. }
  463. return ret;
  464. }
  465. static te_expr *list(state *s) {
  466. /* <list> = <expr> {"," <expr>} */
  467. te_expr *ret = expr(s);
  468. CHECK_NULL(ret);
  469. while (s->type == TOK_SEP) {
  470. next_token(s);
  471. te_expr *e = expr(s);
  472. CHECK_NULL(e, te_free(ret));
  473. te_expr *prev = ret;
  474. ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, e);
  475. CHECK_NULL(ret, te_free(e), te_free(prev));
  476. ret->function = comma;
  477. }
  478. return ret;
  479. }
  480. #define TE_FUN(...) ((double(*)(__VA_ARGS__))n->function)
  481. #define M(e) te_eval(n->parameters[e])
  482. double te_eval(const te_expr *n) {
  483. if (!n) return NAN;
  484. switch(TYPE_MASK(n->type)) {
  485. case TE_CONSTANT: return n->value;
  486. case TE_VARIABLE: return *n->bound;
  487. case TE_FUNCTION0: case TE_FUNCTION1: case TE_FUNCTION2: case TE_FUNCTION3:
  488. case TE_FUNCTION4: case TE_FUNCTION5: case TE_FUNCTION6: case TE_FUNCTION7:
  489. switch(ARITY(n->type)) {
  490. case 0: return TE_FUN(void)();
  491. case 1: return TE_FUN(double)(M(0));
  492. case 2: return TE_FUN(double, double)(M(0), M(1));
  493. case 3: return TE_FUN(double, double, double)(M(0), M(1), M(2));
  494. case 4: return TE_FUN(double, double, double, double)(M(0), M(1), M(2), M(3));
  495. case 5: return TE_FUN(double, double, double, double, double)(M(0), M(1), M(2), M(3), M(4));
  496. case 6: return TE_FUN(double, double, double, double, double, double)(M(0), M(1), M(2), M(3), M(4), M(5));
  497. case 7: return TE_FUN(double, double, double, double, double, double, double)(M(0), M(1), M(2), M(3), M(4), M(5), M(6));
  498. default: return NAN;
  499. }
  500. case TE_CLOSURE0: case TE_CLOSURE1: case TE_CLOSURE2: case TE_CLOSURE3:
  501. case TE_CLOSURE4: case TE_CLOSURE5: case TE_CLOSURE6: case TE_CLOSURE7:
  502. switch(ARITY(n->type)) {
  503. case 0: return TE_FUN(void*)(n->parameters[0]);
  504. case 1: return TE_FUN(void*, double)(n->parameters[1], M(0));
  505. case 2: return TE_FUN(void*, double, double)(n->parameters[2], M(0), M(1));
  506. case 3: return TE_FUN(void*, double, double, double)(n->parameters[3], M(0), M(1), M(2));
  507. case 4: return TE_FUN(void*, double, double, double, double)(n->parameters[4], M(0), M(1), M(2), M(3));
  508. case 5: return TE_FUN(void*, double, double, double, double, double)(n->parameters[5], M(0), M(1), M(2), M(3), M(4));
  509. case 6: return TE_FUN(void*, double, double, double, double, double, double)(n->parameters[6], M(0), M(1), M(2), M(3), M(4), M(5));
  510. case 7: return TE_FUN(void*, double, double, double, double, double, double, double)(n->parameters[7], M(0), M(1), M(2), M(3), M(4), M(5), M(6));
  511. default: return NAN;
  512. }
  513. default: return NAN;
  514. }
  515. }
  516. #undef TE_FUN
  517. #undef M
  518. static void optimize(te_expr *n) {
  519. /* Evaluates as much as possible. */
  520. if (n->type == TE_CONSTANT) return;
  521. if (n->type == TE_VARIABLE) return;
  522. /* Only optimize out functions flagged as pure. */
  523. if (IS_PURE(n->type)) {
  524. const int arity = ARITY(n->type);
  525. int known = 1;
  526. int i;
  527. for (i = 0; i < arity; ++i) {
  528. optimize(n->parameters[i]);
  529. if (((te_expr*)(n->parameters[i]))->type != TE_CONSTANT) {
  530. known = 0;
  531. }
  532. }
  533. if (known) {
  534. const double value = te_eval(n);
  535. te_free_parameters(n);
  536. n->type = TE_CONSTANT;
  537. n->value = value;
  538. }
  539. }
  540. }
  541. te_expr *te_compile(const char *expression, const te_variable *variables, int var_count, int *error) {
  542. state s;
  543. s.start = s.next = expression;
  544. s.lookup = variables;
  545. s.lookup_len = var_count;
  546. next_token(&s);
  547. te_expr *root = list(&s);
  548. if (root == NULL) {
  549. if (error) *error = -1;
  550. return NULL;
  551. }
  552. if (s.type != TOK_END) {
  553. te_free(root);
  554. if (error) {
  555. *error = (s.next - s.start);
  556. if (*error == 0) *error = 1;
  557. }
  558. return 0;
  559. } else {
  560. optimize(root);
  561. if (error) *error = 0;
  562. return root;
  563. }
  564. }
  565. double te_interp(const char *expression, int *error) {
  566. te_expr *n = te_compile(expression, 0, 0, error);
  567. if (n == NULL) {
  568. return NAN;
  569. }
  570. double ret;
  571. if (n) {
  572. ret = te_eval(n);
  573. te_free(n);
  574. } else {
  575. ret = NAN;
  576. }
  577. return ret;
  578. }
  579. static void pn (const te_expr *n, int depth) {
  580. int i, arity;
  581. printf("%*s", depth, "");
  582. switch(TYPE_MASK(n->type)) {
  583. case TE_CONSTANT: printf("%f\n", n->value); break;
  584. case TE_VARIABLE: printf("bound %p\n", n->bound); break;
  585. case TE_FUNCTION0: case TE_FUNCTION1: case TE_FUNCTION2: case TE_FUNCTION3:
  586. case TE_FUNCTION4: case TE_FUNCTION5: case TE_FUNCTION6: case TE_FUNCTION7:
  587. case TE_CLOSURE0: case TE_CLOSURE1: case TE_CLOSURE2: case TE_CLOSURE3:
  588. case TE_CLOSURE4: case TE_CLOSURE5: case TE_CLOSURE6: case TE_CLOSURE7:
  589. arity = ARITY(n->type);
  590. printf("f%d", arity);
  591. for(i = 0; i < arity; i++) {
  592. printf(" %p", n->parameters[i]);
  593. }
  594. printf("\n");
  595. for(i = 0; i < arity; i++) {
  596. pn(n->parameters[i], depth + 1);
  597. }
  598. break;
  599. }
  600. }
  601. void te_print(const te_expr *n) {
  602. pn(n, 0);
  603. }