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
25#ifndef __TINYEXPR_H__
26#define __TINYEXPR_H__
27
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33
34
35typedef struct te_expr {
36 int type;
37 union {double value; const double *bound; const void *function;};
38 void *parameters[1];
39} te_expr;
40
41
42enum {
43 TE_VARIABLE = 0,
44
45 TE_FUNCTION0 = 8, TE_FUNCTION1, TE_FUNCTION2, TE_FUNCTION3,
46 TE_FUNCTION4, TE_FUNCTION5, TE_FUNCTION6, TE_FUNCTION7,
47
48 TE_CLOSURE0 = 16, TE_CLOSURE1, TE_CLOSURE2, TE_CLOSURE3,
49 TE_CLOSURE4, TE_CLOSURE5, TE_CLOSURE6, TE_CLOSURE7,
50
51 TE_FLAG_PURE = 32
52};
53
54typedef struct te_variable {
55 const char *name;
56 const void *address;
57 int type;
58 void *context;
59} te_variable;
60
61
62
63/* Parses the input expression, evaluates it, and frees it. */
64/* Returns NaN on error. */
65double te_interp(const char *expression, int *error);
66
67/* Parses the input expression and binds variables. */
68/* Returns NULL on error. */
69te_expr *te_compile(const char *expression, const te_variable *variables, int var_count, int *error);
70
71/* Evaluates the expression. */
72double te_eval(const te_expr *n);
73
74/* Prints debugging information on the syntax tree. */
75void te_print(const te_expr *n);
76
77/* Frees the expression. */
78/* This is safe to call on NULL pointers. */
79void te_free(te_expr *n);
80
81
82#ifdef __cplusplus
83}
84#endif
85
86#endif /*__TINYEXPR_H__*/
87