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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
#ifndef EXPRESSION_H
#define EXPRESSION_H
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "vec.h"
#define EXPRESSION_NAME_MAX 64
enum expression_error_code {
EXPRESSION_OK,
EXPRESSION_SYNTAX_ERROR,
EXPRESSION_PAREN_ERROR,
EXPRESSION_UNKNOWN_VARIABLE,
EXPRESSION_UNSET_VARIABLE,
EXPRESSION_DIVISION_BY_ZERO,
};
struct expression_error {
enum expression_error_code code;
size_t position;
};
struct expression_variable {
char name[EXPRESSION_NAME_MAX];
double value;
bool assigned;
};
struct expression_context {
struct vec variables;
};
static inline void expression_init(struct expression_context *ctx)
{
vec_init(&ctx->variables, struct expression_variable);
}
static inline void expression_destroy(struct expression_context *ctx)
{
vec_destroy(&ctx->variables);
}
static inline void expression_error(struct expression_error *error,
enum expression_error_code code,
size_t position)
{
if (error) {
error->code = code;
error->position = position;
}
}
static inline bool expression_name_start(unsigned char c)
{
return isalpha(c) || c == '_';
}
static inline bool expression_name_char(unsigned char c)
{
return isalnum(c) || c == '_';
}
static inline struct expression_variable *
expression_variable(struct expression_context *ctx, const char *name)
{
for (size_t i = 0; i < ctx->variables.size; ++i) {
struct expression_variable *v = vec_at(&ctx->variables, i);
if (strcmp(v->name, name) == 0) {
return v;
}
}
return NULL;
}
static inline bool expression_set_variable(struct expression_context *ctx,
const char *name,
double value)
{
if (!expression_name_start((unsigned char)name[0]) ||
strlen(name) >= EXPRESSION_NAME_MAX) return false;
for (const unsigned char *p = (const unsigned char *)name + 1; *p; ++p) {
if (!expression_name_char(*p)) {
return false;
}
}
struct expression_variable *v = expression_variable(ctx, name);
if (!v) {
struct expression_variable item = { .value = value, .assigned = true };
strcpy(item.name, name);
if (!vec_push_back(&ctx->variables, &item)) {
return false;
}
} else {
v->value = value;
v->assigned = true;
}
return true;
}
static inline bool expression_collect_variables(struct expression_context *ctx,
const char *text,
struct expression_error *error)
{
vec_clear(&ctx->variables);
for (size_t i = 0; text[i];) {
unsigned char c = (unsigned char)text[i];
if (isspace(c)) {
++i;
} else if (expression_name_start(c)) {
size_t start = i++;
while (expression_name_char((unsigned char)text[i])) {
++i;
}
size_t length = i - start;
if (length >= EXPRESSION_NAME_MAX) {
expression_error(error, EXPRESSION_SYNTAX_ERROR, start);
return false;
}
char name[EXPRESSION_NAME_MAX];
memcpy(name, text + start, length);
name[length] = '\0';
if (!expression_variable(ctx, name)) {
struct expression_variable item = { .assigned = false };
strcpy(item.name, name);
if (!vec_push_back(&ctx->variables, &item)) {
expression_error(error, EXPRESSION_SYNTAX_ERROR, start);
return false;
}
}
} else if (isdigit(c) || c == '.' || c == '+' || c == '-' ||
c == '*' || c == '/' || c == '(' || c == ')') {
++i;
} else {
expression_error(error, EXPRESSION_SYNTAX_ERROR, i);
return false;
}
}
expression_error(error, EXPRESSION_OK, 0);
return true;
}
struct expression_parser {
const char *text;
size_t position;
const struct expression_context *ctx;
struct expression_error *error;
};
static inline void expression_skip(struct expression_parser *p)
{
while (isspace((unsigned char)p->text[p->position])) {
++p->position;
}
}
static inline bool expression_parse_sum(struct expression_parser *, double *);
static inline bool expression_parse_primary(struct expression_parser *p,
double *out)
{
expression_skip(p);
size_t start = p->position;
char c = p->text[start];
if (c == '(') {
++p->position;
if (!expression_parse_sum(p, out)) return false;
expression_skip(p);
if (p->text[p->position] != ')') {
expression_error(p->error, EXPRESSION_PAREN_ERROR, p->position);
return false;
}
++p->position;
return true;
}
if (isdigit((unsigned char)c) || c == '.') {
char *end;
errno = 0;
*out = strtod(p->text + start, &end);
if (end == p->text + start || errno == ERANGE) {
expression_error(p->error, EXPRESSION_SYNTAX_ERROR, start);
return false;
}
p->position = (size_t)(end - p->text);
return true;
}
if (expression_name_start((unsigned char)c)) {
char name[EXPRESSION_NAME_MAX];
size_t length;
++p->position;
while (expression_name_char((unsigned char)p->text[p->position])) ++p->position;
length = p->position - start;
if (length >= sizeof(name)) {
expression_error(p->error, EXPRESSION_SYNTAX_ERROR, start);
return false;
}
memcpy(name, p->text + start, length);
name[length] = '\0';
const struct expression_variable *v =
expression_variable((struct expression_context *)p->ctx, name);
if (!v) expression_error(p->error, EXPRESSION_UNKNOWN_VARIABLE, start);
else if (!v->assigned) expression_error(p->error, EXPRESSION_UNSET_VARIABLE, start);
else *out = v->value;
return v && v->assigned;
}
expression_error(p->error, EXPRESSION_SYNTAX_ERROR, start);
return false;
}
static inline bool expression_parse_unary(struct expression_parser *p, double *out)
{
expression_skip(p);
char c = p->text[p->position];
if (c == '+' || c == '-') {
++p->position;
if (!expression_parse_unary(p, out)) return false;
if (c == '-') *out = -*out;
return true;
}
return expression_parse_primary(p, out);
}
static inline bool expression_parse_product(struct expression_parser *p, double *out)
{
if (!expression_parse_unary(p, out)) return false;
for (;;) {
expression_skip(p);
char op = p->text[p->position];
if (op != '*' && op != '/') return true;
++p->position;
double rhs;
if (!expression_parse_unary(p, &rhs)) return false;
if (op == '/') {
if (rhs == 0.0) {
expression_error(p->error, EXPRESSION_DIVISION_BY_ZERO, p->position);
return false;
}
*out /= rhs;
} else *out *= rhs;
}
}
static inline bool expression_parse_sum(struct expression_parser *p, double *out)
{
if (!expression_parse_product(p, out)) return false;
for (;;) {
expression_skip(p);
char op = p->text[p->position];
if (op != '+' && op != '-') return true;
++p->position;
double rhs;
if (!expression_parse_product(p, &rhs)) return false;
if (op == '+') *out += rhs;
else *out -= rhs;
}
}
static inline bool expression_eval(const struct expression_context *ctx,
const char *text,
double *result,
struct expression_error *error)
{
struct expression_parser p = { text, 0, ctx, error };
if (!expression_parse_sum(&p, result)) return false;
expression_skip(&p);
if (p.text[p.position]) {
expression_error(error,
p.text[p.position] == ')' ? EXPRESSION_PAREN_ERROR
: EXPRESSION_SYNTAX_ERROR,
p.position);
return false;
}
expression_error(error, EXPRESSION_OK, 0);
return true;
}
#endif /* EXPRESSION_H */
|