1 /* GNU m4 -- A simple macro processor
2 Copyright (C) 1989-1994, 2001, 2006-2010, 2013-2014, 2017 Free
3 Software Foundation, Inc.
5 This file is part of GNU M4.
7 GNU M4 is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU M4 is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 /* This file contains the functions to evaluate integer expressions
22 for the "eval" and "evalmp" builtins. It is a little, fairly
23 self-contained module, with its own scanner, and a recursive descent
26 It has been carefully factored for use from the GMP module builtin,
27 mpeval: any actual operation performed on numbers is abstracted by
28 a set of macro definitions. For plain `eval', `number' is some
29 long int type, and `numb_*' manipulate those long ints. When
30 using GMP, `number' is typedef'd to `mpq_t' (the arbritrary
31 precision fractional numbers type of GMP), and `numb_*' are mapped
34 There is only one entry point, `m4_evaluate', a single function for
35 both `eval' and `mpeval', but which is redefined appropriately when
36 this file is #included into its clients. */
40 typedef enum eval_token
45 TIMES
, DIVIDE
, MODULO
, RATIO
,
46 EQ
, NOTEQ
, GT
, GTEQ
, LS
, LSEQ
,
47 LSHIFT
, RSHIFT
, URSHIFT
,
51 QUESTION
, COLON
, COMMA
,
58 typedef enum eval_error
64 /* All errors prior to SYNTAX_ERROR can be ignored in a dead
65 branch of && and ||. All errors after are just more details
66 about a syntax error. */
76 static eval_error
comma_term (m4
*, eval_token
, number
*);
77 static eval_error
condition_term (m4
*, eval_token
, number
*);
78 static eval_error
logical_or_term (m4
*, eval_token
, number
*);
79 static eval_error
logical_and_term (m4
*, eval_token
, number
*);
80 static eval_error
or_term (m4
*, eval_token
, number
*);
81 static eval_error
xor_term (m4
*, eval_token
, number
*);
82 static eval_error
and_term (m4
*, eval_token
, number
*);
83 static eval_error
equality_term (m4
*, eval_token
, number
*);
84 static eval_error
cmp_term (m4
*, eval_token
, number
*);
85 static eval_error
shift_term (m4
*, eval_token
, number
*);
86 static eval_error
add_term (m4
*, eval_token
, number
*);
87 static eval_error
mult_term (m4
*, eval_token
, number
*);
88 static eval_error
exp_term (m4
*, eval_token
, number
*);
89 static eval_error
unary_term (m4
*, eval_token
, number
*);
90 static eval_error
simple_term (m4
*, eval_token
, number
*);
91 static eval_error
numb_pow (number
*, number
*);
95 /* --- LEXICAL FUNCTIONS --- */
97 /* Pointer to next character of input text. */
98 static const char *eval_text
;
100 /* Value of eval_text, from before last call of eval_lex (). This is so we
101 can back up, if we have read too much. */
102 static const char *last_text
;
104 /* Detect when to end parsing. */
105 static const char *end_text
;
107 /* Prime the lexer at the start of TEXT, with length LEN. */
109 eval_init_lex (const char *text
, size_t len
)
112 end_text
= text
+ len
;
119 eval_text
= last_text
;
122 /* VAL is numerical value, if any. Recognize C assignment operators,
123 even though we cannot support them, to issue better error
127 eval_lex (number
*val
)
129 while (eval_text
!= end_text
&& isspace (to_uchar (*eval_text
)))
132 last_text
= eval_text
;
134 if (eval_text
== end_text
)
137 if (isdigit (to_uchar (*eval_text
)))
141 if (*eval_text
== '0')
162 while (isdigit (to_uchar (*eval_text
)) && base
<= 36)
163 base
= 10 * base
+ *eval_text
++ - '0';
164 if (base
== 0 || base
> 36 || *eval_text
!= ':')
176 numb_set_si (val
, 0);
177 for (; *eval_text
; eval_text
++)
179 if (isdigit (to_uchar (*eval_text
)))
180 digit
= *eval_text
- '0';
181 else if (islower (to_uchar (*eval_text
)))
182 digit
= *eval_text
- 'a' + 10;
183 else if (isupper (to_uchar (*eval_text
)))
184 digit
= *eval_text
- 'A' + 10;
192 else if (digit
== 0 && numb_zerop (*val
))
197 else if (digit
>= base
)
204 /* (*val) = (*val) * base; */
206 numb_set_si (&xbase
, base
);
207 numb_times (*val
, xbase
);
209 /* (*val) = (*val) + digit; */
211 numb_set_si (&xdigit
, digit
);
212 numb_plus (*val
, xdigit
);
219 switch (*eval_text
++)
222 if (*eval_text
== '+' || *eval_text
== '=')
226 if (*eval_text
== '-' || *eval_text
== '=')
230 if (*eval_text
== '*')
235 else if (*eval_text
== '=')
239 if (*eval_text
== '=')
243 if (*eval_text
== '=')
249 if (*eval_text
== '=')
256 if (*eval_text
== '=')
263 if (*eval_text
== '=')
268 else if (*eval_text
== '>')
271 if (*eval_text
== '=')
273 else if (*eval_text
== '>')
283 if (*eval_text
== '=')
288 else if (*eval_text
== '<')
290 if (*++eval_text
== '=')
297 if (*eval_text
== '=')
303 if (*eval_text
== '&')
308 else if (*eval_text
== '=')
312 if (*eval_text
== '|')
317 else if (*eval_text
== '=')
335 /* Recursive descent parser. */
337 comma_term (m4
*context
, eval_token et
, number
*v1
)
342 if ((er
= condition_term (context
, et
, v1
)) != NO_ERROR
)
346 while ((et
= eval_lex (&v2
)) == COMMA
)
350 return UNKNOWN_INPUT
;
352 if ((er
= condition_term (context
, et
, &v2
)) != NO_ERROR
)
358 return UNKNOWN_INPUT
;
365 condition_term (m4
*context
, eval_token et
, number
*v1
)
371 if ((er
= logical_or_term (context
, et
, v1
)) != NO_ERROR
)
376 if ((et
= eval_lex (&v2
)) == QUESTION
)
380 return UNKNOWN_INPUT
;
382 /* Implement short-circuiting of valid syntax. */
383 er
= comma_term (context
, et
, &v2
);
385 && !(numb_zerop (*v1
) && er
< SYNTAX_ERROR
))
390 return UNKNOWN_INPUT
;
392 return MISSING_COLON
;
396 return UNKNOWN_INPUT
;
398 er
= condition_term (context
, et
, &v3
);
400 && !(! numb_zerop (*v1
) && er
< SYNTAX_ERROR
))
403 numb_set (*v1
, ! numb_zerop (*v1
) ? v2
: v3
);
408 return UNKNOWN_INPUT
;
415 logical_or_term (m4
*context
, eval_token et
, number
*v1
)
420 if ((er
= logical_and_term (context
, et
, v1
)) != NO_ERROR
)
424 while ((et
= eval_lex (&v2
)) == LOR
)
428 return UNKNOWN_INPUT
;
430 /* Implement short-circuiting of valid syntax. */
431 er
= logical_and_term (context
, et
, &v2
);
434 else if (! numb_zerop (*v1
) && er
< SYNTAX_ERROR
)
435 numb_set (*v1
, numb_ONE
);
441 return UNKNOWN_INPUT
;
448 logical_and_term (m4
*context
, eval_token et
, number
*v1
)
453 if ((er
= or_term (context
, et
, v1
)) != NO_ERROR
)
457 while ((et
= eval_lex (&v2
)) == LAND
)
461 return UNKNOWN_INPUT
;
463 /* Implement short-circuiting of valid syntax. */
464 er
= or_term (context
, et
, &v2
);
467 else if (numb_zerop (*v1
) && er
< SYNTAX_ERROR
)
468 numb_set (*v1
, numb_ZERO
);
474 return UNKNOWN_INPUT
;
481 or_term (m4
*context
, eval_token et
, number
*v1
)
486 if ((er
= xor_term (context
, et
, v1
)) != NO_ERROR
)
490 while ((et
= eval_lex (&v2
)) == OR
)
494 return UNKNOWN_INPUT
;
496 if ((er
= xor_term (context
, et
, &v2
)) != NO_ERROR
)
499 numb_ior (context
, v1
, &v2
);
503 return UNKNOWN_INPUT
;
510 xor_term (m4
*context
, eval_token et
, number
*v1
)
515 if ((er
= and_term (context
, et
, v1
)) != NO_ERROR
)
519 while ((et
= eval_lex (&v2
)) == XOR
)
523 return UNKNOWN_INPUT
;
525 if ((er
= and_term (context
, et
, &v2
)) != NO_ERROR
)
528 numb_eor (context
, v1
, &v2
);
532 return UNKNOWN_INPUT
;
539 and_term (m4
*context
, eval_token et
, number
*v1
)
544 if ((er
= equality_term (context
, et
, v1
)) != NO_ERROR
)
548 while ((et
= eval_lex (&v2
)) == AND
)
552 return UNKNOWN_INPUT
;
554 if ((er
= equality_term (context
, et
, &v2
)) != NO_ERROR
)
557 numb_and (context
, v1
, &v2
);
561 return UNKNOWN_INPUT
;
568 equality_term (m4
*context
, eval_token et
, number
*v1
)
574 if ((er
= cmp_term (context
, et
, v1
)) != NO_ERROR
)
578 while ((op
= eval_lex (&v2
)) == EQ
|| op
== NOTEQ
)
582 return UNKNOWN_INPUT
;
584 if ((er
= cmp_term (context
, et
, &v2
)) != NO_ERROR
)
594 return UNKNOWN_INPUT
;
601 cmp_term (m4
*context
, eval_token et
, number
*v1
)
607 if ((er
= shift_term (context
, et
, v1
)) != NO_ERROR
)
611 while ((op
= eval_lex (&v2
)) == GT
|| op
== GTEQ
612 || op
== LS
|| op
== LSEQ
)
617 return UNKNOWN_INPUT
;
619 if ((er
= shift_term (context
, et
, &v2
)) != NO_ERROR
)
641 assert (!"INTERNAL ERROR: bad comparison operator in cmp_term ()");
647 return UNKNOWN_INPUT
;
654 shift_term (m4
*context
, eval_token et
, number
*v1
)
660 if ((er
= add_term (context
, et
, v1
)) != NO_ERROR
)
664 while ((op
= eval_lex (&v2
)) == LSHIFT
|| op
== RSHIFT
|| op
== URSHIFT
)
669 return UNKNOWN_INPUT
;
671 if ((er
= add_term (context
, et
, &v2
)) != NO_ERROR
)
677 numb_lshift (context
, v1
, &v2
);
681 numb_rshift (context
, v1
, &v2
);
685 numb_urshift (context
, v1
, &v2
);
689 assert (!"INTERNAL ERROR: bad shift operator in shift_term ()");
695 return UNKNOWN_INPUT
;
702 add_term (m4
*context
, eval_token et
, number
*v1
)
708 if ((er
= mult_term (context
, et
, v1
)) != NO_ERROR
)
712 while ((op
= eval_lex (&v2
)) == PLUS
|| op
== MINUS
)
716 return UNKNOWN_INPUT
;
718 if ((er
= mult_term (context
, et
, &v2
)) != NO_ERROR
)
724 numb_minus (*v1
, v2
);
728 return UNKNOWN_INPUT
;
735 mult_term (m4
*context
, eval_token et
, number
*v1
)
741 if ((er
= exp_term (context
, et
, v1
)) != NO_ERROR
)
745 while (op
= eval_lex (&v2
),
753 return UNKNOWN_INPUT
;
755 if ((er
= exp_term (context
, et
, &v2
)) != NO_ERROR
)
761 numb_times (*v1
, v2
);
768 numb_divide(v1
, &v2
);
775 numb_ratio (*v1
, v2
);
782 numb_modulo (context
, v1
, &v2
);
786 assert (!"INTERNAL ERROR: bad operator in mult_term ()");
792 return UNKNOWN_INPUT
;
799 exp_term (m4
*context
, eval_token et
, number
*v1
)
804 if ((er
= unary_term (context
, et
, v1
)) != NO_ERROR
)
808 while ((et
= eval_lex (&v2
)) == EXPONENT
)
812 return UNKNOWN_INPUT
;
814 if ((er
= exp_term (context
, et
, &v2
)) != NO_ERROR
)
817 if ((er
= numb_pow (v1
, &v2
)) != NO_ERROR
)
822 return UNKNOWN_INPUT
;
829 unary_term (m4
*context
, eval_token et
, number
*v1
)
833 if (et
== PLUS
|| et
== MINUS
|| et
== NOT
|| et
== LNOT
)
835 eval_token et2
= eval_lex (v1
);
837 return UNKNOWN_INPUT
;
839 if ((er
= unary_term (context
, et2
, v1
)) != NO_ERROR
)
845 numb_not (context
, v1
);
849 else if ((er
= simple_term (context
, et
, v1
)) != NO_ERROR
)
856 simple_term (m4
*context
, eval_token et
, number
*v1
)
866 return UNKNOWN_INPUT
;
868 if ((er
= comma_term (context
, et
, v1
)) != NO_ERROR
)
873 return UNKNOWN_INPUT
;
876 return MISSING_RIGHT
;
884 return INVALID_OPERATOR
;
892 /* Main entry point, called from "eval" and "mpeval" builtins. */
894 m4_evaluate (m4
*context
, m4_obstack
*obs
, size_t argc
, m4_macro_args
*argv
)
896 const m4_call_info
*me
= m4_arg_info (argv
);
897 const char * str
= M4ARG (1);
902 eval_error err
= NO_ERROR
;
904 if (!m4_arg_empty (argv
, 2)
905 && !m4_numeric_arg (context
, me
, M4ARG (2), M4ARGLEN (2), &radix
))
908 if (radix
< 1 || radix
> 36)
910 m4_warn (context
, 0, me
, _("radix out of range: %d"), radix
);
914 if (argc
>= 4 && !m4_numeric_arg (context
, me
, M4ARG (3), M4ARGLEN (3),
920 m4_warn (context
, 0, me
, _("negative width: %d"), min
);
925 eval_init_lex (str
, M4ARGLEN (1));
928 et
= eval_lex (&val
);
931 m4_warn (context
, 0, me
, _("empty string treated as 0"));
932 numb_set (val
, numb_ZERO
);
935 err
= comma_term (context
, et
, &val
);
937 if (err
== NO_ERROR
&& *eval_text
!= '\0')
939 if (eval_lex (&val
) == BADOP
)
940 err
= INVALID_OPERATOR
;
946 str
= quotearg_style_mem (locale_quoting_style
, str
, M4ARGLEN (1));
950 numb_obstack (obs
, val
, radix
, min
);
954 m4_warn (context
, 0, me
, _("missing right parenthesis: %s"), str
);
958 m4_warn (context
, 0, me
, _("missing colon: %s"), str
);
962 m4_warn (context
, 0, me
, _("bad expression: %s"), str
);
966 m4_warn (context
, 0, me
, _("bad input: %s"), str
);
970 m4_warn (context
, 0, me
, _("excess input: %s"), str
);
973 case INVALID_OPERATOR
:
974 m4_warn (context
, 0, me
, _("invalid operator: %s"), str
);
978 m4_warn (context
, 0, me
, _("divide by zero: %s"), str
);
982 m4_warn (context
, 0, me
, _("modulo by zero: %s"), str
);
985 case NEGATIVE_EXPONENT
:
986 m4_warn (context
, 0, me
, _("negative exponent: %s"), str
);
990 assert (!"INTERNAL ERROR: bad error code in evaluate ()");
998 numb_pow (number
*x
, number
*y
)
1000 /* y should be integral */
1005 numb_set_si (&ans
, 1);
1007 if (numb_zerop (*x
) && numb_zerop (*y
))
1013 if (numb_negativep (yy
))
1019 while (numb_positivep (yy
))
1021 numb_times (ans
, *x
);