Debian Codegraph @

lib/expr/expr.h

raw
1 /*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11 #pragma once
12
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16
17 /*
18 * Glenn Fowler
19 * AT&T Research
20 *
21 * expression library definitions
22 */
23
24 #include <ast/ast.h>
25 #include <inttypes.h>
26
27 #include <expr/exparse.h>
28
29 #include <assert.h>
30 #include <cdt.h>
31 #include <stdarg.h>
32 #include <stdbool.h>
33 #include <stddef.h>
34 #include <stdio.h>
35 #include <util/agxbuf.h>
36 #include <util/arena.h>
37
38 #define EX_VERSION 20000101L
39
40 /*
41 * flags
42 */
43
44 #define EX_CHARSTRING (1<<0) /* '...' same as "..." */
45 #define EX_UNDECLARED (1<<9) /* allow undeclared identifiers */
46
47 #define EX_ARRAY (-3) /* getval() array elt */
48 #define EX_CALL (-2) /* getval() function call elt */
49 #define EX_SCALAR (-1) /* getval() scalar elt */
50
51 #define EX_NAMELEN 32 /* default Exid_t.name length */
52
53 #define EX_ID(n, l, i, t) {{0}, (l), (i), (t), 0, 0, 0, n}
54
55 #define DELETE_T MINTOKEN /* exexpr() delete `type' */
56
57 #define INTEGRAL(t) ((t)>=INTEGER&&(t)<=CHARACTER)
58 #define BUILTIN(t) ((t) > MINTOKEN)
59
60 /* function type mechanism
61 * types are encoded in TBITS
62 * Thus, maximum # of parameters, including return type,
63 * is sizeof(Exid_t.type)/TBITS. Also see T in exgram.h
64 */
65
66 /*
67 * arg 0 is the return value type
68 */
69
70 #define F 01 /* FLOATING */
71 #define I 02 /* INTEGER */
72 #define S 03 /* STRING */
73
74 #define TBITS 4
75 #define TMASK ((1<<TBITS)-1)
76 #define A(n,t) ((t)<<((n)*TBITS)) /* function arg n is type t */
77 #define N(t) ((t)>>=TBITS) /* shift for next arg */
78
79 typedef EX_STYPE Extype_t;
80
81 typedef union Exdata_u Exdata_t;
82 typedef struct Exdisc_s Exdisc_t;
83 typedef struct Exnode_s Exnode_t;
84 typedef struct Expr_s Expr_t;
85 typedef struct Exref_s Exref_t;
86
87 typedef void (*Exerror_f) (Expr_t *, Exdisc_t *, int, const char *, ...);
88 typedef void (*Exexit_f)(void *, int);
89
90 typedef struct Exid_s /* id symbol table info */
91 {
92 Dtlink_t link; /* symbol table link */
93 long lex; /* lex class */
94 long index; /* user defined index */
95 long type; /* symbol and arg types */
96 long index_type; /* index type for arrays */
97 Exnode_t* value; /* value */
98 void *local; ///< user defined local stuff
99 char name[EX_NAMELEN];/* symbol name */
100 } Exid_t;
101
102 struct Exref_s /* . reference list */
103 {
104 Exref_t* next; /* next in list */
105 Exid_t* symbol; /* reference id symbol */
106 Exnode_t* index; /* optional reference index */
107 };
108
109 union Exdata_u
110 {
111
112 struct
113 {
114 Extype_t value; /* constant value */
115 Exid_t* reference; /* conversion reference symbol */
116 } constant; /* variable reference */
117
118 struct
119 {
120 Exnode_t* left; /* left operand */
121 Exnode_t* right; /* right operand */
122 } operand; /* operands */
123
124 struct
125 {
126 Exnode_t* statement; /* case label statement(s) */
127 Exnode_t* next; /* next case item */
128 Extype_t** constant; /* case label constant array */
129 } select; /* case item */
130
131 struct
132 {
133 Exid_t* symbol; /* id symbol table entry */
134 Exref_t* reference; /* . reference list */
135 Exnode_t* index; /* array index expression */
136 Exnode_t* dyna; /* dynamic expression */
137 } variable; /* variable reference */
138
139 #ifdef _EX_DATA_PRIVATE_
140 _EX_DATA_PRIVATE_
141 #endif
142
143 };
144
145 struct Exnode_s /* expression tree node */
146 {
147 long type; ///< value type
148 long op; ///< operator
149 bool binary; ///< data.operand.{left,right} ok
150 union
151 {
152 double (*floating)(char**); /* FLOATING return value */
153 long long (*integer)(char **); ///< INTEGER|UNSIGNED return value
154 char* (*string)(char**); /* STRING return value */
155 } compiled; /* compiled function pointer */
156 Exdata_t data; /* node data */
157
158 #ifdef _EX_NODE_PRIVATE_
159 _EX_NODE_PRIVATE_
160 #endif
161
162 };
163
164 struct Exdisc_s /* discipline */
165 {
166 uint64_t version; /* EX_VERSION */
167 uint64_t flags; /* EX_* flags */
168 Exid_t* symbols; /* static symbols */
169 char** data; /* compiled function arg data */
170 int (*castf)(Expr_t*, Exnode_t*, const char*, int, Exid_t*, int, Exdisc_t*);
171 /* unknown cast function */
172 int (*convertf)(Exnode_t *, long, int);
173 /* type conversion function */
174 int (*binaryf) (Exnode_t *, Exnode_t *, Exnode_t *, int);
175 /* binary operator function */
176 char *(*typename)(long);
177 /* application type names */
178 int (*stringof) (Expr_t *, Exnode_t *, int);
179 /* value to string conversion */
180 Extype_t (*keyf)(Extype_t, long);
181 /* dictionary key for external type objects */
182 Exerror_f errorf; /* error function */
183 Extype_t (*getf)(Expr_t*, Exnode_t*, Exid_t*, Exref_t*, void*, int, Exdisc_t*);
184 /* get value function */
185 Extype_t (*reff)(Expr_t*, Exnode_t*, Exid_t*, Exref_t*);
186 /* reference value function */
187 int (*setf)(Expr_t*, Exnode_t*, Exid_t*, Exref_t*, void*, Extype_t);
188 /* set value function */
189 /// length function
190 ///
191 /// This callback allows a user-defined '#' handler. That is, extending the
192 /// array length operator’s function.
193 ///
194 /// @param rhs The right-hand side operand. That is 'foo' in the expression
195 /// '# foo'.
196 /// @param disc Pointer to the containing discipline.
197 /// @return The “length”, according to the user’s interpretation.
198 Extype_t (*lengthf)(Exid_t *rhs, Exdisc_t *disc);
199 /// array membership function
200 ///
201 /// This callback allows a user-defined `in` handler. That is, extending the
202 /// array membership test’s function.
203 ///
204 /// @param lhs The left-hand side operand. That is 'foo' in the expression
205 /// 'foo in bar'.
206 /// @param rhs The right-hand side operand. That is 'bar' in the expression
207 /// 'foo in bar'.
208 /// @param disc Pointer to the containing discipline.
209 /// @return 0 if `lhs` is not in the array `rhs`, non-zero otherwise
210 int (*inf)(Extype_t lhs, Exid_t *rhs, Exdisc_t *disc);
211 /* exit function */
212 Exexit_f exitf;
213 int* types;
214 void* user;
215 };
216
217 struct Expr_s /* ex program state */
218 {
219 const char* id; /* library id */
220 Dt_t* symbols; /* symbol table */
221 FILE* file[10]; /* io streams */
222 arena_t vm; /* program store */
223
224 #ifdef _EX_PROG_PRIVATE_
225 _EX_PROG_PRIVATE_
226 #endif
227
228 };
229
230 extern Exnode_t *excast(Expr_t *, Exnode_t *, long, Exnode_t *, int);
231 extern Exnode_t* exnoncast(Exnode_t *);
232 extern void exclose(Expr_t *);
233
234 /** Compile an expression
235 *
236 * The callee takes ownership of the pointer \p prefix and will free it during
237 * \p expop or \p exclose.
238 *
239 * \param p Program structure to store result in
240 * \param name Filename of originating source file
241 * \param line Line number of originating source file
242 * \param fp Handle to source file
243 * \param prefix Optional program text to include ahead of the file content
244 * \return 0 on success
245 */
246 extern int excomp(Expr_t *p, const char *name, int line, FILE *fp,
247 char *prefix);
248
249 extern char* excontext(Expr_t*, char*, int);
250 extern void exdump(Expr_t *, Exnode_t *, agxbuf *);
251 #ifdef __GNUC__
252 __attribute__((format(printf, 1, 2)))
253 #endif
254 extern void exerror(const char*, ...);
255 #ifdef __GNUC__
256 __attribute__((format(printf, 1, 2)))
257 #endif
258 extern void exwarn(const char *, ...);
259 extern Extype_t exeval(Expr_t*, Exnode_t*, void*);
260 extern Exnode_t* exexpr(Expr_t*, const char*, Exid_t*, int);
261 extern void exfreenode(Expr_t*, Exnode_t*);
262 extern Exnode_t *exnewnode(Expr_t *, long, bool, long, Exnode_t *, Exnode_t *);
263 extern char* exnospace(void);
264 extern Expr_t* exopen(Exdisc_t*);
265 extern int expop(Expr_t*);
266 extern int expush(Expr_t*, const char*, int, FILE*);
267 extern int extoken_fn(Expr_t*);
268 extern char* exstring(Expr_t *, char *);
269 extern void* exstralloc(Expr_t *, size_t);
270 extern char* extype(long int);
271 extern Extype_t exzero(long int);
272 extern char *exopname(long);
273 extern void exinit(void);
274 extern char *extypename(Expr_t *p, long);
275 extern int exisAssign(Exnode_t *);
276
277 /** Construct an arena-backed string.
278 *
279 * \param vm Allocator to use.
280 * \param fmt printf-style format sting.
281 * \param ... printf-style varargs.
282 * \return Dynamically allocated string.
283 */
284 #ifdef __GNUC__
285 __attribute__((format(printf, 2, 3)))
286 #endif
287 static inline char *exprintf(arena_t *vm, const char *fmt, ...) {
288 va_list ap;
289 va_start(ap, fmt);
290
291 // how many bytes do we need for this string?
292 va_list ap2;
293 va_copy(ap2, ap);
294 int len = vsnprintf(NULL, 0, fmt, ap2);
295 assert(len >= 0 && "invalid vsnprintf call");
296 ++len; // for NUL terminator
297 va_end(ap2);
298
299 // ask arena for enough space for this
300 char *const s = gv_arena_alloc(vm, 1, (size_t)len);
301
302 // construct the output string
303 (void)vsnprintf(s, (size_t)len, fmt, ap);
304 va_end(ap);
305
306 return s;
307 }
308
309 #ifdef __cplusplus
310 }
311 #endif