From 8e2c678f1fbc8321372c80447eec2ae9eabba747 Mon Sep 17 00:00:00 2001 From: "Stephen M. Cameron" Date: Tue, 30 Sep 2014 10:32:28 -0500 Subject: [PATCH] enable informing arithmetic parser of implicit units This is so that "runtime=100" can be interpreted as 100 seconds rather than 100 microseconds, but runtime=(100ms + 100s) can also get the right answer (in microseconds). Signed-off-by: Stephen M. Cameron Signed-off-by: Jens Axboe --- exp/expression-parser.l | 8 ++++---- exp/expression-parser.y | 23 ++++++++++++++++------- exp/test-expression-parser.c | 4 ++-- parse.c | 21 ++++++++++++++------- 4 files changed, 36 insertions(+), 20 deletions(-) diff --git a/exp/expression-parser.l b/exp/expression-parser.l index c084db6a..6ec08661 100644 --- a/exp/expression-parser.l +++ b/exp/expression-parser.l @@ -31,7 +31,7 @@ extern int lexer_input(char* buffer, int *nbytes, int buffersize); lexer_input((buffer), &(bytes_read), (bytes_requested)) extern int yyerror(long long *result, double *dresult, - int *has_error, int *bye, const char *msg); + int *has_error, int *units_specified, int *bye, const char *msg); static void __attribute__((unused)) yyunput(int c,char *buf_ptr); static int __attribute__((unused)) input(void); @@ -122,7 +122,7 @@ bye return BYE; yylval.v.has_error = 0; return NUMBER; } else { - yyerror(0, 0, 0, 0, "bad number\n"); + yyerror(0, 0, 0, 0, 0, "bad number\n"); yylval.v.has_error = 1; return NUMBER; } @@ -137,7 +137,7 @@ bye return BYE; yylval.v.has_error = 0; return NUMBER; } else { - yyerror(0, 0, 0, 0, "bad number\n"); + yyerror(0, 0, 0, 0, 0, "bad number\n"); yylval.v.has_error = 1; return NUMBER; } @@ -152,7 +152,7 @@ bye return BYE; yylval.v.has_error = 0; return NUMBER; } else { - yyerror(0, 0, 0, 0, "bad number\n"); + yyerror(0, 0, 0, 0, 0, "bad number\n"); yylval.v.has_error = 1; return NUMBER; } diff --git a/exp/expression-parser.y b/exp/expression-parser.y index 87ead8ac..1cc07918 100644 --- a/exp/expression-parser.y +++ b/exp/expression-parser.y @@ -37,6 +37,7 @@ typedef union valtype { int yyerror(__attribute__((unused)) long long *result, __attribute__((unused)) double *dresult, __attribute__((unused)) int *has_error, + __attribute__((unused)) int *units_specified, __attribute__((unused)) int *bye, const char *msg); extern int yylex(void); @@ -65,6 +66,7 @@ extern void yyrestart(FILE *file); %parse-param { long long *result } %parse-param { double *dresult } %parse-param { int *has_error } +%parse-param { int *units_specified } %parse-param { int *bye } %type expression @@ -106,11 +108,11 @@ expression: expression '+' expression { } | expression '/' expression { if ($3.ival == 0) - yyerror(0, 0, 0, 0, "divide by zero"); + yyerror(0, 0, 0, 0, 0, "divide by zero"); else $$.ival = $1.ival / $3.ival; if ($3.dval < 1e-20 && $3.dval > -1e-20) - yyerror(0, 0, 0, 0, "divide by zero"); + yyerror(0, 0, 0, 0, 0, "divide by zero"); else $$.dval = $1.dval / $3.dval; if ($3.has_dval || $1.has_dval) @@ -133,12 +135,13 @@ expression: expression '+' expression { else $$.dval = $1.ival * $2.ival; $$.has_error = $1.has_error || $2.has_error; + *units_specified = 1; } | expression '%' expression { if ($1.has_dval || $3.has_dval) - yyerror(0, 0, 0, 0, "modulo on floats"); + yyerror(0, 0, 0, 0, 0, "modulo on floats"); if ($3.ival == 0) - yyerror(0, 0, 0, 0, "divide by zero"); + yyerror(0, 0, 0, 0, 0, "divide by zero"); else { $$.ival = $1.ival % $3.ival; $$.dval = $$.ival; @@ -211,24 +214,30 @@ static void setup_to_parse_string(const char *string) lexer_read_offset = 0; } -int evaluate_arithmetic_expression(const char *buffer, long long *ival, double *dval) +int evaluate_arithmetic_expression(const char *buffer, long long *ival, double *dval, + double implied_units) { - int rc, bye = 0, has_error = 0; + int rc, units_specified = 0, bye = 0, has_error = 0; setup_to_parse_string(buffer); - rc = yyparse(ival, dval, &has_error, &bye); + rc = yyparse(ival, dval, &has_error, &units_specified, &bye); yyrestart(NULL); if (rc || bye || has_error) { *ival = 0; *dval = 0; has_error = 1; } + if (!units_specified) { + *ival = (int) ((double) *ival * implied_units); + *dval = *dval * implied_units; + } return has_error; } int yyerror(__attribute__((unused)) long long *result, __attribute__((unused)) double *dresult, __attribute__((unused)) int *has_error, + __attribute__((unused)) int *units_specified, __attribute__((unused)) int *bye, __attribute__((unused)) const char *msg) { diff --git a/exp/test-expression-parser.c b/exp/test-expression-parser.c index 45151442..93c766a1 100644 --- a/exp/test-expression-parser.c +++ b/exp/test-expression-parser.c @@ -25,7 +25,7 @@ #include "../y.tab.h" extern int evaluate_arithmetic_expression(const char *buffer, long long *ival, - double *dval); + double *dval, double implied_units); int main(int argc, char *argv[]) { @@ -40,7 +40,7 @@ int main(int argc, char *argv[]) rc = strlen(buffer); if (rc > 0 && buffer[rc - 1] == '\n') buffer[rc - 1] = '\0'; - rc = evaluate_arithmetic_expression(buffer, &result, &dresult); + rc = evaluate_arithmetic_expression(buffer, &result, &dresult, 1.0); if (!rc) { printf("%lld (%20.20lf)\n", result, dresult); } else { diff --git a/parse.c b/parse.c index 3a5fbcc4..9e31908a 100644 --- a/parse.c +++ b/parse.c @@ -269,7 +269,7 @@ static unsigned long long get_mult_bytes(const char *str, int len, void *data, } extern int evaluate_arithmetic_expression(const char *buffer, long long *ival, - double *dval); + double *dval, double implied_units); #ifdef CONFIG_ARITHMETIC /* @@ -278,7 +278,7 @@ extern int evaluate_arithmetic_expression(const char *buffer, long long *ival, * original number parsing code. Once sufficiently sure that the arithmetic * code is always getting the right answers, these can be removed. */ -static void verify_exp_parser_float(const char *str) +static void verify_exp_parser_float(const char *str, double implied_units) { long long ival; double dval, tmpval; @@ -286,7 +286,7 @@ static void verify_exp_parser_float(const char *str) if (sscanf(str, "%lf", &tmpval) != 1) return; - if (evaluate_arithmetic_expression(str, &ival, &dval) != 0) { + if (evaluate_arithmetic_expression(str, &ival, &dval, implied_units) != 0) { log_info("Arithmetic failed on '%s'\n", str); return; } @@ -301,8 +301,12 @@ static void verify_exp_parser_decimal(const char *str, long long val, int kilo, int rc; long long ival; double dval; + double implied_units = 1.0; - rc = evaluate_arithmetic_expression(str, &ival, &dval); + if (is_seconds) + implied_units = 1000000.0; + + rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units); if (!rc) { if (ival != val) log_info("Arithmetic failed on '%s', expected %lld, got %lld\n", @@ -324,13 +328,13 @@ int str_to_float(const char *str, double *val) double dval; if (str[0] == '(') { - rc = evaluate_arithmetic_expression(str, &ival, &dval); + rc = evaluate_arithmetic_expression(str, &ival, &dval, 1.0); if (!rc) { *val = dval; return 1; } } else { - verify_exp_parser_float(str); + verify_exp_parser_float(str, 1.0); } #endif return 1 == sscanf(str, "%lf", val); @@ -347,6 +351,7 @@ int str_to_decimal(const char *str, long long *val, int kilo, void *data, #ifdef CONFIG_ARITHMETIC long long ival; double dval; + double implied_units = 1.0; #endif len = strlen(str); @@ -354,8 +359,10 @@ int str_to_decimal(const char *str, long long *val, int kilo, void *data, return 1; #ifdef CONFIG_ARITHMETIC + if (is_seconds) + implied_units = 1000000.0; if (str[0] == '(') - rc = evaluate_arithmetic_expression(str, &ival, &dval); + rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units); if (str[0] == '(' && !rc) { if (!kilo && is_seconds) *val = ival / 1000000LL; -- 2.25.1