1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1982-2012 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                  David Korn <dgk@research.att.com>                   *
18 *                                                                      *
19 ***********************************************************************/
20 #pragma prototyped
21 #ifndef SEQPOINT
22 /*
23  * D. G. Korn
24  *
25  * arithmetic expression evaluator
26  */
27 
28 /* The following only is needed for const */
29 #include	<ast.h>
30 #include	<math.h>
31 #include	"defs.h"
32 #if _AST_VERSION >= 20030127L
33 #   include	<ast_float.h>
34 #endif
35 
36 #if _ast_fltmax_double
37 #define LDBL_LLONG_MAX		DBL_LLONG_MAX
38 #define LDBL_ULLONG_MAX		DBL_ULLONG_MAX
39 #define LDBL_LLONG_MIN		DBL_LLONG_MIN
40 #endif
41 
42 #ifndef LDBL_LLONG_MAX
43 #   ifdef LLONG_MAX
44 #	define LDBL_LLONG_MAX	((Sfdouble_t)LLONG_MAX)
45 #   else
46 #	ifdef LLONG_MAX
47 #	   define LDBL_LLONG_MAX	((Sfdouble_t)LLONG_MAX)
48 #	else
49 #	   define LDBL_LLONG_MAX	((Sfdouble_t)((((Sflong_t)1) << (8*sizeof(Sflong_t)-1)) -1 ))
50 #	endif
51 #   endif
52 #endif
53 #ifndef LDBL_ULLONG_MAX
54 #   ifdef ULLONG_MAX
55 #	define LDBL_ULLONG_MAX		((Sfdouble_t)ULLONG_MAX)
56 #   else
57 #	define LDBL_ULLONG_MAX		(2.*((Sfdouble_t)LDBL_LLONG_MAX))
58 #   endif
59 #endif
60 #ifndef LDBL_LLONG_MIN
61 #   ifdef LLONG_MIN
62 #	define LDBL_LLONG_MIN		((Sfdouble_t)LLONG_MIN)
63 #   else
64 #	define LDBL_LLONG_MIN		(-LDBL_LLONG_MAX)
65 #   endif
66 #endif
67 #ifndef LDBL_DIG
68 #   define LDBL_DIG DBL_DIG
69 #endif
70 
71 struct lval
72 {
73 	Shell_t		*shp;
74 	char		*value;
75 	char		*ovalue;
76 	Sfdouble_t	(*fun)(Sfdouble_t,...);
77 	const char	*expr;
78 	const  void	*ptr;
79 	int		nosub;
80 	short		flag;
81 	short		nargs;
82 	short		emode;
83 	short		level;
84 	short		elen;
85 	char		eflag;
86 	char		isfloat;
87 };
88 
89 struct mathtab
90 {
91 	char		fname[16];
92 	Sfdouble_t	(*fnptr)(Sfdouble_t,...);
93 };
94 
95 typedef struct _arith_
96 {
97 	Shell_t		*shp;
98 	unsigned char	*code;
99 	const char	*expr;
100 	Sfdouble_t	(*fun)(const char**,struct lval*,int,Sfdouble_t);
101 	short		size;
102 	short		staksize;
103 	short		emode;
104 	short		elen;
105 } Arith_t;
106 #define ARITH_COMP	04	/* set when compile separate from execute */
107 #define ARITH_ASSIGNOP	010	/* set during assignment operators  */
108 
109 #define MAXPREC		15	/* maximum precision level */
110 #define SEQPOINT	0200	/* sequence point */
111 #define NOASSIGN	0100	/* assignment legal with this operator */
112 #define RASSOC		040	/* right associative */
113 #define NOFLOAT		020	/* illegal with floating point */
114 #define PRECMASK	017	/* precision bit mask */
115 
116 #define A_EOF		1
117 #define A_NEQ		2
118 #define A_NOT		3
119 #define A_MOD		4
120 #define A_ANDAND	5
121 #define A_AND		6
122 #define A_LPAR		7
123 #define A_RPAR		8
124 #define A_POW		9
125 #define A_TIMES		10
126 #define A_PLUSPLUS	11
127 #define A_PLUS		12
128 #define A_COMMA		13
129 #define A_MINUSMINUS	14
130 #define A_MINUS		15
131 #define A_DIV		16
132 #define A_LSHIFT	17
133 #define A_LE		18
134 #define A_LT		19
135 #define A_EQ		20
136 #define A_ASSIGN	21
137 #define A_COLON		22
138 #define A_RSHIFT	23
139 #define A_GE		24
140 #define A_GT		25
141 #define A_QCOLON	26
142 #define A_QUEST		27
143 #define A_XOR		28
144 #define A_OROR		29
145 #define A_OR		30
146 #define A_TILDE		31
147 #define A_REG		32
148 #define A_DIG		33
149 #define A_INCR		34
150 #define A_DECR  	35
151 #define A_PUSHV 	36
152 #define A_PUSHL 	37
153 #define A_PUSHN 	38
154 #define A_PUSHF 	39
155 #define A_STORE 	40
156 #define A_POP   	41
157 #define A_SWAP  	42
158 #define A_UMINUS	43
159 #define A_JMPZ  	44
160 #define A_JMPNZ		45
161 #define A_JMP		46
162 #define A_CALL1F	47
163 #define A_CALL2F	48
164 #define A_CALL3F	49
165 #define A_CALL1I	50
166 #define A_CALL2I	51
167 #define A_DOT		52
168 #define A_LIT		53
169 #define A_NOTNOT        54
170 #define A_ASSIGNOP	55
171 #define A_ENUM		56
172 #define A_ASSIGNOP1	57
173 
174 
175 /* define error messages */
176 extern const unsigned char	strval_precedence[35];
177 extern const char		strval_states[64];
178 extern const char		e_moretokens[];
179 extern const char		e_argcount[];
180 extern const char		e_paren[];
181 extern const char		e_badnum[];
182 extern const char		e_badcolon[];
183 extern const char		e_recursive[];
184 extern const char		e_divzero[];
185 extern const char		e_synbad[];
186 extern const char		e_notlvalue[];
187 extern const char		e_function[];
188 extern const char		e_questcolon[];
189 extern const char		e_incompatible[];
190 extern const char		e_domain[];
191 extern const char		e_overflow[];
192 extern const char		e_singularity[];
193 extern const char		e_dict[];
194 extern const char		e_charconst[];
195 extern const struct 		mathtab shtab_math[];
196 
197 /* function code for the convert function */
198 
199 #define LOOKUP	0
200 #define ASSIGN	1
201 #define VALUE	2
202 #define MESSAGE	3
203 
204 extern Sfdouble_t strval(Shell_t*,const char*,char**,Sfdouble_t(*)(const char**,struct lval*,int,Sfdouble_t),int);
205 extern Arith_t *arith_compile(Shell_t *,const char*,char**,Sfdouble_t(*)(const char**,struct lval*,int,Sfdouble_t),int);
206 extern Sfdouble_t arith_exec(Arith_t*);
207 #endif /* !SEQPOINT */
208