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