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