Get rid if --header-file argument for lex
[fio.git] / exp / expression-parser.y
CommitLineData
b470a02c
SC
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>
ae46966a 23#include <math.h>
c9fa1c8d 24
b470a02c
SC
25struct parser_value_type {
26 double dval;
27 long long ival;
28 int has_dval;
29 int has_error;
30};
31
32typedef union valtype {
33 struct parser_value_type v;
34} PARSER_VALUE_TYPE;
35
36#define YYSTYPE PARSER_VALUE_TYPE
37
38int yyerror(__attribute__((unused)) long long *result,
39 __attribute__((unused)) double *dresult,
40 __attribute__((unused)) int *has_error,
8e2c678f 41 __attribute__((unused)) int *units_specified,
3b0d05e8 42 __attribute__((unused)) const char *msg);
b470a02c
SC
43
44extern int yylex(void);
45extern void yyrestart(FILE *file);
46
47%}
48
49%union valtype {
50 struct parser_value_type {
51 double dval;
52 long long ival;
53 int has_dval;
54 int has_error;
55 } v;
56};
57
58%token <v> NUMBER
59%token <v> BYE
18722a18 60%token <v> SUFFIX
b470a02c 61%left '-' '+'
65641c1e 62%right SUFFIX
b470a02c 63%left '*' '/'
ae46966a 64%right '^'
886e5ecd 65%left '%'
b470a02c
SC
66%nonassoc UMINUS
67%parse-param { long long *result }
68%parse-param { double *dresult }
69%parse-param { int *has_error }
8e2c678f 70%parse-param { int *units_specified }
b470a02c
SC
71
72%type <v> expression
73%%
74
75top_level: expression {
76 *result = $1.ival;
77 *dresult = $1.dval;
78 *has_error = $1.has_error;
79 }
80 | expression error {
81 *result = $1.ival;
82 *dresult = $1.dval;
83 *has_error = 1;
84 }
85expression: expression '+' expression {
86 if (!$1.has_dval && !$3.has_dval)
87 $$.ival = $1.ival + $3.ival;
88 else
89 $$.ival = (long long) ($1.dval + $3.dval);
90 $$.dval = $1.dval + $3.dval;
91 $$.has_error = $1.has_error || $3.has_error;
92 }
93 | expression '-' expression {
94 if (!$1.has_dval && !$3.has_dval)
95 $$.ival = $1.ival - $3.ival;
96 else
97 $$.ival = (long long) ($1.dval - $3.dval);
98 $$.dval = $1.dval - $3.dval;
99 $$.has_error = $1.has_error || $3.has_error;
100 }
101 | expression '*' expression {
102 if (!$1.has_dval && !$3.has_dval)
103 $$.ival = $1.ival * $3.ival;
104 else
105 $$.ival = (long long) ($1.dval * $3.dval);
106 $$.dval = $1.dval * $3.dval;
107 $$.has_error = $1.has_error || $3.has_error;
108 }
109 | expression '/' expression {
110 if ($3.ival == 0)
3b0d05e8 111 yyerror(0, 0, 0, 0, "divide by zero");
b470a02c
SC
112 else
113 $$.ival = $1.ival / $3.ival;
114 if ($3.dval < 1e-20 && $3.dval > -1e-20)
3b0d05e8 115 yyerror(0, 0, 0, 0, "divide by zero");
b470a02c
SC
116 else
117 $$.dval = $1.dval / $3.dval;
118 if ($3.has_dval || $1.has_dval)
119 $$.ival = (long long) $$.dval;
120 $$.has_error = $1.has_error || $3.has_error;
121 }
122 | '-' expression %prec UMINUS {
123 $$.ival = -$2.ival;
124 $$.dval = -$2.dval;
125 $$.has_error = $2.has_error;
126 }
127 | '(' expression ')' { $$ = $2; }
18722a18
SC
128 | expression SUFFIX {
129 if (!$1.has_dval && !$2.has_dval)
130 $$.ival = $1.ival * $2.ival;
131 else
132 $$.ival = (long long) $1.dval * $2.dval;
133 if ($1.has_dval || $2.has_dval)
134 $$.dval = $1.dval * $2.dval;
135 else
136 $$.dval = $1.ival * $2.ival;
137 $$.has_error = $1.has_error || $2.has_error;
8e2c678f 138 *units_specified = 1;
18722a18 139 }
886e5ecd
JA
140 | expression '%' expression {
141 if ($1.has_dval || $3.has_dval)
3b0d05e8 142 yyerror(0, 0, 0, 0, "modulo on floats");
886e5ecd 143 if ($3.ival == 0)
3b0d05e8 144 yyerror(0, 0, 0, 0, "divide by zero");
180bd010 145 else {
886e5ecd 146 $$.ival = $1.ival % $3.ival;
180bd010
JA
147 $$.dval = $$.ival;
148 }
886e5ecd
JA
149 $$.has_error = $1.has_error || $3.has_error;
150 }
ae46966a
SC
151 | expression '^' expression {
152 $$.has_error = $1.has_error || $3.has_error;
153 if (!$1.has_dval && !$3.has_dval) {
154 int i;
155
156 if ($3.ival == 0) {
157 $$.ival = 1;
158 } else if ($3.ival > 0) {
159 long long tmp = $1.ival;
160 $$.ival = 1.0;
161 for (i = 0; i < $3.ival; i++)
162 $$.ival *= tmp;
163 } else {
164 /* integers, 2^-3, ok, we now have doubles */
165 double tmp;
166 if ($1.ival == 0 && $3.ival == 0) {
167 tmp = 1.0;
168 $$.has_error = 1;
169 } else {
5fe4c886
JA
170 double x = (double) $1.ival;
171 double y = (double) $3.ival;
172 tmp = pow(x, y);
ae46966a
SC
173 }
174 $$.ival = (long long) tmp;
175 }
176 $$.dval = pow($1.dval, $3.dval);
177 } else {
178 $$.dval = pow($1.dval, $3.dval);
179 $$.ival = (long long) $$.dval;
180 }
181 }
3b0d05e8 182 | NUMBER { $$ = $1; };
b470a02c
SC
183%%
184#include <stdio.h>
185
186/* Urgh. yacc and lex are kind of horrible. This is not thread safe, obviously. */
187static int lexer_read_offset = 0;
188static char lexer_input_buffer[1000];
189
de06a468 190int lexer_input(char* buffer, unsigned int *bytes_read, int bytes_requested)
b470a02c
SC
191{
192 int bytes_left = strlen(lexer_input_buffer) - lexer_read_offset;
193
194 if (bytes_requested > bytes_left )
195 bytes_requested = bytes_left;
196 memcpy(buffer, &lexer_input_buffer[lexer_read_offset], bytes_requested);
197 *bytes_read = bytes_requested;
198 lexer_read_offset += bytes_requested;
199 return 0;
200}
201
202static void setup_to_parse_string(const char *string)
203{
204 unsigned int len;
205
206 len = strlen(string);
207 if (len > sizeof(lexer_input_buffer) - 3)
208 len = sizeof(lexer_input_buffer) - 3;
209
210 strncpy(lexer_input_buffer, string, len);
211 lexer_input_buffer[len] = '\0';
212 lexer_input_buffer[len + 1] = '\0'; /* lex/yacc want string double null terminated! */
213 lexer_read_offset = 0;
214}
215
8e2c678f
SC
216int evaluate_arithmetic_expression(const char *buffer, long long *ival, double *dval,
217 double implied_units)
b470a02c 218{
3b0d05e8 219 int rc, units_specified = 0, has_error = 0;
b470a02c
SC
220
221 setup_to_parse_string(buffer);
3b0d05e8 222 rc = yyparse(ival, dval, &has_error, &units_specified);
b470a02c 223 yyrestart(NULL);
3b0d05e8 224 if (rc || has_error) {
b470a02c
SC
225 *ival = 0;
226 *dval = 0;
227 has_error = 1;
228 }
8e2c678f
SC
229 if (!units_specified) {
230 *ival = (int) ((double) *ival * implied_units);
231 *dval = *dval * implied_units;
232 }
b470a02c
SC
233 return has_error;
234}
235
236int yyerror(__attribute__((unused)) long long *result,
237 __attribute__((unused)) double *dresult,
238 __attribute__((unused)) int *has_error,
8e2c678f 239 __attribute__((unused)) int *units_specified,
7ff01fbe 240 __attribute__((unused)) const char *msg)
b470a02c 241{
7ff01fbe 242 /* We do not need to do anything here. */
b470a02c
SC
243 return 0;
244}
245