6ab6408874b829d2dffbf0ed6fa82ebc9558414d
[fio.git] / exp / expression-parser.y
1 %{
2
3 /*
4  * (C) Copyright 2014, Stephen M. Cameron.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License version 2 as
8  *  published by the Free Software Foundation.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <math.h>
24 struct parser_value_type {
25         double dval;
26         long long ival;
27         int has_dval;
28         int has_error;
29 };
30
31 typedef union valtype {
32         struct parser_value_type v;
33 } PARSER_VALUE_TYPE;
34
35 #define YYSTYPE PARSER_VALUE_TYPE
36
37 int yyerror(__attribute__((unused)) long long *result,
38                 __attribute__((unused)) double *dresult,
39                 __attribute__((unused)) int *has_error,
40                 __attribute__((unused)) int *bye, const char *msg);
41
42 extern int yylex(void);
43 extern void yyrestart(FILE *file);
44
45 %}
46
47 %union valtype {
48         struct parser_value_type {
49                 double dval;
50                 long long ival;
51                 int has_dval;
52                 int has_error;
53         } v;
54 };
55
56 %token <v> NUMBER
57 %token <v> BYE
58 %token <v> SUFFIX 
59 %left '-' '+'
60 %right SUFFIX
61 %left '*' '/'
62 %right '^'
63 %left '%'
64 %nonassoc UMINUS
65 %parse-param { long long *result }
66 %parse-param { double *dresult }
67 %parse-param { int *has_error }
68 %parse-param { int *bye }
69
70 %type <v> expression
71 %%
72
73 top_level:      expression {
74                                 *result = $1.ival;
75                                 *dresult = $1.dval;
76                                 *has_error = $1.has_error;
77                         }
78                 | expression error {
79                                 *result = $1.ival;
80                                 *dresult = $1.dval;
81                                 *has_error = 1;
82                         }
83 expression:     expression '+' expression { 
84                         if (!$1.has_dval && !$3.has_dval)
85                                 $$.ival = $1.ival + $3.ival;
86                         else
87                                 $$.ival = (long long) ($1.dval + $3.dval);
88                         $$.dval = $1.dval + $3.dval;
89                         $$.has_error = $1.has_error || $3.has_error;
90                 }
91         |       expression '-' expression {
92                         if (!$1.has_dval && !$3.has_dval)
93                                 $$.ival = $1.ival - $3.ival; 
94                         else
95                                 $$.ival = (long long) ($1.dval - $3.dval); 
96                         $$.dval = $1.dval - $3.dval; 
97                         $$.has_error = $1.has_error || $3.has_error;
98                 }
99         |       expression '*' expression {
100                         if (!$1.has_dval && !$3.has_dval)
101                                 $$.ival = $1.ival * $3.ival;
102                         else
103                                 $$.ival = (long long) ($1.dval * $3.dval);
104                         $$.dval = $1.dval * $3.dval;
105                         $$.has_error = $1.has_error || $3.has_error;
106                 }
107         |       expression '/' expression {
108                         if ($3.ival == 0)
109                                 yyerror(0, 0, 0, 0, "divide by zero");
110                         else
111                                 $$.ival = $1.ival / $3.ival;
112                         if ($3.dval < 1e-20 && $3.dval > -1e-20)
113                                 yyerror(0, 0, 0, 0, "divide by zero");
114                         else
115                                 $$.dval = $1.dval / $3.dval;
116                         if ($3.has_dval || $1.has_dval)
117                                 $$.ival = (long long) $$.dval;
118                         $$.has_error = $1.has_error || $3.has_error;
119                 }
120         |       '-' expression %prec UMINUS {
121                         $$.ival = -$2.ival;
122                         $$.dval = -$2.dval;
123                         $$.has_error = $2.has_error;
124                 }
125         |       '(' expression ')' { $$ = $2; }
126         |       expression SUFFIX {
127                         if (!$1.has_dval && !$2.has_dval)
128                                 $$.ival = $1.ival * $2.ival;
129                         else
130                                 $$.ival = (long long) $1.dval * $2.dval;
131                         if ($1.has_dval || $2.has_dval)
132                                 $$.dval = $1.dval * $2.dval;
133                         else
134                                 $$.dval = $1.ival * $2.ival;
135                         $$.has_error = $1.has_error || $2.has_error;
136                 }
137         |       expression '%' expression {
138                         if ($1.has_dval || $3.has_dval)
139                                 yyerror(0, 0, 0, 0, "modulo on floats");
140                         if ($3.ival == 0)
141                                 yyerror(0, 0, 0, 0, "divide by zero");
142                         else {
143                                 $$.ival = $1.ival % $3.ival;
144                                 $$.dval = $$.ival;
145                         }
146                         $$.has_error = $1.has_error || $3.has_error;
147                 }
148         |       expression '^' expression {
149                         $$.has_error = $1.has_error || $3.has_error;
150                         if (!$1.has_dval && !$3.has_dval) {
151                                 int i;
152
153                                 if ($3.ival == 0) {
154                                         $$.ival = 1;
155                                 } else if ($3.ival > 0) {
156                                         long long tmp = $1.ival;
157                                         $$.ival = 1.0;
158                                         for (i = 0; i < $3.ival; i++)
159                                                 $$.ival *= tmp;
160                                 }  else {
161                                         /* integers, 2^-3, ok, we now have doubles */
162                                         double tmp;
163                                         if ($1.ival == 0 && $3.ival == 0) {
164                                                 tmp = 1.0;
165                                                 $$.has_error = 1;
166                                         } else {
167                                                 double x = (double) $1.ival;
168                                                 double y = (double) $3.ival;
169                                                 tmp = pow(x, y);
170                                         }
171                                         $$.ival = (long long) tmp;
172                                 }
173                                 $$.dval = pow($1.dval, $3.dval);
174                         } else {
175                                 $$.dval = pow($1.dval, $3.dval);
176                                 $$.ival = (long long) $$.dval;
177                         }
178                 }
179         |       NUMBER { $$ = $1; }
180         |       BYE { $$ = $1; *bye = 1; };
181 %%
182 #include <stdio.h>
183
184 /* Urgh.  yacc and lex are kind of horrible.  This is not thread safe, obviously. */
185 static int lexer_read_offset = 0;
186 static char lexer_input_buffer[1000];
187
188 int lexer_input(char* buffer, int *bytes_read, int bytes_requested)
189 {
190         int bytes_left = strlen(lexer_input_buffer) - lexer_read_offset;
191
192         if (bytes_requested > bytes_left )
193                 bytes_requested = bytes_left;
194         memcpy(buffer, &lexer_input_buffer[lexer_read_offset], bytes_requested);
195         *bytes_read = bytes_requested;
196         lexer_read_offset += bytes_requested;
197         return 0;
198 }
199
200 static void setup_to_parse_string(const char *string)
201 {
202         unsigned int len;
203
204         len = strlen(string);
205         if (len > sizeof(lexer_input_buffer) - 3)
206                 len = sizeof(lexer_input_buffer) - 3;
207
208         strncpy(lexer_input_buffer, string, len);
209         lexer_input_buffer[len] = '\0'; 
210         lexer_input_buffer[len + 1] = '\0';  /* lex/yacc want string double null terminated! */
211         lexer_read_offset = 0;
212 }
213
214 int evaluate_arithmetic_expression(const char *buffer, long long *ival, double *dval)
215 {
216         int rc, bye = 0, has_error = 0;
217
218         setup_to_parse_string(buffer);
219         rc = yyparse(ival, dval, &has_error, &bye);
220         yyrestart(NULL);
221         if (rc || bye || has_error) {
222                 *ival = 0;
223                 *dval = 0;
224                 has_error = 1;
225         }
226         return has_error;
227 }
228
229 int yyerror(__attribute__((unused)) long long *result,
230                 __attribute__((unused)) double *dresult,
231                 __attribute__((unused)) int *has_error,
232                 __attribute__((unused)) int *bye, const char *msg)
233 {
234         fprintf(stderr, "%s\n", msg);
235         return 0;
236 }
237