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 {
61 %parse-param { long long *result }
62 %parse-param { double *dresult }
63 %parse-param { int *has_error }
64 %parse-param { int *bye }
69 top_level: expression {
72 *has_error = $1.has_error;
79 expression: expression '+' expression {
80 if (!$1.has_dval && !$3.has_dval)
81 $$.ival = $1.ival + $3.ival;
83 $$.ival = (long long) ($1.dval + $3.dval);
84 $$.dval = $1.dval + $3.dval;
85 $$.has_error = $1.has_error || $3.has_error;
87 | expression '-' expression {
88 if (!$1.has_dval && !$3.has_dval)
89 $$.ival = $1.ival - $3.ival;
91 $$.ival = (long long) ($1.dval - $3.dval);
92 $$.dval = $1.dval - $3.dval;
93 $$.has_error = $1.has_error || $3.has_error;
95 | expression '*' expression {
96 if (!$1.has_dval && !$3.has_dval)
97 $$.ival = $1.ival * $3.ival;
99 $$.ival = (long long) ($1.dval * $3.dval);
100 $$.dval = $1.dval * $3.dval;
101 $$.has_error = $1.has_error || $3.has_error;
103 | expression '/' expression {
105 yyerror(0, 0, 0, 0, "divide by zero");
107 $$.ival = $1.ival / $3.ival;
108 if ($3.dval < 1e-20 && $3.dval > -1e-20)
109 yyerror(0, 0, 0, 0, "divide by zero");
111 $$.dval = $1.dval / $3.dval;
112 if ($3.has_dval || $1.has_dval)
113 $$.ival = (long long) $$.dval;
114 $$.has_error = $1.has_error || $3.has_error;
116 | '-' expression %prec UMINUS {
119 $$.has_error = $2.has_error;
121 | '(' expression ')' { $$ = $2; }
122 | NUMBER { $$ = $1; }
123 | BYE { $$ = $1; *bye = 1; };
127 /* Urgh. yacc and lex are kind of horrible. This is not thread safe, obviously. */
128 static int lexer_read_offset = 0;
129 static char lexer_input_buffer[1000];
131 int lexer_input(char* buffer, int *bytes_read, int bytes_requested)
133 int bytes_left = strlen(lexer_input_buffer) - lexer_read_offset;
135 if (bytes_requested > bytes_left )
136 bytes_requested = bytes_left;
137 memcpy(buffer, &lexer_input_buffer[lexer_read_offset], bytes_requested);
138 *bytes_read = bytes_requested;
139 lexer_read_offset += bytes_requested;
143 static void setup_to_parse_string(const char *string)
147 len = strlen(string);
148 if (len > sizeof(lexer_input_buffer) - 3)
149 len = sizeof(lexer_input_buffer) - 3;
151 strncpy(lexer_input_buffer, string, len);
152 lexer_input_buffer[len] = '\0';
153 lexer_input_buffer[len + 1] = '\0'; /* lex/yacc want string double null terminated! */
154 lexer_read_offset = 0;
157 int evaluate_arithmetic_expression(const char *buffer, long long *ival, double *dval)
159 int rc, bye = 0, has_error = 0;
161 setup_to_parse_string(buffer);
162 rc = yyparse(ival, dval, &has_error, &bye);
164 if (rc || bye || has_error) {
172 int yyerror(__attribute__((unused)) long long *result,
173 __attribute__((unused)) double *dresult,
174 __attribute__((unused)) int *has_error,
175 __attribute__((unused)) int *bye, const char *msg)
177 fprintf(stderr, "%s\n", msg);