1 /* GNU m4 -- A simple macro processor
3 Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 2006, 2007
4 Free Software Foundation, Inc.
6 This file is part of GNU M4.
8 GNU M4 is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU M4 is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /* This file contains the functions to evaluate integer expressions for
23 the "eval" macro. It is a little, fairly self-contained module, with
24 its own scanner, and a recursive descent parser. The only entry point
29 /* Evaluates token types. */
31 typedef enum eval_token
36 TIMES
, DIVIDE
, MODULO
,
37 ASSIGN
, EQ
, NOTEQ
, GT
, GTEQ
, LS
, LSEQ
,
48 typedef enum eval_error
54 /* All errors prior to SYNTAX_ERROR can be ignored in a dead
55 branch of && and ||. All errors after are just more details
56 about a syntax error. */
65 static eval_error
logical_or_term (eval_token
, int32_t *);
66 static eval_error
logical_and_term (eval_token
, int32_t *);
67 static eval_error
or_term (eval_token
, int32_t *);
68 static eval_error
xor_term (eval_token
, int32_t *);
69 static eval_error
and_term (eval_token
, int32_t *);
70 static eval_error
equality_term (eval_token
, int32_t *);
71 static eval_error
cmp_term (eval_token
, int32_t *);
72 static eval_error
shift_term (eval_token
, int32_t *);
73 static eval_error
add_term (eval_token
, int32_t *);
74 static eval_error
mult_term (eval_token
, int32_t *);
75 static eval_error
exp_term (eval_token
, int32_t *);
76 static eval_error
unary_term (eval_token
, int32_t *);
77 static eval_error
simple_term (eval_token
, int32_t *);
79 /*--------------------.
80 | Lexical functions. |
81 `--------------------*/
83 /* Pointer to next character of input text. */
84 static const char *eval_text
;
86 /* Value of eval_text, from before last call of eval_lex (). This is so we
87 can back up, if we have read too much. */
88 static const char *last_text
;
91 eval_init_lex (const char *text
)
100 eval_text
= last_text
;
103 /* VAL is numerical value, if any. */
106 eval_lex (int32_t *val
)
108 while (isspace (to_uchar (*eval_text
)))
111 last_text
= eval_text
;
113 if (*eval_text
== '\0')
116 if (isdigit (to_uchar (*eval_text
)))
120 if (*eval_text
== '0')
141 while (isdigit (to_uchar (*eval_text
)) && base
<= 36)
142 base
= 10 * base
+ *eval_text
++ - '0';
143 if (base
== 0 || base
> 36 || *eval_text
!= ':')
155 /* FIXME - this calculation can overflow. Consider xstrtol. */
157 for (; *eval_text
; eval_text
++)
159 if (isdigit (to_uchar (*eval_text
)))
160 digit
= *eval_text
- '0';
161 else if (islower (to_uchar (*eval_text
)))
162 digit
= *eval_text
- 'a' + 10;
163 else if (isupper (to_uchar (*eval_text
)))
164 digit
= *eval_text
- 'A' + 10;
172 else if (digit
== 0 && !*val
)
177 else if (digit
>= base
)
180 *val
= *val
* base
+ digit
;
185 switch (*eval_text
++)
188 if (*eval_text
== '+' || *eval_text
== '=')
192 if (*eval_text
== '-' || *eval_text
== '=')
196 if (*eval_text
== '*')
201 else if (*eval_text
== '=')
205 if (*eval_text
== '=')
209 if (*eval_text
== '=')
213 if (*eval_text
== '=')
220 if (*eval_text
== '=')
227 if (*eval_text
== '=')
232 else if (*eval_text
== '>')
234 if (*++eval_text
== '=')
240 if (*eval_text
== '=')
245 else if (*eval_text
== '<')
247 if (*++eval_text
== '=')
253 if (*eval_text
== '=')
259 if (*eval_text
== '&')
264 else if (*eval_text
== '=')
268 if (*eval_text
== '|')
273 else if (*eval_text
== '=')
285 /*---------------------------------------.
286 | Main entry point, called from "eval". |
287 `---------------------------------------*/
290 evaluate (const char *expr
, int32_t *val
)
295 eval_init_lex (expr
);
297 err
= logical_or_term (et
, val
);
299 if (err
== NO_ERROR
&& *eval_text
!= '\0')
301 if (eval_lex (val
) == BADOP
)
302 err
= INVALID_OPERATOR
;
313 M4ERROR ((warning_status
, 0,
314 "bad expression in eval (missing right parenthesis): %s",
319 M4ERROR ((warning_status
, 0,
320 "bad expression in eval: %s", expr
));
324 M4ERROR ((warning_status
, 0,
325 "bad expression in eval (bad input): %s", expr
));
329 M4ERROR ((warning_status
, 0,
330 "bad expression in eval (excess input): %s", expr
));
333 case INVALID_OPERATOR
:
334 M4ERROR ((warning_status
, 0,
335 "invalid operator in eval: %s", expr
));
336 retcode
= EXIT_FAILURE
;
340 M4ERROR ((warning_status
, 0,
341 "divide by zero in eval: %s", expr
));
345 M4ERROR ((warning_status
, 0,
346 "modulo by zero in eval: %s", expr
));
349 case NEGATIVE_EXPONENT
:
350 M4ERROR ((warning_status
, 0,
351 "negative exponent in eval: %s", expr
));
355 M4ERROR ((warning_status
, 0,
356 "INTERNAL ERROR: bad error code in evaluate ()"));
360 return err
!= NO_ERROR
;
363 /*---------------------------.
364 | Recursive descent parser. |
365 `---------------------------*/
368 logical_or_term (eval_token et
, int32_t *v1
)
373 if ((er
= logical_and_term (et
, v1
)) != NO_ERROR
)
376 while ((et
= eval_lex (&v2
)) == LOR
)
380 return UNKNOWN_INPUT
;
382 /* Implement short-circuiting of valid syntax. */
383 er
= logical_and_term (et
, &v2
);
386 else if (*v1
!= 0 && er
< SYNTAX_ERROR
)
392 return UNKNOWN_INPUT
;
399 logical_and_term (eval_token et
, int32_t *v1
)
404 if ((er
= or_term (et
, v1
)) != NO_ERROR
)
407 while ((et
= eval_lex (&v2
)) == LAND
)
411 return UNKNOWN_INPUT
;
413 /* Implement short-circuiting of valid syntax. */
414 er
= or_term (et
, &v2
);
417 else if (*v1
== 0 && er
< SYNTAX_ERROR
)
418 ; /* v1 is already 0 */
423 return UNKNOWN_INPUT
;
430 or_term (eval_token et
, int32_t *v1
)
435 if ((er
= xor_term (et
, v1
)) != NO_ERROR
)
438 while ((et
= eval_lex (&v2
)) == OR
)
442 return UNKNOWN_INPUT
;
444 if ((er
= xor_term (et
, &v2
)) != NO_ERROR
)
450 return UNKNOWN_INPUT
;
457 xor_term (eval_token et
, int32_t *v1
)
462 if ((er
= and_term (et
, v1
)) != NO_ERROR
)
465 while ((et
= eval_lex (&v2
)) == XOR
)
469 return UNKNOWN_INPUT
;
471 if ((er
= and_term (et
, &v2
)) != NO_ERROR
)
477 return UNKNOWN_INPUT
;
484 and_term (eval_token et
, int32_t *v1
)
489 if ((er
= equality_term (et
, v1
)) != NO_ERROR
)
492 while ((et
= eval_lex (&v2
)) == AND
)
496 return UNKNOWN_INPUT
;
498 if ((er
= equality_term (et
, &v2
)) != NO_ERROR
)
504 return UNKNOWN_INPUT
;
511 equality_term (eval_token et
, int32_t *v1
)
517 if ((er
= cmp_term (et
, v1
)) != NO_ERROR
)
520 /* In the 1.4.x series, we maintain the traditional behavior that
521 '=' is a synonym for '=='; however, this is contrary to POSIX and
522 we hope to convert '=' to mean assignment in 2.0. */
523 while ((op
= eval_lex (&v2
)) == EQ
|| op
== NOTEQ
|| op
== ASSIGN
)
527 return UNKNOWN_INPUT
;
529 if ((er
= cmp_term (et
, &v2
)) != NO_ERROR
)
534 M4ERROR ((warning_status
, 0, "\
535 Warning: recommend ==, not =, for equality operator"));
538 *v1
= (op
== EQ
) == (*v1
== v2
);
541 return UNKNOWN_INPUT
;
548 cmp_term (eval_token et
, int32_t *v1
)
554 if ((er
= shift_term (et
, v1
)) != NO_ERROR
)
557 while ((op
= eval_lex (&v2
)) == GT
|| op
== GTEQ
558 || op
== LS
|| op
== LSEQ
)
563 return UNKNOWN_INPUT
;
565 if ((er
= shift_term (et
, &v2
)) != NO_ERROR
)
587 M4ERROR ((warning_status
, 0,
588 "INTERNAL ERROR: bad comparison operator in cmp_term ()"));
593 return UNKNOWN_INPUT
;
600 shift_term (eval_token et
, int32_t *v1
)
607 if ((er
= add_term (et
, v1
)) != NO_ERROR
)
610 while ((op
= eval_lex (&v2
)) == LSHIFT
|| op
== RSHIFT
)
615 return UNKNOWN_INPUT
;
617 if ((er
= add_term (et
, &v2
)) != NO_ERROR
)
620 /* Minimize undefined C behavior (shifting by a negative number,
621 shifting by the width or greater, left shift overflow, or
622 right shift of a negative number). Implement Java 32-bit
623 wrap-around semantics. This code assumes that the
624 implementation-defined overflow when casting unsigned to
625 signed is a silent twos-complement wrap-around. */
630 u1
<<= (uint32_t) (v2
& 0x1f);
635 u1
= *v1
< 0 ? ~*v1
: *v1
;
636 u1
>>= (uint32_t) (v2
& 0x1f);
637 *v1
= *v1
< 0 ? ~u1
: u1
;
641 M4ERROR ((warning_status
, 0,
642 "INTERNAL ERROR: bad shift operator in shift_term ()"));
647 return UNKNOWN_INPUT
;
654 add_term (eval_token et
, int32_t *v1
)
660 if ((er
= mult_term (et
, v1
)) != NO_ERROR
)
663 while ((op
= eval_lex (&v2
)) == PLUS
|| op
== MINUS
)
667 return UNKNOWN_INPUT
;
669 if ((er
= mult_term (et
, &v2
)) != NO_ERROR
)
672 /* Minimize undefined C behavior on overflow. This code assumes
673 that the implementation-defined overflow when casting
674 unsigned to signed is a silent twos-complement
677 *v1
= (int32_t) ((uint32_t) *v1
+ (uint32_t) v2
);
679 *v1
= (int32_t) ((uint32_t) *v1
- (uint32_t) v2
);
682 return UNKNOWN_INPUT
;
689 mult_term (eval_token et
, int32_t *v1
)
695 if ((er
= exp_term (et
, v1
)) != NO_ERROR
)
698 while ((op
= eval_lex (&v2
)) == TIMES
|| op
== DIVIDE
|| op
== MODULO
)
702 return UNKNOWN_INPUT
;
704 if ((er
= exp_term (et
, &v2
)) != NO_ERROR
)
707 /* Minimize undefined C behavior on overflow. This code assumes
708 that the implementation-defined overflow when casting
709 unsigned to signed is a silent twos-complement
714 *v1
= (int32_t) ((uint32_t) *v1
* (uint32_t) v2
);
721 /* Avoid overflow, and the x86 SIGFPE on INT_MIN / -1. */
722 *v1
= (int32_t) -(uint32_t) *v1
;
731 /* Avoid the x86 SIGFPE on INT_MIN % -1. */
738 M4ERROR ((warning_status
, 0,
739 "INTERNAL ERROR: bad operator in mult_term ()"));
744 return UNKNOWN_INPUT
;
751 exp_term (eval_token et
, int32_t *v1
)
757 if ((er
= unary_term (et
, v1
)) != NO_ERROR
)
760 while ((et
= eval_lex (&v2
)) == EXPONENT
)
764 return UNKNOWN_INPUT
;
766 if ((er
= exp_term (et
, &v2
)) != NO_ERROR
)
769 /* Minimize undefined C behavior on overflow. This code assumes
770 that the implementation-defined overflow when casting
771 unsigned to signed is a silent twos-complement
775 return NEGATIVE_EXPONENT
;
776 if (*v1
== 0 && v2
== 0)
779 result
*= (uint32_t) *v1
;
783 return UNKNOWN_INPUT
;
790 unary_term (eval_token et
, int32_t *v1
)
795 if (et
== PLUS
|| et
== MINUS
|| et
== NOT
|| et
== LNOT
)
799 return UNKNOWN_INPUT
;
801 if ((er
= unary_term (et2
, v1
)) != NO_ERROR
)
804 /* Minimize undefined C behavior on overflow. This code assumes
805 that the implementation-defined overflow when casting
806 unsigned to signed is a silent twos-complement
809 *v1
= (int32_t) -(uint32_t) *v1
;
813 *v1
= *v1
== 0 ? 1 : 0;
815 else if ((er
= simple_term (et
, v1
)) != NO_ERROR
)
822 simple_term (eval_token et
, int32_t *v1
)
832 return UNKNOWN_INPUT
;
834 if ((er
= logical_or_term (et
, v1
)) != NO_ERROR
)
839 return UNKNOWN_INPUT
;
842 return MISSING_RIGHT
;
850 return INVALID_OPERATOR
;