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
|
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1982-2011 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.eclipse.org/org/documents/epl-v10.html *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* D. G. Korn
* AT&T Labs
*
* arithmetic expression evaluator
*
* this version compiles the expression onto a stack
* and has a separate executor
*/
#include "streval.h"
#include <ctype.h>
#include <error.h>
#include <stak.h>
#include "FEATURE/externs"
#include "defs.h" /* for sh.decomma */
#ifndef ERROR_dictionary
# define ERROR_dictionary(s) (s)
#endif
#ifndef SH_DICT
# define SH_DICT "libshell"
#endif
#define MAXLEVEL 9
#define SMALL_STACK 12
/*
* The following are used with tokenbits() macro
*/
#define T_OP 0x3f /* mask for operator number */
#define T_BINARY 0x40 /* binary operators */
#define T_NOFLOAT 0x80 /* non floating point operator */
#define A_LVALUE (2*MAXPREC+2)
#define pow2size(x) ((x)<=2?2:(x)<=4?4:(x)<=8?8:(x)<=16?16:(x)<=32?32:64)
#define round(x,size) (((x)+(size)-1)&~((size)-1))
#define stakpush(v,val,type) ((((v)->offset=round(staktell(),pow2size(sizeof(type)))),\
stakseek((v)->offset+sizeof(type)), \
*((type*)stakptr((v)->offset)) = (val)),(v)->offset)
#define roundptr(ep,cp,type) (((unsigned char*)(ep))+round(cp-((unsigned char*)(ep)),pow2size(sizeof(type))))
static int level;
struct vars /* vars stacked per invocation */
{
Shell_t *shp;
const char *expr; /* current expression */
const char *nextchr; /* next char in current expression */
const char *errchr; /* next char after error */
const char *errstr; /* error string */
struct lval errmsg; /* error message text */
int offset; /* offset for pushchr macro */
int staksize; /* current stack size needed */
int stakmaxsize; /* maximum stack size needed */
unsigned char paren; /* parenthesis level */
char infun; /* incremented by comma inside function */
int emode;
Sfdouble_t (*convert)(const char**,struct lval*,int,Sfdouble_t);
};
typedef Sfdouble_t (*Math_f)(Sfdouble_t,...);
typedef Sfdouble_t (*Math_1f_f)(Sfdouble_t);
typedef int (*Math_1i_f)(Sfdouble_t);
typedef Sfdouble_t (*Math_2f_f)(Sfdouble_t,Sfdouble_t);
typedef Sfdouble_t (*Math_2f_i)(Sfdouble_t,int);
typedef int (*Math_2i_f)(Sfdouble_t,Sfdouble_t);
typedef Sfdouble_t (*Math_3f_f)(Sfdouble_t,Sfdouble_t,Sfdouble_t);
typedef int (*Math_3i_f)(Sfdouble_t,Sfdouble_t,Sfdouble_t);
#define getchr(vp) (*(vp)->nextchr++)
#define peekchr(vp) (*(vp)->nextchr)
#define ungetchr(vp) ((vp)->nextchr--)
#if ('a'==97) /* ASCII encodings */
# define getop(c) (((c) >= sizeof(strval_states))? \
((c)=='|'?A_OR:((c)=='^'?A_XOR:((c)=='~'?A_TILDE:A_REG))):\
strval_states[(c)])
#else
# define getop(c) (isdigit(c)?A_DIG:((c==' '||c=='\t'||c=='\n'||c=='"')?0: \
(c=='<'?A_LT:(c=='>'?A_GT:(c=='='?A_ASSIGN: \
(c=='+'?A_PLUS:(c=='-'?A_MINUS:(c=='*'?A_TIMES: \
(c=='/'?A_DIV:(c=='%'?A_MOD:(c==','?A_COMMA: \
(c=='&'?A_AND:(c=='!'?A_NOT:(c=='('?A_LPAR: \
(c==')'?A_RPAR:(c==0?A_EOF:(c==':'?A_COLON: \
(c=='?'?A_QUEST:(c=='|'?A_OR:(c=='^'?A_XOR: \
(c=='\''?A_LIT: \
(c=='.'?A_DOT:(c=='~'?A_TILDE:A_REG)))))))))))))))))))))))
#endif
#define seterror(v,msg) _seterror(v,ERROR_dictionary(msg))
#define ERROR(vp,msg) return(seterror((vp),msg))
/*
* set error message string and return(0)
*/
static int _seterror(struct vars *vp,const char *msg)
{
if(!vp->errmsg.value)
vp->errmsg.value = (char*)msg;
vp->errchr = vp->nextchr;
vp->nextchr = "";
level = 0;
return(0);
}
static void arith_error(const char *message,const char *expr, int mode)
{
level = 0;
mode = (mode&3)!=0;
errormsg(SH_DICT,ERROR_exit(mode),message,expr);
}
#if _ast_no_um2fm
static Sfdouble_t U2F(Sfulong_t u)
{
Sflong_t s = u;
Sfdouble_t f;
if (s >= 0)
return s;
s = u / 2;
f = s;
f *= 2;
if (u & 1)
f++;
return f;
}
#else
#define U2F(x) x
#endif
Sfdouble_t arith_exec(Arith_t *ep)
{
register Sfdouble_t num=0,*dp,*sp;
register unsigned char *cp = ep->code;
register int c,type=0;
register char *tp;
Sfdouble_t small_stack[SMALL_STACK+1],arg[9];
const char *ptr = "";
char *lastval=0;
int lastsub;
Math_f fun;
struct lval node;
Shell_t *shp = ep->shp;
node.shp = shp;
node.emode = ep->emode;
node.expr = ep->expr;
node.elen = ep->elen;
node.value = 0;
node.nosub = 0;
node.ptr = 0;
node.eflag = 0;
if(level++ >=MAXLEVEL)
{
arith_error(e_recursive,ep->expr,ep->emode);
return(0);
}
if(ep->staksize < SMALL_STACK)
sp = small_stack;
else
sp = (Sfdouble_t*)stakalloc(ep->staksize*(sizeof(Sfdouble_t)+1));
tp = (char*)(sp+ep->staksize);
tp--,sp--;
while(c = *cp++)
{
if(c&T_NOFLOAT)
{
if(type==1 || ((c&T_BINARY) && (c&T_OP)!=A_MOD && tp[-1]==1))
arith_error(e_incompatible,ep->expr,ep->emode);
}
switch(c&T_OP)
{
case A_JMP: case A_JMPZ: case A_JMPNZ:
c &= T_OP;
cp = roundptr(ep,cp,short);
if((c==A_JMPZ && num) || (c==A_JMPNZ &&!num))
cp += sizeof(short);
else
cp = (unsigned char*)ep + *((short*)cp);
continue;
case A_NOTNOT:
num = (num!=0);
type=0;
break;
case A_PLUSPLUS:
node.nosub = -1;
(*ep->fun)(&ptr,&node,ASSIGN,num+1);
break;
case A_MINUSMINUS:
node.nosub = -1;
(*ep->fun)(&ptr,&node,ASSIGN,num-1);
break;
case A_INCR:
num = num+1;
node.nosub = -1;
num = (*ep->fun)(&ptr,&node,ASSIGN,num);
break;
case A_DECR:
num = num-1;
node.nosub = -1;
num = (*ep->fun)(&ptr,&node,ASSIGN,num);
break;
case A_SWAP:
num = sp[-1];
sp[-1] = *sp;
type = tp[-1];
tp[-1] = *tp;
break;
case A_POP:
sp--;
continue;
case A_ASSIGNOP1:
node.emode |= ARITH_ASSIGNOP;
case A_PUSHV:
cp = roundptr(ep,cp,Sfdouble_t*);
dp = *((Sfdouble_t**)cp);
cp += sizeof(Sfdouble_t*);
c = *(short*)cp;
cp += sizeof(short);
lastval = node.value = (char*)dp;
if(node.flag = c)
lastval = 0;
node.isfloat=0;
node.level = level;
node.nosub = 0;
num = (*ep->fun)(&ptr,&node,VALUE,num);
if(node.emode&ARITH_ASSIGNOP)
{
lastsub = node.nosub;
node.nosub = 0;
node.emode &= ~ARITH_ASSIGNOP;
}
if(node.value != (char*)dp)
arith_error(node.value,ptr,ep->emode);
*++sp = num;
type = node.isfloat;
if(num > LDBL_ULLONG_MAX || num < LDBL_LLONG_MIN)
type = 1;
else
{
Sfdouble_t d=num;
if(num > LDBL_LLONG_MAX && num <= LDBL_ULLONG_MAX)
{
type = 2;
d -= LDBL_LLONG_MAX;
}
if((Sflong_t)d!=d)
type = 1;
}
*++tp = type;
c = 0;
break;
case A_ENUM:
node.eflag = 1;
continue;
case A_ASSIGNOP:
node.nosub = lastsub;
case A_STORE:
cp = roundptr(ep,cp,Sfdouble_t*);
dp = *((Sfdouble_t**)cp);
cp += sizeof(Sfdouble_t*);
c = *(short*)cp;
if(c<0)
c = 0;
cp += sizeof(short);
node.value = (char*)dp;
node.flag = c;
if(lastval)
node.eflag = 1;
node.ptr = 0;
num = (*ep->fun)(&ptr,&node,ASSIGN,num);
if(lastval && node.ptr)
{
Sfdouble_t r;
node.flag = 0;
node.value = lastval;
r = (*ep->fun)(&ptr,&node,VALUE,num);
if(r!=num)
{
node.flag=c;
node.value = (char*)dp;
num = (*ep->fun)(&ptr,&node,ASSIGN,r);
}
}
lastval = 0;
c=0;
break;
case A_PUSHF:
cp = roundptr(ep,cp,Math_f);
*++sp = (Sfdouble_t)(cp-ep->code);
cp += sizeof(Math_f);
*++tp = *cp++;
continue;
case A_PUSHN:
cp = roundptr(ep,cp,Sfdouble_t);
num = *((Sfdouble_t*)cp);
cp += sizeof(Sfdouble_t);
*++sp = num;
*++tp = type = *cp++;
break;
case A_NOT:
type=0;
num = !num;
break;
case A_UMINUS:
num = -num;
break;
case A_TILDE:
num = ~((Sflong_t)(num));
break;
case A_PLUS:
num += sp[-1];
break;
case A_MINUS:
num = sp[-1] - num;
break;
case A_TIMES:
num *= sp[-1];
break;
case A_POW:
num = pow(sp[-1],num);
break;
case A_MOD:
if(!(Sflong_t)num)
arith_error(e_divzero,ep->expr,ep->emode);
if(type==2 || tp[-1]==2)
num = U2F((Sfulong_t)(sp[-1]) % (Sfulong_t)(num));
else
num = (Sflong_t)(sp[-1]) % (Sflong_t)(num);
break;
case A_DIV:
if(type==1 || tp[-1]==1)
{
num = sp[-1]/num;
type = 1;
}
else if((Sfulong_t)(num)==0)
arith_error(e_divzero,ep->expr,ep->emode);
else if(type==2 || tp[-1]==2)
num = U2F((Sfulong_t)(sp[-1]) / (Sfulong_t)(num));
else
num = (Sflong_t)(sp[-1]) / (Sflong_t)(num);
break;
case A_LSHIFT:
if(tp[-1]==2)
num = U2F((Sfulong_t)(sp[-1]) << (long)(num));
else
num = (Sflong_t)(sp[-1]) << (long)(num);
break;
case A_RSHIFT:
if(tp[-1]==2)
num = U2F((Sfulong_t)(sp[-1]) >> (long)(num));
else
num = (Sflong_t)(sp[-1]) >> (long)(num);
break;
case A_XOR:
if(type==2 || tp[-1]==2)
num = U2F((Sfulong_t)(sp[-1]) ^ (Sfulong_t)(num));
else
num = (Sflong_t)(sp[-1]) ^ (Sflong_t)(num);
break;
case A_OR:
if(type==2 || tp[-1]==2)
num = U2F((Sfulong_t)(sp[-1]) | (Sfulong_t)(num));
else
num = (Sflong_t)(sp[-1]) | (Sflong_t)(num);
break;
case A_AND:
if(type==2 || tp[-1]==2)
num = U2F((Sfulong_t)(sp[-1]) & (Sfulong_t)(num));
else
num = (Sflong_t)(sp[-1]) & (Sflong_t)(num);
break;
case A_EQ:
num = (sp[-1]==num);
type=0;
break;
case A_NEQ:
num = (sp[-1]!=num);
type=0;
break;
case A_LE:
num = (sp[-1]<=num);
type=0;
break;
case A_GE:
num = (sp[-1]>=num);
type=0;
break;
case A_GT:
num = (sp[-1]>num);
type=0;
break;
case A_LT:
num = (sp[-1]<num);
type=0;
break;
case A_CALL1F:
sp--,tp--;
fun = *((Math_f*)(ep->code+(int)(*sp)));
type = *tp;
if(c&T_BINARY)
{
c &= ~T_BINARY;
arg[0] = num;
arg[1] = 0;
num = sh_mathfun(shp,(void*)fun,1,arg);
break;
}
num = (*((Math_1f_f)fun))(num);
break;
case A_CALL1I:
sp--,tp--;
fun = *((Math_f*)(ep->code+(int)(*sp)));
type = *tp;
num = (*((Math_1i_f)fun))(num);
break;
case A_CALL2F:
sp-=2,tp-=2;
fun = *((Math_f*)(ep->code+(int)(*sp)));
type = *tp;
if(c&T_BINARY)
{
c &= ~T_BINARY;
arg[0] = sp[1];
arg[1] = num;
arg[2] = 0;
num = sh_mathfun(shp,(void*)fun,2,arg);
break;
}
if(c&T_NOFLOAT)
num = (*((Math_2f_i)fun))(sp[1],(int)num);
else
num = (*((Math_2f_f)fun))(sp[1],num);
break;
case A_CALL2I:
sp-=2,tp-=2;
fun = *((Math_f*)(ep->code+(int)(*sp)));
type = *tp;
num = (*((Math_2i_f)fun))(sp[1],num);
break;
case A_CALL3F:
sp-=3,tp-=3;
fun = *((Math_f*)(ep->code+(int)(*sp)));
type = *tp;
if(c&T_BINARY)
{
c &= ~T_BINARY;
arg[0] = sp[1];
arg[1] = sp[2];
arg[2] = num;
arg[3] = 0;
num = sh_mathfun(shp,(void*)fun,3,arg);
break;
}
num = (*((Math_3f_f)fun))(sp[1],sp[2],num);
break;
}
if(c)
lastval = 0;
if(c&T_BINARY)
{
node.ptr = 0;
sp--,tp--;
type |= (*tp!=0);
}
*sp = num;
*tp = type;
}
if(level>0)
level--;
return(num);
}
/*
* This returns operator tokens or A_REG or A_NUM
*/
static int gettok(register struct vars *vp)
{
register int c,op;
vp->errchr = vp->nextchr;
while(1)
{
c = getchr(vp);
switch(op=getop(c))
{
case 0:
vp->errchr = vp->nextchr;
continue;
case A_EOF:
vp->nextchr--;
break;
case A_COMMA:
if(vp->shp->decomma && (c=peekchr(vp))>='0' && c<='9')
{
op = A_DIG;
goto keep;
}
break;
case A_DOT:
if((c=peekchr(vp))>='0' && c<='9')
op = A_DIG;
else
op = A_REG;
/*FALL THRU*/
case A_DIG: case A_REG: case A_LIT:
keep:
ungetchr(vp);
break;
case A_QUEST:
if(peekchr(vp)==':')
{
getchr(vp);
op = A_QCOLON;
}
break;
case A_LT: case A_GT:
if(peekchr(vp)==c)
{
getchr(vp);
op -= 2;
break;
}
/* FALL THRU */
case A_NOT: case A_COLON:
c = '=';
/* FALL THRU */
case A_ASSIGN:
case A_TIMES:
case A_PLUS: case A_MINUS:
case A_OR: case A_AND:
if(peekchr(vp)==c)
{
getchr(vp);
op--;
}
}
return(op);
}
}
/*
* evaluate a subexpression with precedence
*/
static int expr(register struct vars *vp,register int precedence)
{
register int c, op;
int invalid,wasop=0;
struct lval lvalue,assignop;
const char *pos;
Sfdouble_t d;
lvalue.value = 0;
lvalue.nargs = 0;
lvalue.fun = 0;
lvalue.shp = vp->shp;
again:
op = gettok(vp);
c = 2*MAXPREC+1;
switch(op)
{
case A_PLUS:
goto again;
case A_EOF:
if(precedence>2)
ERROR(vp,e_moretokens);
return(1);
case A_MINUS:
op = A_UMINUS;
goto common;
case A_NOT:
goto common;
case A_MINUSMINUS:
c = A_LVALUE;
op = A_DECR|T_NOFLOAT;
goto common;
case A_PLUSPLUS:
c = A_LVALUE;
op = A_INCR|T_NOFLOAT;
/* FALL THRU */
case A_TILDE:
op |= T_NOFLOAT;
common:
if(!expr(vp,c))
return(0);
stakputc(op);
break;
default:
vp->nextchr = vp->errchr;
wasop = 1;
}
invalid = wasop;
while(1)
{
assignop.value = 0;
op = gettok(vp);
if(op==A_DIG || op==A_REG || op==A_LIT)
{
if(!wasop)
ERROR(vp,e_synbad);
goto number;
}
if(wasop++ && op!=A_LPAR)
ERROR(vp,e_synbad);
/* check for assignment operation */
if(peekchr(vp)== '=' && !(strval_precedence[op]&NOASSIGN))
{
if((!lvalue.value || precedence > 3))
ERROR(vp,e_notlvalue);
if(precedence==3)
precedence = 2;
assignop = lvalue;
getchr(vp);
c = 3;
}
else
{
c = (strval_precedence[op]&PRECMASK);
if(c==MAXPREC || op==A_POW)
c++;
c *= 2;
}
/* from here on c is the new precedence level */
if(lvalue.value && (op!=A_ASSIGN))
{
if(vp->staksize++>=vp->stakmaxsize)
vp->stakmaxsize = vp->staksize;
if(op==A_EQ || op==A_NEQ)
stakputc(A_ENUM);
stakputc(assignop.value?A_ASSIGNOP1:A_PUSHV);
stakpush(vp,lvalue.value,char*);
if(lvalue.flag<0)
lvalue.flag = 0;
stakpush(vp,lvalue.flag,short);
if(vp->nextchr==0)
ERROR(vp,e_badnum);
if(!(strval_precedence[op]&SEQPOINT))
lvalue.value = 0;
invalid = 0;
}
else if(precedence==A_LVALUE)
ERROR(vp,e_notlvalue);
if(invalid && op>A_ASSIGN)
ERROR(vp,e_synbad);
if(precedence >= c)
goto done;
if(strval_precedence[op]&RASSOC)
c--;
if((c < (2*MAXPREC+1)) && !(strval_precedence[op]&SEQPOINT))
{
wasop = 0;
if(!expr(vp,c))
return(0);
}
switch(op)
{
case A_RPAR:
if(!vp->paren)
ERROR(vp,e_paren);
if(invalid)
ERROR(vp,e_synbad);
goto done;
case A_COMMA:
wasop = 0;
if(vp->infun)
vp->infun++;
else
{
stakputc(A_POP);
vp->staksize--;
}
if(!expr(vp,c))
{
stakseek(staktell()-1);
return(0);
}
lvalue.value = 0;
break;
case A_LPAR:
{
int infun = vp->infun;
int userfun=0;
Sfdouble_t (*fun)(Sfdouble_t,...);
int nargs = lvalue.nargs;
if(nargs<0)
nargs = -nargs;
fun = lvalue.fun;
lvalue.fun = 0;
if(fun)
{
if(vp->staksize++>=vp->stakmaxsize)
vp->stakmaxsize = vp->staksize;
vp->infun=1;
if((int)lvalue.nargs<0)
userfun = T_BINARY;
else if((int)lvalue.nargs&040)
userfun = T_NOFLOAT;
stakputc(A_PUSHF);
stakpush(vp,fun,Math_f);
stakputc(1);
}
else
vp->infun = 0;
if(!invalid)
ERROR(vp,e_synbad);
vp->paren++;
if(!expr(vp,1))
return(0);
vp->paren--;
if(fun)
{
int x= (nargs&010)?2:-1;
nargs &= 7;
if(vp->infun != nargs)
ERROR(vp,e_argcount);
if((vp->staksize+=nargs)>=vp->stakmaxsize)
vp->stakmaxsize = vp->staksize+nargs;
stakputc(A_CALL1F+userfun+nargs+x);
vp->staksize -= nargs;
}
vp->infun = infun;
if (gettok(vp) != A_RPAR)
ERROR(vp,e_paren);
wasop = 0;
break;
}
case A_PLUSPLUS:
case A_MINUSMINUS:
wasop=0;
op |= T_NOFLOAT;
case A_ASSIGN:
if(!lvalue.value)
ERROR(vp,e_notlvalue);
if(op==A_ASSIGN)
{
stakputc(A_STORE);
stakpush(vp,lvalue.value,char*);
stakpush(vp,lvalue.flag,short);
vp->staksize--;
}
else
stakputc(op);
lvalue.value = 0;
break;
case A_QUEST:
{
int offset1,offset2;
stakputc(A_JMPZ);
offset1 = stakpush(vp,0,short);
stakputc(A_POP);
if(!expr(vp,1))
return(0);
if(gettok(vp)!=A_COLON)
ERROR(vp,e_questcolon);
stakputc(A_JMP);
offset2 = stakpush(vp,0,short);
*((short*)stakptr(offset1)) = staktell();
stakputc(A_POP);
if(!expr(vp,3))
return(0);
*((short*)stakptr(offset2)) = staktell();
lvalue.value = 0;
wasop = 0;
break;
}
case A_COLON:
ERROR(vp,e_badcolon);
break;
case A_QCOLON:
case A_ANDAND:
case A_OROR:
{
int offset;
if(op==A_ANDAND)
op = A_JMPZ;
else
op = A_JMPNZ;
stakputc(op);
offset = stakpush(vp,0,short);
stakputc(A_POP);
if(!expr(vp,c))
return(0);
*((short*)stakptr(offset)) = staktell();
if(op!=A_QCOLON)
stakputc(A_NOTNOT);
lvalue.value = 0;
wasop=0;
break;
}
case A_AND: case A_OR: case A_XOR: case A_LSHIFT:
case A_RSHIFT: case A_MOD:
op |= T_NOFLOAT;
/* FALL THRU */
case A_PLUS: case A_MINUS: case A_TIMES: case A_DIV:
case A_EQ: case A_NEQ: case A_LT: case A_LE:
case A_GT: case A_GE: case A_POW:
stakputc(op|T_BINARY);
vp->staksize--;
break;
case A_NOT: case A_TILDE:
default:
ERROR(vp,e_synbad);
number:
wasop = 0;
if(*vp->nextchr=='L' && vp->nextchr[1]=='\'')
{
vp->nextchr++;
op = A_LIT;
}
pos = vp->nextchr;
lvalue.isfloat = 0;
lvalue.expr = vp->expr;
lvalue.emode = vp->emode;
if(op==A_LIT)
{
/* character constants */
if(pos[1]=='\\' && pos[2]=='\'' && pos[3]!='\'')
{
d = '\\';
vp->nextchr +=2;
}
else
d = chresc(pos+1,(char**)&vp->nextchr);
/* posix allows the trailing ' to be optional */
if(*vp->nextchr=='\'')
vp->nextchr++;
}
else
d = (*vp->convert)(&vp->nextchr, &lvalue, LOOKUP, 0);
if (vp->nextchr == pos)
{
if(vp->errmsg.value = lvalue.value)
vp->errstr = pos;
ERROR(vp,op==A_LIT?e_charconst:e_synbad);
}
if(op==A_DIG || op==A_LIT)
{
stakputc(A_PUSHN);
if(vp->staksize++>=vp->stakmaxsize)
vp->stakmaxsize = vp->staksize;
stakpush(vp,d,Sfdouble_t);
stakputc(lvalue.isfloat);
}
/* check for function call */
if(lvalue.fun)
continue;
break;
}
invalid = 0;
if(assignop.value)
{
if(vp->staksize++>=vp->stakmaxsize)
vp->stakmaxsize = vp->staksize;
if(assignop.flag<0)
assignop.flag = 0;
stakputc(c&1?A_ASSIGNOP:A_STORE);
stakpush(vp,assignop.value,char*);
stakpush(vp,assignop.flag,short);
}
}
done:
vp->nextchr = vp->errchr;
return(1);
}
Arith_t *arith_compile(Shell_t *shp,const char *string,char **last,Sfdouble_t(*fun)(const char**,struct lval*,int,Sfdouble_t),int emode)
{
struct vars cur;
register Arith_t *ep;
int offset;
memset((void*)&cur,0,sizeof(cur));
cur.shp = shp;
cur.expr = cur.nextchr = string;
cur.convert = fun;
cur.emode = emode;
cur.errmsg.value = 0;
cur.errmsg.emode = emode;
stakseek(sizeof(Arith_t));
if(!expr(&cur,0) && cur.errmsg.value)
{
if(cur.errstr)
string = cur.errstr;
if((*fun)( &string , &cur.errmsg, MESSAGE, 0) < 0)
{
stakseek(0);
*last = (char*)Empty;
return(0);
}
cur.nextchr = cur.errchr;
}
stakputc(0);
offset = staktell();
ep = (Arith_t*)stakfreeze(0);
ep->shp = shp;
ep->expr = string;
ep->elen = strlen(string);
ep->code = (unsigned char*)(ep+1);
ep->fun = fun;
ep->emode = emode;
ep->size = offset - sizeof(Arith_t);
ep->staksize = cur.stakmaxsize+1;
if(last)
*last = (char*)(cur.nextchr);
return(ep);
}
/*
* evaluate an integer arithmetic expression in s
*
* (Sfdouble_t)(*convert)(char** end, struct lval* string, int type, Sfdouble_t value)
* is a user supplied conversion routine that is called when unknown
* chars are encountered.
* *end points to the part to be converted and must be adjusted by convert to
* point to the next non-converted character; if typ is MESSAGE then string
* points to an error message string
*
* NOTE: (*convert)() may call strval()
*/
Sfdouble_t strval(Shell_t *shp,const char *s,char **end,Sfdouble_t(*conv)(const char**,struct lval*,int,Sfdouble_t),int emode)
{
Arith_t *ep;
Sfdouble_t d;
char *sp=0;
int offset;
if(offset=staktell())
sp = stakfreeze(1);
ep = arith_compile(shp,s,end,conv,emode);
ep->emode = emode;
d = arith_exec(ep);
stakset(sp?sp:(char*)ep,offset);
return(d);
}
#if _mem_name__exception
#undef _mem_name_exception
#define _mem_name_exception 1
#undef exception
#define exception _exception
#undef matherr
#endif
#if _mem_name_exception
#undef error
#if _BLD_shell && defined(__EXPORT__)
#define extern __EXPORT__
#endif
#ifndef DOMAIN
#define DOMAIN _DOMAIN
#endif
#ifndef OVERFLOW
#define OVERFLOW _OVERFLOW
#endif
#ifndef SING
#define SING _SING
#endif
extern int matherr(struct exception *ep)
{
const char *message;
switch(ep->type)
{
#ifdef DOMAIN
case DOMAIN:
message = ERROR_dictionary(e_domain);
break;
#endif
#ifdef OVERFLOW
case OVERFLOW:
message = ERROR_dictionary(e_overflow);
break;
#endif
#ifdef SING
case SING:
message = ERROR_dictionary(e_singularity);
break;
#endif
default:
return(1);
}
level=0;
errormsg(SH_DICT,ERROR_exit(1),message,ep->name);
return(0);
}
#undef extern
#endif /* _mem_name_exception */
|