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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 struct parser_value_type {
31 typedef union valtype {
32 struct parser_value_type v;
35 #define YYSTYPE PARSER_VALUE_TYPE
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);
42 extern int yylex(void);
43 extern void yyrestart(FILE *file);
48 struct parser_value_type {
63 %parse-param { long long *result }
64 %parse-param { double *dresult }
65 %parse-param { int *has_error }
66 %parse-param { int *bye }
71 top_level: expression {
74 *has_error = $1.has_error;
81 expression: expression '+' expression {
82 if (!$1.has_dval && !$3.has_dval)
83 $$.ival = $1.ival + $3.ival;
85 $$.ival = (long long) ($1.dval + $3.dval);
86 $$.dval = $1.dval + $3.dval;
87 $$.has_error = $1.has_error || $3.has_error;
89 | expression '-' expression {
90 if (!$1.has_dval && !$3.has_dval)
91 $$.ival = $1.ival - $3.ival;
93 $$.ival = (long long) ($1.dval - $3.dval);
94 $$.dval = $1.dval - $3.dval;
95 $$.has_error = $1.has_error || $3.has_error;
97 | expression '*' expression {
98 if (!$1.has_dval && !$3.has_dval)
99 $$.ival = $1.ival * $3.ival;
101 $$.ival = (long long) ($1.dval * $3.dval);
102 $$.dval = $1.dval * $3.dval;
103 $$.has_error = $1.has_error || $3.has_error;
105 | expression '/' expression {
107 yyerror(0, 0, 0, 0, "divide by zero");
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");
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;
118 | '-' expression %prec UMINUS {
121 $$.has_error = $2.has_error;
123 | '(' expression ')' { $$ = $2; }
124 | expression SUFFIX {
125 if (!$1.has_dval && !$2.has_dval)
126 $$.ival = $1.ival * $2.ival;
128 $$.ival = (long long) $1.dval * $2.dval;
129 if ($1.has_dval || $2.has_dval)
130 $$.dval = $1.dval * $2.dval;
132 $$.dval = $1.ival * $2.ival;
133 $$.has_error = $1.has_error || $2.has_error;
135 | expression '^' expression {
136 $$.has_error = $1.has_error || $3.has_error;
137 if (!$1.has_dval && !$3.has_dval) {
142 } else if ($3.ival > 0) {
143 long long tmp = $1.ival;
145 for (i = 0; i < $3.ival; i++)
148 /* integers, 2^-3, ok, we now have doubles */
150 if ($1.ival == 0 && $3.ival == 0) {
154 tmp = pow((double) $1.ival,
157 $$.ival = (long long) tmp;
159 $$.dval = pow($1.dval, $3.dval);
161 $$.dval = pow($1.dval, $3.dval);
162 $$.ival = (long long) $$.dval;
165 | NUMBER { $$ = $1; }
166 | BYE { $$ = $1; *bye = 1; };
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];
174 int lexer_input(char* buffer, int *bytes_read, int bytes_requested)
176 int bytes_left = strlen(lexer_input_buffer) - lexer_read_offset;
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;
186 static void setup_to_parse_string(const char *string)
190 len = strlen(string);
191 if (len > sizeof(lexer_input_buffer) - 3)
192 len = sizeof(lexer_input_buffer) - 3;
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;
200 int evaluate_arithmetic_expression(const char *buffer, long long *ival, double *dval)
202 int rc, bye = 0, has_error = 0;
204 setup_to_parse_string(buffer);
205 rc = yyparse(ival, dval, &has_error, &bye);
207 if (rc || bye || has_error) {
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)
220 fprintf(stderr, "%s\n", msg);