1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 #include "sl_pp_context.h"
31 #include "sl_pp_expression.h"
32 #include "sl_pp_public.h"
33 #include "sl_pp_token.h"
36 struct parse_context
{
37 struct sl_pp_context
*context
;
38 const struct sl_pp_token_info
*input
;
42 _parse_or(struct parse_context
*ctx
,
46 _parse_primary(struct parse_context
*ctx
,
49 if (ctx
->input
->token
== SL_PP_UINT
) {
50 *result
= atoi(sl_pp_context_cstr(ctx
->context
, ctx
->input
->data
._uint
));
53 if (ctx
->input
->token
!= SL_PP_LPAREN
) {
54 strcpy(ctx
->context
->error_msg
, "expected `('");
58 if (_parse_or(ctx
, result
)) {
61 if (ctx
->input
->token
!= SL_PP_RPAREN
) {
62 strcpy(ctx
->context
->error_msg
, "expected `)'");
71 _parse_unary(struct parse_context
*ctx
,
74 if (!_parse_primary(ctx
, result
)) {
78 switch (ctx
->input
->token
) {
81 if (_parse_unary(ctx
, result
)) {
89 if (_parse_unary(ctx
, result
)) {
97 if (_parse_unary(ctx
, result
)) {
105 if (_parse_unary(ctx
, result
)) {
119 _parse_multiplicative(struct parse_context
*ctx
,
122 if (_parse_unary(ctx
, result
)) {
128 switch (ctx
->input
->token
) {
131 if (_parse_unary(ctx
, &right
)) {
134 *result
= *result
* right
;
139 if (_parse_unary(ctx
, &right
)) {
142 *result
= *result
/ right
;
147 if (_parse_unary(ctx
, &right
)) {
150 *result
= *result
% right
;
160 _parse_additive(struct parse_context
*ctx
,
163 if (_parse_multiplicative(ctx
, result
)) {
169 switch (ctx
->input
->token
) {
172 if (_parse_multiplicative(ctx
, &right
)) {
175 *result
= *result
+ right
;
180 if (_parse_multiplicative(ctx
, &right
)) {
183 *result
= *result
- right
;
193 _parse_shift(struct parse_context
*ctx
,
196 if (_parse_additive(ctx
, result
)) {
202 switch (ctx
->input
->token
) {
205 if (_parse_additive(ctx
, &right
)) {
208 *result
= *result
<< right
;
213 if (_parse_additive(ctx
, &right
)) {
216 *result
= *result
>> right
;
226 _parse_relational(struct parse_context
*ctx
,
229 if (_parse_shift(ctx
, result
)) {
235 switch (ctx
->input
->token
) {
236 case SL_PP_LESSEQUAL
:
238 if (_parse_shift(ctx
, &right
)) {
241 *result
= *result
<= right
;
244 case SL_PP_GREATEREQUAL
:
246 if (_parse_shift(ctx
, &right
)) {
249 *result
= *result
>= right
;
254 if (_parse_shift(ctx
, &right
)) {
257 *result
= *result
< right
;
262 if (_parse_shift(ctx
, &right
)) {
265 *result
= *result
> right
;
275 _parse_equality(struct parse_context
*ctx
,
278 if (_parse_relational(ctx
, result
)) {
284 switch (ctx
->input
->token
) {
287 if (_parse_relational(ctx
, &right
)) {
290 *result
= *result
== right
;
295 if (_parse_relational(ctx
, &right
)) {
298 *result
= *result
!= right
;
308 _parse_bitand(struct parse_context
*ctx
,
311 if (_parse_equality(ctx
, result
)) {
314 while (ctx
->input
->token
== SL_PP_BITAND
) {
318 if (_parse_equality(ctx
, &right
)) {
321 *result
= *result
& right
;
327 _parse_xor(struct parse_context
*ctx
,
330 if (_parse_bitand(ctx
, result
)) {
333 while (ctx
->input
->token
== SL_PP_XOR
) {
337 if (_parse_bitand(ctx
, &right
)) {
340 *result
= *result
^ right
;
346 _parse_bitor(struct parse_context
*ctx
,
349 if (_parse_xor(ctx
, result
)) {
352 while (ctx
->input
->token
== SL_PP_BITOR
) {
356 if (_parse_xor(ctx
, &right
)) {
359 *result
= *result
| right
;
365 _parse_and(struct parse_context
*ctx
,
368 if (_parse_bitor(ctx
, result
)) {
371 while (ctx
->input
->token
== SL_PP_AND
) {
375 if (_parse_bitor(ctx
, &right
)) {
378 *result
= *result
&& right
;
384 _parse_or(struct parse_context
*ctx
,
387 if (_parse_and(ctx
, result
)) {
390 while (ctx
->input
->token
== SL_PP_OR
) {
394 if (_parse_and(ctx
, &right
)) {
397 *result
= *result
|| right
;
403 sl_pp_execute_expression(struct sl_pp_context
*context
,
404 const struct sl_pp_token_info
*input
,
407 struct parse_context ctx
;
409 ctx
.context
= context
;
412 return _parse_or(&ctx
, result
);