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.

87 lines
2.3KB

  1. /*
  2. * TINYEXPR - Tiny recursive descent parser and evaluation engine in C
  3. *
  4. * Copyright (c) 2015-2018 Lewis Van Winkle
  5. *
  6. * http://CodePlea.com
  7. *
  8. * This software is provided 'as-is', without any express or implied
  9. * warranty. In no event will the authors be held liable for any damages
  10. * arising from the use of this software.
  11. *
  12. * Permission is granted to anyone to use this software for any purpose,
  13. * including commercial applications, and to alter it and redistribute it
  14. * freely, subject to the following restrictions:
  15. *
  16. * 1. The origin of this software must not be misrepresented; you must not
  17. * claim that you wrote the original software. If you use this software
  18. * in a product, an acknowledgement in the product documentation would be
  19. * appreciated but is not required.
  20. * 2. Altered source versions must be plainly marked as such, and must not be
  21. * misrepresented as being the original software.
  22. * 3. This notice may not be removed or altered from any source distribution.
  23. */
  24. #ifndef __TINYEXPR_H__
  25. #define __TINYEXPR_H__
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. typedef struct te_expr {
  30. int type;
  31. union {double value; const double *bound; const void *function;};
  32. void *parameters[1];
  33. } te_expr;
  34. enum {
  35. TE_VARIABLE = 0,
  36. TE_FUNCTION0 = 8, TE_FUNCTION1, TE_FUNCTION2, TE_FUNCTION3,
  37. TE_FUNCTION4, TE_FUNCTION5, TE_FUNCTION6, TE_FUNCTION7,
  38. TE_CLOSURE0 = 16, TE_CLOSURE1, TE_CLOSURE2, TE_CLOSURE3,
  39. TE_CLOSURE4, TE_CLOSURE5, TE_CLOSURE6, TE_CLOSURE7,
  40. TE_FLAG_PURE = 32
  41. };
  42. typedef struct te_variable {
  43. const char *name;
  44. const void *address;
  45. int type;
  46. void *context;
  47. } te_variable;
  48. /* Parses the input expression, evaluates it, and frees it. */
  49. /* Returns NaN on error. */
  50. double te_interp(const char *expression, int *error);
  51. /* Parses the input expression and binds variables. */
  52. /* Returns NULL on error. */
  53. te_expr *te_compile(const char *expression, const te_variable *variables, int var_count, int *error);
  54. /* Evaluates the expression. */
  55. double te_eval(const te_expr *n);
  56. /* Prints debugging information on the syntax tree. */
  57. void te_print(const te_expr *n);
  58. /* Frees the expression. */
  59. /* This is safe to call on NULL pointers. */
  60. void te_free(te_expr *n);
  61. #ifdef __cplusplus
  62. }
  63. #endif
  64. #endif /*__TINYEXPR_H__*/