1 /***********************************************************************
2  *                                                                      *
3  *               This software is part of the ast package               *
4  *          Copyright (c) 1982-2013 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 <dgkorn@gmail.com>                     *
18  *                                                                      *
19  ***********************************************************************/
20 //
21 // D. G. Korn
22 //
23 // Arithmetic expression evaluator.
24 //
25 // The following only is needed for const.
26 #ifndef _STREVAL_H
27 #define _STREVAL_H 1
28 
29 #include <math.h>
30 
31 #include "ast.h"
32 #include "ast_float.h"
33 #include "defs.h"
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 #define TYPE_U 1
71 #define TYPE_F 2
72 #define TYPE_D 3
73 #define TYPE_LD 4
74 
75 struct lval {
76     Shell_t *shp;
77     char *value;
78     char *ovalue;
79     Sfdouble_t (*fun)(Sfdouble_t, ...);
80     const char *expr;
81     const void *ptr;
82     int nosub;
83     short flag;
84     short nargs;
85     short emode;
86     short level;
87     short elen;
88     char nextop;
89     char eflag;
90     char userfn;
91     char isfloat;
92 };
93 
94 struct mathtab {
95     char fname[16];
96     Sfdouble_t (*fnptr)(Sfdouble_t, ...);
97 };
98 
99 typedef struct _arith_ {
100     Shell_t *shp;
101     unsigned char *code;
102     const char *expr;
103     Sfdouble_t (*fun)(const char **, struct lval *, int, Sfdouble_t);
104     short size;
105     short staksize;
106     short emode;
107     short elen;
108 } Arith_t;
109 #define ARITH_COMP 04       // set when compile separate from execute
110 #define ARITH_ASSIGNOP 010  // set during assignment operators
111 
112 #define MAXPREC 15     // maximum precision level
113 #define SEQPOINT 0200  // sequence point
114 #define NOASSIGN 0100  // assignment legal with this operator
115 #define RASSOC 040     // right associative
116 #define NOFLOAT 020    // illegal with floating point
117 #define PRECMASK 017   // precision bit mask
118 
119 #define A_EOF 1
120 #define A_NEQ 2
121 #define A_NOT 3
122 #define A_MOD 4
123 #define A_ANDAND 5
124 #define A_AND 6
125 #define A_LPAR 7
126 #define A_RPAR 8
127 #define A_POW 9
128 #define A_TIMES 10
129 #define A_PLUSPLUS 11
130 #define A_PLUS 12
131 #define A_COMMA 13
132 #define A_MINUSMINUS 14
133 #define A_MINUS 15
134 #define A_DIV 16
135 #define A_LSHIFT 17
136 #define A_LE 18
137 #define A_LT 19
138 #define A_EQ 20
139 #define A_ASSIGN 21
140 #define A_COLON 22
141 #define A_RSHIFT 23
142 #define A_GE 24
143 #define A_GT 25
144 #define A_QCOLON 26
145 #define A_QUEST 27
146 #define A_XOR 28
147 #define A_OROR 29
148 #define A_OR 30
149 #define A_TILDE 31
150 #define A_REG 32
151 #define A_DIG 33
152 #define A_INCR 34
153 #define A_DECR 35
154 #define A_PUSHV 36
155 // #define A_PUSHL 37
156 #define A_PUSHN 38
157 #define A_PUSHF 39
158 #define A_STORE 40
159 #define A_POP 41
160 #define A_SWAP 42
161 #define A_UMINUS 43
162 #define A_JMPZ 44
163 #define A_JMPNZ 45
164 #define A_JMP 46
165 #define A_CALL1F 47
166 #define A_CALL2F 48
167 #define A_CALL3F 49
168 #define A_CALL1I 50
169 #define A_CALL2I 51
170 #define A_DOT 52
171 #define A_LIT 53
172 #define A_NOTNOT 54
173 #define A_ASSIGNOP 55
174 #define A_ENUM 56
175 #define A_ASSIGNOP1 57
176 #define A_CALL1V 58
177 #define A_CALL2V 59
178 // #define A_CALL3V 60
179 
180 // Define error messages.
181 extern const unsigned char strval_precedence[35];
182 extern const char strval_states[64];
183 extern const char e_moretokens[];
184 extern const char e_argcount[];
185 extern const char e_paren[];
186 extern const char e_badnum[];
187 extern const char e_badcolon[];
188 extern const char e_recursive[];
189 extern const char e_divzero[];
190 extern const char e_synbad[];
191 extern const char e_notlvalue[];
192 extern const char e_function[];
193 extern const char e_questcolon[];
194 extern const char e_incompatible[];
195 extern const char e_domain[];
196 extern const char e_overflow[];
197 extern const char e_singularity[];
198 extern const char e_dict[];
199 extern const char e_charconst[];
200 extern const struct mathtab shtab_math[];
201 
202 // Function code for the convert function.
203 #define LOOKUP 0
204 #define ASSIGN 1
205 #define VALUE 2
206 #define MESSAGE 3
207 
208 extern Sfdouble_t strval(Shell_t *, const char *, char **,
209                          Sfdouble_t (*)(const char **, struct lval *, int, Sfdouble_t), int);
210 extern Arith_t *arith_compile(Shell_t *, const char *, char **,
211                               Sfdouble_t (*)(const char **, struct lval *, int, Sfdouble_t), int);
212 extern Sfdouble_t arith_exec(Arith_t *);
213 
214 #endif  // _STREVAL_H
215