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_expression.h"
31 #include "sl_pp_public.h"
34 struct parse_context
{
35 struct sl_pp_context
*context
;
36 const struct sl_pp_token_info
*input
;
40 _parse_or(struct parse_context
*ctx
,
44 _parse_primary(struct parse_context
*ctx
,
47 if (ctx
->input
->token
== SL_PP_UINT
) {
48 *result
= atoi(sl_pp_context_cstr(ctx
->context
, ctx
->input
->data
._uint
));
51 if (ctx
->input
->token
!= SL_PP_LPAREN
) {
52 strcpy(ctx
->context
->error_msg
, "expected `('");
56 if (_parse_or(ctx
, result
)) {
59 if (ctx
->input
->token
!= SL_PP_RPAREN
) {
60 strcpy(ctx
->context
->error_msg
, "expected `)'");
69 _parse_unary(struct parse_context
*ctx
,
72 if (!_parse_primary(ctx
, result
)) {
76 switch (ctx
->input
->token
) {
79 if (_parse_unary(ctx
, result
)) {
87 if (_parse_unary(ctx
, result
)) {
95 if (_parse_unary(ctx
, result
)) {
103 if (_parse_unary(ctx
, result
)) {
117 _parse_multiplicative(struct parse_context
*ctx
,
120 if (_parse_unary(ctx
, result
)) {
126 switch (ctx
->input
->token
) {
129 if (_parse_unary(ctx
, &right
)) {
132 *result
= *result
* right
;
137 if (_parse_unary(ctx
, &right
)) {
140 *result
= *result
/ right
;
145 if (_parse_unary(ctx
, &right
)) {
148 *result
= *result
% right
;
158 _parse_additive(struct parse_context
*ctx
,
161 if (_parse_multiplicative(ctx
, result
)) {
167 switch (ctx
->input
->token
) {
170 if (_parse_multiplicative(ctx
, &right
)) {
173 *result
= *result
+ right
;
178 if (_parse_multiplicative(ctx
, &right
)) {
181 *result
= *result
- right
;
191 _parse_shift(struct parse_context
*ctx
,
194 if (_parse_additive(ctx
, result
)) {
200 switch (ctx
->input
->token
) {
203 if (_parse_additive(ctx
, &right
)) {
206 *result
= *result
<< right
;
211 if (_parse_additive(ctx
, &right
)) {
214 *result
= *result
>> right
;
224 _parse_relational(struct parse_context
*ctx
,
227 if (_parse_shift(ctx
, result
)) {
233 switch (ctx
->input
->token
) {
234 case SL_PP_LESSEQUAL
:
236 if (_parse_shift(ctx
, &right
)) {
239 *result
= *result
<= right
;
242 case SL_PP_GREATEREQUAL
:
244 if (_parse_shift(ctx
, &right
)) {
247 *result
= *result
>= right
;
252 if (_parse_shift(ctx
, &right
)) {
255 *result
= *result
< right
;
260 if (_parse_shift(ctx
, &right
)) {
263 *result
= *result
> right
;
273 _parse_equality(struct parse_context
*ctx
,
276 if (_parse_relational(ctx
, result
)) {
282 switch (ctx
->input
->token
) {
285 if (_parse_relational(ctx
, &right
)) {
288 *result
= *result
== right
;
293 if (_parse_relational(ctx
, &right
)) {
296 *result
= *result
!= right
;
306 _parse_bitand(struct parse_context
*ctx
,
309 if (_parse_equality(ctx
, result
)) {
312 while (ctx
->input
->token
== SL_PP_BITAND
) {
316 if (_parse_equality(ctx
, &right
)) {
319 *result
= *result
& right
;
325 _parse_xor(struct parse_context
*ctx
,
328 if (_parse_bitand(ctx
, result
)) {
331 while (ctx
->input
->token
== SL_PP_XOR
) {
335 if (_parse_bitand(ctx
, &right
)) {
338 *result
= *result
^ right
;
344 _parse_bitor(struct parse_context
*ctx
,
347 if (_parse_xor(ctx
, result
)) {
350 while (ctx
->input
->token
== SL_PP_BITOR
) {
354 if (_parse_xor(ctx
, &right
)) {
357 *result
= *result
| right
;
363 _parse_and(struct parse_context
*ctx
,
366 if (_parse_bitor(ctx
, result
)) {
369 while (ctx
->input
->token
== SL_PP_AND
) {
373 if (_parse_bitor(ctx
, &right
)) {
376 *result
= *result
&& right
;
382 _parse_or(struct parse_context
*ctx
,
385 if (_parse_and(ctx
, result
)) {
388 while (ctx
->input
->token
== SL_PP_OR
) {
392 if (_parse_and(ctx
, &right
)) {
395 *result
= *result
|| right
;
401 sl_pp_execute_expression(struct sl_pp_context
*context
,
402 const struct sl_pp_token_info
*input
,
405 struct parse_context ctx
;
407 ctx
.context
= context
;
410 return _parse_or(&ctx
, result
);