Debian Codegraph @

Externals/expr/include/expr.h

raw symbols SCIP
1 #ifndef EXPR_H
2 #define EXPR_H
3
4 #ifdef _MSC_VER
5 #pragma warning(push)
6 // Disable warning for zero-sized array:
7 #pragma warning(disable : 4200)
8 #endif
9
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13
14 #include <ctype.h> /* for isspace */
15 #include <limits.h>
16 #include <math.h> /* for pow */
17 #include <stdint.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 /*
23 * Simple expandable vector implementation
24 */
25 static int vec_expand(char **buf, int *length, int *cap, int memsz) {
26 if (*length + 1 > *cap) {
27 void *ptr;
28 int n = (*cap == 0) ? 1 : *cap << 1;
29 ptr = realloc(*buf, n * memsz);
30 if (ptr == NULL) {
31 return -1; /* allocation failed */
32 }
33 *buf = (char *)ptr;
34 *cap = n;
35 }
36 return 0;
37 }
38 #define vec(T) \
39 struct { \
40 T *buf; \
41 int len; \
42 int cap; \
43 }
44 #define vec_init() \
45 { NULL, 0, 0 }
46 #define vec_len(v) ((v)->len)
47 #define vec_unpack(v) \
48 (char **)&(v)->buf, &(v)->len, &(v)->cap, sizeof(*(v)->buf)
49 #define vec_push(v, val) \
50 vec_expand(vec_unpack(v)) ? -1 : ((v)->buf[(v)->len++] = (val), 0)
51 #define vec_nth(v, i) (v)->buf[i]
52 #define vec_peek(v) (v)->buf[(v)->len - 1]
53 #define vec_pop(v) (v)->buf[--(v)->len]
54 #define vec_free(v) (free((v)->buf), (v)->buf = NULL, (v)->len = (v)->cap = 0)
55 #define vec_foreach(v, var, iter) \
56 if ((v)->len > 0) \
57 for ((iter) = 0; (iter) < (v)->len && (((var) = (v)->buf[(iter)]), 1); \
58 ++(iter))
59
60 /*
61 * Expression data types
62 */
63 struct expr;
64 struct expr_func;
65
66 enum expr_type {
67 OP_UNKNOWN,
68 OP_UNARY_MINUS,
69 OP_UNARY_LOGICAL_NOT,
70 OP_UNARY_BITWISE_NOT,
71
72 OP_POWER,
73 OP_DIVIDE,
74 OP_MULTIPLY,
75 OP_REMAINDER,
76
77 OP_PLUS,
78 OP_MINUS,
79
80 OP_SHL,
81 OP_SHR,
82
83 OP_LT,
84 OP_LE,
85 OP_GT,
86 OP_GE,
87 OP_EQ,
88 OP_NE,
89
90 OP_BITWISE_AND,
91 OP_BITWISE_OR,
92 OP_BITWISE_XOR,
93
94 OP_LOGICAL_AND,
95 OP_LOGICAL_OR,
96
97 OP_ASSIGN,
98 OP_COMMA,
99
100 OP_CONST,
101 OP_VAR,
102 OP_STRING,
103 OP_FUNC,
104 };
105
106 static int prec[] = {0, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 5, 5,
107 5, 5, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0};
108
109 typedef vec(struct expr) vec_expr_t;
110 typedef void (*exprfn_cleanup_t)(struct expr_func *f, void *context);
111 typedef double (*exprfn_t)(struct expr_func *f, vec_expr_t *args,
112 void *context);
113
114 struct expr {
115 enum expr_type type;
116 union {
117 struct {
118 double value;
119 } num;
120 struct {
121 double *value;
122 } var;
123 struct {
124 vec_expr_t args;
125 } op;
126 struct {
127 char *s;
128 } str;
129 struct {
130 struct expr_func *f;
131 vec_expr_t args;
132 void *context;
133 } func;
134 } param;
135 };
136
137 #define expr_init() \
138 { (enum expr_type)0 }
139
140 struct expr_string {
141 const char *s;
142 int n;
143 };
144 struct expr_arg {
145 int oslen;
146 int eslen;
147 vec_expr_t args;
148 };
149
150 typedef vec(struct expr_string) vec_str_t;
151 typedef vec(struct expr_arg) vec_arg_t;
152
153 static int expr_is_unary(enum expr_type op) {
154 return op == OP_UNARY_MINUS || op == OP_UNARY_LOGICAL_NOT ||
155 op == OP_UNARY_BITWISE_NOT;
156 }
157
158 static int expr_is_binary(enum expr_type op) {
159 return !expr_is_unary(op) && op != OP_CONST && op != OP_VAR &&
160 op != OP_FUNC && op != OP_UNKNOWN && op != OP_STRING;
161 }
162
163 static int expr_prec(enum expr_type a, enum expr_type b) {
164 int left =
165 expr_is_binary(a) && a != OP_ASSIGN && a != OP_POWER && a != OP_COMMA;
166 return (left && prec[a] >= prec[b]) || (prec[a] > prec[b]);
167 }
168
169 #define isfirstvarchr(c) \
170 (((unsigned char)c >= '@' && c != '^' && c != '|' && c != '~') || c == '$')
171 #define isvarchr(c) \
172 (((unsigned char)c >= '@' && c != '^' && c != '|' && c != '~') || \
173 c == '$' || c == '#' || (c >= '0' && c <= '9'))
174
175 static struct {
176 const char *s;
177 const enum expr_type op;
178 } OPS[] = {
179 {"-u", OP_UNARY_MINUS},
180 {"!u", OP_UNARY_LOGICAL_NOT},
181 {"~u", OP_UNARY_BITWISE_NOT},
182 {"**", OP_POWER},
183 {"*", OP_MULTIPLY},
184 {"/", OP_DIVIDE},
185 {"%", OP_REMAINDER},
186 {"+", OP_PLUS},
187 {"-", OP_MINUS},
188 {"<<", OP_SHL},
189 {">>", OP_SHR},
190 {"<", OP_LT},
191 {"<=", OP_LE},
192 {">", OP_GT},
193 {">=", OP_GE},
194 {"==", OP_EQ},
195 {"!=", OP_NE},
196 {"&", OP_BITWISE_AND},
197 {"|", OP_BITWISE_OR},
198 {"^", OP_BITWISE_XOR},
199 {"&&", OP_LOGICAL_AND},
200 {"||", OP_LOGICAL_OR},
201 {"=", OP_ASSIGN},
202 {",", OP_COMMA},
203
204 /* These are used by lexer and must be ignored by parser, so we put
205 them at the end */
206 {"-", OP_UNARY_MINUS},
207 {"!", OP_UNARY_LOGICAL_NOT},
208 {"~", OP_UNARY_BITWISE_NOT},
209 };
210
211 static enum expr_type expr_op(const char *s, size_t len, int unary) {
212 for (unsigned int i = 0; i < sizeof(OPS) / sizeof(OPS[0]); i++) {
213 if (strlen(OPS[i].s) == len && strncmp(OPS[i].s, s, len) == 0 &&
214 (unary == -1 || expr_is_unary(OPS[i].op) == unary)) {
215 return OPS[i].op;
216 }
217 }
218 return OP_UNKNOWN;
219 }
220
221 static double expr_parse_number(const char *s, size_t len) {
222 double num = 0;
223 char buf[32];
224 char *sz = buf;
225 char *end = NULL;
226 if (len >= sizeof(buf)) {
227 sz = (char *)calloc(1, len + 1);
228 if (sz == NULL) {
229 return NAN;
230 }
231 }
232 strncpy(sz, s, len);
233 sz[len] = '\0';
234 num = strtod(sz, &end);
235 if (sz != buf) {
236 free(sz);
237 }
238 return (end == sz + len ? num : NAN);
239 }
240
241 /*
242 * Functions
243 */
244 struct expr_func {
245 const char *name;
246 exprfn_t f;
247 exprfn_cleanup_t cleanup;
248 size_t ctxsz;
249 };
250
251 static struct expr_func *expr_get_func(struct expr_func *funcs, const char *s,
252 size_t len) {
253 for (struct expr_func *f = funcs; f->name; f++) {
254 if (strlen(f->name) == len && strncmp(f->name, s, len) == 0) {
255 return f;
256 }
257 }
258 return NULL;
259 }
260
261 /*
262 * Variables
263 */
264 struct expr_var {
265 double value;
266 struct expr_var *next;
267 char name[];
268 };
269
270 struct expr_var_list {
271 struct expr_var *head;
272 };
273
274 static struct expr_var *expr_get_var(struct expr_var_list *vars, const char *s,
275 size_t len) {
276 struct expr_var *v = NULL;
277 if (len == 0 || !isfirstvarchr(*s)) {
278 return NULL;
279 }
280 for (v = vars->head; v; v = v->next) {
281 if (strlen(v->name) == len && strncmp(v->name, s, len) == 0) {
282 return v;
283 }
284 }
285 v = (struct expr_var *)calloc(1, sizeof(struct expr_var) + len + 1);
286 if (v == NULL) {
287 return NULL; /* allocation failed */
288 }
289 v->next = vars->head;
290 v->value = 0;
291 strncpy(v->name, s, len);
292 v->name[len] = '\0';
293 vars->head = v;
294 return v;
295 }
296
297 static int64_t to_int(double x) {
298 if (isnan(x)) {
299 return 0;
300 } else if (isinf(x) != 0) {
301 return INT64_MAX * isinf(x);
302 } else {
303 return (int64_t)x;
304 }
305 }
306
307 static const char *expr_get_str(struct expr *e) {
308 if (e->type != OP_STRING)
309 return NULL;
310
311 return e->param.str.s;
312 }
313
314 static double expr_eval(struct expr *e) {
315 double n;
316 switch (e->type) {
317 case OP_UNARY_MINUS:
318 return -(expr_eval(&e->param.op.args.buf[0]));
319 case OP_UNARY_LOGICAL_NOT:
320 return !(expr_eval(&e->param.op.args.buf[0]));
321 case OP_UNARY_BITWISE_NOT:
322 return ~(to_int(expr_eval(&e->param.op.args.buf[0])));
323 case OP_POWER:
324 return pow(expr_eval(&e->param.op.args.buf[0]),
325 expr_eval(&e->param.op.args.buf[1]));
326 case OP_MULTIPLY:
327 return expr_eval(&e->param.op.args.buf[0]) *
328 expr_eval(&e->param.op.args.buf[1]);
329 case OP_DIVIDE:
330 return expr_eval(&e->param.op.args.buf[0]) /
331 expr_eval(&e->param.op.args.buf[1]);
332 case OP_REMAINDER:
333 return fmod(expr_eval(&e->param.op.args.buf[0]),
334 expr_eval(&e->param.op.args.buf[1]));
335 case OP_PLUS:
336 return expr_eval(&e->param.op.args.buf[0]) +
337 expr_eval(&e->param.op.args.buf[1]);
338 case OP_MINUS:
339 return expr_eval(&e->param.op.args.buf[0]) -
340 expr_eval(&e->param.op.args.buf[1]);
341 case OP_SHL:
342 return to_int(expr_eval(&e->param.op.args.buf[0]))
343 << to_int(expr_eval(&e->param.op.args.buf[1]));
344 case OP_SHR:
345 return to_int(expr_eval(&e->param.op.args.buf[0])) >>
346 to_int(expr_eval(&e->param.op.args.buf[1]));
347 case OP_LT:
348 return expr_eval(&e->param.op.args.buf[0]) <
349 expr_eval(&e->param.op.args.buf[1]);
350 case OP_LE:
351 return expr_eval(&e->param.op.args.buf[0]) <=
352 expr_eval(&e->param.op.args.buf[1]);
353 case OP_GT:
354 return expr_eval(&e->param.op.args.buf[0]) >
355 expr_eval(&e->param.op.args.buf[1]);
356 case OP_GE:
357 return expr_eval(&e->param.op.args.buf[0]) >=
358 expr_eval(&e->param.op.args.buf[1]);
359 case OP_EQ:
360 return expr_eval(&e->param.op.args.buf[0]) ==
361 expr_eval(&e->param.op.args.buf[1]);
362 case OP_NE:
363 return expr_eval(&e->param.op.args.buf[0]) !=
364 expr_eval(&e->param.op.args.buf[1]);
365 case OP_BITWISE_AND:
366 return to_int(expr_eval(&e->param.op.args.buf[0])) &
367 to_int(expr_eval(&e->param.op.args.buf[1]));
368 case OP_BITWISE_OR:
369 return to_int(expr_eval(&e->param.op.args.buf[0])) |
370 to_int(expr_eval(&e->param.op.args.buf[1]));
371 case OP_BITWISE_XOR:
372 return to_int(expr_eval(&e->param.op.args.buf[0])) ^
373 to_int(expr_eval(&e->param.op.args.buf[1]));
374 case OP_LOGICAL_AND:
375 n = expr_eval(&e->param.op.args.buf[0]);
376 if (n != 0) {
377 n = expr_eval(&e->param.op.args.buf[1]);
378 if (n != 0) {
379 return n;
380 }
381 }
382 return 0;
383 case OP_LOGICAL_OR:
384 n = expr_eval(&e->param.op.args.buf[0]);
385 if (n != 0 && !isnan(n)) {
386 return n;
387 } else {
388 n = expr_eval(&e->param.op.args.buf[1]);
389 if (n != 0) {
390 return n;
391 }
392 }
393 return 0;
394 case OP_ASSIGN:
395 n = expr_eval(&e->param.op.args.buf[1]);
396 if (vec_nth(&e->param.op.args, 0).type == OP_VAR) {
397 *e->param.op.args.buf[0].param.var.value = n;
398 }
399 return n;
400 case OP_COMMA:
401 expr_eval(&e->param.op.args.buf[0]);
402 return expr_eval(&e->param.op.args.buf[1]);
403 case OP_CONST:
404 return e->param.num.value;
405 case OP_VAR:
406 return *e->param.var.value;
407 case OP_FUNC:
408 return e->param.func.f->f(e->param.func.f, &e->param.func.args,
409 e->param.func.context);
410 default:
411 return NAN;
412 }
413 }
414
415 #define EXPR_TOP (1 << 0)
416 #define EXPR_TOPEN (1 << 1)
417 #define EXPR_TCLOSE (1 << 2)
418 #define EXPR_TLITERAL (1 << 3)
419 #define EXPR_TWORD (1 << 4)
420 #define EXPR_TDEFAULT (EXPR_TOPEN | EXPR_TLITERAL | EXPR_TWORD)
421
422 #define EXPR_UNARY (1 << 5)
423 #define EXPR_COMMA (1 << 6)
424
425 static int expr_next_token(const char *s, size_t len, int *flags) {
426 unsigned int i = 0;
427 if (len == 0) {
428 return 0;
429 }
430 char c = s[0];
431 if (c == '#') {
432 for (; i < len && s[i] != '\n'; i++)
433 ;
434 return i;
435 } else if (c == '\n') {
436 for (; i < len && isspace(s[i]); i++)
437 ;
438 if (*flags & EXPR_TOP) {
439 if (i == len || s[i] == ')') {
440 *flags = *flags & (~EXPR_COMMA);
441 } else {
442 *flags = EXPR_TLITERAL | EXPR_TWORD | EXPR_TOPEN | EXPR_COMMA;
443 }
444 }
445 return i;
446 } else if (isspace(c)) {
447 while (i < len && isspace(s[i]) && s[i] != '\n') {
448 i++;
449 }
450 return i;
451 } else if (isdigit(c)) {
452 if ((*flags & EXPR_TLITERAL) == 0) {
453 return -1; // unexpected number
454 }
455 *flags = EXPR_TOP | EXPR_TCLOSE;
456 if (c == '0') {
457 i++;
458 if (i < len && (s[i] == 'x' || s[i] == 'X')) {
459 i++;
460 for (; i < len && isxdigit(s[i]); i++)
461 ;
462 return i;
463 }
464 }
465 for (; i < len && (s[i] == '.' || isdigit(s[i])); i++)
466 ;
467 if (i < len && (s[i] == 'e' || s[i] == 'E')) {
468 i++;
469 if (i < len && (s[i] == '+' || s[i] == '-'))
470 i++;
471 for (; i < len && isdigit(s[i]); i++)
472 ;
473 }
474 return i;
475 } else if (c == '"') {
476 if ((*flags & EXPR_TLITERAL) == 0) {
477 return -6; // unexpected string
478 }
479 *flags = EXPR_TOP | EXPR_TCLOSE;
480 i++;
481 for (; i < len && s[i] != '"'; i++)
482 ;
483 if (i >= len) {
484 return -7; // missing expected quote
485 }
486 i++;
487 return i;
488 } else if (isfirstvarchr(c)) {
489 if ((*flags & EXPR_TWORD) == 0) {
490 return -2; // unexpected word
491 }
492 *flags = EXPR_TOP | EXPR_TOPEN | EXPR_TCLOSE;
493 while ((isvarchr(c)) && i < len) {
494 i++;
495 c = s[i];
496 }
497 return i;
498 } else if (c == '(' || c == ')') {
499 if (c == '(' && (*flags & EXPR_TOPEN) != 0) {
500 *flags = EXPR_TLITERAL | EXPR_TWORD | EXPR_TOPEN | EXPR_TCLOSE;
501 } else if (c == ')' && (*flags & EXPR_TCLOSE) != 0) {
502 *flags = EXPR_TOP | EXPR_TCLOSE;
503 } else {
504 return -3; // unexpected parenthesis
505 }
506 return 1;
507 } else {
508 if ((*flags & EXPR_TOP) == 0) {
509 if (expr_op(&c, 1, 1) == OP_UNKNOWN) {
510 return -4; // missing expected operand
511 }
512 *flags = EXPR_TLITERAL | EXPR_TWORD | EXPR_TOPEN | EXPR_UNARY;
513 return 1;
514 } else {
515 int found = 0;
516 while (!isvarchr(c) && !isspace(c) && c != '(' && c != ')' && i < len) {
517 if (expr_op(s, i + 1, 0) != OP_UNKNOWN) {
518 found = 1;
519 } else if (found) {
520 break;
521 }
522 i++;
523 c = s[i];
524 }
525 if (!found) {
526 return -5; // unknown operator
527 }
528 *flags = EXPR_TLITERAL | EXPR_TWORD | EXPR_TOPEN;
529 return i;
530 }
531 }
532 }
533
534 #define EXPR_PAREN_ALLOWED 0
535 #define EXPR_PAREN_EXPECTED 1
536 #define EXPR_PAREN_FORBIDDEN 2
537
538 static int expr_bind(const char *s, size_t len, vec_expr_t *es) {
539 enum expr_type op = expr_op(s, len, -1);
540 if (op == OP_UNKNOWN) {
541 return -1;
542 }
543
544 if (expr_is_unary(op)) {
545 if (vec_len(es) < 1) {
546 return -1;
547 }
548 struct expr arg = vec_pop(es);
549 struct expr unary = expr_init();
550 unary.type = op;
551 vec_push(&unary.param.op.args, arg);
552 vec_push(es, unary);
553 } else {
554 if (vec_len(es) < 2) {
555 return -1;
556 }
557 struct expr b = vec_pop(es);
558 struct expr a = vec_pop(es);
559 struct expr binary = expr_init();
560 binary.type = op;
561 if (op == OP_ASSIGN && a.type != OP_VAR) {
562 return -1; /* Bad assignment */
563 }
564 vec_push(&binary.param.op.args, a);
565 vec_push(&binary.param.op.args, b);
566 vec_push(es, binary);
567 }
568 return 0;
569 }
570
571 static struct expr expr_const(double value) {
572 struct expr e = expr_init();
573 e.type = OP_CONST;
574 e.param.num.value = value;
575 return e;
576 }
577
578 static struct expr expr_varref(struct expr_var *v) {
579 struct expr e = expr_init();
580 e.type = OP_VAR;
581 e.param.var.value = &v->value;
582 return e;
583 }
584
585 static struct expr expr_binary(enum expr_type type, struct expr a,
586 struct expr b) {
587 struct expr e = expr_init();
588 e.type = type;
589 vec_push(&e.param.op.args, a);
590 vec_push(&e.param.op.args, b);
591 return e;
592 }
593
594 static inline void expr_copy(struct expr *dst, struct expr *src) {
595 int i;
596 struct expr arg;
597 dst->type = src->type;
598 if (src->type == OP_FUNC) {
599 dst->param.func.f = src->param.func.f;
600 vec_foreach(&src->param.func.args, arg, i) {
601 struct expr tmp = expr_init();
602 expr_copy(&tmp, &arg);
603 vec_push(&dst->param.func.args, tmp);
604 }
605 if (src->param.func.f->ctxsz > 0) {
606 dst->param.func.context = calloc(1, src->param.func.f->ctxsz);
607 }
608 } else if (src->type == OP_CONST) {
609 dst->param.num.value = src->param.num.value;
610 } else if (src->type == OP_VAR) {
611 dst->param.var.value = src->param.var.value;
612 } else if (src->type == OP_STRING) {
613 size_t len = strlen(src->param.str.s);
614 dst->param.str.s = (char *)calloc(1, len + 1);
615 if (dst->param.str.s != NULL) {
616 strncpy(dst->param.str.s, src->param.str.s, len);
617 }
618 } else {
619 vec_foreach(&src->param.op.args, arg, i) {
620 struct expr tmp = expr_init();
621 expr_copy(&tmp, &arg);
622 vec_push(&dst->param.op.args, tmp);
623 }
624 }
625 }
626
627 static void expr_destroy_args(struct expr *e);
628
629 static struct expr *expr_create(const char *s, size_t len,
630 struct expr_var_list *vars,
631 struct expr_func *funcs) {
632 double num;
633 const char *id = NULL;
634 size_t idn = 0;
635
636 struct expr *result = NULL;
637
638 vec_expr_t es = vec_init();
639 vec_str_t os = vec_init();
640 vec_arg_t as = vec_init();
641
642 struct macro {
643 char *name;
644 vec_expr_t body;
645 };
646 vec(struct macro) macros = vec_init();
647
648 int flags = EXPR_TDEFAULT;
649 int paren = EXPR_PAREN_ALLOWED;
650 for (;;) {
651 int n = expr_next_token(s, len, &flags);
652 if (n == 0) {
653 break;
654 } else if (n < 0) {
655 goto cleanup;
656 }
657 const char *tok = s;
658 s = s + n;
659 len = len - n;
660 if (*tok == '#') {
661 continue;
662 }
663 if (flags & EXPR_UNARY) {
664 if (n == 1) {
665 switch (*tok) {
666 case '-':
667 tok = "-u";
668 break;
669 case '~':
670 tok = "~u";
671 break;
672 case '!':
673 tok = "!u";
674 break;
675 default:
676 goto cleanup;
677 }
678 n = 2;
679 }
680 }
681 if (*tok == '\n' && (flags & EXPR_COMMA)) {
682 flags = flags & (~EXPR_COMMA);
683 n = 1;
684 tok = ",";
685 }
686 if (isspace(*tok)) {
687 continue;
688 }
689 int paren_next = EXPR_PAREN_ALLOWED;
690
691 if (idn > 0) {
692 struct expr_var *v;
693 if (n == 1 && *tok == '(') {
694 int i;
695 int has_macro = 0;
696 struct macro m;
697 vec_foreach(&macros, m, i) {
698 if (strlen(m.name) == idn && strncmp(m.name, id, idn) == 0) {
699 has_macro = 1;
700 break;
701 }
702 }
703 if ((idn == 1 && id[0] == '$') || has_macro ||
704 expr_get_func(funcs, id, idn) != NULL) {
705 struct expr_string str = {id, (int)idn};
706 vec_push(&os, str);
707 paren = EXPR_PAREN_EXPECTED;
708 } else {
709 goto cleanup; /* invalid function name */
710 }
711 } else if ((v = expr_get_var(vars, id, idn)) != NULL) {
712 vec_push(&es, expr_varref(v));
713 paren = EXPR_PAREN_FORBIDDEN;
714 }
715 id = NULL;
716 idn = 0;
717 }
718
719 if (n == 1 && *tok == '(') {
720 if (paren == EXPR_PAREN_EXPECTED) {
721 struct expr_string str = {"{", 1};
722 vec_push(&os, str);
723 struct expr_arg arg = {vec_len(&os), vec_len(&es), vec_init()};
724 vec_push(&as, arg);
725 } else if (paren == EXPR_PAREN_ALLOWED) {
726 struct expr_string str = {"(", 1};
727 vec_push(&os, str);
728 } else {
729 goto cleanup; // Bad call
730 }
731 } else if (paren == EXPR_PAREN_EXPECTED) {
732 goto cleanup; // Bad call
733 } else if (n == 1 && *tok == ')') {
734 int minlen = (vec_len(&as) > 0 ? vec_peek(&as).oslen : 0);
735 while (vec_len(&os) > minlen && *vec_peek(&os).s != '(' &&
736 *vec_peek(&os).s != '{') {
737 struct expr_string str = vec_pop(&os);
738 if (expr_bind(str.s, str.n, &es) == -1) {
739 goto cleanup;
740 }
741 }
742 if (vec_len(&os) == 0) {
743 goto cleanup; // Bad parens
744 }
745 struct expr_string str = vec_pop(&os);
746 if (str.n == 1 && *str.s == '{') {
747 str = vec_pop(&os);
748 struct expr_arg arg = vec_pop(&as);
749 if (vec_len(&es) > arg.eslen) {
750 vec_push(&arg.args, vec_pop(&es));
751 }
752 if (str.n == 1 && str.s[0] == '$') {
753 if (vec_len(&arg.args) < 1) {
754 vec_free(&arg.args);
755 goto cleanup; /* too few arguments for $() function */
756 }
757 struct expr *u = &vec_nth(&arg.args, 0);
758 if (u->type != OP_VAR) {
759 vec_free(&arg.args);
760 goto cleanup; /* first argument is not a variable */
761 }
762 for (struct expr_var *v = vars->head; v; v = v->next) {
763 if (&v->value == u->param.var.value) {
764 struct macro m = {v->name, arg.args};
765 vec_push(&macros, m);
766 break;
767 }
768 }
769 vec_push(&es, expr_const(0));
770 } else {
771 int i = 0;
772 int found = -1;
773 struct macro m;
774 vec_foreach(&macros, m, i) {
775 if (strlen(m.name) == (size_t)str.n &&
776 strncmp(m.name, str.s, str.n) == 0) {
777 found = i;
778 }
779 }
780 if (found != -1) {
781 m = vec_nth(&macros, found);
782 struct expr root = expr_const(0);
783 struct expr *p = &root;
784 /* Assign macro parameters */
785 for (int j = 0; j < vec_len(&arg.args); j++) {
786 char varname[13];
787 snprintf(varname, sizeof(varname), "$%d", (j + 1));
788 struct expr_var *v = expr_get_var(vars, varname, strlen(varname));
789 struct expr ev = expr_varref(v);
790 struct expr assign =
791 expr_binary(OP_ASSIGN, ev, vec_nth(&arg.args, j));
792 *p = expr_binary(OP_COMMA, assign, expr_const(0));
793 p = &vec_nth(&p->param.op.args, 1);
794 }
795 /* Expand macro body */
796 for (int j = 1; j < vec_len(&m.body); j++) {
797 if (j < vec_len(&m.body) - 1) {
798 *p = expr_binary(OP_COMMA, expr_const(0), expr_const(0));
799 expr_copy(&vec_nth(&p->param.op.args, 0), &vec_nth(&m.body, j));
800 } else {
801 expr_copy(p, &vec_nth(&m.body, j));
802 }
803 p = &vec_nth(&p->param.op.args, 1);
804 }
805 vec_push(&es, root);
806 vec_free(&arg.args);
807 } else {
808 struct expr_func *f = expr_get_func(funcs, str.s, str.n);
809 struct expr bound_func = expr_init();
810 bound_func.type = OP_FUNC;
811 bound_func.param.func.f = f;
812 bound_func.param.func.args = arg.args;
813 if (f->ctxsz > 0) {
814 void *p = calloc(1, f->ctxsz);
815 if (p == NULL) {
816 goto cleanup; /* allocation failed */
817 }
818 bound_func.param.func.context = p;
819 }
820 vec_push(&es, bound_func);
821 }
822 }
823 }
824 paren_next = EXPR_PAREN_FORBIDDEN;
825 } else if (!isnan(num = expr_parse_number(tok, n))) {
826 vec_push(&es, expr_const(num));
827 paren_next = EXPR_PAREN_FORBIDDEN;
828 } else if (n > 1 && *tok == '"') {
829 char *str = (char *)calloc(1, n - 1);
830 if (str == NULL) {
831 goto cleanup; /* allocation failed */
832 }
833 strncpy(str, tok + 1, n - 2);
834 struct expr e = expr_init();
835 e.type = OP_STRING;
836 e.param.str.s = str;
837 vec_push(&es, e);
838 paren_next = EXPR_PAREN_FORBIDDEN;
839 } else if (expr_op(tok, n, -1) != OP_UNKNOWN) {
840 enum expr_type op = expr_op(tok, n, -1);
841 struct expr_string o2 = {NULL, 0};
842 if (vec_len(&os) > 0) {
843 o2 = vec_peek(&os);
844 }
845 for (;;) {
846 if (n == 1 && *tok == ',' && vec_len(&os) > 0) {
847 struct expr_string str = vec_peek(&os);
848 if (str.n == 1 && *str.s == '{') {
849 struct expr e = vec_pop(&es);
850 vec_push(&vec_peek(&as).args, e);
851 break;
852 }
853 }
854 enum expr_type type2 = expr_op(o2.s, o2.n, -1);
855 if (!(type2 != OP_UNKNOWN && expr_prec(op, type2))) {
856 struct expr_string str = {tok, n};
857 vec_push(&os, str);
858 break;
859 }
860
861 if (expr_bind(o2.s, o2.n, &es) == -1) {
862 goto cleanup;
863 }
864 (void)vec_pop(&os);
865 if (vec_len(&os) > 0) {
866 o2 = vec_peek(&os);
867 } else {
868 o2.n = 0;
869 }
870 }
871 } else {
872 if (n > 0 && !isdigit(*tok)) {
873 /* Valid identifier, a variable or a function */
874 id = tok;
875 idn = n;
876 } else {
877 goto cleanup; // Bad variable name, e.g. '2.3.4' or '4ever'
878 }
879 }
880 paren = paren_next;
881 }
882
883 if (idn > 0) {
884 vec_push(&es, expr_varref(expr_get_var(vars, id, idn)));
885 }
886
887 while (vec_len(&os) > 0) {
888 struct expr_string rest = vec_pop(&os);
889 if (rest.n == 1 && (*rest.s == '(' || *rest.s == ')')) {
890 goto cleanup; // Bad paren
891 }
892 if (expr_bind(rest.s, rest.n, &es) == -1) {
893 goto cleanup;
894 }
895 }
896
897 result = (struct expr *)calloc(1, sizeof(struct expr));
898 if (result != NULL) {
899 if (vec_len(&es) == 0) {
900 result->type = OP_CONST;
901 } else {
902 *result = vec_pop(&es);
903 }
904 }
905
906 int i, j;
907 struct macro m;
908 struct expr e;
909 struct expr_arg a;
910 cleanup:
911 vec_foreach(&macros, m, i) {
912 struct expr e2;
913 vec_foreach(&m.body, e2, j) { expr_destroy_args(&e2); }
914 vec_free(&m.body);
915 }
916 vec_free(&macros);
917
918 vec_foreach(&es, e, i) { expr_destroy_args(&e); }
919 vec_free(&es);
920
921 vec_foreach(&as, a, i) {
922 vec_foreach(&a.args, e, j) { expr_destroy_args(&e); }
923 vec_free(&a.args);
924 }
925 vec_free(&as);
926
927 /*vec_foreach(&os, o, i) {vec_free(&m.body);}*/
928 vec_free(&os);
929 return result;
930 }
931
932 static void expr_destroy_args(struct expr *e) {
933 int i;
934 struct expr arg;
935 if (e->type == OP_FUNC) {
936 vec_foreach(&e->param.func.args, arg, i) { expr_destroy_args(&arg); }
937 vec_free(&e->param.func.args);
938 if (e->param.func.context != NULL) {
939 if (e->param.func.f->cleanup != NULL) {
940 e->param.func.f->cleanup(e->param.func.f, e->param.func.context);
941 }
942 free(e->param.func.context);
943 }
944 } else if (e->type == OP_STRING) {
945 free(e->param.str.s);
946 } else if (e->type != OP_CONST && e->type != OP_VAR) {
947 vec_foreach(&e->param.op.args, arg, i) { expr_destroy_args(&arg); }
948 vec_free(&e->param.op.args);
949 }
950 }
951
952 static void expr_destroy(struct expr *e, struct expr_var_list *vars) {
953 if (e != NULL) {
954 expr_destroy_args(e);
955 free(e);
956 }
957 if (vars != NULL) {
958 for (struct expr_var *v = vars->head; v;) {
959 struct expr_var *next = v->next;
960 free(v);
961 v = next;
962 }
963 }
964 }
965
966 #ifdef __cplusplus
967 } /* extern "C" */
968 #endif
969
970 #ifdef _MSC_VER
971 #pragma warning(pop)
972 #endif
973
974 #endif /* EXPR_H */