1 /* GNU m4 -- A simple macro processor
3 Copyright (C) 1989-1994, 2006-2007, 2009-2014, 2016-2017, 2020-2021
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 <https://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 (c_isspace (*eval_text))
111 last_text = eval_text;
113 if (*eval_text == '\0')
116 if (c_isdigit (*eval_text))
118 unsigned int base, digit;
119 /* The documentation says that "overflow silently results in wraparound".
120 Therefore use an unsigned integer type to avoid undefined behaviour
121 when parsing '-2147483648'. */
124 if (*eval_text == '0')
145 while (c_isdigit (*eval_text) && base <= 36)
146 base = 10 * base + *eval_text++ - '0';
147 if (base == 0 || base > 36 || *eval_text != ':')
160 for (; *eval_text; eval_text++)
162 if (c_isdigit (*eval_text))
163 digit = *eval_text - '0';
164 else if (c_islower (*eval_text))
165 digit = *eval_text - 'a' + 10;
166 else if (c_isupper (*eval_text))
167 digit = *eval_text - 'A' + 10;
175 else if (digit == 0 && value == 0)
180 else if (digit >= base)
183 value = value * base + digit;
189 switch (*eval_text++)
192 if (*eval_text == '+' || *eval_text == '=')
196 if (*eval_text == '-' || *eval_text == '=')
200 if (*eval_text == '*')
205 else if (*eval_text == '=')
209 if (*eval_text == '=')
213 if (*eval_text == '=')
217 if (*eval_text == '=')
224 if (*eval_text == '=')
231 if (*eval_text == '=')
236 else if (*eval_text == '>')
238 if (*++eval_text == '=')
244 if (*eval_text == '=')
249 else if (*eval_text == '<')
251 if (*++eval_text == '=')
257 if (*eval_text == '=')
263 if (*eval_text == '&')
268 else if (*eval_text == '=')
272 if (*eval_text == '|')
277 else if (*eval_text == '=')
289 /*---------------------------------------.
290 | Main entry point, called from "eval". |
291 `---------------------------------------*/
294 evaluate (const char *expr, int32_t *val)
299 eval_init_lex (expr);
301 err = logical_or_term (et, val);
303 if (err == NO_ERROR && *eval_text != '\0')
305 if (eval_lex (val) == BADOP)
306 err = INVALID_OPERATOR;
317 M4ERROR ((warning_status, 0,
318 _("bad expression in eval (missing right parenthesis): %s"),
323 M4ERROR ((warning_status, 0,
324 _("bad expression in eval: %s"), expr));
328 M4ERROR ((warning_status, 0,
329 _("bad expression in eval (bad input): %s"), expr));
333 M4ERROR ((warning_status, 0,
334 _("bad expression in eval (excess input): %s"), expr));
337 case INVALID_OPERATOR:
338 M4ERROR ((warning_status, 0,
339 _("invalid operator in eval: %s"), expr));
340 retcode = EXIT_FAILURE;
344 M4ERROR ((warning_status, 0,
345 _("divide by zero in eval: %s"), expr));
349 M4ERROR ((warning_status, 0,
350 _("modulo by zero in eval: %s"), expr));
353 case NEGATIVE_EXPONENT:
354 M4ERROR ((warning_status, 0,
355 _("negative exponent in eval: %s"), expr));
359 M4ERROR ((warning_status, 0,
360 "INTERNAL ERROR: bad error code in evaluate ()"));
364 return err != NO_ERROR;
367 /*---------------------------.
368 | Recursive descent parser. |
369 `---------------------------*/
372 logical_or_term (eval_token et, int32_t *v1)
377 if ((er = logical_and_term (et, v1)) != NO_ERROR)
380 while ((et = eval_lex (&v2)) == LOR)
384 return UNKNOWN_INPUT;
386 /* Implement short-circuiting of valid syntax. */
387 er = logical_and_term (et, &v2);
390 else if (*v1 != 0 && er < SYNTAX_ERROR)
396 return UNKNOWN_INPUT;
403 logical_and_term (eval_token et, int32_t *v1)
408 if ((er = or_term (et, v1)) != NO_ERROR)
411 while ((et = eval_lex (&v2)) == LAND)
415 return UNKNOWN_INPUT;
417 /* Implement short-circuiting of valid syntax. */
418 er = or_term (et, &v2);
421 else if (*v1 == 0 && er < SYNTAX_ERROR)
422 ; /* v1 is already 0 */
427 return UNKNOWN_INPUT;
434 or_term (eval_token et, int32_t *v1)
439 if ((er = xor_term (et, v1)) != NO_ERROR)
442 while ((et = eval_lex (&v2)) == OR)
446 return UNKNOWN_INPUT;
448 if ((er = xor_term (et, &v2)) != NO_ERROR)
454 return UNKNOWN_INPUT;
461 xor_term (eval_token et, int32_t *v1)
466 if ((er = and_term (et, v1)) != NO_ERROR)
469 while ((et = eval_lex (&v2)) == XOR)
473 return UNKNOWN_INPUT;
475 if ((er = and_term (et, &v2)) != NO_ERROR)
481 return UNKNOWN_INPUT;
488 and_term (eval_token et, int32_t *v1)
493 if ((er = equality_term (et, v1)) != NO_ERROR)
496 while ((et = eval_lex (&v2)) == AND)
500 return UNKNOWN_INPUT;
502 if ((er = equality_term (et, &v2)) != NO_ERROR)
508 return UNKNOWN_INPUT;
515 equality_term (eval_token et, int32_t *v1)
521 if ((er = cmp_term (et, v1)) != NO_ERROR)
524 /* In the 1.4.x series, we maintain the traditional behavior that
525 '=' is a synonym for '=='; however, this is contrary to POSIX and
526 we hope to convert '=' to mean assignment in 2.0. */
527 while ((op = eval_lex (&v2)) == EQ || op == NOTEQ || op == ASSIGN)
531 return UNKNOWN_INPUT;
533 if ((er = cmp_term (et, &v2)) != NO_ERROR)
538 M4ERROR ((warning_status, 0, _("\
539 Warning: recommend ==, not =, for equality operator")));
542 *v1 = (op == EQ) == (*v1 == v2);
545 return UNKNOWN_INPUT;
552 cmp_term (eval_token et, int32_t *v1)
558 if ((er = shift_term (et, v1)) != NO_ERROR)
561 while ((op = eval_lex (&v2)) == GT || op == GTEQ
562 || op == LS || op == LSEQ)
567 return UNKNOWN_INPUT;
569 if ((er = shift_term (et, &v2)) != NO_ERROR)
591 M4ERROR ((warning_status, 0,
592 "INTERNAL ERROR: bad comparison operator in cmp_term ()"));
597 return UNKNOWN_INPUT;
604 shift_term (eval_token et, int32_t *v1)
611 if ((er = add_term (et, v1)) != NO_ERROR)
614 while ((op = eval_lex (&v2)) == LSHIFT || op == RSHIFT)
619 return UNKNOWN_INPUT;
621 if ((er = add_term (et, &v2)) != NO_ERROR)
624 /* Minimize undefined C behavior (shifting by a negative number,
625 shifting by the width or greater, left shift overflow, or
626 right shift of a negative number). Implement Java 32-bit
627 wrap-around semantics. This code assumes that the
628 implementation-defined overflow when casting unsigned to
629 signed is a silent twos-complement wrap-around. */
634 u1 <<= (uint32_t) (v2 & 0x1f);
639 u1 = *v1 < 0 ? ~*v1 : *v1;
640 u1 >>= (uint32_t) (v2 & 0x1f);
641 *v1 = *v1 < 0 ? ~u1 : u1;
645 M4ERROR ((warning_status, 0,
646 "INTERNAL ERROR: bad shift operator in shift_term ()"));
651 return UNKNOWN_INPUT;
658 add_term (eval_token et, int32_t *v1)
664 if ((er = mult_term (et, v1)) != NO_ERROR)
667 while ((op = eval_lex (&v2)) == PLUS || op == MINUS)
671 return UNKNOWN_INPUT;
673 if ((er = mult_term (et, &v2)) != NO_ERROR)
676 /* Minimize undefined C behavior on overflow. This code assumes
677 that the implementation-defined overflow when casting
678 unsigned to signed is a silent twos-complement
681 *v1 = (int32_t) ((uint32_t) *v1 + (uint32_t) v2);
683 *v1 = (int32_t) ((uint32_t) *v1 - (uint32_t) v2);
686 return UNKNOWN_INPUT;
693 mult_term (eval_token et, int32_t *v1)
699 if ((er = exp_term (et, v1)) != NO_ERROR)
702 while ((op = eval_lex (&v2)) == TIMES || op == DIVIDE || op == MODULO)
706 return UNKNOWN_INPUT;
708 if ((er = exp_term (et, &v2)) != NO_ERROR)
711 /* Minimize undefined C behavior on overflow. This code assumes
712 that the implementation-defined overflow when casting
713 unsigned to signed is a silent twos-complement
718 *v1 = (int32_t) ((uint32_t) *v1 * (uint32_t) v2);
725 /* Avoid overflow, and the x86 SIGFPE on INT_MIN / -1. */
726 *v1 = (int32_t) -(uint32_t) *v1;
735 /* Avoid the x86 SIGFPE on INT_MIN % -1. */
742 M4ERROR ((warning_status, 0,
743 "INTERNAL ERROR: bad operator in mult_term ()"));
748 return UNKNOWN_INPUT;
755 exp_term (eval_token et, int32_t *v1)
761 if ((er = unary_term (et, v1)) != NO_ERROR)
764 while ((et = eval_lex (&v2)) == EXPONENT)
768 return UNKNOWN_INPUT;
770 if ((er = exp_term (et, &v2)) != NO_ERROR)
773 /* Minimize undefined C behavior on overflow. This code assumes
774 that the implementation-defined overflow when casting
775 unsigned to signed is a silent twos-complement
779 return NEGATIVE_EXPONENT;
780 if (*v1 == 0 && v2 == 0)
783 result *= (uint32_t) *v1;
787 return UNKNOWN_INPUT;
794 unary_term (eval_token et, int32_t *v1)
798 if (et == PLUS || et == MINUS || et == NOT || et == LNOT)
800 eval_token et2 = eval_lex (v1);
802 return UNKNOWN_INPUT;
804 if ((er = unary_term (et2, v1)) != NO_ERROR)
807 /* Minimize undefined C behavior on overflow. This code assumes
808 that the implementation-defined overflow when casting
809 unsigned to signed is a silent twos-complement
812 *v1 = (int32_t) -(uint32_t) *v1;
816 *v1 = *v1 == 0 ? 1 : 0;
818 else if ((er = simple_term (et, v1)) != NO_ERROR)
825 simple_term (eval_token et, int32_t *v1)
835 return UNKNOWN_INPUT;
837 if ((er = logical_or_term (et, v1)) != NO_ERROR)
842 return UNKNOWN_INPUT;
845 return MISSING_RIGHT;
853 return INVALID_OPERATOR;