| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #ifndef NETDATA_EVAL_H |
| 4 | #define NETDATA_EVAL_H 1 |
| 5 | |
| 6 | #include "../libnetdata.h" |
| 7 | |
| 8 | #define EVAL_MAX_VARIABLE_NAME_LENGTH 300 |
| 9 | |
| 10 | struct eval_expression; |
| 11 | typedef struct eval_expression EVAL_EXPRESSION; |
| 12 | typedef bool (*eval_expression_variable_lookup_t)(STRING *variable, void *data, NETDATA_DOUBLE *result); |
| 13 | |
| 14 | // parsing and evaluation |
| 15 | #define EVAL_ERROR_OK 0 |
| 16 | |
| 17 | // parsing errors |
| 18 | #define EVAL_ERROR_MISSING_CLOSE_SUBEXPRESSION 1 |
| 19 | #define EVAL_ERROR_UNKNOWN_OPERAND 2 |
| 20 | #define EVAL_ERROR_MISSING_OPERAND 3 |
| 21 | #define EVAL_ERROR_MISSING_OPERATOR 4 |
| 22 | #define EVAL_ERROR_REMAINING_GARBAGE 5 |
| 23 | #define EVAL_ERROR_IF_THEN_ELSE_MISSING_ELSE 6 |
| 24 | |
| 25 | // evaluation errors |
| 26 | #define EVAL_ERROR_INVALID_VALUE 101 |
| 27 | #define EVAL_ERROR_INVALID_NUMBER_OF_OPERANDS 102 |
| 28 | #define EVAL_ERROR_VALUE_IS_NAN 103 |
| 29 | #define EVAL_ERROR_VALUE_IS_INFINITE 104 |
| 30 | #define EVAL_ERROR_UNKNOWN_VARIABLE 105 |
| 31 | |
| 32 | // parse the given string as an expression and return: |
| 33 | // a pointer to an expression if it parsed OK |
| 34 | // NULL in which case the pointer to error has the error code |
| 35 | EVAL_EXPRESSION *expression_parse(const char *string, const char **failed_at, int *error); |
| 36 | |
| 37 | // free all resources allocated for an expression |
| 38 | void expression_free(EVAL_EXPRESSION *expression); |
| 39 | |
| 40 | // convert an error code to a message |
| 41 | const char *expression_strerror(int error); |
| 42 | |
| 43 | // evaluate an expression and return |
| 44 | // 1 = OK, the result is in: expression->result |
| 45 | // 2 = FAILED, the error message is in: buffer_tostring(expression->error_msg) |
| 46 | int expression_evaluate(EVAL_EXPRESSION *expression); |
| 47 | |
| 48 | const char *expression_source(EVAL_EXPRESSION *expression); |
| 49 | const char *expression_parsed_as(EVAL_EXPRESSION *expression); |
| 50 | const char *expression_error_msg(EVAL_EXPRESSION *expression); |
| 51 | NETDATA_DOUBLE expression_result(EVAL_EXPRESSION *expression); |
| 52 | void expression_set_variable_lookup_callback(EVAL_EXPRESSION *expression, eval_expression_variable_lookup_t cb, void *data); |
| 53 | |
| 54 | void expression_hardcode_variable(EVAL_EXPRESSION *expression, STRING *variable, NETDATA_DOUBLE value); |
| 55 | |
| 56 | #endif //NETDATA_EVAL_H |