1 /* evaluate.h (C) 2000-2002 Kyzer/CSG. */ 2 /* Released under the terms of the GNU General Public Licence version 2. */ 3 /* http://www.kyzer.me.uk/code/evaluate/ */ 4 5 #include <stddef.h> 6 #include <stdlib.h> 7 8 #define T_INT 0 9 #define T_REAL 1 10 11 /* value */ 12 struct val { 13 long ival; /* if type = T_INT, this is the result */ 14 double rval; /* if type = T_REAL, this is the result */ 15 char type; /* either T_INT or T_REAL */ 16 }; 17 18 /* variable */ 19 struct var { 20 struct var *next; /* next variable in table or NULL */ 21 struct val val; /* value of variable */ 22 char *name; /* name of variable */ 23 }; 24 25 /* variable table */ 26 struct vartable { 27 struct var *first; /* first entry in variable table */ 28 struct memh *mh; 29 }; 30 31 /* creates a new variable table (NULL if no memory) */ 32 struct vartable *create_vartable(void); 33 34 /* frees a variable table */ 35 void free_vartable(struct vartable *vt); 36 37 /* gets a variable from a variable table (NULL if not found) */ 38 struct var *get_var(struct vartable *vt, char *name); 39 40 /* puts a variable into a variable table (NULL if no memory) */ 41 struct var *put_var(struct vartable *vt, char *name, struct val *value); 42 43 /* callbacks */ 44 typedef struct val*(*get_var_cb_t)(char*, struct val*); 45 typedef struct val*(*get_func_result_cb_t)(char*, struct val*, struct val*); 46 void eval_set_cb_get_var(get_var_cb_t cb); 47 void eval_set_cb_get_func_result(get_func_result_cb_t cb); 48 49 /* THE FUNCTION YOU WANT TO CALL */ 50 51 /* given a string to evaluate (not NULL), a result to put the answer in 52 * (not NULL) and optionally your own variable table (NULL for 'internal 53 * only' vartable), will return an error code (and result, etc) 54 */ 55 int evaluate(char *eval, struct val *result, struct vartable *variables); 56 57 /* errors */ 58 #define RESULT_OK 0 /* all OK */ 59 #define ERROR_SYNTAX 2 /* invalid expression */ 60 #define ERROR_VARNOTFOUND 3 /* variable not found */ 61 #define ERROR_FUNCNOTFOUND 4 /* function not found */ 62 #define ERROR_NOMEM 8 /* not enough memory available */ 63 #define ERROR_DIV0 9 /* division by zero */ 64 #define ERROR_BUSY 10 /* busy now */ 65 66 /* configuration */ 67 #define TOKEN_DEBUG 0 68 #define EVAL_DEBUG 0 69 #define EVAL_MALLOC 0 70 #define USE_MATH_LIB 0 71 #define MEM_DEBUG 0 72 #define MEM_LOW_FOOTPRINT 1 73 #define VAR_FROM_ENV 0 74 75