Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief UBF library
0003  *   Declarations for a expression evaluator.
0004  *
0005  * @file expr.h
0006  */
0007 /* -----------------------------------------------------------------------------
0008  * Enduro/X Middleware Platform for Distributed Transaction Processing
0009  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0010  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0011  * This software is released under one of the following licenses:
0012  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0013  * See LICENSE file for full text.
0014  * -----------------------------------------------------------------------------
0015  * AGPL license:
0016  *
0017  * This program is free software; you can redistribute it and/or modify it under
0018  * the terms of the GNU Affero General Public License, version 3 as published
0019  * by the Free Software Foundation;
0020  *
0021  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0022  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0023  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0024  * for more details.
0025  *
0026  * You should have received a copy of the GNU Affero General Public License along 
0027  * with this program; if not, write to the Free Software Foundation, Inc.,
0028  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0029  *
0030  * -----------------------------------------------------------------------------
0031  * A commercial use license is available from Mavimax, Ltd
0032  * contact@mavimax.com
0033  * -----------------------------------------------------------------------------
0034  */
0035 
0036 #ifndef __EXPR_H
0037 #define __EXPR_H
0038 
0039 #ifdef  __cplusplus
0040 extern "C" {
0041 #endif
0042 
0043 /*---------------------------Includes-----------------------------------*/
0044 #include <ubf.h>
0045 #include <ndrstandard.h>
0046 #include <sys/types.h>
0047 #include <regex.h>
0048 #include <ubf_int.h>
0049 #include <exhash.h>
0050 
0051 /*---------------------------Externs------------------------------------*/
0052 /*---------------------------Macros-------------------------------------*/
0053 #ifndef EXTRUE
0054 #define EXTRUE  1
0055 #endif 
0056 
0057 #ifndef EXFALSE
0058 #define EXFALSE 0
0059 #endif 
0060 
0061 #define MAX_TEXT    511
0062 /* Currently this is not supported! */
0063 #define OCC_ANY     -2
0064 #define MAX_FUNC_NAME   66      /* Maximum function name len */
0065     
0066 #define NDRX_CBFUNTYPE_NOARGS  1       /**< Callback with out arguments */
0067 #define NDRX_CBFUNTYPE_ARG1    2       /**< Callback with arg1          */
0068     
0069 typedef long (*functionPtr_t)(UBFH *p_ub, char *funcname);
0070 typedef long (*functionPtr2_t)(UBFH *p_ub, char *funcname, char *arg1);
0071 
0072 
0073 /***************** AST NODE TYPES **************/
0074 #define NODE_TYPE_OR        0
0075 #define NODE_TYPE_AND       1
0076 #define NODE_TYPE_XOR       2
0077 #define NODE_TYPE_EQOP      3
0078 #define NODE_TYPE_RELOP     4
0079 #define NODE_TYPE_ADDOP     5
0080 #define NODE_TYPE_MULTOP    6
0081 #define NODE_TYPE_UNARY     7
0082 #define NODE_TYPE_FLD       8
0083 #define NODE_TYPE_STR       9
0084 #define NODE_TYPE_FLOAT     10
0085 #define NODE_TYPE_LONG      11
0086 #define NODE_TYPE_FUNC      12
0087 
0088 /************* AST SUB-NODE TYPES **************/
0089 /* Each sub-type we will have it's own identifier! */
0090 /* Default node subtype/no subtype */
0091 #define NODE_SUB_TYPE_DEF       0
0092 /* Equality Operations subtypes */
0093 #define EQOP_EQUAL              1
0094 #define EQOP_NOT_EQUAL          2
0095 #define EQOP_REGEX_EQUAL        3
0096 #define EQOP_REGEX_NOT_EQUAL    4
0097 /* Rel OPs */
0098 #define RELOP_LESS              5
0099 #define RELOP_LESS_EQUAL        6
0100 #define RELOP_GREATER           7
0101 #define RELOP_GREATER_EQUAL     8
0102 /* Addition OPs */
0103 #define ADDOP_PLUS              9
0104 #define ADDOP_MINUS             10
0105 /* Unary operations - IDs must be above ADDOP_* */
0106 #define UNARY_CMPL              11
0107 #define UNARY_INV               12
0108 /* Multipliation OPs */
0109 #define MULOP_DOT               13
0110 #define MULOP_DIV               14
0111 #define MULOP_MOD               15
0112 /* Master Ops sub operation defs (for printing only) */
0113 #define SUB_OR_OP               16
0114 #define SUB_AND_OP              17
0115 #define SUB_XOR_OP              18
0116 /***********************************************/
0117 
0118 /* Valute types for value block */
0119 #define VALUE_TYPE_BOOL     0
0120 #define VALUE_TYPE_LONG     1
0121 #define VALUE_TYPE_FLOAT    2
0122 /* Value exracted from FLD */
0123 #define VALUE_TYPE_FLD_STR  3
0124 /* String value in ' ' in expression */
0125 #define VALUE_TYPE_STRING   4
0126 /*---------------------------Enums--------------------------------------*/
0127 /*---------------------------Typedefs-----------------------------------*/
0128 
0129 /**
0130  * Function callback argument
0131  */
0132 typedef struct {
0133     char funcname[MAX_FUNC_NAME+1]; /**< Function name */
0134     char arg1[MAX_TEXT+1]; /**< Callback argument (if any) */
0135 } ndrx_symbfunc_t;
0136 /**
0137  *  symbol table 
0138  */
0139 struct symbol 
0140 {       /* a variable name */
0141     char *name;
0142     double value;
0143 
0144     char strval[MAX_TEXT+1];
0145     ndrx_ubf_rfldid_t fld;
0146     ndrx_symbfunc_t *funccall;
0147 };
0148 
0149 /** Hash list for function callback pointers */
0150 struct func_hash {
0151     char name[MAX_FUNC_NAME+1]; /**< key (string is WITHIN the structure) */
0152     void *fptr;                 /**< Pointer to function                  */
0153     int functype;               /**< See NDRX_CBFUNTYPE_                  */
0154     EX_hash_handle hh;          /**< makes this structure hashable        */
0155 };
0156 typedef struct func_hash func_hash_t;
0157 
0158 /* nodes in the Abstract Syntax Tree */
0159 /* all have common initial nodetype */
0160 
0161 typedef struct {
0162     int compiled;
0163     regex_t re;
0164 } regex_compl_t;
0165 
0166 struct ast {
0167     int nodetype;
0168     int sub_type;
0169     int nodeid;
0170     struct ast *l;
0171     struct ast *r;
0172 };
0173 
0174 struct ast_fld {
0175     int nodetype;
0176     int sub_type;
0177     int nodeid;
0178     ndrx_ubf_rfldid_t fld;
0179 };
0180 
0181 /* Pointer to function */
0182 struct ast_func {
0183     int nodetype;
0184     int sub_type;
0185     int nodeid;
0186     func_hash_t *f;
0187     ndrx_symbfunc_t* funcall;
0188 };
0189 
0190 struct ast_string {
0191     int nodetype;
0192     int sub_type;
0193     int nodeid;
0194     /*char str[MAX_TEXT+1]; */
0195     char *str;
0196     size_t str_bufsz;
0197     regex_compl_t regex;
0198 };
0199 
0200 struct ast_long {
0201     int nodetype;
0202     int sub_type;
0203     int nodeid;
0204     long l;
0205 };
0206 
0207 struct ast_float {
0208     int nodetype;
0209     int sub_type;
0210     int nodeid;
0211     double d;
0212 };
0213 
0214 typedef struct {
0215     short dyn_alloc;
0216     short value_type;
0217     int is_null;
0218     short boolval;
0219     long longval;
0220     double floatval;
0221     /* char strval[MAX_TEXT+1]; */
0222     char *strval;
0223 } value_block_t;
0224 
0225 /*************** Dynamic list for allocated resources ***********************/
0226 struct list_node {
0227     char *mem;
0228     struct list_node *next;
0229 };
0230 /****************************************************************************/
0231 
0232 /*---------------------------Globals------------------------------------*/
0233 /*---------------------------Statics------------------------------------*/
0234 /*---------------------------Prototypes---------------------------------*/
0235 /* build an AST */
0236 struct ast *newast(int nodetype, int sub_type, struct ast *l, struct ast *r);
0237 
0238 struct ast *newfld(ndrx_ubf_rfldid_t f);
0239 struct ast *newstring(char *str);
0240 struct ast *newfloat(double d);
0241 struct ast *newlong(long l);
0242 struct ast *newfunc(ndrx_symbfunc_t *funcname);
0243 
0244 /* evaluate an AST */
0245 int eval(UBFH *p_ub, struct ast *a, value_block_t *v);
0246 
0247 /* delete and free an AST */
0248 void treefree(struct ast *);
0249 
0250 /* interface to the lexer */
0251 extern int yylineno; /* from lexer */
0252 void yyerror(char *s, ...);
0253 
0254 extern int debug;
0255 void dumpast(struct ast *a, int level);
0256 
0257 extern char * ndrx_Bboolco (char * expr);
0258 extern int ndrx_Bboolev (UBFH * p_ub, char *tree);
0259 extern double ndrx_Bfloatev (UBFH * p_ub, char *tree);
0260 extern void ndrx_Btreefree (char *tree);
0261 
0262 extern __thread struct ast *G_p_root_node;
0263 extern __thread int G_node_count;
0264 extern __thread int G_error;
0265 
0266 #ifdef  __cplusplus
0267 }
0268 #endif
0269 
0270 #endif  /* __EXPR_H */
0271 /* vim: set ts=4 sw=4 et smartindent: */