fio: update FSF address
[fio.git] / exp / expression-parser.y
index dbd8b6c27c40bf58c598564a8ca2d9283edde74e..04a6e07a38ae924ed04cbb6736b87894eeceee61 100644 (file)
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  */
 
 #include <stdio.h>
 #include <string.h>
+#include <math.h>
 
 struct parser_value_type {
        double dval;
@@ -37,10 +38,12 @@ typedef union valtype {
 int yyerror(__attribute__((unused)) long long *result,
                __attribute__((unused)) double *dresult,
                __attribute__((unused)) int *has_error,
-               __attribute__((unused)) int *bye, const char *msg);
+               __attribute__((unused)) int *units_specified,
+               __attribute__((unused)) const char *msg);
 
 extern int yylex(void);
 extern void yyrestart(FILE *file);
+extern int lexer_value_is_time;
 
 %}
 
@@ -55,13 +58,17 @@ extern void yyrestart(FILE *file);
 
 %token <v> NUMBER
 %token <v> BYE
+%token <v> SUFFIX 
 %left '-' '+'
+%right SUFFIX
 %left '*' '/'
+%right '^'
+%left '%'
 %nonassoc UMINUS
 %parse-param { long long *result }
 %parse-param { double *dresult }
 %parse-param { int *has_error }
-%parse-param { int *bye }
+%parse-param { int *units_specified }
 
 %type <v> expression
 %%
@@ -119,8 +126,61 @@ expression:        expression '+' expression {
                        $$.has_error = $2.has_error;
                }
        |       '(' expression ')' { $$ = $2; }
-       |       NUMBER { $$ = $1; }
-       |       BYE { $$ = $1; *bye = 1; };
+       |       expression SUFFIX {
+                       if (!$1.has_dval && !$2.has_dval)
+                               $$.ival = $1.ival * $2.ival;
+                       else
+                               $$.ival = (long long) $1.dval * $2.dval;
+                       if ($1.has_dval || $2.has_dval)
+                               $$.dval = $1.dval * $2.dval;
+                       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");
+                       if ($3.ival == 0)
+                               yyerror(0, 0, 0, 0, "divide by zero");
+                       else {
+                               $$.ival = $1.ival % $3.ival;
+                               $$.dval = $$.ival;
+                       }
+                       $$.has_error = $1.has_error || $3.has_error;
+               }
+       |       expression '^' expression {
+                       $$.has_error = $1.has_error || $3.has_error;
+                       if (!$1.has_dval && !$3.has_dval) {
+                               int i;
+
+                               if ($3.ival == 0) {
+                                       $$.ival = 1;
+                               } else if ($3.ival > 0) {
+                                       long long tmp = $1.ival;
+                                       $$.ival = 1.0;
+                                       for (i = 0; i < $3.ival; i++)
+                                               $$.ival *= tmp;
+                               }  else {
+                                       /* integers, 2^-3, ok, we now have doubles */
+                                       double tmp;
+                                       if ($1.ival == 0 && $3.ival == 0) {
+                                               tmp = 1.0;
+                                               $$.has_error = 1;
+                                       } else {
+                                               double x = (double) $1.ival;
+                                               double y = (double) $3.ival;
+                                               tmp = pow(x, y);
+                                       }
+                                       $$.ival = (long long) tmp;
+                               }
+                               $$.dval = pow($1.dval, $3.dval);
+                       } else {
+                               $$.dval = pow($1.dval, $3.dval);
+                               $$.ival = (long long) $$.dval;
+                       }
+               }
+       |       NUMBER { $$ = $1; };
 %%
 #include <stdio.h>
 
@@ -128,7 +188,7 @@ expression: expression '+' expression {
 static int lexer_read_offset = 0;
 static char lexer_input_buffer[1000];
 
-int lexer_input(char* buffer, int *bytes_read, int bytes_requested)
+int lexer_input(char* buffer, unsigned int *bytes_read, int bytes_requested)
 {
        int bytes_left = strlen(lexer_input_buffer) - lexer_read_offset;
 
@@ -154,27 +214,34 @@ 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 is_time)
 {
-       int rc, bye = 0, has_error = 0;
+       int rc, units_specified = 0, has_error = 0;
 
+       lexer_value_is_time = is_time;
        setup_to_parse_string(buffer);
-       rc = yyparse(ival, dval, &has_error, &bye);
+       rc = yyparse(ival, dval, &has_error, &units_specified);
        yyrestart(NULL);
-       if (rc || bye || has_error) {
+       if (rc || 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 *bye, const char *msg)
+               __attribute__((unused)) int *units_specified,
+               __attribute__((unused)) const char *msg)
 {
-       fprintf(stderr, "%s\n", msg);
+       /* We do not need to do anything here. */
        return 0;
 }