klib
[Ported from github.com/attractivechaos/klib] added block arena
Brought by drakestapleton
klib / kexpr.h
| 1 | #ifndef KEXPR_H |
| 2 | #define KEXPR_H |
| 3 | |
| 4 | #include <stdint.h> |
| 5 | |
| 6 | struct kexpr_s; |
| 7 | typedef struct kexpr_s kexpr_t; |
| 8 | |
| 9 | // Parse errors |
| 10 | #define KEE_UNQU 0x01 // unmatched quotation marks |
| 11 | #define KEE_UNLP 0x02 // unmatched left parentheses |
| 12 | #define KEE_UNRP 0x04 // unmatched right parentheses |
| 13 | #define KEE_UNOP 0x08 // unknown operators |
| 14 | #define KEE_FUNC 0x10 // wrong function syntax |
| 15 | #define KEE_ARG 0x20 |
| 16 | #define KEE_NUM 0x40 // fail to parse a number |
| 17 | |
| 18 | // Evaluation errors |
| 19 | #define KEE_UNFUNC 0x40 // undefined function |
| 20 | #define KEE_UNVAR 0x80 // unassigned variable |
| 21 | |
| 22 | // Return type |
| 23 | #define KEV_REAL 1 |
| 24 | #define KEV_INT 2 |
| 25 | #define KEV_STR 3 |
| 26 | |
| 27 | #ifdef __cplusplus |
| 28 | extern "C" { |
| 29 | #endif |
| 30 | |
| 31 | // parse an expression and return errors in $err |
| 32 | kexpr_t *ke_parse(const char *_s, int *err); |
| 33 | |
| 34 | // free memory allocated during parsing |
| 35 | void ke_destroy(kexpr_t *ke); |
| 36 | |
| 37 | // set a variable to integer value and return the occurrence of the variable |
| 38 | int ke_set_int(kexpr_t *ke, const char *var, int64_t x); |
| 39 | |
| 40 | // set a variable to real value and return the occurrence of the variable |
| 41 | int ke_set_real(kexpr_t *ke, const char *var, double x); |
| 42 | |
| 43 | // set a variable to string value and return the occurrence of the variable |
| 44 | int ke_set_str(kexpr_t *ke, const char *var, const char *x); |
| 45 | |
| 46 | // set a user-defined function |
| 47 | int ke_set_real_func1(kexpr_t *ke, const char *name, double (*func)(double)); |
| 48 | int ke_set_real_func2(kexpr_t *ke, const char *name, double (*func)(double, double)); |
| 49 | |
| 50 | // set default math functions |
| 51 | int ke_set_default_func(kexpr_t *ke); |
| 52 | |
| 53 | // mark all variable as unset |
| 54 | void ke_unset(kexpr_t *e); |
| 55 | |
| 56 | // evaluate expression; return error code; final value is returned via pointers |
| 57 | int ke_eval(const kexpr_t *ke, int64_t *_i, double *_r, const char **_s, int *ret_type); |
| 58 | int64_t ke_eval_int(const kexpr_t *ke, int *err); |
| 59 | double ke_eval_real(const kexpr_t *ke, int *err); |
| 60 | |
| 61 | // print the expression in Reverse Polish notation (RPN) |
| 62 | void ke_print(const kexpr_t *ke); |
| 63 | |
| 64 | #ifdef __cplusplus |
| 65 | } |
| 66 | #endif |
| 67 | |
| 68 | #endif |