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
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
|
/*
* math.c - mathematical expression evaluation
*
* This file is part of zsh, the Z shell.
*
* Copyright (c) 1992-1997 Paul Falstad
* All rights reserved.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and to distribute modified versions of this software for any
* purpose, provided that the above copyright notice and the following
* two paragraphs appear in all copies of this software.
*
* In no event shall Paul Falstad or the Zsh Development Group be liable
* to any party for direct, indirect, special, incidental, or consequential
* damages arising out of the use of this software and its documentation,
* even if Paul Falstad and the Zsh Development Group have been advised of
* the possibility of such damage.
*
* Paul Falstad and the Zsh Development Group specifically disclaim any
* warranties, including, but not limited to, the implied warranties of
* merchantability and fitness for a particular purpose. The software
* provided hereunder is on an "as is" basis, and Paul Falstad and the
* Zsh Development Group have no obligation to provide maintenance,
* support, updates, enhancements, or modifications.
*
*/
#include "zsh.mdh"
#include "math.pro"
#include <math.h>
/* nonzero means we are not evaluating, just parsing */
/**/
int noeval;
/* integer zero */
/**/
mod_export mnumber zero_mnumber;
/*
* The last value we computed: note this isn't cleared
* until the next computation, unlike unlike yyval.
* Everything else is saved and returned to allow recursive calls.
*/
/**/
mnumber lastmathval;
/* last input base we used */
/**/
int lastbase;
static char *ptr;
static mnumber yyval;
static char *yylval;
#define MAX_MLEVEL 256
static int mlevel = 0;
/* != 0 means recognize unary plus, minus, etc. */
static int unary = 1;
/* LR = left-to-right associativity *
* RL = right-to-left associativity *
* BOOL = short-circuiting boolean */
#define LR 0x0000
#define RL 0x0001
#define BOOL 0x0002
#define MTYPE(x) ((x) & 3)
/*
* OP_A2 2 arguments
* OP_A2IR 2 arguments, return integer
* OP_A2IO 2 arguments, must be integer, return integer
* OP_E2 2 arguments with assignment
* OP_E2IO 2 arguments with assignment, must be integer, return integer
* OP_OP None of the above, but occurs where we are expecting an operator
* rather than an operand.
* OP_OPF Followed by an operator, not an operand.
*
* OP_A2*, OP_E2*, OP_OP*:
* Occur when we need an operator; the next object must be an operand,
* unless OP_OPF is also supplied.
*
* Others:
* Occur when we need an operand; the next object must also be an operand,
* unless OP_OPF is also supplied.
*/
#define OP_A2 0x0004
#define OP_A2IR 0x0008
#define OP_A2IO 0x0010
#define OP_E2 0x0020
#define OP_E2IO 0x0040
#define OP_OP 0x0080
#define OP_OPF 0x0100
#define M_INPAR 0
#define M_OUTPAR 1
#define NOT 2
#define COMP 3
#define POSTPLUS 4
#define POSTMINUS 5
#define UPLUS 6
#define UMINUS 7
#define AND 8
#define XOR 9
#define OR 10
#define MUL 11
#define DIV 12
#define MOD 13
#define PLUS 14
#define MINUS 15
#define SHLEFT 16
#define SHRIGHT 17
#define LES 18
#define LEQ 19
#define GRE 20
#define GEQ 21
#define DEQ 22
#define NEQ 23
#define DAND 24
#define DOR 25
#define DXOR 26
#define QUEST 27
#define COLON 28
#define EQ 29
#define PLUSEQ 30
#define MINUSEQ 31
#define MULEQ 32
#define DIVEQ 33
#define MODEQ 34
#define ANDEQ 35
#define XOREQ 36
#define OREQ 37
#define SHLEFTEQ 38
#define SHRIGHTEQ 39
#define DANDEQ 40
#define DOREQ 41
#define DXOREQ 42
#define COMMA 43
#define EOI 44
#define PREPLUS 45
#define PREMINUS 46
#define NUM 47
#define ID 48
#define POWER 49
#define CID 50
#define POWEREQ 51
#define FUNC 52
#define TOKCOUNT 53
/* precedences */
static int prec[TOKCOUNT] =
{
1, 137, 2, 2, 2,
2, 2, 2, 4, 5,
6, 8, 8, 8, 9,
9, 3, 3, 10, 10,
10, 10, 11, 11, 12,
13, 13, 14, 15, 16,
16, 16, 16, 16, 16,
16, 16, 16, 16, 16,
16, 16, 16, 17, 200,
2, 2, 0, 0, 7,
0, 16, 0
};
#define TOPPREC 18
#define ARGPREC 16
static int type[TOKCOUNT] =
{
/* 0 */ LR, LR|OP_OP|OP_OPF, RL, RL, RL|OP_OP|OP_OPF,
/* 5 */ RL|OP_OP|OP_OPF, RL, RL, LR|OP_A2IO, LR|OP_A2IO,
/* 10 */ LR|OP_A2IO, LR|OP_A2, LR|OP_A2, LR|OP_A2IO, LR|OP_A2,
/* 15 */ LR|OP_A2, LR|OP_A2IO, LR|OP_A2IO, LR|OP_A2IR, LR|OP_A2IR,
/* 20 */ LR|OP_A2IR, LR|OP_A2IR, LR|OP_A2IR, LR|OP_A2IR, BOOL|OP_A2IO,
/* 25 */ BOOL|OP_A2IO, LR|OP_A2IO, RL|OP_OP, RL|OP_OP, RL|OP_E2,
/* 30 */ RL|OP_E2, RL|OP_E2, RL|OP_E2, RL|OP_E2, RL|OP_E2IO,
/* 35 */ RL|OP_E2IO, RL|OP_E2IO, RL|OP_E2IO, RL|OP_E2IO, RL|OP_E2IO,
/* 40 */ BOOL|OP_E2IO, BOOL|OP_E2IO, RL|OP_A2IO, RL|OP_A2, RL|OP_OP,
/* 45 */ RL, RL, LR|OP_OPF, LR|OP_OPF, RL|OP_A2,
/* 50 */ LR|OP_OPF, RL|OP_E2, LR|OP_OPF
};
static int
lexconstant(void)
{
#ifdef USE_LOCALE
char *prev_locale;
#endif
char *nptr;
nptr = ptr;
if (*nptr == '-')
nptr++;
if (*nptr == '0')
{
nptr++;
if (*nptr == 'x' || *nptr == 'X') {
/* Let zstrtol parse number with base */
yyval.u.l = zstrtol(ptr, &ptr, 0);
/* Should we set lastbase here? */
lastbase = 16;
return NUM;
}
else if (isset(OCTALZEROES) &&
(memchr(nptr, '.', strlen(nptr)) == NULL) &&
idigit(*nptr)) {
yyval.u.l = zstrtol(ptr, &ptr, 0);
lastbase = 8;
return NUM;
}
}
while (idigit(*nptr))
nptr++;
if (*nptr == '.' || *nptr == 'e' || *nptr == 'E') {
/* it's a float */
yyval.type = MN_FLOAT;
#ifdef USE_LOCALE
prev_locale = dupstring(setlocale(LC_NUMERIC, NULL));
setlocale(LC_NUMERIC, "POSIX");
#endif
yyval.u.d = strtod(ptr, &nptr);
#ifdef USE_LOCALE
if (prev_locale) setlocale(LC_NUMERIC, prev_locale);
#endif
if (ptr == nptr || *nptr == '.') {
zerr("bad floating point constant");
return EOI;
}
ptr = nptr;
} else {
/* it's an integer */
yyval.u.l = zstrtol(ptr, &ptr, 10);
if (*ptr == '#') {
ptr++;
yyval.u.l = zstrtol(ptr, &ptr, lastbase = yyval.u.l);
}
}
return NUM;
}
/**/
int outputradix;
/**/
static int
zzlex(void)
{
int cct = 0;
yyval.type = MN_INTEGER;
for (;; cct = 0)
switch (*ptr++) {
case '+':
if (*ptr == '+' && (unary || !ialnum(*ptr))) {
ptr++;
return (unary) ? PREPLUS : POSTPLUS;
}
if (*ptr == '=') {
ptr++;
return PLUSEQ;
}
return (unary) ? UPLUS : PLUS;
case '-':
if (*ptr == '-' && (unary || !ialnum(*ptr))) {
ptr++;
return (unary) ? PREMINUS : POSTMINUS;
}
if (*ptr == '=') {
ptr++;
return MINUSEQ;
}
if (unary) {
if (idigit(*ptr) || *ptr == '.') {
ptr--;
return lexconstant();
} else
return UMINUS;
} else
return MINUS;
case '(':
return M_INPAR;
case ')':
return M_OUTPAR;
case '!':
if (*ptr == '=') {
ptr++;
return NEQ;
}
return NOT;
case '~':
return COMP;
case '&':
if (*ptr == '&') {
if (*++ptr == '=') {
ptr++;
return DANDEQ;
}
return DAND;
} else if (*ptr == '=') {
ptr++;
return ANDEQ;
}
return AND;
case '|':
if (*ptr == '|') {
if (*++ptr == '=') {
ptr++;
return DOREQ;
}
return DOR;
} else if (*ptr == '=') {
ptr++;
return OREQ;
}
return OR;
case '^':
if (*ptr == '^') {
if (*++ptr == '=') {
ptr++;
return DXOREQ;
}
return DXOR;
} else if (*ptr == '=') {
ptr++;
return XOREQ;
}
return XOR;
case '*':
if (*ptr == '*') {
if (*++ptr == '=') {
ptr++;
return POWEREQ;
}
return POWER;
}
if (*ptr == '=') {
ptr++;
return MULEQ;
}
return MUL;
case '/':
if (*ptr == '=') {
ptr++;
return DIVEQ;
}
return DIV;
case '%':
if (*ptr == '=') {
ptr++;
return MODEQ;
}
return MOD;
case '<':
if (*ptr == '<') {
if (*++ptr == '=') {
ptr++;
return SHLEFTEQ;
}
return SHLEFT;
} else if (*ptr == '=') {
ptr++;
return LEQ;
}
return LES;
case '>':
if (*ptr == '>') {
if (*++ptr == '=') {
ptr++;
return SHRIGHTEQ;
}
return SHRIGHT;
} else if (*ptr == '=') {
ptr++;
return GEQ;
}
return GRE;
case '=':
if (*ptr == '=') {
ptr++;
return DEQ;
}
return EQ;
case '$':
yyval.u.l = mypid;
return NUM;
case '?':
if (unary) {
yyval.u.l = lastval;
return NUM;
}
return QUEST;
case ':':
return COLON;
case ',':
return COMMA;
case '\0':
ptr--;
return EOI;
case '[':
{
int n;
if (idigit(*ptr)) {
n = zstrtol(ptr, &ptr, 10);
if (*ptr != ']' || !idigit(*++ptr)) {
zerr("bad base syntax");
return EOI;
}
yyval.u.l = zstrtol(ptr, &ptr, lastbase = n);
return NUM;
}
if (*ptr == '#') {
n = 1;
if (*++ptr == '#') {
n = -1;
ptr++;
}
if (!idigit(*ptr))
goto bofs;
outputradix = n * zstrtol(ptr, &ptr, 10);
} else {
bofs:
zerr("bad output format specification");
return EOI;
}
if(*ptr != ']')
goto bofs;
ptr++;
break;
}
case ' ':
case '\t':
case '\n':
break;
/* Fall through! */
default:
if (idigit(*--ptr) || *ptr == '.')
return lexconstant();
if (*ptr == '#') {
if (*++ptr == '\\' || *ptr == '#') {
int v;
ptr++;
if (!*ptr) {
zerr("character missing after ##");
return EOI;
}
ptr = getkeystring(ptr, NULL, 6, &v);
yyval.u.l = v;
return NUM;
}
cct = 1;
}
if (iident(*ptr)) {
int func = 0;
char *p;
p = ptr;
while (iident(*++ptr));
if (*ptr == '[' || (!cct && *ptr == '(')) {
char op = *ptr, cp = ((*ptr == '[') ? ']' : ')');
int l;
func = (op == '(');
for (ptr++, l = 1; *ptr && l; ptr++) {
if (*ptr == op)
l++;
if (*ptr == cp)
l--;
if (*ptr == '\\' && ptr[1])
ptr++;
}
}
yylval = dupstrpfx(p, ptr - p);
return (func ? FUNC : (cct ? CID : ID));
}
else if (cct) {
yyval.u.l = poundgetfn(NULL);
return NUM;
}
return EOI;
}
}
/* the value stack */
#define STACKSZ 100
static int mtok; /* last token */
static int sp = -1; /* stack pointer */
struct mathvalue {
char *lval;
mnumber val;
};
static struct mathvalue *stack;
/**/
static void
push(mnumber val, char *lval, int getme)
{
if (sp == STACKSZ - 1)
zerr("stack overflow");
else
sp++;
stack[sp].val = val;
stack[sp].lval = lval;
if (getme)
stack[sp].val.type = MN_UNSET;
}
/**/
static mnumber
pop(int noget)
{
struct mathvalue *mv = stack+sp;
if (mv->val.type == MN_UNSET && !noget)
mv->val = getnparam(mv->lval);
sp--;
return errflag ? zero_mnumber : mv->val;
}
/**/
static mnumber
getcvar(char *s)
{
char *t;
mnumber mn;
mn.type = MN_INTEGER;
queue_signals();
if (!(t = getsparam(s)))
mn.u.l = 0;
else {
#ifdef MULTIBYTE_SUPPORT
if (isset(MULTIBYTE)) {
wint_t wc;
(void)mb_metacharlenconv(t, &wc);
if (wc != WEOF) {
mn.u.l = (zlong)wc;
unqueue_signals();
return mn;
}
}
#endif
mn.u.l = STOUC(*t == Meta ? t[1] ^ 32 : *t);
}
unqueue_signals();
return mn;
}
/**/
static mnumber
setvar(char *s, mnumber v)
{
if (!s) {
zerr("lvalue required");
v.type = MN_INTEGER;
v.u.l = 0;
return v;
}
if (noeval)
return v;
untokenize(s);
setnparam(s, v);
return v;
}
/**/
static mnumber
callmathfunc(char *o)
{
MathFunc f;
char *a, *n;
static mnumber dummy;
n = a = dupstring(o);
while (*a != '(')
a++;
*a++ = '\0';
a[strlen(a) - 1] = '\0';
if ((f = getmathfunc(n, 1))) {
if (f->flags & MFF_STR) {
return f->sfunc(n, a, f->funcid);
} else {
int argc = 0;
mnumber *argv = NULL, *q, marg;
LinkList l = newlinklist();
LinkNode node;
if (f->flags & MFF_USERFUNC) {
/* first argument is function name: always use mathfunc */
addlinknode(l, n);
}
while (iblank(*a))
a++;
while (*a) {
if (*a) {
argc++;
if (f->flags & MFF_USERFUNC) {
/* need to pass strings */
char *str;
marg = mathevall(a, ARGPREC, &a);
if (marg.type & MN_FLOAT) {
/* convfloat is off the heap */
str = convfloat(marg.u.d, 0, 0, NULL);
} else {
char buf[BDIGBUFSIZE];
convbase(buf, marg.u.l, 10);
str = dupstring(buf);
}
addlinknode(l, str);
} else {
q = (mnumber *) zhalloc(sizeof(mnumber));
*q = mathevall(a, ARGPREC, &a);
addlinknode(l, q);
}
if (errflag || mtok != COMMA)
break;
}
}
if (*a && !errflag)
zerr("bad math expression: illegal character: %c", *a);
if (!errflag) {
if (argc >= f->minargs && (f->maxargs < 0 ||
argc <= f->maxargs)) {
if (f->flags & MFF_USERFUNC) {
char *shfnam = f->module ? f->module : n;
Eprog prog = getshfunc(shfnam);
if (prog == &dummy_eprog)
zerr("no such function: %s", shfnam);
else {
doshfunc(n, prog, l, 0, 1);
return lastmathval;
}
} else {
if (argc) {
q = argv =
(mnumber *)zhalloc(argc * sizeof(mnumber));
for (node = firstnode(l); node; incnode(node))
*q++ = *(mnumber *)getdata(node);
}
return f->nfunc(n, argc, argv, f->funcid);
}
} else
zerr("wrong number of arguments: %s", o);
}
}
} else
zerr("unknown function: %s", n);
dummy.type = MN_INTEGER;
dummy.u.l = 0;
return dummy;
}
/**/
static int
notzero(mnumber a)
{
if ((a.type & MN_INTEGER) ? a.u.l == 0 : a.u.d == 0.0) {
zerr("division by zero");
return 0;
}
return 1;
}
/* macro to pop three values off the value stack */
/**/
void
op(int what)
{
mnumber a, b, c, *spval;
char *lv;
int tp = type[what];
if (errflag)
return;
if (sp < 0) {
zerr("bad math expression: stack empty");
return;
}
if (tp & (OP_A2|OP_A2IR|OP_A2IO|OP_E2|OP_E2IO)) {
/* Make sure anyone seeing this message reports it. */
DPUTS(sp < 1, "BUG: math: not enough wallabies in outback.");
b = pop(0);
a = pop(what == EQ);
if (errflag)
return;
if (tp & (OP_A2IO|OP_E2IO)) {
/* coerce to integers */
if (a.type & MN_FLOAT) {
a.type = MN_INTEGER;
a.u.l = (zlong)a.u.d;
}
if (b.type & MN_FLOAT) {
b.type = MN_INTEGER;
b.u.l = (zlong)b.u.d;
}
} else if (a.type != b.type && what != COMMA &&
(a.type != MN_UNSET || what != EQ)) {
/*
* Different types, so coerce to float.
* It may happen during an assignment that the LHS
* variable is actually an integer, but there's still
* no harm in doing the arithmetic in floating point;
* the assignment will do the correct conversion.
* This way, if the parameter is actually a scalar, but
* used to contain an integer, we can write a float into it.
*/
if (a.type & MN_INTEGER) {
a.type = MN_FLOAT;
a.u.d = (double)a.u.l;
}
if (b.type & MN_INTEGER) {
b.type = MN_FLOAT;
b.u.d = (double)b.u.l;
}
}
if (noeval) {
c.type = MN_INTEGER;
c.u.l = 0;
} else {
/*
* type for operation: usually same as operands, but e.g.
* (a == b) returns int.
*/
c.type = (tp & OP_A2IR) ? MN_INTEGER : a.type;
switch(what) {
case AND:
case ANDEQ:
c.u.l = a.u.l & b.u.l;
break;
case XOR:
case XOREQ:
c.u.l = a.u.l ^ b.u.l;
break;
case OR:
case OREQ:
c.u.l = a.u.l | b.u.l;
break;
case MUL:
case MULEQ:
if (c.type == MN_FLOAT)
c.u.d = a.u.d * b.u.d;
else
c.u.l = a.u.l * b.u.l;
break;
case DIV:
case DIVEQ:
if (!notzero(b))
return;
if (c.type == MN_FLOAT)
c.u.d = a.u.d / b.u.d;
else
c.u.l = a.u.l / b.u.l;
break;
case MOD:
case MODEQ:
if (!notzero(b))
return;
c.u.l = a.u.l % b.u.l;
break;
case PLUS:
case PLUSEQ:
if (c.type == MN_FLOAT)
c.u.d = a.u.d + b.u.d;
else
c.u.l = a.u.l + b.u.l;
break;
case MINUS:
case MINUSEQ:
if (c.type == MN_FLOAT)
c.u.d = a.u.d - b.u.d;
else
c.u.l = a.u.l - b.u.l;
break;
case SHLEFT:
case SHLEFTEQ:
c.u.l = a.u.l << b.u.l;
break;
case SHRIGHT:
case SHRIGHTEQ:
c.u.l = a.u.l >> b.u.l;
break;
case LES:
c.u.l = (zlong)
(a.type == MN_FLOAT ? (a.u.d < b.u.d) : (a.u.l < b.u.l));
break;
case LEQ:
c.u.l = (zlong)
(a.type == MN_FLOAT ? (a.u.d <= b.u.d) : (a.u.l <= b.u.l));
break;
case GRE:
c.u.l = (zlong)
(a.type == MN_FLOAT ? (a.u.d > b.u.d) : (a.u.l > b.u.l));
break;
case GEQ:
c.u.l = (zlong)
(a.type == MN_FLOAT ? (a.u.d >= b.u.d) : (a.u.l >= b.u.l));
break;
case DEQ:
c.u.l = (zlong)
(a.type == MN_FLOAT ? (a.u.d == b.u.d) : (a.u.l == b.u.l));
break;
case NEQ:
c.u.l = (zlong)
(a.type == MN_FLOAT ? (a.u.d != b.u.d) : (a.u.l != b.u.l));
break;
case DAND:
case DANDEQ:
c.u.l = (zlong)(a.u.l && b.u.l);
break;
case DOR:
case DOREQ:
c.u.l = (zlong)(a.u.l || b.u.l);
break;
case DXOR:
case DXOREQ:
c.u.l = (zlong)((a.u.l && !b.u.l) || (!a.u.l && b.u.l));
break;
case COMMA:
c = b;
break;
case POWER:
case POWEREQ:
if (c.type == MN_INTEGER && b.u.l < 0) {
/* produces a real result, so cast to real. */
a.type = b.type = c.type = MN_FLOAT;
a.u.d = (double) a.u.l;
b.u.d = (double) b.u.l;
}
if (c.type == MN_INTEGER) {
for (c.u.l = 1; b.u.l--; c.u.l *= a.u.l);
} else {
if (b.u.d <= 0 && !notzero(a))
return;
if (a.u.d < 0) {
/* Error if (-num ** b) and b is not an integer */
double tst = (double)(zlong)b.u.d;
if (tst != b.u.d) {
zerr("imaginary power");
return;
}
}
c.u.d = pow(a.u.d, b.u.d);
}
break;
case EQ:
c = b;
break;
}
}
if (tp & (OP_E2|OP_E2IO)) {
lv = stack[sp+1].lval;
push(setvar(lv,c), lv, 0);
} else
push(c,NULL, 0);
return;
}
spval = &stack[sp].val;
if (stack[sp].val.type == MN_UNSET)
*spval = getnparam(stack[sp].lval);
switch (what) {
case NOT:
if (spval->type & MN_FLOAT) {
spval->u.l = !spval->u.d;
spval->type = MN_INTEGER;
} else
spval->u.l = !spval->u.l;
stack[sp].lval = NULL;
break;
case COMP:
if (spval->type & MN_FLOAT) {
spval->u.l = ~((zlong)spval->u.d);
spval->type = MN_INTEGER;
} else
spval->u.l = ~spval->u.l;
stack[sp].lval = NULL;
break;
case POSTPLUS:
a = *spval;
if (spval->type & MN_FLOAT)
a.u.d++;
else
a.u.l++;
(void)setvar(stack[sp].lval, a);
break;
case POSTMINUS:
a = *spval;
if (spval->type & MN_FLOAT)
a.u.d--;
else
a.u.l--;
(void)setvar(stack[sp].lval, a);
break;
case UPLUS:
stack[sp].lval = NULL;
break;
case UMINUS:
if (spval->type & MN_FLOAT)
spval->u.d = -spval->u.d;
else
spval->u.l = -spval->u.l;
stack[sp].lval = NULL;
break;
case QUEST:
DPUTS(sp < 2, "BUG: math: three shall be the number of the counting.");
c = pop(0);
b = pop(0);
a = pop(0);
if (errflag)
return;
/* b and c can stay different types in this case. */
push(((a.type & MN_FLOAT) ? a.u.d : a.u.l) ? b : c, NULL, 0);
break;
case COLON:
zerr("':' without '?'");
break;
case PREPLUS:
if (spval->type & MN_FLOAT)
spval->u.d++;
else
spval->u.l++;
setvar(stack[sp].lval, *spval);
break;
case PREMINUS:
if (spval->type & MN_FLOAT)
spval->u.d--;
else
spval->u.l--;
setvar(stack[sp].lval, *spval);
break;
default:
zerr("out of integers");
return;
}
}
/**/
static void
bop(int tk)
{
mnumber *spval = &stack[sp].val;
int tst;
if (stack[sp].val.type == MN_UNSET)
*spval = getnparam(stack[sp].lval);
tst = (spval->type & MN_FLOAT) ? (zlong)spval->u.d : spval->u.l;
switch (tk) {
case DAND:
case DANDEQ:
if (!tst)
noeval++;
break;
case DOR:
case DOREQ:
if (tst)
noeval++;
break;
};
}
/**/
static mnumber
mathevall(char *s, int prek, char **ep)
{
int xlastbase, xnoeval, xunary;
char *xptr;
mnumber xyyval;
char *xyylval;
int xsp;
struct mathvalue *xstack = 0, nstack[STACKSZ];
mnumber ret;
if (mlevel >= MAX_MLEVEL) {
xyyval.type = MN_INTEGER;
xyyval.u.l = 0;
zerr("math recursion limit exceeded");
return xyyval;
}
if (mlevel++) {
xlastbase = lastbase;
xnoeval = noeval;
xunary = unary;
xptr = ptr;
xyyval = yyval;
xyylval = yylval;
xsp = sp;
xstack = stack;
} else {
xlastbase = xnoeval = xunary = xsp = 0;
xyyval.type = MN_INTEGER;
xyyval.u.l = 0;
xyylval = NULL;
xptr = NULL;
}
stack = nstack;
lastbase = -1;
ptr = s;
sp = -1;
unary = 1;
stack[0].val.type = MN_INTEGER;
stack[0].val.u.l = 0;
mathparse(prek);
*ep = ptr;
DPUTS(!errflag && sp > 0,
"BUG: math: wallabies roaming too freely in outback");
if (errflag) {
ret.type = MN_INTEGER;
ret.u.l = errflag;
} else {
if (stack[0].val.type == MN_UNSET)
ret = getnparam(stack[0].lval);
else
ret = stack[0].val;
}
if (--mlevel) {
lastbase = xlastbase;
noeval = xnoeval;
unary = xunary;
ptr = xptr;
yyval = xyyval;
yylval = xyylval;
sp = xsp;
stack = xstack;
}
return lastmathval = ret;
}
/**/
mod_export mnumber
matheval(char *s)
{
char *junk;
mnumber x;
int xmtok = mtok;
/* maintain outputradix across levels of evaluation */
if (!mlevel)
outputradix = 0;
if (!*s) {
x.type = MN_INTEGER;
x.u.l = 0;
return x;
}
x = mathevall(s, TOPPREC, &junk);
mtok = xmtok;
if (*junk)
zerr("bad math expression: illegal character: %c", *junk);
return x;
}
/**/
mod_export zlong
mathevali(char *s)
{
mnumber x = matheval(s);
return (x.type & MN_FLOAT) ? (zlong)x.u.d : x.u.l;
}
/**/
zlong
mathevalarg(char *s, char **ss)
{
mnumber x;
int xmtok = mtok;
x = mathevall(s, ARGPREC, ss);
if (mtok == COMMA)
(*ss)--;
mtok = xmtok;
return (x.type & MN_FLOAT) ? (zlong)x.u.d : x.u.l;
}
/*
* Make sure we have an operator or an operand, whatever is expected.
* For this purpose, unary operators constitute part of an operand.
*/
/**/
static void
checkunary(int mtokc, char *mptr)
{
int errmsg = 0;
int tp = type[mtokc];
if (tp & (OP_A2|OP_A2IR|OP_A2IO|OP_E2|OP_E2IO|OP_OP)) {
if (unary)
errmsg = 1;
} else {
if (!unary)
errmsg = 2;
}
if (errmsg) {
int len, over = 0;
while (inblank(*mptr))
mptr++;
len = ztrlen(mptr);
if (len > 10) {
len = 10;
over = 1;
}
zerr("bad math expression: %s expected at `%l%s'",
errmsg == 2 ? "operator" : "operand",
mptr, len, over ? "..." : "");
}
unary = !(tp & OP_OPF);
}
/* operator-precedence parse the string and execute */
/**/
static void
mathparse(int pc)
{
zlong q;
int otok, onoeval;
char *optr = ptr;
if (errflag)
return;
mtok = zzlex();
/* Handle empty input */
if (pc == TOPPREC && mtok == EOI)
return;
checkunary(mtok, optr);
while (prec[mtok] <= pc) {
if (errflag)
return;
switch (mtok) {
case NUM:
push(yyval, NULL, 0);
break;
case ID:
push(zero_mnumber, yylval, !noeval);
break;
case CID:
push((noeval ? zero_mnumber : getcvar(yylval)), yylval, 0);
break;
case FUNC:
push((noeval ? zero_mnumber : callmathfunc(yylval)), yylval, 0);
break;
case M_INPAR:
mathparse(TOPPREC);
if (mtok != M_OUTPAR) {
if (!errflag)
zerr("')' expected");
return;
}
break;
case QUEST:
if (stack[sp].val.type == MN_UNSET)
stack[sp].val = getnparam(stack[sp].lval);
q = (stack[sp].val.type == MN_FLOAT) ? (zlong)stack[sp].val.u.d :
stack[sp].val.u.l;
if (!q)
noeval++;
mathparse(prec[COLON] - 1);
if (!q)
noeval--;
if (mtok != COLON) {
if (!errflag)
zerr("':' expected");
return;
}
if (q)
noeval++;
mathparse(prec[QUEST]);
if (q)
noeval--;
op(QUEST);
continue;
default:
otok = mtok;
onoeval = noeval;
if (MTYPE(type[otok]) == BOOL)
bop(otok);
mathparse(prec[otok] - (MTYPE(type[otok]) != RL));
noeval = onoeval;
op(otok);
continue;
}
optr = ptr;
mtok = zzlex();
checkunary(mtok, optr);
}
}
|