-
Notifications
You must be signed in to change notification settings - Fork 30
/
arith.c
1473 lines (1398 loc) · 48.2 KB
/
arith.c
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
/* Yash: yet another shell */
/* arith.c: arithmetic expansion */
/* (C) 2007-2022 magicant */
/* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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/>. */
#include "common.h"
#include "arith.h"
#include <assert.h>
#include <errno.h>
#include <float.h>
#include <limits.h>
#include <locale.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <wchar.h>
#include <sys/types.h>
#include <wctype.h>
#include "option.h"
#include "strbuf.h"
#include "util.h"
#include "variable.h"
typedef struct word_T {
const wchar_t *contents;
size_t length;
} word_T;
typedef enum valuetype_T {
VT_INVALID, VT_LONG, VT_DOUBLE, VT_VAR,
} valuetype_T;
typedef struct value_T {
valuetype_T type;
union {
long longvalue;
double doublevalue;
word_T varvalue;
} value;
} value_T;
#define v_long value.longvalue
#define v_double value.doublevalue
#define v_var value.varvalue
typedef enum atokentype_T {
TT_NULL, TT_INVALID,
/* symbols */
TT_LPAREN, TT_RPAREN, TT_TILDE, TT_EXCL, TT_PERCENT,
TT_PLUS, TT_MINUS, TT_PLUSPLUS, TT_MINUSMINUS, TT_ASTER, TT_SLASH,
TT_LESS, TT_GREATER, TT_LESSLESS, TT_GREATERGREATER,
TT_LESSEQUAL, TT_GREATEREQUAL, TT_EQUALEQUAL, TT_EXCLEQUAL,
TT_AMP, TT_AMPAMP, TT_HAT, TT_PIPE, TT_PIPEPIPE, TT_QUESTION, TT_COLON,
TT_EQUAL, TT_PLUSEQUAL, TT_MINUSEQUAL, TT_ASTEREQUAL, TT_SLASHEQUAL,
TT_PERCENTEQUAL, TT_LESSLESSEQUAL, TT_GREATERGREATEREQUAL,
TT_AMPEQUAL, TT_HATEQUAL, TT_PIPEEQUAL,
/* numbers */
TT_NUMBER,
/* identifiers */
TT_IDENTIFIER,
} atokentype_T;
typedef struct atoken_T {
atokentype_T type;
word_T word; /* valid only for numbers and identifiers */
} atoken_T;
typedef struct evalinfo_T {
const wchar_t *exp; /* expression to parse and calculate */
size_t index; /* index of next token */
atoken_T atoken; /* current token */
bool parseonly; /* only parse the expression: don't calculate */
bool error; /* true if there is an error */
char *savelocale; /* original LC_NUMERIC locale */
} evalinfo_T;
static void evaluate(
const wchar_t *exp, value_T *result, evalinfo_T *info, bool coerce)
__attribute__((nonnull));
static void parse_assignment(evalinfo_T *info, value_T *result)
__attribute__((nonnull));
static bool do_assignment(const word_T *word, const value_T *value)
__attribute__((nonnull));
static wchar_t *value_to_string(const value_T *value)
__attribute__((nonnull,malloc,warn_unused_result));
static bool do_binary_calculation(
evalinfo_T *info, atokentype_T ttype,
value_T *lhs, value_T *rhs, value_T *result)
__attribute__((nonnull));
static bool do_long_calculation1(
atokentype_T ttype, long v1, long v2, long *result)
__attribute__((nonnull,warn_unused_result));
static bool do_long_calculation2(
atokentype_T ttype, long v1, long v2, long *result)
__attribute__((nonnull,warn_unused_result));
static long do_long_comparison(atokentype_T ttype, long v1, long v2)
__attribute__((const,warn_unused_result));
static bool do_double_calculation(
atokentype_T ttype, double v1, double v2, double *result)
__attribute__((nonnull,warn_unused_result));
static long do_double_comparison(atokentype_T ttype, double v1, double v2);
static void parse_conditional(evalinfo_T *info, value_T *result)
__attribute__((nonnull));
static void parse_logical_or(evalinfo_T *info, value_T *result)
__attribute__((nonnull));
static void parse_logical_and(evalinfo_T *info, value_T *result)
__attribute__((nonnull));
static void parse_inclusive_or(evalinfo_T *info, value_T *result)
__attribute__((nonnull));
static void parse_exclusive_or(evalinfo_T *info, value_T *result)
__attribute__((nonnull));
static void parse_and(evalinfo_T *info, value_T *result)
__attribute__((nonnull));
static void parse_equality(evalinfo_T *info, value_T *result)
__attribute__((nonnull));
static void parse_relational(evalinfo_T *info, value_T *result)
__attribute__((nonnull));
static void parse_shift(evalinfo_T *info, value_T *result)
__attribute__((nonnull));
static void parse_additive(evalinfo_T *info, value_T *result)
__attribute__((nonnull));
static void parse_multiplicative(evalinfo_T *info, value_T *result)
__attribute__((nonnull));
static void parse_prefix(evalinfo_T *info, value_T *result)
__attribute__((nonnull));
static void parse_postfix(evalinfo_T *info, value_T *result)
__attribute__((nonnull));
static bool do_increment_or_decrement(atokentype_T ttype, value_T *value)
__attribute__((nonnull,warn_unused_result));
static void parse_primary(evalinfo_T *info, value_T *result)
__attribute__((nonnull));
static void parse_as_number(evalinfo_T *info, value_T *result)
__attribute__((nonnull));
static void coerce_number(evalinfo_T *info, value_T *value)
__attribute__((nonnull));
static void coerce_integer(evalinfo_T *info, value_T *value)
__attribute__((nonnull));
static valuetype_T coerce_type(evalinfo_T *info,
value_T *value1, value_T *value2)
__attribute__((nonnull));
static void next_token(evalinfo_T *info)
__attribute__((nonnull));
static bool long_mul_will_overflow(long v1, long v2)
__attribute__((const,warn_unused_result));
/* Evaluates the specified string as an arithmetic expression.
* The argument string is freed in this function.
* The result is converted into a string and returned as a newly-malloced
* string. On error, an error message is printed to the standard error and NULL
* is returned. */
wchar_t *evaluate_arithmetic(wchar_t *exp)
{
value_T result;
evalinfo_T info;
evaluate(exp, &result, &info, posixly_correct);
wchar_t *resultstr;
if (info.error) {
resultstr = NULL;
} else if (info.atoken.type == TT_NULL) {
resultstr = value_to_string(&result);
} else {
if (info.atoken.type != TT_INVALID)
xerror(0, Ngt("arithmetic: invalid syntax"));
resultstr = NULL;
}
free(exp);
return resultstr;
}
/* Evaluates the specified string as an arithmetic expression.
* The argument string is freed in this function.
* The expression must yield a valid integer value, which is assigned to
* `*valuep'. Otherwise, an error message is printed.
* Returns true iff successful. */
bool evaluate_index(wchar_t *exp, ssize_t *valuep)
{
value_T result;
evalinfo_T info;
evaluate(exp, &result, &info, true);
bool ok;
if (info.error) {
ok = false;
} else if (info.atoken.type == TT_NULL) {
if (result.type == VT_LONG) {
#if LONG_MAX > SSIZE_MAX
if (result.v_long > (long) SSIZE_MAX)
*valuep = SSIZE_MAX;
else
#endif
#if LONG_MIN < -SSIZE_MAX
if (result.v_long < (long) -SSIZE_MAX)
*valuep = -SSIZE_MAX;
else
#endif
*valuep = (ssize_t) result.v_long;
ok = true;
} else {
xerror(0, Ngt("the index is not an integer"));
ok = false;
}
} else {
if (info.atoken.type != TT_INVALID)
xerror(0, Ngt("arithmetic: invalid syntax"));
ok = false;
}
free(exp);
return ok;
}
void evaluate(
const wchar_t *exp, value_T *result, evalinfo_T *info, bool coerce)
{
info->exp = exp;
info->index = 0;
info->parseonly = false;
info->error = false;
info->savelocale = xstrdup(setlocale(LC_NUMERIC, NULL));
next_token(info);
parse_assignment(info, result);
if (coerce)
coerce_number(info, result);
free(info->savelocale);
}
/* Parses an assignment expression.
* AssignmentExp := ConditionalExp
* | ConditionalExp AssignmentOperator AssignmentExp */
void parse_assignment(evalinfo_T *info, value_T *result)
{
parse_conditional(info, result);
atokentype_T ttype = info->atoken.type;
switch (ttype) {
case TT_EQUAL: case TT_PLUSEQUAL: case TT_MINUSEQUAL:
case TT_ASTEREQUAL: case TT_SLASHEQUAL: case TT_PERCENTEQUAL:
case TT_LESSLESSEQUAL: case TT_GREATERGREATEREQUAL:
case TT_AMPEQUAL: case TT_HATEQUAL: case TT_PIPEEQUAL:
{
value_T rhs;
next_token(info);
parse_assignment(info, &rhs);
if (result->type == VT_VAR) {
word_T saveword = result->v_var;
if (!do_binary_calculation(
info, ttype, result, &rhs, result))
break;
if (!do_assignment(&saveword, result))
info->error = true, result->type = VT_INVALID;
} else if (result->type != VT_INVALID) {
/* TRANSLATORS: This error message is shown when the target
* of an assignment is not a variable. */
xerror(0, Ngt("arithmetic: cannot assign to a number"));
info->error = true;
result->type = VT_INVALID;
}
break;
}
default:
break;
}
}
/* Assigns the specified `value' to the variable specified by `word'.
* Returns false on error. */
bool do_assignment(const word_T *word, const value_T *value)
{
wchar_t *vstr = value_to_string(value);
if (vstr == NULL)
return false;
wchar_t name[word->length + 1];
wmemcpy(name, word->contents, word->length);
name[word->length] = L'\0';
return set_variable(name, vstr, SCOPE_GLOBAL, false);
}
/* Converts `value' to a newly-malloced wide string.
* Returns NULL on error. */
wchar_t *value_to_string(const value_T *value)
{
switch (value->type) {
case VT_INVALID:
return NULL;
case VT_LONG:
return malloc_wprintf(L"%ld", value->v_long);
case VT_DOUBLE:
return malloc_wprintf(L"%.*g", DBL_DIG, value->v_double);
case VT_VAR:
{
wchar_t name[value->v_var.length + 1];
wmemcpy(name, value->v_var.contents, value->v_var.length);
name[value->v_var.length] = L'\0';
const wchar_t *var = getvar(name);
if (var != NULL)
return xwcsdup(var);
if (shopt_unset)
return malloc_wprintf(L"%ld", 0L);
xerror(0, Ngt("arithmetic: parameter `%ls' is not set"), name);
return NULL;
}
}
assert(false);
}
/* Applies the binary operation defined by token `ttype' to operands `lhs' and
* `rhs'. The operands may be modified as a result of coercion. The result is
* assigned to `*result' unless there is an error.
* Returns true iff successful. */
bool do_binary_calculation(
evalinfo_T *info, atokentype_T ttype,
value_T *lhs, value_T *rhs, value_T *result)
{
switch (ttype) {
case TT_ASTER: case TT_ASTEREQUAL:
case TT_SLASH: case TT_SLASHEQUAL:
case TT_PERCENT: case TT_PERCENTEQUAL:
case TT_PLUS: case TT_PLUSEQUAL:
case TT_MINUS: case TT_MINUSEQUAL:
result->type = coerce_type(info, lhs, rhs);
switch (result->type) {
case VT_LONG:
if (!do_long_calculation1(ttype, lhs->v_long, rhs->v_long,
&result->v_long)) {
info->error = true;
result->type = VT_INVALID;
return false;
}
break;
case VT_DOUBLE:
if (!do_double_calculation(ttype, lhs->v_double,
rhs->v_double, &result->v_double)) {
info->error = true;
result->type = VT_INVALID;
return false;
}
break;
case VT_VAR:
assert(false);
case VT_INVALID:
break;
}
break;
case TT_LESSLESS: case TT_LESSLESSEQUAL:
case TT_GREATERGREATER: case TT_GREATERGREATEREQUAL:
case TT_AMP: case TT_AMPEQUAL:
case TT_HAT: case TT_HATEQUAL:
case TT_PIPE: case TT_PIPEEQUAL:
coerce_integer(info, lhs);
coerce_integer(info, rhs);
if (lhs->type == VT_LONG && rhs->type == VT_LONG) {
if (do_long_calculation2(
ttype, lhs->v_long, rhs->v_long, &result->v_long)) {
result->type = VT_LONG;
} else {
info->error = true;
result->type = VT_INVALID;
return false;
}
} else {
result->type = VT_INVALID;
return false;
}
break;
case TT_EQUAL:
*result = *rhs;
break;
default:
assert(false);
}
return true;
}
/* Applies binary operator `ttype' to the given operands `v1' and `v2'.
* If successful, assigns the result to `*result' and returns true.
* Otherwise, prints an error message and returns false. */
bool do_long_calculation1(atokentype_T ttype, long v1, long v2, long *result)
{
switch (ttype) {
case TT_PLUS: case TT_PLUSEQUAL:
if (v2 >= 0 ? LONG_MAX - v2 < v1 : v1 < LONG_MIN - v2)
goto overflow;
*result = v1 + v2;
return true;
case TT_MINUS: case TT_MINUSEQUAL:
if (v2 < 0 ? LONG_MAX + v2 < v1 : v1 < LONG_MIN + v2)
goto overflow;
*result = v1 - v2;
return true;
case TT_ASTER: case TT_ASTEREQUAL:
if (long_mul_will_overflow(v1, v2))
goto overflow;
*result = v1 * v2;
return true;
case TT_SLASH: case TT_SLASHEQUAL:
if (v2 == 0)
goto division_by_zero;
if (v1 == LONG_MIN && v2 == -1)
goto overflow;
*result = v1 / v2;
return true;
case TT_PERCENT: case TT_PERCENTEQUAL:
if (v2 == 0)
goto division_by_zero;
if (v1 == LONG_MIN && v2 == -1)
goto overflow;
*result = v1 % v2;
return true;
default:
assert(false);
}
overflow:
xerror(0, Ngt("arithmetic: overflow"));
return false;
division_by_zero:
xerror(0, Ngt("arithmetic: division by zero"));
return false;
}
/* Applies binary operator `ttype' to the given operands `v1' and `v2'.
* If successful, assigns the result to `*result' and returns true.
* Otherwise, prints an error message and returns false. */
bool do_long_calculation2(atokentype_T ttype, long v1, long v2, long *result)
{
switch (ttype) {
case TT_LESSLESS: case TT_LESSLESSEQUAL:
if (v1 < 0)
goto negative_left_shift;
if (v2 < 0 || v2 >= LONG_BIT)
goto invalid_shift_width;
unsigned long u1 = (unsigned long) v1;
if ((u1 << v2 & (unsigned long) LONG_MAX) >> v2 != u1)
goto overflow;
*result = v1 << v2;
return true;
case TT_GREATERGREATER: case TT_GREATERGREATEREQUAL:
if (v2 < 0 || v2 >= LONG_BIT)
goto invalid_shift_width;
*result = v1 >> v2;
return true;
case TT_AMP: case TT_AMPEQUAL:
*result = v1 & v2;
return true;
case TT_HAT: case TT_HATEQUAL:
*result = v1 ^ v2;
return true;
case TT_PIPE: case TT_PIPEEQUAL:
*result = v1 | v2;
return true;
default:
assert(false);
}
overflow:
xerror(0, Ngt("arithmetic: overflow"));
return false;
negative_left_shift:
xerror(0, Ngt("arithmetic: negative value cannot be shifted to left"));
return false;
invalid_shift_width:
xerror(0, Ngt("arithmetic: invalid shift width"));
return false;
}
/* Applies binary operator `ttype' to the given operands `v1' and `v2'. */
long do_long_comparison(atokentype_T ttype, long v1, long v2)
{
switch (ttype) {
case TT_LESS:
return v1 < v2;
case TT_LESSEQUAL:
return v1 <= v2;
case TT_GREATER:
return v1 > v2;
case TT_GREATEREQUAL:
return v1 >= v2;
case TT_EQUALEQUAL:
return v1 == v2;
case TT_EXCLEQUAL:
return v1 != v2;
default:
assert(false);
}
}
/* Applies binary operator `ttype' to the given operands `v1' and `v2'.
* If successful, assigns the result to `*result' and returns true.
* Otherwise, prints an error message and returns false. */
bool do_double_calculation(
atokentype_T ttype, double v1, double v2, double *result)
{
switch (ttype) {
case TT_PLUS: case TT_PLUSEQUAL:
*result = v1 + v2;
return true;
case TT_MINUS: case TT_MINUSEQUAL:
*result = v1 - v2;
return true;
case TT_ASTER: case TT_ASTEREQUAL:
*result = v1 * v2;
return true;
case TT_SLASH: case TT_SLASHEQUAL:
#if DOUBLE_DIVISION_BY_ZERO_ERROR
if (v2 == 0.0)
goto division_by_zero;
#endif
*result = v1 / v2;
return true;
case TT_PERCENT: case TT_PERCENTEQUAL:
#if DOUBLE_DIVISION_BY_ZERO_ERROR
if (v2 == 0.0)
goto division_by_zero;
#endif
*result = fmod(v1, v2);
return true;
default:
assert(false);
}
#if DOUBLE_DIVISION_BY_ZERO_ERROR
division_by_zero:
xerror(0, Ngt("arithmetic: division by zero"));
return false;
#endif
}
/* Does double comparison according to the specified operator token. */
long do_double_comparison(atokentype_T ttype, double v1, double v2)
{
switch (ttype) {
case TT_LESS:
return v1 < v2;
case TT_LESSEQUAL:
return v1 <= v2;
case TT_GREATER:
return v1 > v2;
case TT_GREATEREQUAL:
return v1 >= v2;
case TT_EQUALEQUAL:
return v1 == v2;
case TT_EXCLEQUAL:
return v1 != v2;
default:
assert(false);
}
}
/* Parses a conditional expression.
* ConditionalExp := LogicalOrExp
* | LogicalOrExp "?" AssignmentExp ":" ConditionalExp */
void parse_conditional(evalinfo_T *info, value_T *result)
{
bool saveparseonly = info->parseonly;
for (;;) {
value_T dummy;
value_T *result2;
result2 = info->parseonly ? &dummy : result;
parse_logical_or(info, result2);
if (info->atoken.type != TT_QUESTION)
break;
bool cond, valid = true;
coerce_number(info, result2);
next_token(info);
switch (result2->type) {
case VT_INVALID: valid = false, cond = true; break;
case VT_LONG: cond = result2->v_long; break;
case VT_DOUBLE: cond = result2->v_double; break;
default: assert(false);
}
bool saveparseonly2 = info->parseonly || !valid;
info->parseonly = saveparseonly2 || !cond;
result2 = info->parseonly ? &dummy : result;
parse_assignment(info, result2);
if (info->atoken.type != TT_COLON) {
xerror(0, Ngt("arithmetic: `%ls' is missing"), L":");
info->error = true;
result->type = VT_INVALID;
break;
}
next_token(info);
info->parseonly = saveparseonly2 || cond;
}
info->parseonly = saveparseonly;
if (info->parseonly)
result->type = VT_INVALID;
}
/* Parses a logical OR expression.
* LogicalOrExp := LogicalAndExp | LogicalOrExp "||" LogicalAndExp */
void parse_logical_or(evalinfo_T *info, value_T *result)
{
bool saveparseonly = info->parseonly;
parse_logical_and(info, result);
while (info->atoken.type == TT_PIPEPIPE) {
bool value, valid = true;
coerce_number(info, result);
next_token(info);
switch (result->type) {
case VT_INVALID: valid = false, value = true; break;
case VT_LONG: value = result->v_long; break;
case VT_DOUBLE: value = result->v_double; break;
default: assert(false);
}
info->parseonly |= value;
parse_logical_and(info, result);
coerce_number(info, result);
if (!value) switch (result->type) {
case VT_INVALID: valid = false; break;
case VT_LONG: value = result->v_long; break;
case VT_DOUBLE: value = result->v_double; break;
default: assert(false);
}
if (valid)
result->type = VT_LONG, result->v_long = value;
else
result->type = VT_INVALID;
}
info->parseonly = saveparseonly;
}
/* Parses a logical AND expression.
* LogicalAndExp := InclusiveOrExp | LogicalAndExp "&&" InclusiveOrExp */
void parse_logical_and(evalinfo_T *info, value_T *result)
{
bool saveparseonly = info->parseonly;
parse_inclusive_or(info, result);
while (info->atoken.type == TT_AMPAMP) {
bool value, valid = true;
coerce_number(info, result);
next_token(info);
switch (result->type) {
case VT_INVALID: valid = false, value = false; break;
case VT_LONG: value = result->v_long; break;
case VT_DOUBLE: value = result->v_double; break;
default: assert(false);
}
info->parseonly |= !value;
parse_inclusive_or(info, result);
coerce_number(info, result);
if (value) switch (result->type) {
case VT_INVALID: valid = false; break;
case VT_LONG: value = result->v_long; break;
case VT_DOUBLE: value = result->v_double; break;
default: assert(false);
}
if (valid)
result->type = VT_LONG, result->v_long = value;
else
result->type = VT_INVALID;
}
info->parseonly = saveparseonly;
}
/* Parses an inclusive OR expression.
* InclusiveOrExp := ExclusiveOrExp | InclusiveOrExp "|" ExclusiveOrExp */
void parse_inclusive_or(evalinfo_T *info, value_T *result)
{
parse_exclusive_or(info, result);
for (;;) {
atokentype_T ttype = info->atoken.type;
value_T rhs;
switch (ttype) {
case TT_PIPE:
next_token(info);
parse_exclusive_or(info, &rhs);
do_binary_calculation(info, TT_PIPE, result, &rhs, result);
break;
default:
return;
}
}
}
/* Parses an exclusive OR expression.
* ExclusiveOrExp := AndExp | ExclusiveOrExp "^" AndExp */
void parse_exclusive_or(evalinfo_T *info, value_T *result)
{
parse_and(info, result);
for (;;) {
atokentype_T ttype = info->atoken.type;
value_T rhs;
switch (ttype) {
case TT_HAT:
next_token(info);
parse_and(info, &rhs);
do_binary_calculation(info, TT_HAT, result, &rhs, result);
break;
default:
return;
}
}
}
/* Parses an AND expression.
* AndExp := EqualityExp | AndExp "&" EqualityExp */
void parse_and(evalinfo_T *info, value_T *result)
{
parse_equality(info, result);
for (;;) {
atokentype_T ttype = info->atoken.type;
value_T rhs;
switch (ttype) {
case TT_AMP:
next_token(info);
parse_equality(info, &rhs);
do_binary_calculation(info, TT_AMP, result, &rhs, result);
break;
default:
return;
}
}
}
/* Parses an equality expression.
* EqualityExp := RelationalExp
* | EqualityExp "==" RelationalExp
* | EqualityExp "!=" RelationalExp */
void parse_equality(evalinfo_T *info, value_T *result)
{
parse_relational(info, result);
for (;;) {
atokentype_T ttype = info->atoken.type;
value_T rhs;
switch (ttype) {
case TT_EQUALEQUAL:
case TT_EXCLEQUAL:
next_token(info);
parse_relational(info, &rhs);
switch (coerce_type(info, result, &rhs)) {
case VT_LONG:
result->v_long = do_long_comparison(ttype,
result->v_long, rhs.v_long);
break;
case VT_DOUBLE:
result->v_long = do_double_comparison(ttype,
result->v_double, rhs.v_double);
result->type = VT_LONG;
break;
case VT_INVALID:
result->type = VT_INVALID;
break;
case VT_VAR:
assert(false);
}
break;
default:
return;
}
}
}
/* Parses a relational expression.
* RelationalExp := ShiftExp
* | RelationalExp "<" ShiftExp
* | RelationalExp ">" ShiftExp
* | RelationalExp "<=" ShiftExp
* | RelationalExp ">=" ShiftExp */
void parse_relational(evalinfo_T *info, value_T *result)
{
parse_shift(info, result);
for (;;) {
atokentype_T ttype = info->atoken.type;
value_T rhs;
switch (ttype) {
case TT_LESS:
case TT_LESSEQUAL:
case TT_GREATER:
case TT_GREATEREQUAL:
next_token(info);
parse_shift(info, &rhs);
switch (coerce_type(info, result, &rhs)) {
case VT_LONG:
result->v_long = do_long_comparison(ttype,
result->v_long, rhs.v_long);
break;
case VT_DOUBLE:
result->v_long = do_double_comparison(ttype,
result->v_double, rhs.v_double);
result->type = VT_LONG;
break;
case VT_INVALID:
result->type = VT_INVALID;
break;
case VT_VAR:
assert(false);
}
break;
default:
return;
}
}
}
/* Parses a shift expression.
* ShiftExp := AdditiveExp
* | ShiftExp "<<" AdditiveExp | ShiftExp ">>" AdditiveExp */
void parse_shift(evalinfo_T *info, value_T *result)
{
parse_additive(info, result);
for (;;) {
atokentype_T ttype = info->atoken.type;
value_T rhs;
switch (ttype) {
case TT_LESSLESS:
case TT_GREATERGREATER:
next_token(info);
parse_additive(info, &rhs);
do_binary_calculation(info, ttype, result, &rhs, result);
break;
default:
return;
}
}
}
/* Parses an additive expression.
* AdditiveExp := MultiplicativeExp
* | AdditiveExp "+" MultiplicativeExp
* | AdditiveExp "-" MultiplicativeExp */
void parse_additive(evalinfo_T *info, value_T *result)
{
parse_multiplicative(info, result);
for (;;) {
atokentype_T ttype = info->atoken.type;
value_T rhs;
switch (ttype) {
case TT_PLUS:
case TT_MINUS:
next_token(info);
parse_multiplicative(info, &rhs);
do_binary_calculation(info, ttype, result, &rhs, result);
break;
default:
return;
}
}
}
/* Parses a multiplicative expression.
* MultiplicativeExp := PrefixExp
* | MultiplicativeExp "*" PrefixExp
* | MultiplicativeExp "/" PrefixExp
* | MultiplicativeExp "%" PrefixExp */
void parse_multiplicative(evalinfo_T *info, value_T *result)
{
parse_prefix(info, result);
for (;;) {
atokentype_T ttype = info->atoken.type;
value_T rhs;
switch (ttype) {
case TT_ASTER:
case TT_SLASH:
case TT_PERCENT:
next_token(info);
parse_prefix(info, &rhs);
do_binary_calculation(info, ttype, result, &rhs, result);
break;
default:
return;
}
}
}
/* Parses a prefix expression.
* PrefixExp := PostfixExp
* | "++" PrefixExp | "--" PrefixExp
* | "+" PrefixExp | "-" PrefixExp
* | "~" PrefixExp | "!" PrefixExp */
void parse_prefix(evalinfo_T *info, value_T *result)
{
atokentype_T ttype = info->atoken.type;
switch (ttype) {
case TT_PLUSPLUS:
case TT_MINUSMINUS:
next_token(info);
parse_prefix(info, result);
if (posixly_correct) {
xerror(0, Ngt("arithmetic: operator `%ls' is not supported"),
(ttype == TT_PLUSPLUS) ? L"++" : L"--");
info->error = true;
result->type = VT_INVALID;
} else if (result->type == VT_VAR) {
word_T saveword = result->v_var;
coerce_number(info, result);
if (!do_increment_or_decrement(ttype, result) ||
!do_assignment(&saveword, result))
info->error = true, result->type = VT_INVALID;
} else if (result->type != VT_INVALID) {
/* TRANSLATORS: This error message is shown when the operand of
* the "++" or "--" operator is not a variable. */
xerror(0, Ngt("arithmetic: operator `%ls' requires a variable"),
(info->atoken.type == TT_PLUSPLUS) ? L"++" : L"--");
info->error = true;
result->type = VT_INVALID;
}
break;
case TT_PLUS:
case TT_MINUS:
next_token(info);
parse_prefix(info, result);
coerce_number(info, result);
if (ttype == TT_MINUS) {
switch (result->type) {
case VT_LONG:
#if LONG_MIN < -LONG_MAX
if (result->v_long == LONG_MIN) {
xerror(0, Ngt("arithmetic: overflow"));
info->error = true;
result->type = VT_INVALID;
break;
}
#endif
result->v_long = -result->v_long;
break;
case VT_DOUBLE: result->v_double = -result->v_double; break;
case VT_INVALID: break;
default: assert(false);
}
}
break;
case TT_TILDE:
next_token(info);
parse_prefix(info, result);
coerce_integer(info, result);
if (result->type == VT_LONG)
result->v_long = ~result->v_long;
break;
case TT_EXCL:
next_token(info);
parse_prefix(info, result);
coerce_number(info, result);
switch (result->type) {
case VT_LONG:
result->v_long = !result->v_long;
break;
case VT_DOUBLE:
result->type = VT_LONG;
result->v_long = !result->v_double;
break;
case VT_INVALID:
break;
default:
assert(false);
}
break;
default:
parse_postfix(info, result);
break;
}
}
/* Parses a postfix expression.
* PostfixExp := PrimaryExp
* | PostfixExp "++" | PostfixExp "--" */
void parse_postfix(evalinfo_T *info, value_T *result)
{
parse_primary(info, result);
for (;;) {
switch (info->atoken.type) {
case TT_PLUSPLUS:
case TT_MINUSMINUS:
if (posixly_correct) {
xerror(0,
Ngt("arithmetic: operator `%ls' is not supported"),
(info->atoken.type == TT_PLUSPLUS) ? L"++" : L"--");
info->error = true;
result->type = VT_INVALID;