GCC Code Coverage Report


Directory: ./
File: util/tinyexpr.c
Date: 2026-10-29 08:11:58
Exec Total Coverage
Lines: 296 355 83.4%
Functions: 29 32 90.6%
Branches: 164 239 68.6%

Line Branch Exec Source
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 /* COMPILE TIME OPTIONS */
26
27 /* Exponentiation associativity:
28 For a^b^c = (a^b)^c and -a^b = (-a)^b do nothing.
29 For a^b^c = a^(b^c) and -a^b = -(a^b) uncomment the next line.*/
30 /* #define TE_POW_FROM_RIGHT */
31
32 /* Logarithms
33 For log = base 10 log do nothing
34 For log = natural log uncomment the next line. */
35 /* #define TE_NAT_LOG */
36
37 #include "tinyexpr.h"
38 #include <stdlib.h>
39 #include <math.h>
40 #include <string.h>
41 #include <stdio.h>
42 #include <limits.h>
43
44 #ifndef NAN
45 #define NAN (0.0/0.0)
46 #endif
47
48 #ifndef INFINITY
49 #define INFINITY (1.0/0.0)
50 #endif
51
52
53 typedef double (*te_fun2)(double, double);
54
55 enum {
56 TOK_NULL = TE_CLOSURE7+1, TOK_ERROR, TOK_END, TOK_SEP,
57 TOK_OPEN, TOK_CLOSE, TOK_NUMBER, TOK_VARIABLE, TOK_INFIX
58 };
59
60
61 enum {TE_CONSTANT = 1};
62
63
64 typedef struct state {
65 const char *start;
66 const char *next;
67 int type;
68 union {double value; const double *bound; const void *function;};
69 void *context;
70
71 const te_variable *lookup;
72 int lookup_len;
73 } state;
74
75
76 #define TYPE_MASK(TYPE) ((TYPE)&0x0000001F)
77
78 #define IS_PURE(TYPE) (((TYPE) & TE_FLAG_PURE) != 0)
79 #define IS_FUNCTION(TYPE) (((TYPE) & TE_FUNCTION0) != 0)
80 #define IS_CLOSURE(TYPE) (((TYPE) & TE_CLOSURE0) != 0)
81 #define ARITY(TYPE) ( ((TYPE) & (TE_FUNCTION0 | TE_CLOSURE0)) ? ((TYPE) & 0x00000007) : 0 )
82 #define NEW_EXPR(type, ...) new_expr((type), (const te_expr*[]){__VA_ARGS__})
83
84 6234 static te_expr *new_expr(const int type, const te_expr *parameters[]) {
85
2/2
✓ Branch 0 taken 2480 times.
✓ Branch 1 taken 3754 times.
6234 const int arity = ARITY(type);
86 6234 const int psize = sizeof(void*) * arity;
87 6234 const int size = (sizeof(te_expr) - sizeof(void*)) + psize + (IS_CLOSURE(type) ? sizeof(void*) : 0);
88 6234 te_expr *ret = malloc(size);
89 6234 memset(ret, 0, size);
90
4/4
✓ Branch 0 taken 2478 times.
✓ Branch 1 taken 3756 times.
✓ Branch 2 taken 2436 times.
✓ Branch 3 taken 42 times.
6234 if (arity && parameters) {
91 2436 memcpy(ret->parameters, parameters, psize);
92 }
93 6234 ret->type = type;
94 6234 ret->bound = 0;
95 6234 return ret;
96 }
97
98
99 6308 void te_free_parameters(te_expr *n) {
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6308 times.
6308 if (!n) return;
101
4/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 18 times.
✓ Branch 5 taken 2437 times.
✓ Branch 6 taken 23 times.
✓ Branch 7 taken 3830 times.
6308 switch (TYPE_MASK(n->type)) {
102 ✗ case TE_FUNCTION7: case TE_CLOSURE7: te_free(n->parameters[6]); /* Falls through. */
103 ✗ case TE_FUNCTION6: case TE_CLOSURE6: te_free(n->parameters[5]); /* Falls through. */
104 ✗ case TE_FUNCTION5: case TE_CLOSURE5: te_free(n->parameters[4]); /* Falls through. */
105 ✗ case TE_FUNCTION4: case TE_CLOSURE4: te_free(n->parameters[3]); /* Falls through. */
106 18 case TE_FUNCTION3: case TE_CLOSURE3: te_free(n->parameters[2]); /* Falls through. */
107 2455 case TE_FUNCTION2: case TE_CLOSURE2: te_free(n->parameters[1]); /* Falls through. */
108 2478 case TE_FUNCTION1: case TE_CLOSURE1: te_free(n->parameters[0]);
109 }
110 }
111
112
113 6234 void te_free(te_expr *n) {
114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6234 times.
6234 if (!n) return;
115 6234 te_free_parameters(n);
116 6234 free(n);
117 }
118
119
120 1 static double pi(void) {return 3.14159265358979323846;}
121 1 static double e(void) {return 2.71828182845904523536;}
122 3 static double fac(double a) {/* simplest version of fac */
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (a < 0.0)
124 ✗ return NAN;
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (a > UINT_MAX)
126 ✗ return INFINITY;
127 3 unsigned int ua = (unsigned int)(a);
128 3 unsigned long int result = 1, i;
129
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 3 times.
10 for (i = 1; i <= ua; i++) {
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (i > ULONG_MAX / result)
131 ✗ return INFINITY;
132 7 result *= i;
133 }
134 3 return (double)result;
135 }
136 2 static double ncr(double n, double r) {
137
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
2 if (n < 0.0 || r < 0.0 || n < r) return NAN;
138
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (n > UINT_MAX || r > UINT_MAX) return INFINITY;
139 2 unsigned long int un = (unsigned int)(n), ur = (unsigned int)(r), i;
140 2 unsigned long int result = 1;
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ur > un / 2) ur = un - ur;
142
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (i = 1; i <= ur; i++) {
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (result > ULONG_MAX / (un - ur + i))
144 ✗ return INFINITY;
145 4 result *= un - ur + i;
146 4 result /= i;
147 }
148 2 return result;
149 }
150 1 static double npr(double n, double r) {return ncr(n, r) * fac(r);}
151
152 15 static double rgb(double rd, double gd, double bd) {
153 15 int r = (int)rd;
154 15 int g = (int)gd;
155 15 int b = (int)bd;
156 15 int c = (r << 16) + (g << 8) + b;
157 15 return c;
158 }
159 13 static double hsv(double h, double s, double v) {
160
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
13 if (s == 0.0) {
161 1 v *= 255.0;
162 1 return rgb(v, v, v);
163 }
164 double red, green, blue;
165
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
12 if (!isfinite(h)) {
166 2 h = 0.0;
167 }
168 12 h -= floor(h); // wrap round the colour wheel, so 1.0 is red again
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (h >= 1.0) { // a tiny negative h rounds up to 1.0
170 ✗ h = 0.0;
171 }
172 12 double hue = h * 6.0; // sector 0 to 5
173 12 int i = (int)floor(hue);
174 12 double f = hue - i; // fractional part of h
175 12 double p = v * (1.0 - s);
176
177
6/6
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 1 times.
12 switch (i) {
178 5 case 0:
179 5 red = v;
180 5 green = v * (1.0 - s * (1.0 - f));
181 5 blue = p;
182 5 break;
183 1 case 1:
184 1 red = v * (1.0 - s * f);
185 1 green = v;
186 1 blue = p;
187 1 break;
188
189 1 case 2:
190 1 red = p;
191 1 green = v;
192 1 blue = v * (1.0 - s * (1.0 - f));
193 1 break;
194
195 2 case 3:
196 2 red = p;
197 2 green = v * (1.0 - s * f);
198 2 blue = v;
199 2 break;
200
201 2 case 4:
202 2 red = v * (1.0 - s * (1.0 - f));
203 2 green = p;
204 2 blue = v;
205 2 break;
206
207 1 default: // case 5:
208 1 red = v;
209 1 green = p;
210 1 blue = v * (1.0 - s * f);
211 1 break;
212 }
213 12 return rgb(red * 255.0, green * 255.0, blue * 255.0);
214 }
215 3 static double iffunc(double i, double v1, double v2) {
216
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (fabs(i) < 0.0001) {
217 2 return v2;
218 }
219 1 return v1;
220 }
221
222 static const te_variable functions[] = {
223 /* must be in alphabetical order */
224 {"abs", fabs, TE_FUNCTION1 | TE_FLAG_PURE, 0},
225 {"acos", acos, TE_FUNCTION1 | TE_FLAG_PURE, 0},
226 {"asin", asin, TE_FUNCTION1 | TE_FLAG_PURE, 0},
227 {"atan", atan, TE_FUNCTION1 | TE_FLAG_PURE, 0},
228 {"atan2", atan2, TE_FUNCTION2 | TE_FLAG_PURE, 0},
229 {"ceil", ceil, TE_FUNCTION1 | TE_FLAG_PURE, 0},
230 {"cos", cos, TE_FUNCTION1 | TE_FLAG_PURE, 0},
231 {"cosh", cosh, TE_FUNCTION1 | TE_FLAG_PURE, 0},
232 {"e", e, TE_FUNCTION0 | TE_FLAG_PURE, 0},
233 {"exp", exp, TE_FUNCTION1 | TE_FLAG_PURE, 0},
234 {"fac", fac, TE_FUNCTION1 | TE_FLAG_PURE, 0},
235 {"floor", floor, TE_FUNCTION1 | TE_FLAG_PURE, 0},
236 {"hsv", hsv, TE_FUNCTION3 | TE_FLAG_PURE, 0}, /* added for FPP */
237 {"if", iffunc, TE_FUNCTION3 | TE_FLAG_PURE, 0}, /* added for FPP */
238 {"ln", log, TE_FUNCTION1 | TE_FLAG_PURE, 0},
239 #ifdef TE_NAT_LOG
240 {"log", log, TE_FUNCTION1 | TE_FLAG_PURE, 0},
241 #else
242 {"log", log10, TE_FUNCTION1 | TE_FLAG_PURE, 0},
243 #endif
244 {"log10", log10, TE_FUNCTION1 | TE_FLAG_PURE, 0},
245 {"ncr", ncr, TE_FUNCTION2 | TE_FLAG_PURE, 0},
246 {"npr", npr, TE_FUNCTION2 | TE_FLAG_PURE, 0},
247 {"pi", pi, TE_FUNCTION0 | TE_FLAG_PURE, 0},
248 {"pow", pow, TE_FUNCTION2 | TE_FLAG_PURE, 0},
249 {"rgb", rgb, TE_FUNCTION3 | TE_FLAG_PURE, 0}, /* added for FPP */
250 {"sin", sin, TE_FUNCTION1 | TE_FLAG_PURE, 0},
251 {"sinh", sinh, TE_FUNCTION1 | TE_FLAG_PURE, 0},
252 {"sqrt", sqrt, TE_FUNCTION1 | TE_FLAG_PURE, 0},
253 {"tan", tan, TE_FUNCTION1 | TE_FLAG_PURE, 0},
254 {"tanh", tanh, TE_FUNCTION1 | TE_FLAG_PURE, 0},
255
256 {0, 0, 0, 0}
257 };
258
259 60 static const te_variable *find_builtin(const char *name, int len) {
260 60 int imin = 0;
261 60 int imax = sizeof(functions) / sizeof(te_variable) - 2;
262
263 /*Binary search.*/
264
2/2
✓ Branch 0 taken 265 times.
✓ Branch 1 taken 16 times.
281 while (imax >= imin) {
265 265 const int i = (imin + ((imax-imin)/2));
266 265 int c = strncmp(name, functions[i].name, len);
267
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 217 times.
265 if (!c) c = '\0' - functions[i].name[len];
268
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 221 times.
265 if (c == 0) {
269 44 return functions + i;
270
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 81 times.
221 } else if (c > 0) {
271 140 imin = i + 1;
272 } else {
273 81 imax = i - 1;
274 }
275 }
276
277 16 return 0;
278 }
279
280 1275 static const te_variable *find_lookup(const state *s, const char *name, int len) {
281 int iters;
282 const te_variable *var;
283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1275 times.
1275 if (!s->lookup) return 0;
284
285
2/2
✓ Branch 0 taken 37214 times.
✓ Branch 1 taken 60 times.
37274 for (var = s->lookup, iters = s->lookup_len; iters; ++var, --iters) {
286
4/4
✓ Branch 0 taken 1229 times.
✓ Branch 1 taken 35985 times.
✓ Branch 2 taken 1215 times.
✓ Branch 3 taken 14 times.
37214 if (strncmp(name, var->name, len) == 0 && var->name[len] == '\0') {
287 1215 return var;
288 }
289 }
290 60 return 0;
291 }
292
293
294
295 1214 static double add(double a, double b) {return a + b;}
296 4 static double sub(double a, double b) {return a - b;}
297 1197 static double mul(double a, double b) {return a * b;}
298 9 static double divide(double a, double b) {return a / b;}
299 3 static double negate(double a) {return -a;}
300 1 static double comma(double a, double b) {(void)a; return b;}
301
302
303 7665 void next_token(state *s) {
304 7665 s->type = TOK_NULL;
305
306 do {
307
308
2/2
✓ Branch 0 taken 1266 times.
✓ Branch 1 taken 6403 times.
7669 if (!*s->next){
309 1266 s->type = TOK_END;
310 1266 return;
311 }
312
313 /* Try reading a number. */
314
5/6
✓ Branch 0 taken 3796 times.
✓ Branch 1 taken 2607 times.
✓ Branch 2 taken 1276 times.
✓ Branch 3 taken 2520 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3883 times.
6403 if ((s->next[0] >= '0' && s->next[0] <= '9') || s->next[0] == '.') {
315 2520 s->value = strtod(s->next, (char**)&s->next);
316 2520 s->type = TOK_NUMBER;
317 } else {
318 /* Look for a variable or builtin function call. */
319
3/4
✓ Branch 0 taken 1275 times.
✓ Branch 1 taken 2608 times.
✓ Branch 2 taken 1275 times.
✗ Branch 3 not taken.
5158 if (s->next[0] >= 'a' && s->next[0] <= 'z') {
320 const char *start;
321 1275 start = s->next;
322
9/10
✓ Branch 0 taken 6318 times.
✓ Branch 1 taken 1292 times.
✓ Branch 2 taken 6318 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 17 times.
✓ Branch 5 taken 1275 times.
✓ Branch 6 taken 11 times.
✓ Branch 7 taken 6 times.
✓ Branch 8 taken 6 times.
✓ Branch 9 taken 1275 times.
7610 while ((s->next[0] >= 'a' && s->next[0] <= 'z') || (s->next[0] >= '0' && s->next[0] <= '9') || (s->next[0] == '_')) s->next++;
323
324 1275 const te_variable *var = find_lookup(s, start, s->next - start);
325
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 1215 times.
1275 if (!var) var = find_builtin(start, s->next - start);
326
327
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1259 times.
1275 if (!var) {
328 16 s->type = TOK_ERROR;
329 } else {
330
2/4
✓ Branch 0 taken 1215 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
1259 switch(TYPE_MASK(var->type))
331 {
332 1215 case TE_VARIABLE:
333 1215 s->type = TOK_VARIABLE;
334 1215 s->bound = var->address;
335 1215 break;
336
337 ✗ case TE_CLOSURE0: case TE_CLOSURE1: case TE_CLOSURE2: case TE_CLOSURE3: /* Falls through. */
338 case TE_CLOSURE4: case TE_CLOSURE5: case TE_CLOSURE6: case TE_CLOSURE7: /* Falls through. */
339 ✗ s->context = var->context; /* Falls through. */
340
341 44 case TE_FUNCTION0: case TE_FUNCTION1: case TE_FUNCTION2: case TE_FUNCTION3: /* Falls through. */
342 case TE_FUNCTION4: case TE_FUNCTION5: case TE_FUNCTION6: case TE_FUNCTION7: /* Falls through. */
343 44 s->type = var->type;
344 44 s->function = var->address;
345 44 break;
346 }
347 }
348
349 } else {
350 /* Look for an operator or special character. */
351
11/11
✓ Branch 0 taken 1218 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 1197 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 64 times.
✓ Branch 7 taken 63 times.
✓ Branch 8 taken 41 times.
✓ Branch 9 taken 4 times.
✓ Branch 10 taken 1 times.
2608 switch (s->next++[0]) {
352 1218 case '+': s->type = TOK_INFIX; s->function = add; break;
353 9 case '-': s->type = TOK_INFIX; s->function = sub; break;
354 1197 case '*': s->type = TOK_INFIX; s->function = mul; break;
355 9 case '/': s->type = TOK_INFIX; s->function = divide; break;
356 1 case '^': s->type = TOK_INFIX; s->function = pow; break;
357 1 case '%': s->type = TOK_INFIX; s->function = fmod; break;
358 64 case '(': s->type = TOK_OPEN; break;
359 63 case ')': s->type = TOK_CLOSE; break;
360 41 case ',': s->type = TOK_SEP; break;
361 4 case ' ': case '\t': case '\n': case '\r': break;
362 1 default: s->type = TOK_ERROR; break;
363 }
364 }
365 }
366
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6399 times.
6403 } while (s->type == TOK_NULL);
367 }
368
369
370 static te_expr *list(state *s);
371 static te_expr *expr(state *s);
372 static te_expr *power(state *s);
373
374 3840 static te_expr *base(state *s) {
375 /* <base> = <constant> | <variable> | <function-0> {"(" ")"} | <function-1> <power> | <function-X> "(" <expr> {"," <expr>} ")" | "(" <list> ")" */
376 te_expr *ret;
377 int arity;
378
379
7/7
✓ Branch 0 taken 2520 times.
✓ Branch 1 taken 1215 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 20 times.
✓ Branch 4 taken 22 times.
✓ Branch 5 taken 42 times.
✓ Branch 6 taken 19 times.
3840 switch (TYPE_MASK(s->type)) {
380 2520 case TOK_NUMBER:
381 2520 ret = new_expr(TE_CONSTANT, 0);
382 2520 ret->value = s->value;
383 2520 next_token(s);
384 2520 break;
385
386 1215 case TOK_VARIABLE:
387 1215 ret = new_expr(TE_VARIABLE, 0);
388 1215 ret->bound = s->bound;
389 1215 next_token(s);
390 1215 break;
391
392 2 case TE_FUNCTION0:
393 case TE_CLOSURE0:
394 2 ret = new_expr(s->type, 0);
395 2 ret->function = s->function;
396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (IS_CLOSURE(s->type)) ret->parameters[0] = s->context;
397 2 next_token(s);
398
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->type == TOK_OPEN) {
399 ✗ next_token(s);
400 ✗ if (s->type != TOK_CLOSE) {
401 ✗ s->type = TOK_ERROR;
402 } else {
403 ✗ next_token(s);
404 }
405 }
406 2 break;
407
408 20 case TE_FUNCTION1:
409 case TE_CLOSURE1:
410 20 ret = new_expr(s->type, 0);
411 20 ret->function = s->function;
412
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (IS_CLOSURE(s->type)) ret->parameters[1] = s->context;
413 20 next_token(s);
414 20 ret->parameters[0] = power(s);
415 20 break;
416
417 22 case TE_FUNCTION2: case TE_FUNCTION3: case TE_FUNCTION4:
418 case TE_FUNCTION5: case TE_FUNCTION6: case TE_FUNCTION7:
419 case TE_CLOSURE2: case TE_CLOSURE3: case TE_CLOSURE4:
420 case TE_CLOSURE5: case TE_CLOSURE6: case TE_CLOSURE7:
421
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 arity = ARITY(s->type);
422
423 22 ret = new_expr(s->type, 0);
424 22 ret->function = s->function;
425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (IS_CLOSURE(s->type)) ret->parameters[arity] = s->context;
426 22 next_token(s);
427
428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (s->type != TOK_OPEN) {
429 ✗ s->type = TOK_ERROR;
430 } else {
431 int i;
432
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 for(i = 0; i < arity; i++) {
433 62 next_token(s);
434 62 ret->parameters[i] = expr(s);
435
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 40 times.
62 if(s->type != TOK_SEP) {
436 22 break;
437 }
438 }
439
2/4
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
22 if(s->type != TOK_CLOSE || i != arity - 1) {
440 ✗ s->type = TOK_ERROR;
441 } else {
442 22 next_token(s);
443 }
444 }
445
446 22 break;
447
448 42 case TOK_OPEN:
449 42 next_token(s);
450 42 ret = list(s);
451
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 41 times.
42 if (s->type != TOK_CLOSE) {
452 1 s->type = TOK_ERROR;
453 } else {
454 41 next_token(s);
455 }
456 42 break;
457
458 19 default:
459 19 ret = new_expr(0, 0);
460 19 s->type = TOK_ERROR;
461 19 ret->value = NAN;
462 19 break;
463 }
464
465 3840 return ret;
466 }
467
468
469 3840 static te_expr *power(state *s) {
470 /* <power> = {("-" | "+")} <base> */
471 3840 int sign = 1;
472
4/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3840 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3843 while (s->type == TOK_INFIX && (s->function == add || s->function == sub)) {
473
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (s->function == sub) sign = -sign;
474 3 next_token(s);
475 }
476
477 te_expr *ret;
478
479
2/2
✓ Branch 0 taken 3837 times.
✓ Branch 1 taken 3 times.
3840 if (sign == 1) {
480 3837 ret = base(s);
481 } else {
482 3 ret = NEW_EXPR(TE_FUNCTION1 | TE_FLAG_PURE, base(s));
483 3 ret->function = negate;
484 }
485
486 3840 return ret;
487 }
488
489 #ifdef TE_POW_FROM_RIGHT
490 static te_expr *factor(state *s) {
491 /* <factor> = <power> {"^" <power>} */
492 te_expr *ret = power(s);
493
494 int neg = 0;
495 te_expr *insertion = 0;
496
497 if (ret->type == (TE_FUNCTION1 | TE_FLAG_PURE) && ret->function == negate) {
498 te_expr *se = ret->parameters[0];
499 free(ret);
500 ret = se;
501 neg = 1;
502 }
503
504 while (s->type == TOK_INFIX && (s->function == pow)) {
505 te_fun2 t = s->function;
506 next_token(s);
507
508 if (insertion) {
509 /* Make exponentiation go right-to-left. */
510 te_expr *insert = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, insertion->parameters[1], power(s));
511 insert->function = t;
512 insertion->parameters[1] = insert;
513 insertion = insert;
514 } else {
515 ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, power(s));
516 ret->function = t;
517 insertion = ret;
518 }
519 }
520
521 if (neg) {
522 ret = NEW_EXPR(TE_FUNCTION1 | TE_FLAG_PURE, ret);
523 ret->function = negate;
524 }
525
526 return ret;
527 }
528 #else
529 3819 static te_expr *factor(state *s) {
530 /* <factor> = <power> {"^" <power>} */
531 3819 te_expr *ret = power(s);
532
533
4/4
✓ Branch 0 taken 2432 times.
✓ Branch 1 taken 1388 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2431 times.
3820 while (s->type == TOK_INFIX && (s->function == pow)) {
534 1 te_fun2 t = s->function;
535 1 next_token(s);
536 1 ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, power(s));
537 1 ret->function = t;
538 }
539
540 3819 return ret;
541 }
542 #endif
543
544
545
546 2612 static te_expr *term(state *s) {
547 /* <term> = <factor> {("*" | "/" | "%") <factor>} */
548 2612 te_expr *ret = factor(s);
549
550
8/8
✓ Branch 0 taken 2431 times.
✓ Branch 1 taken 1388 times.
✓ Branch 2 taken 1197 times.
✓ Branch 3 taken 1234 times.
✓ Branch 4 taken 9 times.
✓ Branch 5 taken 1225 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 1224 times.
3819 while (s->type == TOK_INFIX && (s->function == mul || s->function == divide || s->function == fmod)) {
551 1207 te_fun2 t = s->function;
552 1207 next_token(s);
553 1207 ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, factor(s));
554 1207 ret->function = t;
555 }
556
557 2612 return ret;
558 }
559
560
561 1388 static te_expr *expr(state *s) {
562 /* <expr> = <term> {("+" | "-") <term>} */
563 1388 te_expr *ret = term(s);
564
565
5/6
✓ Branch 0 taken 1224 times.
✓ Branch 1 taken 1388 times.
✓ Branch 2 taken 1218 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
2612 while (s->type == TOK_INFIX && (s->function == add || s->function == sub)) {
566 1224 te_fun2 t = s->function;
567 1224 next_token(s);
568 1224 ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, term(s));
569 1224 ret->function = t;
570 }
571
572 1388 return ret;
573 }
574
575
576 1325 static te_expr *list(state *s) {
577 /* <list> = <expr> {"," <expr>} */
578 1325 te_expr *ret = expr(s);
579
580
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1325 times.
1326 while (s->type == TOK_SEP) {
581 1 next_token(s);
582 1 ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, expr(s));
583 1 ret->function = comma;
584 }
585
586 1325 return ret;
587 }
588
589
590 #define TE_FUN(...) ((double(*)(__VA_ARGS__))n->function)
591 #define M(e) te_eval(n->parameters[e])
592
593
594 6272 double te_eval(const te_expr *n) {
595
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6272 times.
6272 if (!n) return NAN;
596
597
3/5
✓ Branch 0 taken 2588 times.
✓ Branch 1 taken 1211 times.
✓ Branch 2 taken 2473 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
6272 switch(TYPE_MASK(n->type)) {
598 2588 case TE_CONSTANT: return n->value;
599 1211 case TE_VARIABLE: return *n->bound;
600
601 2473 case TE_FUNCTION0: case TE_FUNCTION1: case TE_FUNCTION2: case TE_FUNCTION3:
602 case TE_FUNCTION4: case TE_FUNCTION5: case TE_FUNCTION6: case TE_FUNCTION7:
603
5/11
✓ Branch 0 taken 2473 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 2431 times.
✓ Branch 5 taken 18 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
2473 switch(ARITY(n->type)) {
604 2 case 0: return TE_FUN(void)();
605 22 case 1: return TE_FUN(double)(M(0));
606 2431 case 2: return TE_FUN(double, double)(M(0), M(1));
607 18 case 3: return TE_FUN(double, double, double)(M(0), M(1), M(2));
608 ✗ case 4: return TE_FUN(double, double, double, double)(M(0), M(1), M(2), M(3));
609 ✗ case 5: return TE_FUN(double, double, double, double, double)(M(0), M(1), M(2), M(3), M(4));
610 ✗ case 6: return TE_FUN(double, double, double, double, double, double)(M(0), M(1), M(2), M(3), M(4), M(5));
611 ✗ 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));
612 ✗ default: return NAN;
613 }
614
615 ✗ case TE_CLOSURE0: case TE_CLOSURE1: case TE_CLOSURE2: case TE_CLOSURE3:
616 case TE_CLOSURE4: case TE_CLOSURE5: case TE_CLOSURE6: case TE_CLOSURE7:
617 ✗ switch(ARITY(n->type)) {
618 ✗ case 0: return TE_FUN(void*)(n->parameters[0]);
619 ✗ case 1: return TE_FUN(void*, double)(n->parameters[1], M(0));
620 ✗ case 2: return TE_FUN(void*, double, double)(n->parameters[2], M(0), M(1));
621 ✗ case 3: return TE_FUN(void*, double, double, double)(n->parameters[3], M(0), M(1), M(2));
622 ✗ case 4: return TE_FUN(void*, double, double, double, double)(n->parameters[4], M(0), M(1), M(2), M(3));
623 ✗ case 5: return TE_FUN(void*, double, double, double, double, double)(n->parameters[5], M(0), M(1), M(2), M(3), M(4));
624 ✗ 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));
625 ✗ 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));
626 ✗ default: return NAN;
627 }
628
629 ✗ default: return NAN;
630 }
631
632 }
633
634 #undef TE_FUN
635 #undef M
636
637 6204 static void optimize(te_expr *n) {
638 /* Evaluates as much as possible. */
639
2/2
✓ Branch 0 taken 2516 times.
✓ Branch 1 taken 3688 times.
6204 if (n->type == TE_CONSTANT) return;
640
2/2
✓ Branch 0 taken 1213 times.
✓ Branch 1 taken 2475 times.
3688 if (n->type == TE_VARIABLE) return;
641
642 /* Only optimize out functions flagged as pure. */
643
1/2
✓ Branch 0 taken 2475 times.
✗ Branch 1 not taken.
2475 if (IS_PURE(n->type)) {
644
1/2
✓ Branch 0 taken 2475 times.
✗ Branch 1 not taken.
2475 const int arity = ARITY(n->type);
645 2475 int known = 1;
646 int i;
647
2/2
✓ Branch 0 taken 4941 times.
✓ Branch 1 taken 2475 times.
7416 for (i = 0; i < arity; ++i) {
648 4941 optimize(n->parameters[i]);
649
2/2
✓ Branch 0 taken 2401 times.
✓ Branch 1 taken 2540 times.
4941 if (((te_expr*)(n->parameters[i]))->type != TE_CONSTANT) {
650 2401 known = 0;
651 }
652 }
653
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 2401 times.
2475 if (known) {
654 74 const double value = te_eval(n);
655 74 te_free_parameters(n);
656 74 n->type = TE_CONSTANT;
657 74 n->value = value;
658 }
659 }
660 }
661
662
663 1283 te_expr *te_compile(const char *expression, const te_variable *variables, int var_count, int *error) {
664 state s;
665 1283 s.start = s.next = expression;
666 1283 s.lookup = variables;
667 1283 s.lookup_len = var_count;
668
669 1283 next_token(&s);
670 1283 te_expr *root = list(&s);
671
672
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1263 times.
1283 if (s.type != TOK_END) {
673 20 te_free(root);
674
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (error) {
675 20 *error = (s.next - s.start);
676
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (*error == 0) *error = 1;
677 }
678 20 return 0;
679 } else {
680 1263 optimize(root);
681
1/2
✓ Branch 0 taken 1263 times.
✗ Branch 1 not taken.
1263 if (error) *error = 0;
682 1263 return root;
683 }
684 }
685
686
687 ✗ double te_interp(const char *expression, int *error) {
688 ✗ te_expr *n = te_compile(expression, 0, 0, error);
689 double ret;
690 ✗ if (n) {
691 ✗ ret = te_eval(n);
692 ✗ te_free(n);
693 } else {
694 ✗ ret = NAN;
695 }
696 ✗ return ret;
697 }
698
699 ✗ static void pn (const te_expr *n, int depth) {
700 int i, arity;
701 ✗ printf("%*s", depth, "");
702
703 ✗ switch(TYPE_MASK(n->type)) {
704 ✗ case TE_CONSTANT: printf("%f\n", n->value); break;
705 ✗ case TE_VARIABLE: printf("bound %p\n", n->bound); break;
706
707 ✗ case TE_FUNCTION0: case TE_FUNCTION1: case TE_FUNCTION2: case TE_FUNCTION3:
708 case TE_FUNCTION4: case TE_FUNCTION5: case TE_FUNCTION6: case TE_FUNCTION7:
709 case TE_CLOSURE0: case TE_CLOSURE1: case TE_CLOSURE2: case TE_CLOSURE3:
710 case TE_CLOSURE4: case TE_CLOSURE5: case TE_CLOSURE6: case TE_CLOSURE7:
711 ✗ arity = ARITY(n->type);
712 ✗ printf("f%d", arity);
713 ✗ for(i = 0; i < arity; i++) {
714 ✗ printf(" %p", n->parameters[i]);
715 }
716 ✗ printf("\n");
717 ✗ for(i = 0; i < arity; i++) {
718 ✗ pn(n->parameters[i], depth + 1);
719 }
720 ✗ break;
721 }
722 ✗ }
723
724
725 ✗ void te_print(const te_expr *n) {
726 ✗ pn(n, 0);
727 ✗ }
728