Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
expr.h
Go to the documentation of this file.
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
14extern "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 <vmalloc/vmalloc.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, 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#define exalloc(p,n) vmalloc((p)->vm, (n))
80
82
83typedef union Exdata_u Exdata_t;
84typedef struct Exdisc_s Exdisc_t;
85typedef struct Exnode_s Exnode_t;
86typedef struct Expr_s Expr_t;
87typedef struct Exref_s Exref_t;
88
89typedef void (*Exerror_f) (Expr_t *, Exdisc_t *, int, const char *, ...);
90typedef void (*Exexit_f)(void *, int);
91
92typedef struct Exid_s /* id symbol table info */
93{
94 Dtlink_t link; /* symbol table link */
95 long lex; /* lex class */
96 long index; /* user defined index */
97 long type; /* symbol and arg types */
98 long index_type; /* index type for arrays */
99 long flags; /* user defined flags */
100 Exnode_t* value; /* value */
101 void *local;
102 char name[EX_NAMELEN];/* symbol name */
104
105struct Exref_s /* . reference list */
106{
107 Exref_t* next; /* next in list */
108 Exid_t* symbol; /* reference id symbol */
109 Exnode_t* index; /* optional reference index */
110};
111
113{
114
115 struct
116 {
117 Extype_t value; /* constant value */
118 Exid_t* reference; /* conversion reference symbol */
119 } constant; /* variable reference */
120
121 struct
122 {
123 Exnode_t* left; /* left operand */
124 Exnode_t* right; /* right operand */
125 Exnode_t* last; /* for cons */
126 } operand; /* operands */
127
128 struct
129 {
130 Exnode_t* statement; /* case label statement(s) */
131 Exnode_t* next; /* next case item */
132 Extype_t** constant; /* case label constant array */
133 } select; /* case item */
134
135 struct
136 {
137 Exid_t* symbol; /* id symbol table entry */
138 Exref_t* reference; /* . reference list */
139 Exnode_t* index; /* array index expression */
140 Exnode_t* dyna; /* dynamic expression */
141 } variable; /* variable reference */
142
143#ifdef _EX_DATA_PRIVATE_
145#endif
146
147};
148
149struct Exnode_s /* expression tree node */
150{
151 long type;
152 long op;
153 bool binary;
154 union
155 {
156 double (*floating)(char**); /* FLOATING return value */
157 long long (*integer)(char **);
158 char* (*string)(char**); /* STRING return value */
159 } compiled; /* compiled function pointer */
160 Exdata_t data; /* node data */
161
162#ifdef _EX_NODE_PRIVATE_
164#endif
165
166};
167
168struct Exdisc_s /* discipline */
169{
170 uint64_t version; /* EX_VERSION */
171 uint64_t flags; /* EX_* flags */
172 Exid_t* symbols; /* static symbols */
173 char** data; /* compiled function arg data */
174 int (*castf)(Expr_t*, Exnode_t*, const char*, int, Exid_t*, int, Exdisc_t*);
175 /* unknown cast function */
176 int (*convertf)(Exnode_t *, long, int);
177 /* type conversion function */
178 int (*binaryf) (Exnode_t *, Exnode_t *, Exnode_t *, int);
179 /* binary operator function */
180 char *(*typename)(long);
181 /* application type names */
182 int (*stringof) (Expr_t *, Exnode_t *, int);
183 /* value to string conversion */
185 /* dictionary key for external type objects */
186 Exerror_f errorf; /* error function */
188 /* get value function */
190 /* reference value function */
191 int (*setf)(Expr_t*, Exnode_t*, Exid_t*, Exref_t*, void*, Extype_t);
192 /* set value function */
193 /* exit function */
195 int* types;
196 void* user;
197};
198
199struct Expr_s /* ex program state */
200{
201 const char* id; /* library id */
202 Dt_t* symbols; /* symbol table */
203 FILE* file[10]; /* io streams */
204 Vmalloc_t* vm; /* program store */
205
206#ifdef _EX_PROG_PRIVATE_
208#endif
209
210};
211
212extern Exnode_t *excast(Expr_t *, Exnode_t *, long, Exnode_t *, int);
214extern void exclose(Expr_t*, int);
215
228extern int excomp(Expr_t *p, const char *name, int line, FILE *fp,
229 char *prefix);
230
231extern char* excontext(Expr_t*, char*, int);
232extern void exdump(Expr_t *, Exnode_t *, agxbuf *);
233#ifdef __GNUC__
234__attribute__((format(printf, 1, 2)))
235#endif
236extern void exerror(const char*, ...);
237#ifdef __GNUC__
238__attribute__((format(printf, 1, 2)))
239#endif
240extern void exwarn(const char *, ...);
241extern Extype_t exeval(Expr_t*, Exnode_t*, void*);
242extern Exnode_t* exexpr(Expr_t*, const char*, Exid_t*, int);
243extern void exfreenode(Expr_t*, Exnode_t*);
244extern Exnode_t *exnewnode(Expr_t *, long, bool, long, Exnode_t *, Exnode_t *);
245extern char* exnospace(void);
246extern Expr_t* exopen(Exdisc_t*);
247extern int expop(Expr_t*);
248extern int expush(Expr_t*, const char*, int, FILE*);
249extern int extoken_fn(Expr_t*);
250extern char* exstring(Expr_t *, char *);
251extern void* exstralloc(Expr_t *, size_t);
252extern char* extype(long int);
253extern Extype_t exzero(long int);
254extern char *exopname(long);
255extern void exinit(void);
256extern char *extypename(Expr_t *p, long);
257extern int exisAssign(Exnode_t *);
258
266#ifdef __GNUC__
267__attribute__((format(printf, 2, 3)))
268#endif
269static inline char *exprintf(Vmalloc_t *vm, const char *fmt, ...) {
270 va_list ap;
271 va_start(ap, fmt);
272
273 // how many bytes do we need for this string?
274 va_list ap2;
275 va_copy(ap2, ap);
276 int len = vsnprintf(NULL, 0, fmt, ap2);
277 assert(len >= 0 && "invalid vsnprintf call");
278 ++len; // for NUL terminator
279 va_end(ap2);
280
281 // ask vmalloc for enough space for this
282 char *s = vmalloc(vm, (size_t)len);
283 if (s == NULL) {
284 va_end(ap);
285 return exnospace();
286 }
287
288 // construct the output string
289 (void)vsnprintf(s, (size_t)len, fmt, ap);
290 va_end(ap);
291
292 return s;
293}
294
295#ifdef __cplusplus
296}
297#endif
container data types API
#define _EX_NODE_PRIVATE_
Definition exlib.h:96
#define _EX_PROG_PRIVATE_
Definition exlib.h:99
#define _EX_DATA_PRIVATE_
Definition exlib.h:51
char * extype(long int)
Definition extype.c:24
int exisAssign(Exnode_t *)
#define EX_NAMELEN
Definition expr.h:51
void exerror(const char *,...)
Definition exerror.c:62
Extype_t exeval(Expr_t *, Exnode_t *, void *)
Definition exeval.c:1965
struct Exid_s Exid_t
char * exnospace(void)
Definition exnospace.c:25
void * exstralloc(Expr_t *, size_t)
Definition exeval.c:2008
void(* Exerror_f)(Expr_t *, Exdisc_t *, int, const char *,...)
Definition expr.h:89
void exdump(Expr_t *, Exnode_t *, agxbuf *)
Definition excc.c:629
int extoken_fn(Expr_t *)
Definition extoken.c:311
Exnode_t * exnoncast(Exnode_t *)
EX_STYPE Extype_t
Definition expr.h:81
Exnode_t * excast(Expr_t *, Exnode_t *, long, Exnode_t *, int)
static char * exprintf(Vmalloc_t *vm, const char *fmt,...)
Definition expr.h:269
int expop(Expr_t *)
char * excontext(Expr_t *, char *, int)
Definition excontext.c:27
char * exstring(Expr_t *, char *)
Definition exeval.c:2000
void exclose(Expr_t *, int)
void exinit(void)
Exnode_t * exnewnode(Expr_t *, long, bool, long, Exnode_t *, Exnode_t *)
void(* Exexit_f)(void *, int)
Definition expr.h:90
void exfreenode(Expr_t *, Exnode_t *)
char * extypename(Expr_t *p, long)
void exwarn(const char *,...)
Definition exerror.c:79
char * exopname(long)
Definition excc.c:41
Expr_t * exopen(Exdisc_t *)
Definition exopen.c:28
int expush(Expr_t *, const char *, int, FILE *)
int excomp(Expr_t *p, const char *name, int line, FILE *fp, char *prefix)
Extype_t exzero(long int)
Definition exzero.c:24
Exnode_t * exexpr(Expr_t *, const char *, Exid_t *, int)
Definition exexpr.c:26
static double len(glCompPoint p)
Definition glutils.c:150
require define api prefix
Definition gmlparse.y:17
node NULL
Definition grammar.y:163
GVIO_API const char * format
Definition gvio.h:51
Exid_t * symbols
Definition expr.h:172
Extype_t(* reff)(Expr_t *, Exnode_t *, Exid_t *, Exref_t *)
Definition expr.h:189
int * types
Definition expr.h:195
Exerror_f errorf
Definition expr.h:186
Extype_t(* keyf)(Extype_t, long)
Definition expr.h:184
int(* castf)(Expr_t *, Exnode_t *, const char *, int, Exid_t *, int, Exdisc_t *)
Definition expr.h:174
int(* convertf)(Exnode_t *, long, int)
Definition expr.h:176
int(* binaryf)(Exnode_t *, Exnode_t *, Exnode_t *, int)
Definition expr.h:178
Extype_t(* getf)(Expr_t *, Exnode_t *, Exid_t *, Exref_t *, void *, int, Exdisc_t *)
Definition expr.h:187
uint64_t version
Definition expr.h:170
int(* stringof)(Expr_t *, Exnode_t *, int)
Definition expr.h:182
Exexit_f exitf
Definition expr.h:194
char ** data
Definition expr.h:173
uint64_t flags
Definition expr.h:171
int(* setf)(Expr_t *, Exnode_t *, Exid_t *, Exref_t *, void *, Extype_t)
Definition expr.h:191
void * user
Definition expr.h:196
Definition expr.h:93
long flags
Definition expr.h:99
long index_type
Definition expr.h:98
long type
Definition expr.h:97
long lex
Definition expr.h:95
char name[EX_NAMELEN]
Definition expr.h:102
long index
Definition expr.h:96
Dtlink_t link
Definition expr.h:94
void * local
user defined local stuff
Definition expr.h:101
Exnode_t * value
Definition expr.h:100
long op
operator
Definition expr.h:152
long long(* integer)(char **)
INTEGER|UNSIGNED return value.
Definition expr.h:157
double(* floating)(char **)
Definition expr.h:156
long type
value type
Definition expr.h:151
bool binary
data.operand.{left,right} ok
Definition expr.h:153
union Exnode_s::@88 compiled
Exdata_t data
Definition expr.h:160
Definition expr.h:200
Dt_t * symbols
Definition expr.h:202
FILE * file[10]
Definition expr.h:203
Vmalloc_t * vm
Definition expr.h:204
const char * id
Definition expr.h:201
Exid_t * symbol
Definition expr.h:108
Exref_t * next
Definition expr.h:107
Exnode_t * index
Definition expr.h:109
Definition cdt.h:100
struct Exdata_u::@87 variable
Exnode_t * last
Definition expr.h:125
Exnode_t * left
Definition expr.h:123
Extype_t ** constant
Definition expr.h:132
struct Exdata_u::@84 constant
Exnode_t * right
Definition expr.h:124
Exref_t * reference
Definition expr.h:138
Exid_t * reference
Definition expr.h:118
Exnode_t * next
Definition expr.h:131
Exnode_t * statement
Definition expr.h:130
Exid_t * symbol
Definition expr.h:137
struct Exdata_u::@85 operand
struct Exdata_u::@86 select
Exnode_t * dyna
Definition expr.h:140
Extype_t value
Definition expr.h:117
Exnode_t * index
Definition expr.h:139
Definition grammar.c:93
void * vmalloc(Vmalloc_t *vm, size_t size)
Definition vmalloc.c:40