1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1989-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 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                                                                      *
19 ***********************************************************************/
20 #pragma prototyped
21 /*
22  * Glenn Fowler
23  * AT&T Research
24  *
25  * expression library definitions
26  */
27 
28 #ifndef _EXPR_H
29 #define _EXPR_H
30 
31 #include <ast.h>
32 
33 #undef	RS	/* hp.pa <signal.h> grabs this!! */
34 
35 #if _BLD_expr && defined(__EXPORT__)
36 #define extern		__EXPORT__
37 #endif
38 #if !_BLD_expr && defined(__IMPORT__)
39 #define extern		extern __IMPORT__
40 #endif
41 
42 /*
43  * bison -pPREFIX misses YYSTYPE
44  */
45 
46 #if defined(YYSTYPE) || defined(YYBISON)
47 #define EXSTYPE		YYSTYPE
48 #else
49 #include <exparse.h>
50 #if defined(YYSTYPE) || defined(yystype)
51 #define EXSTYPE		YYSTYPE
52 #endif
53 #endif
54 
55 #undef	extern
56 
57 #include <cdt.h>
58 #include <vmalloc.h>
59 
60 #define EX_VERSION	20000101L
61 
62 /*
63  * flags
64  */
65 
66 #define EX_CHARSTRING	(1<<0)		/* '...' same as "..."		*/
67 #define EX_CONSTANT	(1<<1)		/* compile to constant expr	*/
68 #define EX_FATAL	(1<<2)		/* errors are fatal		*/
69 #define EX_INTERACTIVE	(1<<3)		/* interactive input		*/
70 #define EX_PURE		(1<<4)		/* no default symbols/keywords	*/
71 #define EX_QUALIFY	(1<<5)		/* '.' refs qualified in id tab	*/
72 #define EX_RETAIN	(1<<6)		/* retain expressions on redef	*/
73 #define EX_SIZED	(1<<7)		/* strings are sized buffers	*/
74 #define EX_STRICT	(1<<8)		/* don't override null label	*/
75 #define EX_UNDECLARED	(1<<9)		/* allow undeclared identifiers	*/
76 
77 #define EX_CALL		(-2)		/* getval() function call elt	*/
78 #define EX_SCALAR	(-1)		/* getval() scalar elt		*/
79 
80 #define EX_NAMELEN	32		/* default Exid_t.name length	*/
81 
82 #define EXID(n,l,i,t,f)	{{0},(l),(i),(t),(f),0,{0},0,n}
83 
84 #define DELETE		MINTOKEN		/* exexpr() delete `type'	*/
85 
86 #define INTEGRAL(t)	((t)>=CHAR&&(t)<=UNSIGNED)
87 
88 /*
89  * arg 0 is the return value type
90  */
91 
92 #define F		01		/* FLOATING			*/
93 #define I		02		/* INTEGER			*/
94 #define S		03		/* STRING			*/
95 
96 #define A(n,t)		((t)<<((n)*2))	/* function arg n is type t	*/
97 #define N(t)		((t)>>=2)	/* shift for next arg		*/
98 #define T(t)		a2t[(t)&03]	/* get actual type for t	*/
99 
100 #define exalloc(p,n)		exnewof(p,0,char,n,0)
101 #define exnewof(p,o,t,n,x)	vmnewof((p)->vm,o,t,n,x)
102 #define exfree(p,x)		vmfree((p)->vm,x)
103 #define exstrdup(p,s)		vmstrdup((p)->vm,s)
104 
105 #if LONG_MAX > INT_MAX
106 typedef int Exshort_t;
107 #else
108 typedef short Exshort_t;
109 #endif
110 
111 typedef EXSTYPE Extype_t;
112 
113 union Exdata_u; typedef union Exdata_u Exdata_t;
114 struct Exdisc_s; typedef struct Exdisc_s Exdisc_t;
115 struct Exnode_s; typedef struct Exnode_s Exnode_t;
116 struct Expr_s; typedef struct Expr_s Expr_t;
117 struct Exref_s; typedef struct Exref_s Exref_t;
118 
119 typedef struct Exlocal_s		/* user defined member type	*/
120 {
121 	Sflong_t	number;
122 	char*		pointer;
123 } Exlocal_t;
124 
125 typedef struct Exid_s			/* id symbol table info		*/
126 {
127 	Dtlink_t	link;		/* symbol table link		*/
128 	long		lex;		/* lex class			*/
129 	long		index;		/* user defined index		*/
130 	long		type;		/* symbol and arg types		*/
131 	long		flags;		/* user defined flags		*/
132 	Exnode_t*	value;		/* value			*/
133 	Exlocal_t	local;		/* user defined local stuff	*/
134 	long		isstatic;	/* static			*/
135 	char		name[EX_NAMELEN];/* symbol name			*/
136 } Exid_t;
137 
138 struct Exref_s				/* . reference list		*/
139 {
140 	Exref_t*	next;		/* next in list			*/
141 	Exid_t*		symbol;		/* reference id symbol		*/
142 	Exnode_t*	index;		/* optional reference index	*/
143 };
144 
145 typedef struct Exbuf_s			/* sized buffer			*/
146 {
147 	unsigned long	size;		/* buffer size			*/
148 	char*		data;		/* buffer data			*/
149 } Exbuf_t;
150 
151 union Exdata_u
152 {
153 
154 	struct
155 	{
156 	Extype_t	value;		/* constant value		*/
157 	Exid_t*		reference;	/* conversion reference symbol	*/
158 	}		constant;	/* variable reference		*/
159 
160 	struct
161 	{
162 	Exnode_t*	left;		/* left operand			*/
163 	Exnode_t*	right;		/* right operand		*/
164 	Exnode_t*	last;		/* for cons			*/
165 	}		operand;	/* operands			*/
166 
167 	struct
168 	{
169 	Exnode_t*	statement;	/* case label statement(s)	*/
170 	Exnode_t*	next;		/* next case item		*/
171 	Extype_t**	constant;	/* case label constant array	*/
172 	}		select;		/* case item			*/
173 
174 	struct
175 	{
176 	Exid_t*		symbol;		/* id symbol table entry	*/
177 	Exref_t*	reference;	/* . reference list		*/
178 	Exnode_t*	index;		/* array index expression	*/
179 	}		variable;	/* variable reference		*/
180 
181 #ifdef _EX_DATA_PRIVATE_
182 	_EX_DATA_PRIVATE_
183 #endif
184 
185 };
186 
187 struct Exnode_s				/* expression tree node		*/
188 {
189 	Exshort_t	type;		/* value type			*/
190 	Exshort_t	op;		/* operator			*/
191 	Exshort_t	binary;		/* data.operand.{left,right} ok	*/
192 	Exshort_t	pad_1;		/* padding to help cc		*/
193 	Exlocal_t	local;		/* user defined local stuff	*/
194 	union
195 	{
196 	double	(*floating)(char**);	/* FLOATING return value	*/
197 	Sflong_t(*integer)(char**);	/* INTEGER|UNSIGNED return value*/
198 	char*	(*string)(char**);	/* STRING return value		*/
199 	}		compiled;	/* compiled function pointer	*/
200 	Exdata_t	data;		/* node data			*/
201 
202 #ifdef _EX_NODE_PRIVATE_
203 	_EX_NODE_PRIVATE_
204 #endif
205 
206 };
207 
208 struct Exdisc_s				/* discipline			*/
209 {
210 	unsigned long	version;	/* EX_VERSION			*/
211 	unsigned long	flags;		/* EX_* flags			*/
212 	Exid_t*		symbols;	/* static symbols		*/
213 	char**		data;		/* compiled function arg data	*/
214 	char*		lib;		/* pathfind() lib		*/
215 	char*		type;		/* pathfind() type		*/
216 	int		(*castf)(Expr_t*, Exnode_t*, const char*, int, Exid_t*, int, Exdisc_t*);
217 					/* unknown cast function	*/
218 	int		(*convertf)(Expr_t*, Exnode_t*, int, Exid_t*, int, Exdisc_t*);
219 					/* type conversion function	*/
220 	Error_f		errorf;		/* error function		*/
221 	Extype_t	(*getf)(Expr_t*, Exnode_t*, Exid_t*, Exref_t*, void*, int, Exdisc_t*);
222 					/* get value function		*/
223 	Extype_t	(*reff)(Expr_t*, Exnode_t*, Exid_t*, Exref_t*, char*, int, Exdisc_t*);
224 					/* reference value function	*/
225 	int		(*setf)(Expr_t*, Exnode_t*, Exid_t*, Exref_t*, void*, int, Extype_t, Exdisc_t*);
226 					/* set value function		*/
227 	int		(*matchf)(Expr_t*, Exnode_t*, const char*, Exnode_t*, const char*, void*, Exdisc_t*);
228 };
229 
230 struct Expr_s				/* ex program state		*/
231 {
232 	const char*	id;		/* library id			*/
233 	Dt_t*		symbols;	/* symbol table			*/
234 	const char*	more;		/* more after %% (sp or != 0)	*/
235 	Sfio_t*		file[10];	/* io streams			*/
236 	Vmalloc_t*	vm;		/* program store		*/
237 
238 #ifdef _EX_PROG_PRIVATE_
239 	_EX_PROG_PRIVATE_
240 #endif
241 
242 };
243 
244 struct Excc_s; typedef struct Excc_s Excc_t;
245 struct Exccdisc_s; typedef struct Exccdisc_s Exccdisc_t;
246 
247 struct Exccdisc_s			/* excc() discipline		*/
248 {
249 	Sfio_t*		text;		/* text output stream		*/
250 	char*		id;		/* symbol prefix		*/
251 	unsigned long	flags;		/* EXCC_* flags			*/
252 	int		(*ccf)(Excc_t*, Exnode_t*, Exid_t*, Exref_t*, Exnode_t*, Exccdisc_t*);
253 					/* program generator function	*/
254 };
255 
256 struct Excc_s				/* excc() state			*/
257 {
258 	Expr_t*		expr;		/* exopen() state		*/
259 	Exdisc_t*	disc;		/* exopen() discipline		*/
260 
261 #ifdef _EX_CC_PRIVATE_
262 	_EX_CC_PRIVATE_
263 #endif
264 
265 };
266 
267 #if _BLD_expr && defined(__EXPORT__)
268 #define extern		__EXPORT__
269 #endif
270 
271 extern Exnode_t*	excast(Expr_t*, Exnode_t*, int, Exnode_t*, int);
272 extern int		excc(Excc_t*, const char*, Exid_t*, int);
273 extern int		exccclose(Excc_t*);
274 extern Excc_t*		exccopen(Expr_t*, Exccdisc_t*);
275 extern void		exclose(Expr_t*, int);
276 extern int		excomp(Expr_t*, const char*, int, const char*, Sfio_t*);
277 extern char*		excontext(Expr_t*, char*, int);
278 extern int		exdump(Expr_t*, Exnode_t*, Sfio_t*);
279 extern void		exerror(const char*, ...);
280 extern Extype_t		exeval(Expr_t*, Exnode_t*, void*);
281 extern Exnode_t*	exexpr(Expr_t*, const char*, Exid_t*, int);
282 extern void		exfreenode(Expr_t*, Exnode_t*);
283 extern Exnode_t*	exnewnode(Expr_t*, int, int, int, Exnode_t*, Exnode_t*);
284 extern char*		exnospace(void);
285 extern Expr_t*		exopen(Exdisc_t*);
286 extern int		expop(Expr_t*);
287 extern int		expush(Expr_t*, const char*, int, const char*, Sfio_t*);
288 extern int		exrewind(Expr_t*);
289 extern char*		exstash(Sfio_t*, Vmalloc_t*);
290 extern void		exstatement(Expr_t*);
291 extern int		extoken(Expr_t*);
292 extern char*		extype(int);
293 extern Extype_t		exzero(int);
294 
295 #undef	extern
296 
297 #endif
298