expr.h 8.0 KB

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