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 { | |
5fe4c886 JA |
166 | double x = (double) $1.ival; |
167 | double y = (double) $3.ival; | |
168 | tmp = pow(x, y); | |
ae46966a SC |
169 | } |
170 | $$.ival = (long long) tmp; | |
171 | } | |
172 | $$.dval = pow($1.dval, $3.dval); | |
173 | } else { | |
174 | $$.dval = pow($1.dval, $3.dval); | |
175 | $$.ival = (long long) $$.dval; | |
176 | } | |
177 | } | |
b470a02c SC |
178 | | NUMBER { $$ = $1; } |
179 | | BYE { $$ = $1; *bye = 1; }; | |
180 | %% | |
181 | #include <stdio.h> | |
182 | ||
183 | /* Urgh. yacc and lex are kind of horrible. This is not thread safe, obviously. */ | |
184 | static int lexer_read_offset = 0; | |
185 | static char lexer_input_buffer[1000]; | |
186 | ||
187 | int lexer_input(char* buffer, int *bytes_read, int bytes_requested) | |
188 | { | |
189 | int bytes_left = strlen(lexer_input_buffer) - lexer_read_offset; | |
190 | ||
191 | if (bytes_requested > bytes_left ) | |
192 | bytes_requested = bytes_left; | |
193 | memcpy(buffer, &lexer_input_buffer[lexer_read_offset], bytes_requested); | |
194 | *bytes_read = bytes_requested; | |
195 | lexer_read_offset += bytes_requested; | |
196 | return 0; | |
197 | } | |
198 | ||
199 | static void setup_to_parse_string(const char *string) | |
200 | { | |
201 | unsigned int len; | |
202 | ||
203 | len = strlen(string); | |
204 | if (len > sizeof(lexer_input_buffer) - 3) | |
205 | len = sizeof(lexer_input_buffer) - 3; | |
206 | ||
207 | strncpy(lexer_input_buffer, string, len); | |
208 | lexer_input_buffer[len] = '\0'; | |
209 | lexer_input_buffer[len + 1] = '\0'; /* lex/yacc want string double null terminated! */ | |
210 | lexer_read_offset = 0; | |
211 | } | |
212 | ||
213 | int evaluate_arithmetic_expression(const char *buffer, long long *ival, double *dval) | |
214 | { | |
215 | int rc, bye = 0, has_error = 0; | |
216 | ||
217 | setup_to_parse_string(buffer); | |
218 | rc = yyparse(ival, dval, &has_error, &bye); | |
219 | yyrestart(NULL); | |
220 | if (rc || bye || has_error) { | |
221 | *ival = 0; | |
222 | *dval = 0; | |
223 | has_error = 1; | |
224 | } | |
225 | return has_error; | |
226 | } | |
227 | ||
228 | int yyerror(__attribute__((unused)) long long *result, | |
229 | __attribute__((unused)) double *dresult, | |
230 | __attribute__((unused)) int *has_error, | |
231 | __attribute__((unused)) int *bye, const char *msg) | |
232 | { | |
233 | fprintf(stderr, "%s\n", msg); | |
234 | return 0; | |
235 | } | |
236 |