blob: 100614aa71fc44dff8258013a3e68a06bd03d9fa (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
/******************************************************************************
*
*
*
*
* Copyright (C) 1997-2015 by Dimitri van Heesch.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation under the terms of the GNU General Public License is hereby
* granted. No representations are made about the suitability of this software
* for any purpose. It is provided "as is" without express or implied warranty.
* See the GNU General Public License for more details.
*
* Documents produced by Doxygen are derivative works derived from the
* input used in their production; they are not affected by this license.
*
*/
%{
#include "cppvalue.h"
#include "constexp_p.h"
#include "message.h"
#include <stdio.h>
#include <stdlib.h>
int constexpYYerror(yyscan_t yyscanner, const char *s)
{
struct constexpYY_state* yyextra = constexpYYget_extra(yyscanner);
warn(yyextra->constExpFileName, yyextra->constExpLineNr,
"preprocessing issue while doing constant expression evaluation: %s",s);
return 0;
}
%}
%name-prefix "constexpYY"
%define api.pure full
%lex-param {yyscan_t yyscanner}
%parse-param {yyscan_t yyscanner}
%token TOK_QUESTIONMARK
%token TOK_COLON
%token TOK_OR
%token TOK_AND
%token TOK_BITWISEOR
%token TOK_BITWISEXOR
%token TOK_AMPERSAND
%token TOK_NOTEQUAL
%token TOK_EQUAL
%token TOK_LESSTHAN
%token TOK_GREATERTHAN
%token TOK_LESSTHANOREQUALTO
%token TOK_GREATERTHANOREQUALTO
%token TOK_SHIFTLEFT
%token TOK_SHIFTRIGHT
%token TOK_PLUS
%token TOK_MINUS
%token TOK_STAR
%token TOK_DIVIDE
%token TOK_MOD
%token TOK_TILDE
%token TOK_NOT
%token TOK_LPAREN
%token TOK_RPAREN
%token TOK_OCTALINT
%token TOK_DECIMALINT
%token TOK_HEXADECIMALINT
%token TOK_CHARACTER
%token TOK_FLOAT
%%
start: constant_expression
{
struct constexpYY_state* yyextra = constexpYYget_extra(yyscanner);
yyextra->resultValue = $1; return 0;
}
;
constant_expression: logical_or_expression
{ $$ = $1; }
| logical_or_expression
TOK_QUESTIONMARK logical_or_expression
TOK_COLON logical_or_expression
{
bool c = ($1.isInt() ? ((long)$1 != 0) : ((double)$1 != 0.0));
$$ = c ? $3 : $5;
}
;
logical_or_expression: logical_and_expression
{ $$ = $1; }
| logical_or_expression TOK_OR logical_and_expression
{
$$ = CPPValue( (long)((long)$1 || (long)$3) );
}
;
logical_and_expression: inclusive_or_expression
{ $$ = $1; }
| logical_and_expression TOK_AND inclusive_or_expression
{
$$ = CPPValue( (long)((long)$1 && (long)$3) );
}
;
inclusive_or_expression: exclusive_or_expression
{ $$ = $1; }
| inclusive_or_expression TOK_BITWISEOR
exclusive_or_expression
{
$$ = CPPValue( (long)$1 | (long)$3 );
}
;
exclusive_or_expression: and_expression
{ $$ = $1; }
| exclusive_or_expression TOK_BITWISEXOR and_expression
{
$$ = CPPValue( (long)$1 ^ (long)$3 );
}
;
and_expression: equality_expression
{ $$ = $1; }
| and_expression TOK_AMPERSAND equality_expression
{
$$ = CPPValue( (long)$1 & (long)$3 );
}
;
equality_expression: relational_expression
{ $$ = $1; }
| equality_expression TOK_EQUAL relational_expression
{
$$ = CPPValue( (long)((double)$1 == (double)$3) );
}
| equality_expression TOK_NOTEQUAL relational_expression
{
$$ = CPPValue( (long)((double)$1 != (double)$3) );
}
;
relational_expression: shift_expression
{ $$ = $1; }
| relational_expression TOK_LESSTHAN shift_expression
{
$$ = CPPValue( (long)((double)$1 < (double)$3) );
}
| relational_expression TOK_GREATERTHAN shift_expression
{
$$ = CPPValue( (long)((double)$1 > (double)$3) );
}
| relational_expression TOK_LESSTHANOREQUALTO
shift_expression
{
$$ = CPPValue( (long)((double)$1 <= (double)$3) );
}
| relational_expression TOK_GREATERTHANOREQUALTO
shift_expression
{
$$ = CPPValue( (long)((double)$1 >= (double)$3) );
}
;
shift_expression: additive_expression
{ $$ = $1; }
| shift_expression TOK_SHIFTLEFT additive_expression
{
$$ = CPPValue( (long)$1 << (long)$3 );
}
| shift_expression TOK_SHIFTRIGHT additive_expression
{
$$ = CPPValue( (long)$1 >> (long)$3 );
}
;
additive_expression: multiplicative_expression
{ $$ = $1; }
| additive_expression TOK_PLUS multiplicative_expression
{
if (!$1.isInt() || !$3.isInt())
{
$$ = CPPValue( (double)$1 + (double)$3 );
}
else
{
$$ = CPPValue( (long)$1 + (long)$3 );
}
}
| additive_expression TOK_MINUS multiplicative_expression
{
if (!$1.isInt() || !$3.isInt())
{
$$ = CPPValue( (double)$1 - (double)$3 );
}
else
{
$$ = CPPValue( (long)$1 - (long)$3 );
}
}
;
multiplicative_expression: unary_expression
{ $$ = $1; }
| multiplicative_expression TOK_STAR unary_expression
{
if (!$1.isInt() || !$3.isInt())
{
$$ = CPPValue( (double)$1 * (double)$3 );
}
else
{
$$ = CPPValue( (long)$1 * (long)$3 );
}
}
| multiplicative_expression TOK_DIVIDE unary_expression
{
if (!$1.isInt() || !$3.isInt())
{
$$ = CPPValue( (double)$1 / (double)$3 );
}
else
{
long value = $3;
if (value==0) value=1;
$$ = CPPValue( (long)$1 / value );
}
}
| multiplicative_expression TOK_MOD unary_expression
{
long value = $3;
if (value==0) value=1;
$$ = CPPValue( (long)$1 % value );
}
;
unary_expression: primary_expression
{ $$ = $1; }
| TOK_PLUS unary_expression
{ $$ = $1; }
| TOK_MINUS unary_expression
{
if ($2.isInt())
$$ = CPPValue(-(long)$2);
else
$$ = CPPValue(-(double)$2);
}
| TOK_TILDE unary_expression
{
$$ = CPPValue(~(long)$2);
}
| TOK_NOT unary_expression
{
$$ = CPPValue((long)!(long)$2);
}
;
primary_expression: constant
{ $$ = $1; }
| TOK_LPAREN constant_expression TOK_RPAREN
{ $$ = $2; }
;
constant: TOK_OCTALINT
{
struct constexpYY_state* yyextra = constexpYYget_extra(yyscanner);
$$ = parseOctal(yyextra->strToken);
}
| TOK_DECIMALINT
{
struct constexpYY_state* yyextra = constexpYYget_extra(yyscanner);
$$ = parseDecimal(yyextra->strToken);
}
| TOK_HEXADECIMALINT
{
struct constexpYY_state* yyextra = constexpYYget_extra(yyscanner);
$$ = parseHexadecimal(yyextra->strToken);
}
| TOK_CHARACTER
{
struct constexpYY_state* yyextra = constexpYYget_extra(yyscanner);
$$ = parseCharacter(yyextra->strToken);
}
| TOK_FLOAT
{
struct constexpYY_state* yyextra = constexpYYget_extra(yyscanner);
$$ = parseFloat(yyextra->strToken);
}
;
%%
|