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