Graphviz 13.1.3~dev.20250829.0113
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 <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
80
81typedef union Exdata_u Exdata_t;
82typedef struct Exdisc_s Exdisc_t;
83typedef struct Exnode_s Exnode_t;
84typedef struct Expr_s Expr_t;
85typedef struct Exref_s Exref_t;
86
87typedef void (*Exerror_f) (Expr_t *, Exdisc_t *, int, const char *, ...);
88typedef void (*Exexit_f)(void *, int);
89
90typedef 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;
99 char name[EX_NAMELEN];/* symbol name */
101
102struct 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
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_
141#endif
142
143};
144
145struct Exnode_s /* expression tree node */
146{
147 long type;
148 long op;
149 bool binary;
150 union
151 {
152 double (*floating)(char**); /* FLOATING return value */
153 long long (*integer)(char **);
154 char* (*string)(char**); /* STRING return value */
155 } compiled; /* compiled function pointer */
156 Exdata_t data; /* node data */
157
158#ifdef _EX_NODE_PRIVATE_
160#endif
161
162};
163
164struct 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 */
181 /* dictionary key for external type objects */
182 Exerror_f errorf; /* error function */
184 /* get value function */
186 /* reference value function */
187 int (*setf)(Expr_t*, Exnode_t*, Exid_t*, Exref_t*, void*, Extype_t);
188 /* set value function */
210 int (*inf)(Extype_t lhs, Exid_t *rhs, Exdisc_t *disc);
211 /* exit function */
213 int* types;
214 void* user;
215};
216
217struct 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_
226#endif
227
228};
229
230extern Exnode_t *excast(Expr_t *, Exnode_t *, long, Exnode_t *, int);
232extern void exclose(Expr_t *);
233
246extern int excomp(Expr_t *p, const char *name, int line, FILE *fp,
247 char *prefix);
248
249extern char* excontext(Expr_t*, char*, int);
250extern void exdump(Expr_t *, Exnode_t *, agxbuf *);
251#ifdef __GNUC__
252__attribute__((format(printf, 1, 2)))
253#endif
254extern void exerror(const char*, ...);
255#ifdef __GNUC__
256__attribute__((format(printf, 1, 2)))
257#endif
258extern void exwarn(const char *, ...);
259extern Extype_t exeval(Expr_t*, Exnode_t*, void*);
260extern Exnode_t* exexpr(Expr_t*, const char*, Exid_t*, int);
261extern void exfreenode(Expr_t*, Exnode_t*);
262extern Exnode_t *exnewnode(Expr_t *, long, bool, long, Exnode_t *, Exnode_t *);
263extern char* exnospace(void);
264extern Expr_t* exopen(Exdisc_t*);
265extern int expop(Expr_t*);
266extern int expush(Expr_t*, const char*, int, FILE*);
267extern int extoken_fn(Expr_t*);
268extern char* exstring(Expr_t *, char *);
269extern void* exstralloc(Expr_t *, size_t);
270extern char* extype(long int);
271extern Extype_t exzero(long int);
272extern char *exopname(long);
273extern void exinit(void);
274extern char *extypename(Expr_t *p, long);
275extern int exisAssign(Exnode_t *);
276
284#ifdef __GNUC__
285__attribute__((format(printf, 2, 3)))
286#endif
287static 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
void * gv_arena_alloc(arena_t *arena, size_t alignment, size_t size)
Definition arena.c:116
Region-based memory allocator.
container data types API
#define _EX_NODE_PRIVATE_
Definition exlib.h:99
#define _EX_PROG_PRIVATE_
Definition exlib.h:102
#define _EX_DATA_PRIVATE_
Definition exlib.h:54
static Dtdisc_t disc
Definition exparse.y:209
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:1935
struct Exid_s Exid_t
char * exnospace(void)
Definition exnospace.c:25
void * exstralloc(Expr_t *, size_t)
Definition exeval.c:1978
void(* Exerror_f)(Expr_t *, Exdisc_t *, int, const char *,...)
Definition expr.h:87
void exdump(Expr_t *, Exnode_t *, agxbuf *)
Definition excc.c:629
int extoken_fn(Expr_t *)
Definition extoken.c:313
Exnode_t * exnoncast(Exnode_t *)
EX_STYPE Extype_t
Definition expr.h:79
Exnode_t * excast(Expr_t *, Exnode_t *, long, Exnode_t *, int)
int expop(Expr_t *)
char * excontext(Expr_t *, char *, int)
Definition excontext.c:27
char * exstring(Expr_t *, char *)
Definition exeval.c:1970
static char * exprintf(arena_t *vm, const char *fmt,...)
Definition expr.h:287
void exinit(void)
Exnode_t * exnewnode(Expr_t *, long, bool, long, Exnode_t *, Exnode_t *)
void(* Exexit_f)(void *, int)
Definition expr.h:88
void exfreenode(Expr_t *, Exnode_t *)
void exclose(Expr_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:38
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:136
require define api prefix
Definition gmlparse.y:17
node NULL
Definition grammar.y:181
GVIO_API const char * format
Definition gvio.h:51
Exid_t * symbols
Definition expr.h:168
Extype_t(* reff)(Expr_t *, Exnode_t *, Exid_t *, Exref_t *)
Definition expr.h:185
int * types
Definition expr.h:213
Exerror_f errorf
Definition expr.h:182
Extype_t(* keyf)(Extype_t, long)
Definition expr.h:180
int(* castf)(Expr_t *, Exnode_t *, const char *, int, Exid_t *, int, Exdisc_t *)
Definition expr.h:170
int(* convertf)(Exnode_t *, long, int)
Definition expr.h:172
int(* binaryf)(Exnode_t *, Exnode_t *, Exnode_t *, int)
Definition expr.h:174
Extype_t(* getf)(Expr_t *, Exnode_t *, Exid_t *, Exref_t *, void *, int, Exdisc_t *)
Definition expr.h:183
uint64_t version
Definition expr.h:166
int(* stringof)(Expr_t *, Exnode_t *, int)
Definition expr.h:178
int(* inf)(Extype_t lhs, Exid_t *rhs, Exdisc_t *disc)
Definition expr.h:210
Exexit_f exitf
Definition expr.h:212
char ** data
Definition expr.h:169
uint64_t flags
Definition expr.h:167
int(* setf)(Expr_t *, Exnode_t *, Exid_t *, Exref_t *, void *, Extype_t)
Definition expr.h:187
void * user
Definition expr.h:214
Extype_t(* lengthf)(Exid_t *rhs, Exdisc_t *disc)
Definition expr.h:198
Definition expr.h:91
long index_type
Definition expr.h:96
long type
Definition expr.h:95
long lex
Definition expr.h:93
char name[EX_NAMELEN]
Definition expr.h:99
long index
Definition expr.h:94
Dtlink_t link
Definition expr.h:92
void * local
user defined local stuff
Definition expr.h:98
Exnode_t * value
Definition expr.h:97
long op
operator
Definition expr.h:148
union Exnode_s::@96 compiled
long long(* integer)(char **)
INTEGER|UNSIGNED return value.
Definition expr.h:153
double(* floating)(char **)
Definition expr.h:152
long type
value type
Definition expr.h:147
bool binary
data.operand.{left,right} ok
Definition expr.h:149
Exdata_t data
Definition expr.h:156
Definition expr.h:218
arena_t vm
Definition expr.h:222
Dt_t * symbols
Definition expr.h:220
FILE * file[10]
Definition expr.h:221
const char * id
Definition expr.h:219
Exid_t * symbol
Definition expr.h:105
Exref_t * next
Definition expr.h:104
Exnode_t * index
Definition expr.h:106
Definition cdt.h:100
Exnode_t * left
Definition expr.h:120
Extype_t ** constant
Definition expr.h:128
Exnode_t * right
Definition expr.h:121
Exref_t * reference
Definition expr.h:134
Exid_t * reference
Definition expr.h:115
Exnode_t * next
Definition expr.h:127
struct Exdata_u::@94 select
struct Exdata_u::@93 operand
Exnode_t * statement
Definition expr.h:126
Exid_t * symbol
Definition expr.h:133
struct Exdata_u::@95 variable
Exnode_t * dyna
Definition expr.h:136
struct Exdata_u::@92 constant
Extype_t value
Definition expr.h:114
Exnode_t * index
Definition expr.h:135
Definition grammar.c:90