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