-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression.h
116 lines (93 loc) · 3.76 KB
/
expression.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/** A compact math expression engine
* @file expression.h
*
* @date Apr 6, 2014
* @author Craig Hesling
*/
#ifndef EXPRESSION_H_INCLUDED
#define EXPRESSION_H_INCLUDED
#include <stddef.h> /* size_t */
#include "types.h"
#include "symbolic.h"
#include "expression_lite.h" /* expression_t only exists in expression_lite.h */
/*---------------------------------------------*
* Misc. String Buffer Definitions *
*---------------------------------------------*/
#define EXP_BUF_SIZE 256
typedef char exp_buf[EXP_BUF_SIZE];
/*---------------------------------------------*
* expression tree structure *
*---------------------------------------------*/
/* The tree operation element for the expression structure */
/** Expression data container for the expanded expression.
* Expanded tree expressions are composed of an operation that joins a left sub-expression and a right rub-expression.
*/
struct expression_data_tree {
/** The joining operation of the two two sub expressions.
* \note Previously implemented using enumerations, but proved to be more of a burden.
*/
char op; // '+', '-', '*', or '/'
expression_t left; ///< Left sub-expression
expression_t right; ///< Right sub-expression
};
/*---------------------------------------------*
* expression components *
*---------------------------------------------*/
/** Expression types.
* This is an enumeration of \ref expression_t's types.
* Specific types correspond to specific data containers in \ref expression_data.
*/
enum expression_type {
// EXP_UNDEF, ///< \note Might remove
EXP_VALUE, ///< Contains a simple numeric values and undefines
EXP_TREE, ///< An expression over an operation and sub-expression
EXP_SYMBOLIC ///< Symbolic named references
};
/** Expression data container.
* This union allows for the use of all the different expression data containers that corresponding to each \ref expression_type.
*/
union expression_data {
value_t val; ///< Value for EXP_VALUE type.
struct expression_data_tree tree; ///< Data for EXP_TREE type.
sym_t sym; ///< Data for EXP_SYMBOLIC type.
};
/*---------------------------------------------*
* expression structure *
*---------------------------------------------*/
/** The stored representation of mathematical expressions.
* An expression is either an expanded value of an operation on two other sub-expressions, a direct value (evaluated expression), or a symbolic expression.
* An expression consists of an \ref expression_type and it's \ref expression_data.
*/
/// \note New types must have an entry in the \ref type enumeration and an associated entry in the \ref data union.
struct expression {
enum expression_type type; ///< The expression's selected type
union expression_data data; ///< The expression's data corresponding to it's \ref type.
};
/*---------------------------------------------*
* expression_t allocation functions *
*---------------------------------------------*/
expression_t
expression_new (void);
expression_t
expression_new_value (value_t val);
expression_t
expression_new_tree (char op,
expression_t left,
expression_t right);
expression_t
expression_new_sym (sym_t sym);
void
expression_free (expression_t exp);
/*---------------------------------------------*
* expression_t manipulation functions *
*---------------------------------------------*/
value_t
expression_evaluate (expression_t exp);
void
expression_to_string (char *dst_str,
expression_t src_exp);
expression_t
string_to_expression (size_t str_len,
char const *str);
#endif // EXPRESSION_H_INCLUDED
/* vim: set ts=4 sw=4 expandtab: */