enable informing arithmetic parser of implicit units
authorStephen M. Cameron <stephenmcameron@gmail.com>
Tue, 30 Sep 2014 15:32:28 +0000 (10:32 -0500)
committerJens Axboe <axboe@fb.com>
Tue, 30 Sep 2014 16:41:50 +0000 (10:41 -0600)
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 <stephenmcameron@gmail.com>
Signed-off-by: Jens Axboe <axboe@fb.com>
exp/expression-parser.l
exp/expression-parser.y
exp/test-expression-parser.c
parse.c

index c084db6a1885786f84a015c726ebf8b870eaa06b..6ec08661fe40dff9a6e35a81482b959079573df3 100644 (file)
@@ -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;
                }
index 87ead8ac5d9fef8d0fa2172f2b7410c67b13a6f7..1cc07918df9ceb970e1d279d52ad4fef536ad260 100644 (file)
@@ -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 <v> 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)
 {
index 45151442378392111168b6c277fbe720ca0b169d..93c766a1e61dadde4267fcf5ca98acfc8edddc02 100644 (file)
@@ -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 3a5fbcc4bff557da86edc54f08a8a2abd8b20f07..9e31908aebf41bf17ba69fbcfb68947839099cd7 100644 (file)
--- 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;