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