tinyexpr.c

filter: Statementstmt 54.0% 186 / 344 Branchbran 42.4% 96 / 226 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 49.4% 282 / 570 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Math/SegmentedEnvelope.pm ↑ Summary morpher_jit.c »
1 // SPDX-License-Identifier: Zlib
2 /*
3 * TINYEXPR - Tiny recursive descent parser and evaluation engine in C
4 *
5 * Copyright (c) 2015-2020 Lewis Van Winkle
6 *
7 * http://CodePlea.com
8 *
9 * This software is provided 'as-is', without any express or implied
10 * warranty. In no event will the authors be held liable for any damages
11 * arising from the use of this software.
12 *
13 * Permission is granted to anyone to use this software for any purpose,
14 * including commercial applications, and to alter it and redistribute it
15 * freely, subject to the following restrictions:
16 *
17 * 1. The origin of this software must not be misrepresented; you must not
18 * claim that you wrote the original software. If you use this software
19 * in a product, an acknowledgement in the product documentation would be
20 * appreciated but is not required.
21 * 2. Altered source versions must be plainly marked as such, and must not be
22 * misrepresented as being the original software.
23 * 3. This notice may not be removed or altered from any source distribution.
24 */
25  
26 /* COMPILE TIME OPTIONS */
27  
28 /* Exponentiation associativity:
29 For a^b^c = (a^b)^c and -a^b = (-a)^b do nothing.
30 For a^b^c = a^(b^c) and -a^b = -(a^b) uncomment the next line.*/
31 /* #define TE_POW_FROM_RIGHT */
32  
33 /* Logarithms
34 For log = base 10 log do nothing
35 For log = natural log uncomment the next line. */
36 /* #define TE_NAT_LOG */
37  
38 #include "tinyexpr.h"
39 #include <stdlib.h>
40 #include <math.h>
41 #include <string.h>
42 #include <stdio.h>
43 #include <ctype.h>
44 #include <limits.h>
45  
46 #ifndef NAN
47 #define NAN (0.0/0.0)
48 #endif
49  
50 #ifndef INFINITY
51 #define INFINITY (1.0/0.0)
52 #endif
53  
54  
55 typedef double (*te_fun2)(double, double);
56  
57 enum {
58 TOK_NULL = TE_CLOSURE7+1, TOK_ERROR, TOK_END, TOK_SEP,
59 TOK_OPEN, TOK_CLOSE, TOK_NUMBER, TOK_VARIABLE, TOK_INFIX
60 };
61  
62  
63 enum {TE_CONSTANT = 1};
64  
65  
66 typedef struct state {
67 const char *start;
68 const char *next;
69 int type;
70 union {double value; const double *bound; const void *function;};
71 void *context;
72  
73 const te_variable *lookup;
74 int lookup_len;
75 } state;
76  
77  
78 #define TYPE_MASK(TYPE) ((TYPE)&0x0000001F)
79  
80 #define IS_PURE(TYPE) (((TYPE) & TE_FLAG_PURE) != 0)
81 #define IS_FUNCTION(TYPE) (((TYPE) & TE_FUNCTION0) != 0)
82 #define IS_CLOSURE(TYPE) (((TYPE) & TE_CLOSURE0) != 0)
83 #define ARITY(TYPE) ( ((TYPE) & (TE_FUNCTION0 | TE_CLOSURE0)) ? ((TYPE) & 0x00000007) : 0 )
84 #define NEW_EXPR(type, ...) new_expr((type), (const te_expr*[]){__VA_ARGS__})
85 #define CHECK_NULL(ptr, ...) if ((ptr) == NULL) { __VA_ARGS__; return NULL; }
86  
87 40 static te_expr *new_expr(const int type, const te_expr *parameters[]) {
88 40 ▶ const int arity = ARITY(type);
Branch: 1 branch
BranchTrueFalseTotal
const int arity = ARITY(type); 13 27 40
89 40 const int psize = sizeof(void*) * arity;
90 40 const int size = (sizeof(te_expr) - sizeof(void*)) + psize + (IS_CLOSURE(type) ? sizeof(void*) : 0);
91 40 te_expr *ret = malloc(size);
92 40 ▶ CHECK_NULL(ret);
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(ret); 0 40 40
93  
94 40 memset(ret, 0, size);
95 40 ▶ if (arity && parameters) {
Branch: 2 branches
BranchTrueFalseTotal
if (arity && parameters) { 13 27 40
if (arity && parameters) { 11 2 13
96 11 memcpy(ret->parameters, parameters, psize);
97 }
98 40 ret->type = type;
99 40 ret->bound = 0;
100 40 return ret;
101 }
102  
103  
104 40 void te_free_parameters(te_expr *n) {
105 40 ▶ if (!n) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return; 0 40 40
106 40 switch (TYPE_MASK(n->type)) {
107 0 case TE_FUNCTION7: case TE_CLOSURE7: te_free(n->parameters[6]); /* Falls through. */
108 0 case TE_FUNCTION6: case TE_CLOSURE6: te_free(n->parameters[5]); /* Falls through. */
109 0 case TE_FUNCTION5: case TE_CLOSURE5: te_free(n->parameters[4]); /* Falls through. */
110 0 case TE_FUNCTION4: case TE_CLOSURE4: te_free(n->parameters[3]); /* Falls through. */
111 0 case TE_FUNCTION3: case TE_CLOSURE3: te_free(n->parameters[2]); /* Falls through. */
112 11 case TE_FUNCTION2: case TE_CLOSURE2: te_free(n->parameters[1]); /* Falls through. */
113 13 case TE_FUNCTION1: case TE_CLOSURE1: te_free(n->parameters[0]);
114 }
115 }
116  
117  
118 340 void te_free(te_expr *n) {
119 340 ▶ if (!n) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return; 300 40 340
120 40 te_free_parameters(n);
121 40 free(n);
122 }
123  
124  
125 0 static double pi(void) {return 3.14159265358979323846;}
126 0 static double e(void) {return 2.71828182845904523536;}
127 0 static double fac(double a) {/* simplest version of fac */
128 0 ▶ if (a < 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (a < 0.0) 0 0 0
129 0 return NAN;
130 0 ▶ if (a > UINT_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (a > UINT_MAX) 0 0 0
131 0 return INFINITY;
132 0 unsigned int ua = (unsigned int)(a);
133 0 unsigned long int result = 1, i;
134 0 ▶ for (i = 1; i <= ua; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i <= ua; i++) { 0 0 0
135 0 ▶ if (i > ULONG_MAX / result)
Branch: 1 branch
BranchTrueFalseTotal
if (i > ULONG_MAX / result) 0 0 0
136 0 return INFINITY;
137 0 result *= i;
138 }
139 0 return (double)result;
140 }
141 0 static double ncr(double n, double r) {
142 0 ▶ if (n < 0.0 || r < 0.0 || n < r) return NAN;
Branch: 3 branches
BranchTrueFalseTotal
if (n < 0.0 || r < 0.0 || n < r) return NAN; 0 0 0
if (n < 0.0 || r < 0.0 || n < r) return NAN; 0 0 0
if (n < 0.0 || r < 0.0 || n < r) return NAN; 0 0 0
143 0 ▶ if (n > UINT_MAX || r > UINT_MAX) return INFINITY;
Branch: 2 branches
BranchTrueFalseTotal
if (n > UINT_MAX || r > UINT_MAX) return INFINITY; 0 0 0
if (n > UINT_MAX || r > UINT_MAX) return INFINITY; 0 0 0
144 0 unsigned long int un = (unsigned int)(n), ur = (unsigned int)(r), i;
145 0 unsigned long int result = 1;
146 0 ▶ if (ur > un / 2) ur = un - ur;
Branch: 1 branch
BranchTrueFalseTotal
if (ur > un / 2) ur = un - ur; 0 0 0
147 0 ▶ for (i = 1; i <= ur; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i <= ur; i++) { 0 0 0
148 0 ▶ if (result > ULONG_MAX / (un - ur + i))
Branch: 1 branch
BranchTrueFalseTotal
if (result > ULONG_MAX / (un - ur + i)) 0 0 0
149 0 return INFINITY;
150 0 result *= un - ur + i;
151 0 result /= i;
152 }
153 0 return result;
154 }
155 0 static double npr(double n, double r) {return ncr(n, r) * fac(r);}
156  
157 #ifdef _MSC_VER
158 #pragma function (ceil)
159 #pragma function (floor)
160 #endif
161  
162 static const te_variable functions[] = {
163 /* must be in alphabetical order */
164 {"abs", fabs, TE_FUNCTION1 | TE_FLAG_PURE, 0},
165 {"acos", acos, TE_FUNCTION1 | TE_FLAG_PURE, 0},
166 {"asin", asin, TE_FUNCTION1 | TE_FLAG_PURE, 0},
167 {"atan", atan, TE_FUNCTION1 | TE_FLAG_PURE, 0},
168 {"atan2", atan2, TE_FUNCTION2 | TE_FLAG_PURE, 0},
169 {"ceil", ceil, TE_FUNCTION1 | TE_FLAG_PURE, 0},
170 {"cos", cos, TE_FUNCTION1 | TE_FLAG_PURE, 0},
171 {"cosh", cosh, TE_FUNCTION1 | TE_FLAG_PURE, 0},
172 {"e", e, TE_FUNCTION0 | TE_FLAG_PURE, 0},
173 {"exp", exp, TE_FUNCTION1 | TE_FLAG_PURE, 0},
174 {"fac", fac, TE_FUNCTION1 | TE_FLAG_PURE, 0},
175 {"floor", floor, TE_FUNCTION1 | TE_FLAG_PURE, 0},
176 {"ln", log, TE_FUNCTION1 | TE_FLAG_PURE, 0},
177 #ifdef TE_NAT_LOG
178 {"log", log, TE_FUNCTION1 | TE_FLAG_PURE, 0},
179 #else
180 {"log", log10, TE_FUNCTION1 | TE_FLAG_PURE, 0},
181 #endif
182 {"log10", log10, TE_FUNCTION1 | TE_FLAG_PURE, 0},
183 {"ncr", ncr, TE_FUNCTION2 | TE_FLAG_PURE, 0},
184 {"npr", npr, TE_FUNCTION2 | TE_FLAG_PURE, 0},
185 {"pi", pi, TE_FUNCTION0 | TE_FLAG_PURE, 0},
186 {"pow", pow, TE_FUNCTION2 | TE_FLAG_PURE, 0},
187 {"sin", sin, TE_FUNCTION1 | TE_FLAG_PURE, 0},
188 {"sinh", sinh, TE_FUNCTION1 | TE_FLAG_PURE, 0},
189 {"sqrt", sqrt, TE_FUNCTION1 | TE_FLAG_PURE, 0},
190 {"tan", tan, TE_FUNCTION1 | TE_FLAG_PURE, 0},
191 {"tanh", tanh, TE_FUNCTION1 | TE_FLAG_PURE, 0},
192 {0, 0, 0, 0}
193 };
194  
195 6 static const te_variable *find_builtin(const char *name, int len) {
196 6 int imin = 0;
197 6 int imax = sizeof(functions) / sizeof(te_variable) - 2;
198  
199 /*Binary search.*/
200 32 ▶ while (imax >= imin) {
Branch: 1 branch
BranchTrueFalseTotal
while (imax >= imin) { 28 4 32
201 28 const int i = (imin + ((imax-imin)/2));
202 28 int c = strncmp(name, functions[i].name, len);
203 28 ▶ if (!c) c = '\0' - functions[i].name[len];
Branch: 1 branch
BranchTrueFalseTotal
if (!c) c = '\0' - functions[i].name[len]; 4 24 28
204 28 ▶ if (c == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c == 0) { 2 26 28
205 2 return functions + i;
206 26 ▶ } else if (c > 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c > 0) { 15 11 26
207 15 imin = i + 1;
208 } else {
209 11 imax = i - 1;
210 }
211 }
212  
213 4 return 0;
214 }
215  
216 24 static const te_variable *find_lookup(const state *s, const char *name, int len) {
217 int iters;
218 const te_variable *var;
219 24 ▶ if (!s->lookup) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!s->lookup) return 0; 0 24 24
220  
221 30 ▶ for (var = s->lookup, iters = s->lookup_len; iters; ++var, --iters) {
Branch: 1 branch
BranchTrueFalseTotal
for (var = s->lookup, iters = s->lookup_len; iters; ++var, --iters) { 24 6 30
222 24 ▶ if (strncmp(name, var->name, len) == 0 && var->name[len] == '\0') {
Branch: 2 branches
BranchTrueFalseTotal
if (strncmp(name, var->name, len) == 0 && var->name[len] == '\0') { 18 6 24
if (strncmp(name, var->name, len) == 0 && var->name[len] == '\0') { 18 0 18
223 18 return var;
224 }
225 }
226 6 return 0;
227 }
228  
229  
230  
231 0 static double add(double a, double b) {return a + b;}
232 0 static double sub(double a, double b) {return a - b;}
233 0 static double mul(double a, double b) {return a * b;}
234 0 static double divide(double a, double b) {return a / b;}
235 0 static double negate(double a) {return -a;}
236 0 static double comma(double a, double b) {(void)a; return b;}
237  
238  
239 58 void next_token(state *s) {
240 58 s->type = TOK_NULL;
241  
242 do {
243  
244 81 ▶ if (!*s->next){
Branch: 1 branch
BranchTrueFalseTotal
if (!*s->next){ 12 69 81
245 12 s->type = TOK_END;
246 12 return;
247 }
248  
249 /* Try reading a number. */
250 69 ▶ if ((s->next[0] >= '0' && s->next[0] <= '9') || s->next[0] == '.') {
Branch: 3 branches
BranchTrueFalseTotal
if ((s->next[0] >= '0' && s->next[0] <= '9') || s->next[0] == '.') { 30 39 69
if ((s->next[0] >= '0' && s->next[0] <= '9') || s->next[0] == '.') { 26 4 30
if ((s->next[0] >= '0' && s->next[0] <= '9') || s->next[0] == '.') { 0 65 65
251 4 s->value = strtod(s->next, (char**)&s->next);
252 4 s->type = TOK_NUMBER;
253 } else {
254 /* Look for a variable or builtin function call. */
255 65 ▶ if (isalpha(s->next[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (isalpha(s->next[0])) { 24 41 65
256 const char *start;
257 24 start = s->next;
258 73 ▶ while (isalpha(s->next[0]) || isdigit(s->next[0]) || (s->next[0] == '_')) s->next++;
Branch: 3 branches
BranchTrueFalseTotal
while (isalpha(s->next[0]) || isdigit(s->next[0]) || (s->next[0] == '_')) s->next++; 49 24 73
while (isalpha(s->next[0]) || isdigit(s->next[0]) || (s->next[0] == '_')) s->next++; 0 24 24
while (isalpha(s->next[0]) || isdigit(s->next[0]) || (s->next[0] == '_')) s->next++; 0 24 24
259
260 24 const te_variable *var = find_lookup(s, start, s->next - start);
261 24 ▶ if (!var) var = find_builtin(start, s->next - start);
Branch: 1 branch
BranchTrueFalseTotal
if (!var) var = find_builtin(start, s->next - start); 6 18 24
262  
263 24 ▶ if (!var) {
Branch: 1 branch
BranchTrueFalseTotal
if (!var) { 4 20 24
264 4 s->type = TOK_ERROR;
265 } else {
266 20 switch(TYPE_MASK(var->type))
267 {
268 18 case TE_VARIABLE:
269 18 s->type = TOK_VARIABLE;
270 18 s->bound = var->address;
271 18 break;
272  
273 0 case TE_CLOSURE0: case TE_CLOSURE1: case TE_CLOSURE2: case TE_CLOSURE3: /* Falls through. */
274 case TE_CLOSURE4: case TE_CLOSURE5: case TE_CLOSURE6: case TE_CLOSURE7: /* Falls through. */
275 0 s->context = var->context; /* Falls through. */
276  
277 2 case TE_FUNCTION0: case TE_FUNCTION1: case TE_FUNCTION2: case TE_FUNCTION3: /* Falls through. */
278 case TE_FUNCTION4: case TE_FUNCTION5: case TE_FUNCTION6: case TE_FUNCTION7: /* Falls through. */
279 2 s->type = var->type;
280 2 s->function = var->address;
281 2 break;
282 }
283 }
284  
285 } else {
286 /* Look for an operator or special character. */
287 41 switch (s->next++[0]) {
288 0 case '+': s->type = TOK_INFIX; s->function = add; break;
289 0 case '-': s->type = TOK_INFIX; s->function = sub; break;
290 9 case '*': s->type = TOK_INFIX; s->function = mul; break;
291 0 case '/': s->type = TOK_INFIX; s->function = divide; break;
292 2 case '^': s->type = TOK_INFIX; s->function = pow; break;
293 0 case '%': s->type = TOK_INFIX; s->function = fmod; break;
294 5 case '(': s->type = TOK_OPEN; break;
295 2 case ')': s->type = TOK_CLOSE; break;
296 0 case ',': s->type = TOK_SEP; break;
297 23 case ' ': case '\t': case '\n': case '\r': break;
298 0 default: s->type = TOK_ERROR; break;
299 }
300 }
301 }
302 69 ▶ } while (s->type == TOK_NULL);
Branch: 1 branch
BranchTrueFalseTotal
} while (s->type == TOK_NULL); 23 46 69
303 }
304  
305  
306 static te_expr *list(state *s);
307 static te_expr *expr(state *s);
308 static te_expr *power(state *s);
309  
310 34 static te_expr *base(state *s) {
311 /* <base> = <constant> | <variable> | <function-0> {"(" ")"} | <function-1> <power> | <function-X> "(" <expr> {"," <expr>} ")" | "(" <list> ")" */
312 te_expr *ret;
313 int arity;
314  
315 34 switch (TYPE_MASK(s->type)) {
316 4 case TOK_NUMBER:
317 4 ret = new_expr(TE_CONSTANT, 0);
318 4 ▶ CHECK_NULL(ret);
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(ret); 0 4 4
319  
320 4 ret->value = s->value;
321 4 next_token(s);
322 4 break;
323  
324 18 case TOK_VARIABLE:
325 18 ret = new_expr(TE_VARIABLE, 0);
326 18 ▶ CHECK_NULL(ret);
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(ret); 0 18 18
327  
328 18 ret->bound = s->bound;
329 18 next_token(s);
330 18 break;
331  
332 0 case TE_FUNCTION0:
333 case TE_CLOSURE0:
334 0 ret = new_expr(s->type, 0);
335 0 ▶ CHECK_NULL(ret);
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(ret); 0 0 0
336  
337 0 ret->function = s->function;
338 0 ▶ if (IS_CLOSURE(s->type)) ret->parameters[0] = s->context;
Branch: 1 branch
BranchTrueFalseTotal
if (IS_CLOSURE(s->type)) ret->parameters[0] = s->context; 0 0 0
339 0 next_token(s);
340 0 ▶ if (s->type == TOK_OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->type == TOK_OPEN) { 0 0 0
341 0 next_token(s);
342 0 ▶ if (s->type != TOK_CLOSE) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->type != TOK_CLOSE) { 0 0 0
343 0 s->type = TOK_ERROR;
344 } else {
345 0 next_token(s);
346 }
347 }
348 0 break;
349  
350 2 case TE_FUNCTION1:
351 case TE_CLOSURE1:
352 2 ret = new_expr(s->type, 0);
353 2 ▶ CHECK_NULL(ret);
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(ret); 0 2 2
354  
355 2 ret->function = s->function;
356 2 ▶ if (IS_CLOSURE(s->type)) ret->parameters[1] = s->context;
Branch: 1 branch
BranchTrueFalseTotal
if (IS_CLOSURE(s->type)) ret->parameters[1] = s->context; 0 2 2
357 2 next_token(s);
358 2 ret->parameters[0] = power(s);
359 2 ▶ CHECK_NULL(ret->parameters[0], te_free(ret));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(ret->parameters[0], te_free(ret)); 0 2 2
360 2 break;
361  
362 0 case TE_FUNCTION2: case TE_FUNCTION3: case TE_FUNCTION4:
363 case TE_FUNCTION5: case TE_FUNCTION6: case TE_FUNCTION7:
364 case TE_CLOSURE2: case TE_CLOSURE3: case TE_CLOSURE4:
365 case TE_CLOSURE5: case TE_CLOSURE6: case TE_CLOSURE7:
366 0 ▶ arity = ARITY(s->type);
Branch: 1 branch
BranchTrueFalseTotal
arity = ARITY(s->type); 0 0 0
367  
368 0 ret = new_expr(s->type, 0);
369 0 ▶ CHECK_NULL(ret);
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(ret); 0 0 0
370  
371 0 ret->function = s->function;
372 0 ▶ if (IS_CLOSURE(s->type)) ret->parameters[arity] = s->context;
Branch: 1 branch
BranchTrueFalseTotal
if (IS_CLOSURE(s->type)) ret->parameters[arity] = s->context; 0 0 0
373 0 next_token(s);
374  
375 0 ▶ if (s->type != TOK_OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->type != TOK_OPEN) { 0 0 0
376 0 s->type = TOK_ERROR;
377 } else {
378 int i;
379 0 ▶ for(i = 0; i < arity; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < arity; i++) { 0 0 0
380 0 next_token(s);
381 0 ret->parameters[i] = expr(s);
382 0 ▶ CHECK_NULL(ret->parameters[i], te_free(ret));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(ret->parameters[i], te_free(ret)); 0 0 0
383  
384 0 ▶ if(s->type != TOK_SEP) {
Branch: 1 branch
BranchTrueFalseTotal
if(s->type != TOK_SEP) { 0 0 0
385 0 break;
386 }
387 }
388 0 ▶ if(s->type != TOK_CLOSE || i != arity - 1) {
Branch: 2 branches
BranchTrueFalseTotal
if(s->type != TOK_CLOSE || i != arity - 1) { 0 0 0
if(s->type != TOK_CLOSE || i != arity - 1) { 0 0 0
389 0 s->type = TOK_ERROR;
390 } else {
391 0 next_token(s);
392 }
393 }
394  
395 0 break;
396  
397 5 case TOK_OPEN:
398 5 next_token(s);
399 5 ret = list(s);
400 5 ▶ CHECK_NULL(ret);
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(ret); 0 5 5
401  
402 5 ▶ if (s->type != TOK_CLOSE) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->type != TOK_CLOSE) { 3 2 5
403 3 s->type = TOK_ERROR;
404 } else {
405 2 next_token(s);
406 }
407 5 break;
408  
409 5 default:
410 5 ret = new_expr(0, 0);
411 5 ▶ CHECK_NULL(ret);
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(ret); 0 5 5
412  
413 5 s->type = TOK_ERROR;
414 5 ret->value = NAN;
415 5 break;
416 }
417  
418 34 return ret;
419 }
420  
421  
422 34 static te_expr *power(state *s) {
423 /* <power> = {("-" | "+")} <base> */
424 34 int sign = 1;
425 34 ▶ while (s->type == TOK_INFIX && (s->function == add || s->function == sub)) {
Branch: 3 branches
BranchTrueFalseTotal
while (s->type == TOK_INFIX && (s->function == add || s->function == sub)) { 0 34 34
while (s->type == TOK_INFIX && (s->function == add || s->function == sub)) { 0 0 0
while (s->type == TOK_INFIX && (s->function == add || s->function == sub)) { 0 0 0
426 0 ▶ if (s->function == sub) sign = -sign;
Branch: 1 branch
BranchTrueFalseTotal
if (s->function == sub) sign = -sign; 0 0 0
427 0 next_token(s);
428 }
429  
430 te_expr *ret;
431  
432 34 ▶ if (sign == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (sign == 1) { 34 0 34
433 34 ret = base(s);
434 } else {
435 0 te_expr *b = base(s);
436 0 ▶ CHECK_NULL(b);
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(b); 0 0 0
437  
438 0 ret = NEW_EXPR(TE_FUNCTION1 | TE_FLAG_PURE, b);
439 0 ▶ CHECK_NULL(ret, te_free(b));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(ret, te_free(b)); 0 0 0
440  
441 0 ret->function = negate;
442 }
443  
444 34 return ret;
445 }
446  
447 #ifdef TE_POW_FROM_RIGHT
448 static te_expr *factor(state *s) {
449 /* <factor> = <power> {"^" <power>} */
450 te_expr *ret = power(s);
451 CHECK_NULL(ret);
452  
453 int neg = 0;
454  
455 if (ret->type == (TE_FUNCTION1 | TE_FLAG_PURE) && ret->function == negate) {
456 te_expr *se = ret->parameters[0];
457 free(ret);
458 ret = se;
459 neg = 1;
460 }
461  
462 te_expr *insertion = 0;
463  
464 while (s->type == TOK_INFIX && (s->function == pow)) {
465 te_fun2 t = s->function;
466 next_token(s);
467  
468 if (insertion) {
469 /* Make exponentiation go right-to-left. */
470 te_expr *p = power(s);
471 CHECK_NULL(p, te_free(ret));
472  
473 te_expr *insert = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, insertion->parameters[1], p);
474 CHECK_NULL(insert, te_free(p), te_free(ret));
475  
476 insert->function = t;
477 insertion->parameters[1] = insert;
478 insertion = insert;
479 } else {
480 te_expr *p = power(s);
481 CHECK_NULL(p, te_free(ret));
482  
483 te_expr *prev = ret;
484 ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, p);
485 CHECK_NULL(ret, te_free(p), te_free(prev));
486  
487 ret->function = t;
488 insertion = ret;
489 }
490 }
491  
492 if (neg) {
493 te_expr *prev = ret;
494 ret = NEW_EXPR(TE_FUNCTION1 | TE_FLAG_PURE, ret);
495 CHECK_NULL(ret, te_free(prev));
496  
497 ret->function = negate;
498 }
499  
500 return ret;
501 }
502 #else
503 30 static te_expr *factor(state *s) {
504 /* <factor> = <power> {"^" <power>} */
505 30 te_expr *ret = power(s);
506 30 ▶ CHECK_NULL(ret);
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(ret); 0 30 30
507  
508 32 ▶ while (s->type == TOK_INFIX && (s->function == pow)) {
Branch: 2 branches
BranchTrueFalseTotal
while (s->type == TOK_INFIX && (s->function == pow)) { 11 21 32
while (s->type == TOK_INFIX && (s->function == pow)) { 2 9 11
509 2 te_fun2 t = s->function;
510 2 next_token(s);
511 2 te_expr *p = power(s);
512 2 ▶ CHECK_NULL(p, te_free(ret));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(p, te_free(ret)); 0 2 2
513  
514 2 te_expr *prev = ret;
515 2 ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, p);
516 2 ▶ CHECK_NULL(ret, te_free(p), te_free(prev));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(ret, te_free(p), te_free(prev)); 0 2 2
517  
518 2 ret->function = t;
519 }
520  
521 30 return ret;
522 }
523 #endif
524  
525  
526  
527 21 static te_expr *term(state *s) {
528 /* <term> = <factor> {("*" | "/" | "%") <factor>} */
529 21 te_expr *ret = factor(s);
530 21 ▶ CHECK_NULL(ret);
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(ret); 0 21 21
531  
532 30 ▶ while (s->type == TOK_INFIX && (s->function == mul || s->function == divide || s->function == fmod)) {
Branch: 4 branches
BranchTrueFalseTotal
while (s->type == TOK_INFIX && (s->function == mul || s->function == divide || s->function == fmod)) { 9 21 30
while (s->type == TOK_INFIX && (s->function == mul || s->function == divide || s->function == fmod)) { 9 0 9
while (s->type == TOK_INFIX && (s->function == mul || s->function == divide || s->function == fmod)) { 0 0 0
while (s->type == TOK_INFIX && (s->function == mul || s->function == divide || s->function == fmod)) { 0 0 0
533 9 te_fun2 t = s->function;
534 9 next_token(s);
535 9 te_expr *f = factor(s);
536 9 ▶ CHECK_NULL(f, te_free(ret));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(f, te_free(ret)); 0 9 9
537  
538 9 te_expr *prev = ret;
539 9 ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, f);
540 9 ▶ CHECK_NULL(ret, te_free(f), te_free(prev));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(ret, te_free(f), te_free(prev)); 0 9 9
541  
542 9 ret->function = t;
543 }
544  
545 21 return ret;
546 }
547  
548  
549 21 static te_expr *expr(state *s) {
550 /* <expr> = <term> {("+" | "-") <term>} */
551 21 te_expr *ret = term(s);
552 21 ▶ CHECK_NULL(ret);
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(ret); 0 21 21
553  
554 21 ▶ while (s->type == TOK_INFIX && (s->function == add || s->function == sub)) {
Branch: 3 branches
BranchTrueFalseTotal
while (s->type == TOK_INFIX && (s->function == add || s->function == sub)) { 0 21 21
while (s->type == TOK_INFIX && (s->function == add || s->function == sub)) { 0 0 0
while (s->type == TOK_INFIX && (s->function == add || s->function == sub)) { 0 0 0
555 0 te_fun2 t = s->function;
556 0 next_token(s);
557 0 te_expr *te = term(s);
558 0 ▶ CHECK_NULL(te, te_free(ret));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(te, te_free(ret)); 0 0 0
559  
560 0 te_expr *prev = ret;
561 0 ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, te);
562 0 ▶ CHECK_NULL(ret, te_free(te), te_free(prev));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(ret, te_free(te), te_free(prev)); 0 0 0
563  
564 0 ret->function = t;
565 }
566  
567 21 return ret;
568 }
569  
570  
571 21 static te_expr *list(state *s) {
572 /* <list> = <expr> {"," <expr>} */
573 21 te_expr *ret = expr(s);
574 21 ▶ CHECK_NULL(ret);
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(ret); 0 21 21
575  
576 21 ▶ while (s->type == TOK_SEP) {
Branch: 1 branch
BranchTrueFalseTotal
while (s->type == TOK_SEP) { 0 21 21
577 0 next_token(s);
578 0 te_expr *e = expr(s);
579 0 ▶ CHECK_NULL(e, te_free(ret));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(e, te_free(ret)); 0 0 0
580  
581 0 te_expr *prev = ret;
582 0 ret = NEW_EXPR(TE_FUNCTION2 | TE_FLAG_PURE, ret, e);
583 0 ▶ CHECK_NULL(ret, te_free(e), te_free(prev));
Branch: 1 branch
BranchTrueFalseTotal
CHECK_NULL(ret, te_free(e), te_free(prev)); 0 0 0
584  
585 0 ret->function = comma;
586 }
587  
588 21 return ret;
589 }
590  
591  
592 #define TE_FUN(...) ((double(*)(__VA_ARGS__))n->function)
593 #define M(e) te_eval(n->parameters[e])
594  
595  
596 0 double te_eval(const te_expr *n) {
597 0 ▶ if (!n) return NAN;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NAN; 0 0 0
598  
599 0 switch(TYPE_MASK(n->type)) {
600 0 case TE_CONSTANT: return n->value;
601 0 case TE_VARIABLE: return *n->bound;
602  
603 0 case TE_FUNCTION0: case TE_FUNCTION1: case TE_FUNCTION2: case TE_FUNCTION3:
604 case TE_FUNCTION4: case TE_FUNCTION5: case TE_FUNCTION6: case TE_FUNCTION7:
605 0 ▶ switch(ARITY(n->type)) {
Branch: 1 branch
BranchTrueFalseTotal
switch(ARITY(n->type)) { 0 0 0
606 0 case 0: return TE_FUN(void)();
607 0 case 1: return TE_FUN(double)(M(0));
608 0 case 2: return TE_FUN(double, double)(M(0), M(1));
609 0 case 3: return TE_FUN(double, double, double)(M(0), M(1), M(2));
610 0 case 4: return TE_FUN(double, double, double, double)(M(0), M(1), M(2), M(3));
611 0 case 5: return TE_FUN(double, double, double, double, double)(M(0), M(1), M(2), M(3), M(4));
612 0 case 6: return TE_FUN(double, double, double, double, double, double)(M(0), M(1), M(2), M(3), M(4), M(5));
613 0 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));
614 0 default: return NAN;
615 }
616  
617 0 case TE_CLOSURE0: case TE_CLOSURE1: case TE_CLOSURE2: case TE_CLOSURE3:
618 case TE_CLOSURE4: case TE_CLOSURE5: case TE_CLOSURE6: case TE_CLOSURE7:
619 0 ▶ switch(ARITY(n->type)) {
Branch: 1 branch
BranchTrueFalseTotal
switch(ARITY(n->type)) { 0 0 0
620 0 case 0: return TE_FUN(void*)(n->parameters[0]);
621 0 case 1: return TE_FUN(void*, double)(n->parameters[1], M(0));
622 0 case 2: return TE_FUN(void*, double, double)(n->parameters[2], M(0), M(1));
623 0 case 3: return TE_FUN(void*, double, double, double)(n->parameters[3], M(0), M(1), M(2));
624 0 case 4: return TE_FUN(void*, double, double, double, double)(n->parameters[4], M(0), M(1), M(2), M(3));
625 0 case 5: return TE_FUN(void*, double, double, double, double, double)(n->parameters[5], M(0), M(1), M(2), M(3), M(4));
626 0 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));
627 0 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));
628 0 default: return NAN;
629 }
630  
631 0 default: return NAN;
632 }
633  
634 }
635  
636 #undef TE_FUN
637 #undef M
638  
639 35 static void optimize(te_expr *n) {
640 /* Evaluates as much as possible. */
641 35 ▶ if (n->type == TE_CONSTANT) return;
Branch: 1 branch
BranchTrueFalseTotal
if (n->type == TE_CONSTANT) return; 4 31 35
642 31 ▶ if (n->type == TE_VARIABLE) return;
Branch: 1 branch
BranchTrueFalseTotal
if (n->type == TE_VARIABLE) return; 18 13 31
643  
644 /* Only optimize out functions flagged as pure. */
645 13 ▶ if (IS_PURE(n->type)) {
Branch: 1 branch
BranchTrueFalseTotal
if (IS_PURE(n->type)) { 13 0 13
646 13 ▶ const int arity = ARITY(n->type);
Branch: 1 branch
BranchTrueFalseTotal
const int arity = ARITY(n->type); 13 0 13
647 13 int known = 1;
648 int i;
649 37 ▶ for (i = 0; i < arity; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < arity; ++i) { 24 13 37
650 24 optimize(n->parameters[i]);
651 24 ▶ if (((te_expr*)(n->parameters[i]))->type != TE_CONSTANT) {
Branch: 1 branch
BranchTrueFalseTotal
if (((te_expr*)(n->parameters[i]))->type != TE_CONSTANT) { 20 4 24
652 20 known = 0;
653 }
654 }
655 13 ▶ if (known) {
Branch: 1 branch
BranchTrueFalseTotal
if (known) { 0 13 13
656 0 const double value = te_eval(n);
657 0 te_free_parameters(n);
658 0 n->type = TE_CONSTANT;
659 0 n->value = value;
660 }
661 }
662 }
663  
664  
665 16 te_expr *te_compile(const char *expression, const te_variable *variables, int var_count, int *error) {
666 state s;
667 16 s.start = s.next = expression;
668 16 s.lookup = variables;
669 16 s.lookup_len = var_count;
670  
671 16 next_token(&s);
672 16 te_expr *root = list(&s);
673 16 ▶ if (root == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (root == NULL) { 0 16 16
674 0 ▶ if (error) *error = -1;
Branch: 1 branch
BranchTrueFalseTotal
if (error) *error = -1; 0 0 0
675 0 return NULL;
676 }
677  
678 16 ▶ if (s.type != TOK_END) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.type != TOK_END) { 5 11 16
679 5 te_free(root);
680 5 ▶ if (error) {
Branch: 1 branch
BranchTrueFalseTotal
if (error) { 5 0 5
681 5 *error = (s.next - s.start);
682 5 ▶ if (*error == 0) *error = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (*error == 0) *error = 1; 1 4 5
683 }
684 5 return 0;
685 } else {
686 11 optimize(root);
687 11 ▶ if (error) *error = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (error) *error = 0; 11 0 11
688 11 return root;
689 }
690 }
691  
692  
693 0 double te_interp(const char *expression, int *error) {
694 0 te_expr *n = te_compile(expression, 0, 0, error);
695  
696 double ret;
697 0 ▶ if (n) {
Branch: 1 branch
BranchTrueFalseTotal
if (n) { 0 0 0
698 0 ret = te_eval(n);
699 0 te_free(n);
700 } else {
701 0 ret = NAN;
702 }
703 0 return ret;
704 }
705  
706 7 int te_identify_fn(const void *fn) {
707 7 ▶ if (fn == (const void*)add) return TE_OP_ADD;
Branch: 1 branch
BranchTrueFalseTotal
if (fn == (const void*)add) return TE_OP_ADD; 0 7 7
708 7 ▶ if (fn == (const void*)sub) return TE_OP_SUB;
Branch: 1 branch
BranchTrueFalseTotal
if (fn == (const void*)sub) return TE_OP_SUB; 0 7 7
709 7 ▶ if (fn == (const void*)mul) return TE_OP_MUL;
Branch: 1 branch
BranchTrueFalseTotal
if (fn == (const void*)mul) return TE_OP_MUL; 5 2 7
710 2 ▶ if (fn == (const void*)divide) return TE_OP_DIV;
Branch: 1 branch
BranchTrueFalseTotal
if (fn == (const void*)divide) return TE_OP_DIV; 0 2 2
711 2 ▶ if (fn == (const void*)negate) return TE_OP_NEGATE;
Branch: 1 branch
BranchTrueFalseTotal
if (fn == (const void*)negate) return TE_OP_NEGATE; 0 2 2
712 2 ▶ if (fn == (const void*)comma) return TE_OP_COMMA;
Branch: 1 branch
BranchTrueFalseTotal
if (fn == (const void*)comma) return TE_OP_COMMA; 0 2 2
713 2 return TE_OP_UNKNOWN;
714 }
715  
716 0 static void pn (const te_expr *n, int depth) {
717 int i, arity;
718 0 printf("%*s", depth, "");
719  
720 0 switch(TYPE_MASK(n->type)) {
721 0 case TE_CONSTANT: printf("%f\n", n->value); break;
722 0 case TE_VARIABLE: printf("bound %p\n", (const void *)n->bound); break;
723  
724 0 case TE_FUNCTION0: case TE_FUNCTION1: case TE_FUNCTION2: case TE_FUNCTION3:
725 case TE_FUNCTION4: case TE_FUNCTION5: case TE_FUNCTION6: case TE_FUNCTION7:
726 case TE_CLOSURE0: case TE_CLOSURE1: case TE_CLOSURE2: case TE_CLOSURE3:
727 case TE_CLOSURE4: case TE_CLOSURE5: case TE_CLOSURE6: case TE_CLOSURE7:
728 0 ▶ arity = ARITY(n->type);
Branch: 1 branch
BranchTrueFalseTotal
arity = ARITY(n->type); 0 0 0
729 0 printf("f%d", arity);
730 0 ▶ for(i = 0; i < arity; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < arity; i++) { 0 0 0
731 0 printf(" %p", n->parameters[i]);
732 }
733 0 printf("\n");
734 0 ▶ for(i = 0; i < arity; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < arity; i++) { 0 0 0
735 0 pn(n->parameters[i], depth + 1);
736 }
737 0 break;
738 }
739 0 }
740  
741  
742 0 void te_print(const te_expr *n) {
743 0 pn(n, 0);
744 0 }
« blib/lib/Math/SegmentedEnvelope.pm ↑ Summary morpher_jit.c »