2017-03-21 03:17:05 +07:00
|
|
|
/* Simple expression parser */
|
|
|
|
%{
|
2020-02-28 16:36:13 +07:00
|
|
|
#define YYDEBUG 1
|
|
|
|
#include <stdio.h>
|
2017-03-21 03:17:05 +07:00
|
|
|
#include "util.h"
|
|
|
|
#include "util/debug.h"
|
2019-08-30 02:18:59 +07:00
|
|
|
#include <stdlib.h> // strtod()
|
2017-03-21 03:17:05 +07:00
|
|
|
#define IN_EXPR_Y 1
|
|
|
|
#include "expr.h"
|
2017-08-12 06:26:23 +07:00
|
|
|
#include "smt.h"
|
2017-03-21 03:17:05 +07:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
%}
|
|
|
|
|
2020-01-13 02:22:59 +07:00
|
|
|
%define api.pure full
|
|
|
|
|
2017-03-21 03:17:05 +07:00
|
|
|
%parse-param { double *final_val }
|
|
|
|
%parse-param { struct parse_ctx *ctx }
|
2020-02-28 16:36:13 +07:00
|
|
|
%parse-param {void *scanner}
|
|
|
|
%lex-param {void* scanner}
|
2017-03-21 03:17:05 +07:00
|
|
|
|
|
|
|
%union {
|
2020-02-28 16:36:13 +07:00
|
|
|
double num;
|
|
|
|
char *str;
|
2017-03-21 03:17:05 +07:00
|
|
|
}
|
|
|
|
|
2020-02-28 16:36:13 +07:00
|
|
|
%token EXPR_PARSE EXPR_OTHER EXPR_ERROR
|
2017-03-21 03:17:05 +07:00
|
|
|
%token <num> NUMBER
|
2020-02-28 16:36:13 +07:00
|
|
|
%token <str> ID
|
2017-08-12 06:26:23 +07:00
|
|
|
%token MIN MAX IF ELSE SMT_ON
|
|
|
|
%left MIN MAX IF
|
2017-03-21 03:17:05 +07:00
|
|
|
%left '|'
|
|
|
|
%left '^'
|
|
|
|
%left '&'
|
|
|
|
%left '-' '+'
|
|
|
|
%left '*' '/' '%'
|
|
|
|
%left NEG NOT
|
2017-08-12 06:26:23 +07:00
|
|
|
%type <num> expr if_expr
|
2017-03-21 03:17:05 +07:00
|
|
|
|
|
|
|
%{
|
2020-02-28 16:36:13 +07:00
|
|
|
static void expr_error(double *final_val __maybe_unused,
|
2017-03-21 03:17:05 +07:00
|
|
|
struct parse_ctx *ctx __maybe_unused,
|
2020-02-28 16:36:13 +07:00
|
|
|
void *scanner,
|
2017-03-21 03:17:05 +07:00
|
|
|
const char *s)
|
|
|
|
{
|
|
|
|
pr_debug("%s\n", s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lookup_id(struct parse_ctx *ctx, char *id, double *val)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ctx->num_ids; i++) {
|
|
|
|
if (!strcasecmp(ctx->ids[i].name, id)) {
|
|
|
|
*val = ctx->ids[i].val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
%}
|
|
|
|
%%
|
|
|
|
|
2020-02-28 16:36:13 +07:00
|
|
|
start:
|
|
|
|
EXPR_PARSE all_expr
|
|
|
|
|
|
|
|
|
EXPR_OTHER all_other
|
|
|
|
|
|
|
|
all_other: all_other other
|
|
|
|
|
|
|
|
|
|
|
|
|
other: ID
|
|
|
|
{
|
|
|
|
if (ctx->num_ids + 1 >= EXPR_MAX_OTHER) {
|
|
|
|
pr_err("failed: way too many variables");
|
|
|
|
YYABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->ids[ctx->num_ids++].name = $1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
MIN | MAX | IF | ELSE | SMT_ON | NUMBER | '|' | '^' | '&' | '-' | '+' | '*' | '/' | '%' | '(' | ')'
|
|
|
|
|
|
|
|
|
2017-08-12 06:26:23 +07:00
|
|
|
all_expr: if_expr { *final_val = $1; }
|
|
|
|
;
|
|
|
|
|
|
|
|
if_expr:
|
|
|
|
expr IF expr ELSE expr { $$ = $3 ? $1 : $5; }
|
|
|
|
| expr
|
2017-03-21 03:17:05 +07:00
|
|
|
;
|
|
|
|
|
|
|
|
expr: NUMBER
|
|
|
|
| ID { if (lookup_id(ctx, $1, &$$) < 0) {
|
2017-07-25 06:40:05 +07:00
|
|
|
pr_debug("%s not found\n", $1);
|
2017-03-21 03:17:05 +07:00
|
|
|
YYABORT;
|
|
|
|
}
|
|
|
|
}
|
2017-08-12 06:26:23 +07:00
|
|
|
| expr '|' expr { $$ = (long)$1 | (long)$3; }
|
|
|
|
| expr '&' expr { $$ = (long)$1 & (long)$3; }
|
|
|
|
| expr '^' expr { $$ = (long)$1 ^ (long)$3; }
|
2017-03-21 03:17:05 +07:00
|
|
|
| expr '+' expr { $$ = $1 + $3; }
|
|
|
|
| expr '-' expr { $$ = $1 - $3; }
|
|
|
|
| expr '*' expr { $$ = $1 * $3; }
|
|
|
|
| expr '/' expr { if ($3 == 0) YYABORT; $$ = $1 / $3; }
|
|
|
|
| expr '%' expr { if ((long)$3 == 0) YYABORT; $$ = (long)$1 % (long)$3; }
|
|
|
|
| '-' expr %prec NEG { $$ = -$2; }
|
2017-08-12 06:26:23 +07:00
|
|
|
| '(' if_expr ')' { $$ = $2; }
|
|
|
|
| MIN '(' expr ',' expr ')' { $$ = $3 < $5 ? $3 : $5; }
|
|
|
|
| MAX '(' expr ',' expr ')' { $$ = $3 > $5 ? $3 : $5; }
|
|
|
|
| SMT_ON { $$ = smt_on() > 0; }
|
2017-03-21 03:17:05 +07:00
|
|
|
;
|
|
|
|
|
|
|
|
%%
|