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