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 {
62 %parse-param { long long *result }
63 %parse-param { double *dresult }
64 %parse-param { int *has_error }
65 %parse-param { int *bye }
70 top_level: expression {
73 *has_error = $1.has_error;
80 expression: expression '+' expression {
81 if (!$1.has_dval && !$3.has_dval)
82 $$.ival = $1.ival + $3.ival;
84 $$.ival = (long long) ($1.dval + $3.dval);
85 $$.dval = $1.dval + $3.dval;
86 $$.has_error = $1.has_error || $3.has_error;
88 | expression '-' expression {
89 if (!$1.has_dval && !$3.has_dval)
90 $$.ival = $1.ival - $3.ival;
92 $$.ival = (long long) ($1.dval - $3.dval);
93 $$.dval = $1.dval - $3.dval;
94 $$.has_error = $1.has_error || $3.has_error;
96 | expression '*' expression {
97 if (!$1.has_dval && !$3.has_dval)
98 $$.ival = $1.ival * $3.ival;
100 $$.ival = (long long) ($1.dval * $3.dval);
101 $$.dval = $1.dval * $3.dval;
102 $$.has_error = $1.has_error || $3.has_error;
104 | expression '/' expression {
106 yyerror(0, 0, 0, 0, "divide by zero");
108 $$.ival = $1.ival / $3.ival;
109 if ($3.dval < 1e-20 && $3.dval > -1e-20)
110 yyerror(0, 0, 0, 0, "divide by zero");
112 $$.dval = $1.dval / $3.dval;
113 if ($3.has_dval || $1.has_dval)
114 $$.ival = (long long) $$.dval;
115 $$.has_error = $1.has_error || $3.has_error;
117 | '-' expression %prec UMINUS {
120 $$.has_error = $2.has_error;
122 | '(' expression ')' { $$ = $2; }
123 | expression SUFFIX {
124 if (!$1.has_dval && !$2.has_dval)
125 $$.ival = $1.ival * $2.ival;
127 $$.ival = (long long) $1.dval * $2.dval;
128 if ($1.has_dval || $2.has_dval)
129 $$.dval = $1.dval * $2.dval;
131 $$.dval = $1.ival * $2.ival;
132 $$.has_error = $1.has_error || $2.has_error;
134 | NUMBER { $$ = $1; }
135 | BYE { $$ = $1; *bye = 1; };
139 /* Urgh. yacc and lex are kind of horrible. This is not thread safe, obviously. */
140 static int lexer_read_offset = 0;
141 static char lexer_input_buffer[1000];
143 int lexer_input(char* buffer, int *bytes_read, int bytes_requested)
145 int bytes_left = strlen(lexer_input_buffer) - lexer_read_offset;
147 if (bytes_requested > bytes_left )
148 bytes_requested = bytes_left;
149 memcpy(buffer, &lexer_input_buffer[lexer_read_offset], bytes_requested);
150 *bytes_read = bytes_requested;
151 lexer_read_offset += bytes_requested;
155 static void setup_to_parse_string(const char *string)
159 len = strlen(string);
160 if (len > sizeof(lexer_input_buffer) - 3)
161 len = sizeof(lexer_input_buffer) - 3;
163 strncpy(lexer_input_buffer, string, len);
164 lexer_input_buffer[len] = '\0';
165 lexer_input_buffer[len + 1] = '\0'; /* lex/yacc want string double null terminated! */
166 lexer_read_offset = 0;
169 int evaluate_arithmetic_expression(const char *buffer, long long *ival, double *dval)
171 int rc, bye = 0, has_error = 0;
173 setup_to_parse_string(buffer);
174 rc = yyparse(ival, dval, &has_error, &bye);
176 if (rc || bye || has_error) {
184 int yyerror(__attribute__((unused)) long long *result,
185 __attribute__((unused)) double *dresult,
186 __attribute__((unused)) int *has_error,
187 __attribute__((unused)) int *bye, const char *msg)
189 fprintf(stderr, "%s\n", msg);