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