fa19a51ad68268571d96eb723de2b15d0dafac86
[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 %left '*' '/'
61 %right '^'
62 %left '%'
63 %nonassoc UMINUS
64 %parse-param { long long *result }
65 %parse-param { double *dresult }
66 %parse-param { int *has_error }
67 %parse-param { int *bye }
68
69 %type <v> expression
70 %%
71
72 top_level:      expression {
73                                 *result = $1.ival;
74                                 *dresult = $1.dval;
75                                 *has_error = $1.has_error;
76                         }
77                 | expression error {
78                                 *result = $1.ival;
79                                 *dresult = $1.dval;
80                                 *has_error = 1;
81                         }
82 expression:     expression '+' expression { 
83                         if (!$1.has_dval && !$3.has_dval)
84                                 $$.ival = $1.ival + $3.ival;
85                         else
86                                 $$.ival = (long long) ($1.dval + $3.dval);
87                         $$.dval = $1.dval + $3.dval;
88                         $$.has_error = $1.has_error || $3.has_error;
89                 }
90         |       expression '-' expression {
91                         if (!$1.has_dval && !$3.has_dval)
92                                 $$.ival = $1.ival - $3.ival; 
93                         else
94                                 $$.ival = (long long) ($1.dval - $3.dval); 
95                         $$.dval = $1.dval - $3.dval; 
96                         $$.has_error = $1.has_error || $3.has_error;
97                 }
98         |       expression '*' expression {
99                         if (!$1.has_dval && !$3.has_dval)
100                                 $$.ival = $1.ival * $3.ival;
101                         else
102                                 $$.ival = (long long) ($1.dval * $3.dval);
103                         $$.dval = $1.dval * $3.dval;
104                         $$.has_error = $1.has_error || $3.has_error;
105                 }
106         |       expression '/' expression {
107                         if ($3.ival == 0)
108                                 yyerror(0, 0, 0, 0, "divide by zero");
109                         else
110                                 $$.ival = $1.ival / $3.ival;
111                         if ($3.dval < 1e-20 && $3.dval > -1e-20)
112                                 yyerror(0, 0, 0, 0, "divide by zero");
113                         else
114                                 $$.dval = $1.dval / $3.dval;
115                         if ($3.has_dval || $1.has_dval)
116                                 $$.ival = (long long) $$.dval;
117                         $$.has_error = $1.has_error || $3.has_error;
118                 }
119         |       '-' expression %prec UMINUS {
120                         $$.ival = -$2.ival;
121                         $$.dval = -$2.dval;
122                         $$.has_error = $2.has_error;
123                 }
124         |       '(' expression ')' { $$ = $2; }
125         |       expression SUFFIX {
126                         if (!$1.has_dval && !$2.has_dval)
127                                 $$.ival = $1.ival * $2.ival;
128                         else
129                                 $$.ival = (long long) $1.dval * $2.dval;
130                         if ($1.has_dval || $2.has_dval)
131                                 $$.dval = $1.dval * $2.dval;
132                         else
133                                 $$.dval = $1.ival * $2.ival;
134                         $$.has_error = $1.has_error || $2.has_error;
135                 }
136         |       expression '%' expression {
137                         if ($1.has_dval || $3.has_dval)
138                                 yyerror(0, 0, 0, 0, "modulo on floats");
139                         if ($3.ival == 0)
140                                 yyerror(0, 0, 0, 0, "divide by zero");
141                         else
142                                 $$.ival = $1.ival % $3.ival;
143                         $$.has_error = $1.has_error || $3.has_error;
144                 }
145         |       expression '^' expression {
146                         $$.has_error = $1.has_error || $3.has_error;
147                         if (!$1.has_dval && !$3.has_dval) {
148                                 int i;
149
150                                 if ($3.ival == 0) {
151                                         $$.ival = 1;
152                                 } else if ($3.ival > 0) {
153                                         long long tmp = $1.ival;
154                                         $$.ival = 1.0;
155                                         for (i = 0; i < $3.ival; i++)
156                                                 $$.ival *= tmp;
157                                 }  else {
158                                         /* integers, 2^-3, ok, we now have doubles */
159                                         double tmp;
160                                         if ($1.ival == 0 && $3.ival == 0) {
161                                                 tmp = 1.0;
162                                                 $$.has_error = 1;
163                                         } else {
164                                                 tmp = pow((double) $1.ival,
165                                                                 (double) $3.ival);
166                                         }
167                                         $$.ival = (long long) tmp;
168                                 }
169                                 $$.dval = pow($1.dval, $3.dval);
170                         } else {
171                                 $$.dval = pow($1.dval, $3.dval);
172                                 $$.ival = (long long) $$.dval;
173                         }
174                 }
175         |       NUMBER { $$ = $1; }
176         |       BYE { $$ = $1; *bye = 1; };
177 %%
178 #include <stdio.h>
179
180 /* Urgh.  yacc and lex are kind of horrible.  This is not thread safe, obviously. */
181 static int lexer_read_offset = 0;
182 static char lexer_input_buffer[1000];
183
184 int lexer_input(char* buffer, int *bytes_read, int bytes_requested)
185 {
186         int bytes_left = strlen(lexer_input_buffer) - lexer_read_offset;
187
188         if (bytes_requested > bytes_left )
189                 bytes_requested = bytes_left;
190         memcpy(buffer, &lexer_input_buffer[lexer_read_offset], bytes_requested);
191         *bytes_read = bytes_requested;
192         lexer_read_offset += bytes_requested;
193         return 0;
194 }
195
196 static void setup_to_parse_string(const char *string)
197 {
198         unsigned int len;
199
200         len = strlen(string);
201         if (len > sizeof(lexer_input_buffer) - 3)
202                 len = sizeof(lexer_input_buffer) - 3;
203
204         strncpy(lexer_input_buffer, string, len);
205         lexer_input_buffer[len] = '\0'; 
206         lexer_input_buffer[len + 1] = '\0';  /* lex/yacc want string double null terminated! */
207         lexer_read_offset = 0;
208 }
209
210 int evaluate_arithmetic_expression(const char *buffer, long long *ival, double *dval)
211 {
212         int rc, bye = 0, has_error = 0;
213
214         setup_to_parse_string(buffer);
215         rc = yyparse(ival, dval, &has_error, &bye);
216         yyrestart(NULL);
217         if (rc || bye || has_error) {
218                 *ival = 0;
219                 *dval = 0;
220                 has_error = 1;
221         }
222         return has_error;
223 }
224
225 int yyerror(__attribute__((unused)) long long *result,
226                 __attribute__((unused)) double *dresult,
227                 __attribute__((unused)) int *has_error,
228                 __attribute__((unused)) int *bye, const char *msg)
229 {
230         fprintf(stderr, "%s\n", msg);
231         return 0;
232 }
233