1 /* Simple expression parser */
6 #include "util/debug.h"
7 #include <stdlib.h> // strtod()
17 %parse-param { double *final_val }
18 %parse-param { struct expr_parse_ctx *ctx }
19 %parse-param {void *scanner}
20 %lex-param {void* scanner}
27 %token EXPR_PARSE EXPR_OTHER EXPR_ERROR
30 %destructor { free ($$); } <str>
31 %token MIN MAX IF ELSE SMT_ON
39 %type <num> expr if_expr
42 static void expr_error(double *final_val __maybe_unused,
43 struct expr_parse_ctx *ctx __maybe_unused,
58 all_other: all_other other
63 expr__add_id(ctx, $1, 0.0);
66 MIN | MAX | IF | ELSE | SMT_ON | NUMBER | '|' | '^' | '&' | '-' | '+' | '*' | '/' | '%' | '(' | ')' | ','
69 all_expr: if_expr { *final_val = $1; }
73 expr IF expr ELSE expr { $$ = $3 ? $1 : $5; }
78 | ID { if (expr__get_id(ctx, $1, &$$)) {
79 pr_debug("%s not found\n", $1);
85 | expr '|' expr { $$ = (long)$1 | (long)$3; }
86 | expr '&' expr { $$ = (long)$1 & (long)$3; }
87 | expr '^' expr { $$ = (long)$1 ^ (long)$3; }
88 | expr '+' expr { $$ = $1 + $3; }
89 | expr '-' expr { $$ = $1 - $3; }
90 | expr '*' expr { $$ = $1 * $3; }
91 | expr '/' expr { if ($3 == 0) {
92 pr_debug("division by zero\n");
97 | expr '%' expr { if ((long)$3 == 0) {
98 pr_debug("division by zero\n");
101 $$ = (long)$1 % (long)$3;
103 | '-' expr %prec NEG { $$ = -$2; }
104 | '(' if_expr ')' { $$ = $2; }
105 | MIN '(' expr ',' expr ')' { $$ = $3 < $5 ? $3 : $5; }
106 | MAX '(' expr ',' expr ')' { $$ = $3 > $5 ? $3 : $5; }
107 | SMT_ON { $$ = smt_on() > 0; }