1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
|
/* GNU m4 -- A simple macro processor
Copyright (C) 1989-1994, 2001, 2006-2010, 2013 Free Software
Foundation, Inc.
This file is part of GNU M4.
GNU M4 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GNU M4 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* This file contains the functions to evaluate integer expressions
for the "eval" and "evalmp" builtins. It is a little, fairly
self-contained module, with its own scanner, and a recursive descent
parser.
It has been carefully factored for use from the GMP module builtin,
mpeval: any actual operation performed on numbers is abstracted by
a set of macro definitions. For plain `eval', `number' is some
long int type, and `numb_*' manipulate those long ints. When
using GMP, `number' is typedef'd to `mpq_t' (the arbritrary
precision fractional numbers type of GMP), and `numb_*' are mapped
to GMP functions.
There is only one entry point, `m4_evaluate', a single function for
both `eval' and `mpeval', but which is redefined appropriately when
this file is #included into its clients. */
#include "quotearg.h"
typedef enum eval_token
{
ERROR, BADOP,
PLUS, MINUS,
EXPONENT,
TIMES, DIVIDE, MODULO, RATIO,
EQ, NOTEQ, GT, GTEQ, LS, LSEQ,
LSHIFT, RSHIFT, URSHIFT,
LNOT, LAND, LOR,
NOT, AND, OR, XOR,
LEFTP, RIGHTP,
QUESTION, COLON, COMMA,
NUMBER, EOTEXT
}
eval_token;
/* Error types. */
typedef enum eval_error
{
NO_ERROR,
DIVIDE_ZERO,
MODULO_ZERO,
NEGATIVE_EXPONENT,
/* All errors prior to SYNTAX_ERROR can be ignored in a dead
branch of && and ||. All errors after are just more details
about a syntax error. */
SYNTAX_ERROR,
MISSING_RIGHT,
MISSING_COLON,
UNKNOWN_INPUT,
EXCESS_INPUT,
INVALID_OPERATOR
}
eval_error;
static eval_error comma_term (m4 *, eval_token, number *);
static eval_error condition_term (m4 *, eval_token, number *);
static eval_error logical_or_term (m4 *, eval_token, number *);
static eval_error logical_and_term (m4 *, eval_token, number *);
static eval_error or_term (m4 *, eval_token, number *);
static eval_error xor_term (m4 *, eval_token, number *);
static eval_error and_term (m4 *, eval_token, number *);
static eval_error equality_term (m4 *, eval_token, number *);
static eval_error cmp_term (m4 *, eval_token, number *);
static eval_error shift_term (m4 *, eval_token, number *);
static eval_error add_term (m4 *, eval_token, number *);
static eval_error mult_term (m4 *, eval_token, number *);
static eval_error exp_term (m4 *, eval_token, number *);
static eval_error unary_term (m4 *, eval_token, number *);
static eval_error simple_term (m4 *, eval_token, number *);
static eval_error numb_pow (number *, number *);
/* --- LEXICAL FUNCTIONS --- */
/* Pointer to next character of input text. */
static const char *eval_text;
/* Value of eval_text, from before last call of eval_lex (). This is so we
can back up, if we have read too much. */
static const char *last_text;
/* Detect when to end parsing. */
static const char *end_text;
/* Prime the lexer at the start of TEXT, with length LEN. */
static void
eval_init_lex (const char *text, size_t len)
{
eval_text = text;
end_text = text + len;
last_text = NULL;
}
static void
eval_undo (void)
{
eval_text = last_text;
}
/* VAL is numerical value, if any. Recognize C assignment operators,
even though we cannot support them, to issue better error
messages. */
static eval_token
eval_lex (number *val)
{
while (eval_text != end_text && isspace (to_uchar (*eval_text)))
eval_text++;
last_text = eval_text;
if (eval_text == end_text)
return EOTEXT;
if (isdigit (to_uchar (*eval_text)))
{
int base, digit;
if (*eval_text == '0')
{
eval_text++;
switch (*eval_text)
{
case 'x':
case 'X':
base = 16;
eval_text++;
break;
case 'b':
case 'B':
base = 2;
eval_text++;
break;
case 'r':
case 'R':
base = 0;
eval_text++;
while (isdigit (to_uchar (*eval_text)) && base <= 36)
base = 10 * base + *eval_text++ - '0';
if (base == 0 || base > 36 || *eval_text != ':')
return ERROR;
eval_text++;
break;
default:
base = 8;
}
}
else
base = 10;
numb_set_si (val, 0);
for (; *eval_text; eval_text++)
{
if (isdigit (to_uchar (*eval_text)))
digit = *eval_text - '0';
else if (islower (to_uchar (*eval_text)))
digit = *eval_text - 'a' + 10;
else if (isupper (to_uchar (*eval_text)))
digit = *eval_text - 'A' + 10;
else
break;
if (base == 1)
{
if (digit == 1)
numb_incr (*val);
else if (digit == 0 && numb_zerop (*val))
continue;
else
break;
}
else if (digit >= base)
break;
else
{
number xbase;
number xdigit;
/* (*val) = (*val) * base; */
numb_init (xbase);
numb_set_si (&xbase, base);
numb_times (*val, xbase);
numb_fini (xbase);
/* (*val) = (*val) + digit; */
numb_init (xdigit);
numb_set_si (&xdigit, digit);
numb_plus (*val, xdigit);
numb_fini (xdigit);
}
}
return NUMBER;
}
switch (*eval_text++)
{
case '+':
if (*eval_text == '+' || *eval_text == '=')
return BADOP;
return PLUS;
case '-':
if (*eval_text == '-' || *eval_text == '=')
return BADOP;
return MINUS;
case '*':
if (*eval_text == '*')
{
eval_text++;
return EXPONENT;
}
else if (*eval_text == '=')
return BADOP;
return TIMES;
case '/':
if (*eval_text == '=')
return BADOP;
return DIVIDE;
case '%':
if (*eval_text == '=')
return BADOP;
return MODULO;
case '\\':
return RATIO;
case '=':
if (*eval_text == '=')
{
eval_text++;
return EQ;
}
return BADOP;
case '!':
if (*eval_text == '=')
{
eval_text++;
return NOTEQ;
}
return LNOT;
case '>':
if (*eval_text == '=')
{
eval_text++;
return GTEQ;
}
else if (*eval_text == '>')
{
eval_text++;
if (*eval_text == '=')
return BADOP;
else if (*eval_text == '>')
{
eval_text++;
return URSHIFT;
}
return RSHIFT;
}
else
return GT;
case '<':
if (*eval_text == '=')
{
eval_text++;
return LSEQ;
}
else if (*eval_text == '<')
{
if (*++eval_text == '=')
return BADOP;
return LSHIFT;
}
else
return LS;
case '^':
if (*eval_text == '=')
return BADOP;
return XOR;
case '~':
return NOT;
case '&':
if (*eval_text == '&')
{
eval_text++;
return LAND;
}
else if (*eval_text == '=')
return BADOP;
return AND;
case '|':
if (*eval_text == '|')
{
eval_text++;
return LOR;
}
else if (*eval_text == '=')
return BADOP;
return OR;
case '(':
return LEFTP;
case ')':
return RIGHTP;
case '?':
return QUESTION;
case ':':
return COLON;
case ',':
return COMMA;
default:
return ERROR;
}
}
/* Recursive descent parser. */
static eval_error
comma_term (m4 *context, eval_token et, number *v1)
{
number v2;
eval_error er;
if ((er = condition_term (context, et, v1)) != NO_ERROR)
return er;
numb_init (v2);
while ((et = eval_lex (&v2)) == COMMA)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
if ((er = condition_term (context, et, &v2)) != NO_ERROR)
return er;
numb_set (*v1, v2);
}
numb_fini (v2);
if (et == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
condition_term (m4 *context, eval_token et, number *v1)
{
number v2;
number v3;
eval_error er;
if ((er = logical_or_term (context, et, v1)) != NO_ERROR)
return er;
numb_init (v2);
numb_init (v3);
if ((et = eval_lex (&v2)) == QUESTION)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
/* Implement short-circuiting of valid syntax. */
er = comma_term (context, et, &v2);
if (er != NO_ERROR
&& !(numb_zerop (*v1) && er < SYNTAX_ERROR))
return er;
et = eval_lex (&v3);
if (et == ERROR)
return UNKNOWN_INPUT;
if (et != COLON)
return MISSING_COLON;
et = eval_lex (&v3);
if (et == ERROR)
return UNKNOWN_INPUT;
er = condition_term (context, et, &v3);
if (er != NO_ERROR
&& !(! numb_zerop (*v1) && er < SYNTAX_ERROR))
return er;
numb_set (*v1, ! numb_zerop (*v1) ? v2 : v3);
}
numb_fini (v2);
numb_fini (v3);
if (et == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
logical_or_term (m4 *context, eval_token et, number *v1)
{
number v2;
eval_error er;
if ((er = logical_and_term (context, et, v1)) != NO_ERROR)
return er;
numb_init (v2);
while ((et = eval_lex (&v2)) == LOR)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
/* Implement short-circuiting of valid syntax. */
er = logical_and_term (context, et, &v2);
if (er == NO_ERROR)
numb_lior (*v1, v2);
else if (! numb_zerop (*v1) && er < SYNTAX_ERROR)
numb_set (*v1, numb_ONE);
else
return er;
}
numb_fini (v2);
if (et == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
logical_and_term (m4 *context, eval_token et, number *v1)
{
number v2;
eval_error er;
if ((er = or_term (context, et, v1)) != NO_ERROR)
return er;
numb_init (v2);
while ((et = eval_lex (&v2)) == LAND)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
/* Implement short-circuiting of valid syntax. */
er = or_term (context, et, &v2);
if (er == NO_ERROR)
numb_land (*v1, v2);
else if (numb_zerop (*v1) && er < SYNTAX_ERROR)
numb_set (*v1, numb_ZERO);
else
return er;
}
numb_fini (v2);
if (et == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
or_term (m4 *context, eval_token et, number *v1)
{
number v2;
eval_error er;
if ((er = xor_term (context, et, v1)) != NO_ERROR)
return er;
numb_init (v2);
while ((et = eval_lex (&v2)) == OR)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
if ((er = xor_term (context, et, &v2)) != NO_ERROR)
return er;
numb_ior (context, v1, &v2);
}
numb_fini (v2);
if (et == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
xor_term (m4 *context, eval_token et, number *v1)
{
number v2;
eval_error er;
if ((er = and_term (context, et, v1)) != NO_ERROR)
return er;
numb_init (v2);
while ((et = eval_lex (&v2)) == XOR)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
if ((er = and_term (context, et, &v2)) != NO_ERROR)
return er;
numb_eor (context, v1, &v2);
}
numb_fini (v2);
if (et == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
and_term (m4 *context, eval_token et, number *v1)
{
number v2;
eval_error er;
if ((er = equality_term (context, et, v1)) != NO_ERROR)
return er;
numb_init (v2);
while ((et = eval_lex (&v2)) == AND)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
if ((er = equality_term (context, et, &v2)) != NO_ERROR)
return er;
numb_and (context, v1, &v2);
}
numb_fini (v2);
if (et == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
equality_term (m4 *context, eval_token et, number *v1)
{
eval_token op;
number v2;
eval_error er;
if ((er = cmp_term (context, et, v1)) != NO_ERROR)
return er;
numb_init (v2);
while ((op = eval_lex (&v2)) == EQ || op == NOTEQ)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
if ((er = cmp_term (context, et, &v2)) != NO_ERROR)
return er;
if (op == EQ)
numb_eq (*v1, v2);
else
numb_ne (*v1, v2);
}
numb_fini (v2);
if (op == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
cmp_term (m4 *context, eval_token et, number *v1)
{
eval_token op;
number v2;
eval_error er;
if ((er = shift_term (context, et, v1)) != NO_ERROR)
return er;
numb_init (v2);
while ((op = eval_lex (&v2)) == GT || op == GTEQ
|| op == LS || op == LSEQ)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
if ((er = shift_term (context, et, &v2)) != NO_ERROR)
return er;
switch (op)
{
case GT:
numb_gt (*v1, v2);
break;
case GTEQ:
numb_ge (*v1, v2);
break;
case LS:
numb_lt (*v1, v2);
break;
case LSEQ:
numb_le (*v1, v2);
break;
default:
assert (!"INTERNAL ERROR: bad comparison operator in cmp_term ()");
abort ();
}
}
numb_fini (v2);
if (op == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
shift_term (m4 *context, eval_token et, number *v1)
{
eval_token op;
number v2;
eval_error er;
if ((er = add_term (context, et, v1)) != NO_ERROR)
return er;
numb_init (v2);
while ((op = eval_lex (&v2)) == LSHIFT || op == RSHIFT || op == URSHIFT)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
if ((er = add_term (context, et, &v2)) != NO_ERROR)
return er;
switch (op)
{
case LSHIFT:
numb_lshift (context, v1, &v2);
break;
case RSHIFT:
numb_rshift (context, v1, &v2);
break;
case URSHIFT:
numb_urshift (context, v1, &v2);
break;
default:
assert (!"INTERNAL ERROR: bad shift operator in shift_term ()");
abort ();
}
}
numb_fini (v2);
if (op == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
add_term (m4 *context, eval_token et, number *v1)
{
eval_token op;
number v2;
eval_error er;
if ((er = mult_term (context, et, v1)) != NO_ERROR)
return er;
numb_init (v2);
while ((op = eval_lex (&v2)) == PLUS || op == MINUS)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
if ((er = mult_term (context, et, &v2)) != NO_ERROR)
return er;
if (op == PLUS)
numb_plus (*v1, v2);
else
numb_minus (*v1, v2);
}
numb_fini (v2);
if (op == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
mult_term (m4 *context, eval_token et, number *v1)
{
eval_token op;
number v2;
eval_error er;
if ((er = exp_term (context, et, v1)) != NO_ERROR)
return er;
numb_init (v2);
while (op = eval_lex (&v2),
op == TIMES
|| op == DIVIDE
|| op == MODULO
|| op == RATIO)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
if ((er = exp_term (context, et, &v2)) != NO_ERROR)
return er;
switch (op)
{
case TIMES:
numb_times (*v1, v2);
break;
case DIVIDE:
if (numb_zerop (v2))
return DIVIDE_ZERO;
else
numb_divide(v1, &v2);
break;
case RATIO:
if (numb_zerop (v2))
return DIVIDE_ZERO;
else
numb_ratio (*v1, v2);
break;
case MODULO:
if (numb_zerop (v2))
return MODULO_ZERO;
else
numb_modulo (context, v1, &v2);
break;
default:
assert (!"INTERNAL ERROR: bad operator in mult_term ()");
abort ();
}
}
numb_fini (v2);
if (op == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
exp_term (m4 *context, eval_token et, number *v1)
{
number v2;
eval_error er;
if ((er = unary_term (context, et, v1)) != NO_ERROR)
return er;
numb_init (v2);
while ((et = eval_lex (&v2)) == EXPONENT)
{
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
if ((er = exp_term (context, et, &v2)) != NO_ERROR)
return er;
if ((er = numb_pow (v1, &v2)) != NO_ERROR)
return er;
}
numb_fini (v2);
if (et == ERROR)
return UNKNOWN_INPUT;
eval_undo ();
return NO_ERROR;
}
static eval_error
unary_term (m4 *context, eval_token et, number *v1)
{
eval_error er;
if (et == PLUS || et == MINUS || et == NOT || et == LNOT)
{
eval_token et2 = eval_lex (v1);
if (et2 == ERROR)
return UNKNOWN_INPUT;
if ((er = unary_term (context, et2, v1)) != NO_ERROR)
return er;
if (et == MINUS)
numb_negate(*v1);
else if (et == NOT)
numb_not (context, v1);
else if (et == LNOT)
numb_lnot (*v1);
}
else if ((er = simple_term (context, et, v1)) != NO_ERROR)
return er;
return NO_ERROR;
}
static eval_error
simple_term (m4 *context, eval_token et, number *v1)
{
number v2;
eval_error er;
switch (et)
{
case LEFTP:
et = eval_lex (v1);
if (et == ERROR)
return UNKNOWN_INPUT;
if ((er = comma_term (context, et, v1)) != NO_ERROR)
return er;
et = eval_lex (&v2);
if (et == ERROR)
return UNKNOWN_INPUT;
if (et != RIGHTP)
return MISSING_RIGHT;
break;
case NUMBER:
break;
case BADOP:
return INVALID_OPERATOR;
default:
return SYNTAX_ERROR;
}
return NO_ERROR;
}
/* Main entry point, called from "eval" and "mpeval" builtins. */
void
m4_evaluate (m4 *context, m4_obstack *obs, size_t argc, m4_macro_args *argv)
{
const m4_call_info *me = m4_arg_info (argv);
const char * str = M4ARG (1);
int radix = 10;
int min = 1;
number val;
eval_token et;
eval_error err = NO_ERROR;
if (!m4_arg_empty (argv, 2)
&& !m4_numeric_arg (context, me, M4ARG (2), M4ARGLEN (2), &radix))
return;
if (radix < 1 || radix > 36)
{
m4_warn (context, 0, me, _("radix out of range: %d"), radix);
return;
}
if (argc >= 4 && !m4_numeric_arg (context, me, M4ARG (3), M4ARGLEN (3),
&min))
return;
if (min < 0)
{
m4_warn (context, 0, me, _("negative width: %d"), min);
return;
}
numb_initialise ();
eval_init_lex (str, M4ARGLEN (1));
numb_init (val);
et = eval_lex (&val);
if (et == EOTEXT)
{
m4_warn (context, 0, me, _("empty string treated as 0"));
numb_set (val, numb_ZERO);
}
else
err = comma_term (context, et, &val);
if (err == NO_ERROR && *eval_text != '\0')
{
if (eval_lex (&val) == BADOP)
err = INVALID_OPERATOR;
else
err = EXCESS_INPUT;
}
if (err != NO_ERROR)
str = quotearg_style_mem (locale_quoting_style, str, M4ARGLEN (1));
switch (err)
{
case NO_ERROR:
numb_obstack (obs, val, radix, min);
break;
case MISSING_RIGHT:
m4_warn (context, 0, me, _("missing right parenthesis: %s"), str);
break;
case MISSING_COLON:
m4_warn (context, 0, me, _("missing colon: %s"), str);
break;
case SYNTAX_ERROR:
m4_warn (context, 0, me, _("bad expression: %s"), str);
break;
case UNKNOWN_INPUT:
m4_warn (context, 0, me, _("bad input: %s"), str);
break;
case EXCESS_INPUT:
m4_warn (context, 0, me, _("excess input: %s"), str);
break;
case INVALID_OPERATOR:
m4_warn (context, 0, me, _("invalid operator: %s"), str);
break;
case DIVIDE_ZERO:
m4_warn (context, 0, me, _("divide by zero: %s"), str);
break;
case MODULO_ZERO:
m4_warn (context, 0, me, _("modulo by zero: %s"), str);
break;
case NEGATIVE_EXPONENT:
m4_warn (context, 0, me, _("negative exponent: %s"), str);
break;
default:
assert (!"INTERNAL ERROR: bad error code in evaluate ()");
abort ();
}
numb_fini (val);
}
static eval_error
numb_pow (number *x, number *y)
{
/* y should be integral */
number ans, yy;
numb_init (ans);
numb_set_si (&ans, 1);
if (numb_zerop (*x) && numb_zerop (*y))
return DIVIDE_ZERO;
numb_init (yy);
numb_set (yy, *y);
if (numb_negativep (yy))
{
numb_negate (yy);
numb_invert (*x);
}
while (numb_positivep (yy))
{
numb_times (ans, *x);
numb_decr (yy);
}
numb_set (*x, ans);
numb_fini (ans);
numb_fini (yy);
return NO_ERROR;
}
|