]> arthur.barton.de Git - netdata.git/blob - src/eval.h
Merge remote-tracking branch 'upstream/master' into health
[netdata.git] / src / eval.h
1 #ifndef NETDATA_EVAL_H
2 #define NETDATA_EVAL_H
3
4 typedef struct eval_variable {
5     char *name;
6     struct rrdvar *rrdvar;
7     struct eval_variable *next;
8 } EVAL_VARIABLE;
9
10 typedef struct eval_expression {
11     const char *source;
12     const char *parsed_as;
13
14     calculated_number result;
15
16     int error;
17     BUFFER *error_msg;
18
19     // hidden EVAL_NODE *
20     void *nodes;
21
22     // custom data to be used for looking up variables
23     void *data;
24 } EVAL_EXPRESSION;
25
26 #define EVAL_VALUE_INVALID 0
27 #define EVAL_VALUE_NUMBER 1
28 #define EVAL_VALUE_VARIABLE 2
29 #define EVAL_VALUE_EXPRESSION 3
30
31 #define EVAL_ERROR_OK 0
32
33 // parsing errors
34 #define EVAL_ERROR_MISSING_CLOSE_SUBEXPRESSION 1
35 #define EVAL_ERROR_UNKNOWN_OPERAND 2
36 #define EVAL_ERROR_MISSING_OPERAND 3
37 #define EVAL_ERROR_MISSING_OPERATOR 4
38 #define EVAL_ERROR_REMAINING_GARBAGE 5
39
40 // evaluation errors
41 #define EVAL_ERROR_INVALID_VALUE 11
42 #define EVAL_ERROR_INVALID_NUMBER_OF_OPERANDS 12
43 #define EVAL_ERROR_VALUE_IS_NAN 13
44 #define EVAL_ERROR_VALUE_IS_INFINITE 14
45 #define EVAL_ERROR_UNKNOWN_VARIABLE 15
46
47 // parse the given string as an expression and return:
48 //   a pointer to an expression if it parsed OK
49 //   NULL in which case the pointer to error has the error code
50 extern EVAL_EXPRESSION *expression_parse(const char *string, const char **failed_at, int *error);
51
52 // free all resources allocated for an expression
53 extern void expression_free(EVAL_EXPRESSION *op);
54
55 // convert an error code to a message
56 extern const char *expression_strerror(int error);
57
58 // evaluate an expression and return
59 // 1 = OK, the result is in: expression->result
60 // 2 = FAILED, the error message is in: buffer_tostring(expression->error_msg)
61 extern int expression_evaluate(EVAL_EXPRESSION *expression);
62
63 #endif //NETDATA_EVAL_H