1 /* Simple expression parser */
6 #include "util/debug.h"
7 #include <stdlib.h> // strtod()
13 static double d_ratio
(double val0
, double val1
)
25 %parse
-param
{ double *final_val
}
26 %parse
-param
{ struct expr_parse_ctx
*ctx
}
27 %parse
-param
{void *scanner
}
28 %lex
-param
{void* scanner
}
35 %token EXPR_PARSE EXPR_OTHER EXPR_ERROR
38 %destructor
{ free
($$
); } <str
>
39 %token MIN MAX IF ELSE SMT_ON D_RATIO
48 %type
<num
> expr if_expr
51 static void expr_error
(double *final_val __maybe_unused
,
52 struct expr_parse_ctx
*ctx __maybe_unused
,
67 all_other: all_other other
72 expr__add_id
(ctx
, $1);
75 MIN | MAX | IF | ELSE | SMT_ON | NUMBER |
'|' |
'^' |
'&' |
'-' |
'+' |
'*' |
'/' |
'%' |
'(' |
')' |
','
79 all_expr: if_expr
{ *final_val
= $1; }
83 expr IF expr ELSE expr
{ $$
= $3 ?
$1 : $5; }
89 struct expr_id_data
*data
;
91 if
(expr__resolve_id
(ctx
, $1, &data
)) {
99 | expr
'|' expr
{ $$
= (long)$1 |
(long)$3; }
100 | expr
'&' expr
{ $$
= (long)$1 & (long)$3; }
101 | expr
'^' expr
{ $$
= (long)$1 ^
(long)$3; }
102 | expr
'<' expr
{ $$
= $1 < $3; }
103 | expr
'>' expr
{ $$
= $1 > $3; }
104 | expr
'+' expr
{ $$
= $1 + $3; }
105 | expr
'-' expr
{ $$
= $1 - $3; }
106 | expr
'*' expr
{ $$
= $1 * $3; }
107 | expr
'/' expr
{ if
($3 == 0) {
108 pr_debug
("division by zero\n");
113 | expr
'%' expr
{ if
((long)$3 == 0) {
114 pr_debug
("division by zero\n");
117 $$
= (long)$1 %
(long)$3;
119 |
'-' expr %prec NEG
{ $$
= -$2; }
120 |
'(' if_expr
')' { $$
= $2; }
121 | MIN
'(' expr
',' expr
')' { $$
= $3 < $5 ?
$3 : $5; }
122 | MAX
'(' expr
',' expr
')' { $$
= $3 > $5 ?
$3 : $5; }
123 | SMT_ON
{ $$
= smt_on
() > 0; }
124 | D_RATIO
'(' expr
',' expr
')' { $$
= d_ratio
($3,$5); }