4 * (C) Copyright 2014, Stephen M. Cameron.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 struct parser_value_type {
32 typedef union valtype {
33 struct parser_value_type v;
36 #define YYSTYPE PARSER_VALUE_TYPE
38 int yyerror(__attribute__((unused)) long long *result,
39 __attribute__((unused)) double *dresult,
40 __attribute__((unused)) int *has_error,
41 __attribute__((unused)) int *units_specified,
42 __attribute__((unused)) const char *msg);
44 extern int yylex(void);
45 extern void yyrestart(FILE *file);
46 extern int lexer_value_is_time;
51 struct parser_value_type {
68 %parse-param { long long *result }
69 %parse-param { double *dresult }
70 %parse-param { int *has_error }
71 %parse-param { int *units_specified }
76 top_level: expression {
79 *has_error = $1.has_error;
86 expression: expression '+' expression {
87 if (!$1.has_dval && !$3.has_dval)
88 $$.ival = $1.ival + $3.ival;
90 $$.ival = (long long) ($1.dval + $3.dval);
91 $$.dval = $1.dval + $3.dval;
92 $$.has_error = $1.has_error || $3.has_error;
94 | expression '-' expression {
95 if (!$1.has_dval && !$3.has_dval)
96 $$.ival = $1.ival - $3.ival;
98 $$.ival = (long long) ($1.dval - $3.dval);
99 $$.dval = $1.dval - $3.dval;
100 $$.has_error = $1.has_error || $3.has_error;
102 | expression '*' expression {
103 if (!$1.has_dval && !$3.has_dval)
104 $$.ival = $1.ival * $3.ival;
106 $$.ival = (long long) ($1.dval * $3.dval);
107 $$.dval = $1.dval * $3.dval;
108 $$.has_error = $1.has_error || $3.has_error;
110 | expression '/' expression {
112 yyerror(0, 0, 0, 0, "divide by zero");
114 $$.ival = $1.ival / $3.ival;
115 if ($3.dval < 1e-20 && $3.dval > -1e-20)
116 yyerror(0, 0, 0, 0, "divide by zero");
118 $$.dval = $1.dval / $3.dval;
119 if ($3.has_dval || $1.has_dval)
120 $$.ival = (long long) $$.dval;
121 $$.has_error = $1.has_error || $3.has_error;
123 | '-' expression %prec UMINUS {
126 $$.has_error = $2.has_error;
128 | '(' expression ')' { $$ = $2; }
129 | expression SUFFIX {
130 if (!$1.has_dval && !$2.has_dval)
131 $$.ival = $1.ival * $2.ival;
133 $$.ival = (long long) $1.dval * $2.dval;
134 if ($1.has_dval || $2.has_dval)
135 $$.dval = $1.dval * $2.dval;
137 $$.dval = $1.ival * $2.ival;
138 $$.has_error = $1.has_error || $2.has_error;
139 *units_specified = 1;
141 | expression '%' expression {
142 if ($1.has_dval || $3.has_dval)
143 yyerror(0, 0, 0, 0, "modulo on floats");
145 yyerror(0, 0, 0, 0, "divide by zero");
147 $$.ival = $1.ival % $3.ival;
150 $$.has_error = $1.has_error || $3.has_error;
152 | expression '^' expression {
153 $$.has_error = $1.has_error || $3.has_error;
154 if (!$1.has_dval && !$3.has_dval) {
159 } else if ($3.ival > 0) {
160 long long tmp = $1.ival;
162 for (i = 0; i < $3.ival; i++)
165 /* integers, 2^-3, ok, we now have doubles */
167 if ($1.ival == 0 && $3.ival == 0) {
171 double x = (double) $1.ival;
172 double y = (double) $3.ival;
175 $$.ival = (long long) tmp;
177 $$.dval = pow($1.dval, $3.dval);
179 $$.dval = pow($1.dval, $3.dval);
180 $$.ival = (long long) $$.dval;
183 | NUMBER { $$ = $1; };
187 /* Urgh. yacc and lex are kind of horrible. This is not thread safe, obviously. */
188 static int lexer_read_offset = 0;
189 static char lexer_input_buffer[1000];
191 int lexer_input(char* buffer, unsigned int *bytes_read, int bytes_requested)
193 int bytes_left = strlen(lexer_input_buffer) - lexer_read_offset;
195 if (bytes_requested > bytes_left )
196 bytes_requested = bytes_left;
197 memcpy(buffer, &lexer_input_buffer[lexer_read_offset], bytes_requested);
198 *bytes_read = bytes_requested;
199 lexer_read_offset += bytes_requested;
203 static void setup_to_parse_string(const char *string)
207 len = sizeof(lexer_input_buffer) - 3;
208 if (len > strlen(string))
209 len = strlen(string);
211 strncpy(lexer_input_buffer, string, len);
212 lexer_input_buffer[len] = '\0';
213 lexer_input_buffer[len + 1] = '\0'; /* lex/yacc want string double null terminated! */
214 lexer_read_offset = 0;
217 int evaluate_arithmetic_expression(const char *buffer, long long *ival, double *dval,
218 double implied_units, int is_time)
220 int rc, units_specified = 0, has_error = 0;
222 lexer_value_is_time = is_time;
223 setup_to_parse_string(buffer);
224 rc = yyparse(ival, dval, &has_error, &units_specified);
226 if (rc || has_error) {
231 if (!units_specified) {
232 *ival = (int) ((double) *ival * implied_units);
233 *dval = *dval * implied_units;
238 int yyerror(__attribute__((unused)) long long *result,
239 __attribute__((unused)) double *dresult,
240 __attribute__((unused)) int *has_error,
241 __attribute__((unused)) int *units_specified,
242 __attribute__((unused)) const char *msg)
244 /* We do not need to do anything here. */