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