1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
|
#include "common.h"
// ----------------------------------------------------------------------------
// data structures for storing the parsed expression in memory
typedef struct eval_value {
int type;
union {
calculated_number number;
EVAL_VARIABLE *variable;
struct eval_node *expression;
};
} EVAL_VALUE;
typedef struct eval_node {
int id;
unsigned char operator;
int precedence;
int count;
EVAL_VALUE ops[];
} EVAL_NODE;
// these are used for EVAL_NODE.operator
// they are used as internal IDs to identify an operator
// THEY ARE NOT USED FOR PARSING OPERATORS LIKE THAT
#define EVAL_OPERATOR_NOP '\0'
#define EVAL_OPERATOR_EXPRESSION_OPEN '('
#define EVAL_OPERATOR_EXPRESSION_CLOSE ')'
#define EVAL_OPERATOR_NOT '!'
#define EVAL_OPERATOR_PLUS '+'
#define EVAL_OPERATOR_MINUS '-'
#define EVAL_OPERATOR_AND '&'
#define EVAL_OPERATOR_OR '|'
#define EVAL_OPERATOR_GREATER_THAN_OR_EQUAL 'G'
#define EVAL_OPERATOR_LESS_THAN_OR_EQUAL 'L'
#define EVAL_OPERATOR_NOT_EQUAL '~'
#define EVAL_OPERATOR_EQUAL '='
#define EVAL_OPERATOR_LESS '<'
#define EVAL_OPERATOR_GREATER '>'
#define EVAL_OPERATOR_MULTIPLY '*'
#define EVAL_OPERATOR_DIVIDE '/'
#define EVAL_OPERATOR_SIGN_PLUS 'P'
#define EVAL_OPERATOR_SIGN_MINUS 'M'
#define EVAL_OPERATOR_ABS 'A'
#define EVAL_OPERATOR_IF_THEN_ELSE '?'
// ----------------------------------------------------------------------------
// forward function definitions
static inline void eval_node_free(EVAL_NODE *op);
static inline EVAL_NODE *parse_full_expression(const char **string, int *error);
static inline EVAL_NODE *parse_one_full_operand(const char **string, int *error);
static inline calculated_number eval_node(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error);
static inline void print_parsed_as_node(BUFFER *out, EVAL_NODE *op, int *error);
static inline void print_parsed_as_constant(BUFFER *out, calculated_number n);
// ----------------------------------------------------------------------------
// evaluation of expressions
static inline calculated_number eval_variable(EVAL_EXPRESSION *exp, EVAL_VARIABLE *v, int *error) {
static uint32_t this_hash = 0, now_hash = 0, after_hash = 0, before_hash = 0, status_hash = 0, removed_hash = 0, uninitialized_hash = 0, undefined_hash = 0, clear_hash = 0, warning_hash = 0, critical_hash = 0;
calculated_number n;
if(unlikely(this_hash == 0)) {
this_hash = simple_hash("this");
now_hash = simple_hash("now");
after_hash = simple_hash("after");
before_hash = simple_hash("before");
status_hash = simple_hash("status");
removed_hash = simple_hash("REMOVED");
uninitialized_hash = simple_hash("UNINITIALIZED");
undefined_hash = simple_hash("UNDEFINED");
clear_hash = simple_hash("CLEAR");
warning_hash = simple_hash("WARNING");
critical_hash = simple_hash("CRITICAL");
}
if(unlikely(v->hash == this_hash && !strcmp(v->name, "this"))) {
n = (exp->this)?*exp->this:NAN;
buffer_strcat(exp->error_msg, "[ $this = ");
print_parsed_as_constant(exp->error_msg, n);
buffer_strcat(exp->error_msg, " ] ");
return n;
}
if(unlikely(v->hash == after_hash && !strcmp(v->name, "after"))) {
n = (exp->after && *exp->after)?*exp->after:NAN;
buffer_strcat(exp->error_msg, "[ $after = ");
print_parsed_as_constant(exp->error_msg, n);
buffer_strcat(exp->error_msg, " ] ");
return n;
}
if(unlikely(v->hash == before_hash && !strcmp(v->name, "before"))) {
n = (exp->before && *exp->before)?*exp->before:NAN;
buffer_strcat(exp->error_msg, "[ $before = ");
print_parsed_as_constant(exp->error_msg, n);
buffer_strcat(exp->error_msg, " ] ");
return n;
}
if(unlikely(v->hash == now_hash && !strcmp(v->name, "now"))) {
n = now_realtime_sec();
buffer_strcat(exp->error_msg, "[ $now = ");
print_parsed_as_constant(exp->error_msg, n);
buffer_strcat(exp->error_msg, " ] ");
return n;
}
if(unlikely(v->hash == status_hash && !strcmp(v->name, "status"))) {
n = (exp->status)?*exp->status:RRDCALC_STATUS_UNINITIALIZED;
buffer_strcat(exp->error_msg, "[ $status = ");
print_parsed_as_constant(exp->error_msg, n);
buffer_strcat(exp->error_msg, " ] ");
return n;
}
if(unlikely(v->hash == removed_hash && !strcmp(v->name, "REMOVED"))) {
n = RRDCALC_STATUS_REMOVED;
buffer_strcat(exp->error_msg, "[ $REMOVED = ");
print_parsed_as_constant(exp->error_msg, n);
buffer_strcat(exp->error_msg, " ] ");
return n;
}
if(unlikely(v->hash == uninitialized_hash && !strcmp(v->name, "UNINITIALIZED"))) {
n = RRDCALC_STATUS_UNINITIALIZED;
buffer_strcat(exp->error_msg, "[ $UNINITIALIZED = ");
print_parsed_as_constant(exp->error_msg, n);
buffer_strcat(exp->error_msg, " ] ");
return n;
}
if(unlikely(v->hash == undefined_hash && !strcmp(v->name, "UNDEFINED"))) {
n = RRDCALC_STATUS_UNDEFINED;
buffer_strcat(exp->error_msg, "[ $UNDEFINED = ");
print_parsed_as_constant(exp->error_msg, n);
buffer_strcat(exp->error_msg, " ] ");
return n;
}
if(unlikely(v->hash == clear_hash && !strcmp(v->name, "CLEAR"))) {
n = RRDCALC_STATUS_CLEAR;
buffer_strcat(exp->error_msg, "[ $CLEAR = ");
print_parsed_as_constant(exp->error_msg, n);
buffer_strcat(exp->error_msg, " ] ");
return n;
}
if(unlikely(v->hash == warning_hash && !strcmp(v->name, "WARNING"))) {
n = RRDCALC_STATUS_WARNING;
buffer_strcat(exp->error_msg, "[ $WARNING = ");
print_parsed_as_constant(exp->error_msg, n);
buffer_strcat(exp->error_msg, " ] ");
return n;
}
if(unlikely(v->hash == critical_hash && !strcmp(v->name, "CRITICAL"))) {
n = RRDCALC_STATUS_CRITICAL;
buffer_strcat(exp->error_msg, "[ $CRITICAL = ");
print_parsed_as_constant(exp->error_msg, n);
buffer_strcat(exp->error_msg, " ] ");
return n;
}
if(exp->rrdcalc && health_variable_lookup(v->name, v->hash, exp->rrdcalc, &n)) {
buffer_sprintf(exp->error_msg, "[ ${%s} = ", v->name);
print_parsed_as_constant(exp->error_msg, n);
buffer_strcat(exp->error_msg, " ] ");
return n;
}
*error = EVAL_ERROR_UNKNOWN_VARIABLE;
buffer_sprintf(exp->error_msg, "[ undefined variable '%s' ] ", v->name);
return 0;
}
static inline calculated_number eval_value(EVAL_EXPRESSION *exp, EVAL_VALUE *v, int *error) {
calculated_number n;
switch(v->type) {
case EVAL_VALUE_EXPRESSION:
n = eval_node(exp, v->expression, error);
break;
case EVAL_VALUE_NUMBER:
n = v->number;
break;
case EVAL_VALUE_VARIABLE:
n = eval_variable(exp, v->variable, error);
break;
default:
*error = EVAL_ERROR_INVALID_VALUE;
n = 0;
break;
}
return n;
}
static inline int is_true(calculated_number n) {
if(isnan(n)) return 0;
if(isinf(n)) return 1;
if(n == 0) return 0;
return 1;
}
calculated_number eval_and(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
return is_true(eval_value(exp, &op->ops[0], error)) && is_true(eval_value(exp, &op->ops[1], error));
}
calculated_number eval_or(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
return is_true(eval_value(exp, &op->ops[0], error)) || is_true(eval_value(exp, &op->ops[1], error));
}
calculated_number eval_greater_than_or_equal(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
calculated_number n1 = eval_value(exp, &op->ops[0], error);
calculated_number n2 = eval_value(exp, &op->ops[1], error);
return isgreaterequal(n1, n2);
}
calculated_number eval_less_than_or_equal(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
calculated_number n1 = eval_value(exp, &op->ops[0], error);
calculated_number n2 = eval_value(exp, &op->ops[1], error);
return islessequal(n1, n2);
}
calculated_number eval_equal(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
calculated_number n1 = eval_value(exp, &op->ops[0], error);
calculated_number n2 = eval_value(exp, &op->ops[1], error);
if(isnan(n1) && isnan(n2)) return 1;
if(isinf(n1) && isinf(n2)) return 1;
if(isnan(n1) || isnan(n2)) return 0;
if(isinf(n1) || isinf(n2)) return 0;
return calculated_number_equal(n1, n2);
}
calculated_number eval_not_equal(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
return !eval_equal(exp, op, error);
}
calculated_number eval_less(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
calculated_number n1 = eval_value(exp, &op->ops[0], error);
calculated_number n2 = eval_value(exp, &op->ops[1], error);
return isless(n1, n2);
}
calculated_number eval_greater(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
calculated_number n1 = eval_value(exp, &op->ops[0], error);
calculated_number n2 = eval_value(exp, &op->ops[1], error);
return isgreater(n1, n2);
}
calculated_number eval_plus(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
calculated_number n1 = eval_value(exp, &op->ops[0], error);
calculated_number n2 = eval_value(exp, &op->ops[1], error);
if(isnan(n1) || isnan(n2)) return NAN;
if(isinf(n1) || isinf(n2)) return INFINITY;
return n1 + n2;
}
calculated_number eval_minus(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
calculated_number n1 = eval_value(exp, &op->ops[0], error);
calculated_number n2 = eval_value(exp, &op->ops[1], error);
if(isnan(n1) || isnan(n2)) return NAN;
if(isinf(n1) || isinf(n2)) return INFINITY;
return n1 - n2;
}
calculated_number eval_multiply(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
calculated_number n1 = eval_value(exp, &op->ops[0], error);
calculated_number n2 = eval_value(exp, &op->ops[1], error);
if(isnan(n1) || isnan(n2)) return NAN;
if(isinf(n1) || isinf(n2)) return INFINITY;
return n1 * n2;
}
calculated_number eval_divide(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
calculated_number n1 = eval_value(exp, &op->ops[0], error);
calculated_number n2 = eval_value(exp, &op->ops[1], error);
if(isnan(n1) || isnan(n2)) return NAN;
if(isinf(n1) || isinf(n2)) return INFINITY;
return n1 / n2;
}
calculated_number eval_nop(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
return eval_value(exp, &op->ops[0], error);
}
calculated_number eval_not(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
return !is_true(eval_value(exp, &op->ops[0], error));
}
calculated_number eval_sign_plus(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
return eval_value(exp, &op->ops[0], error);
}
calculated_number eval_sign_minus(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
calculated_number n1 = eval_value(exp, &op->ops[0], error);
if(isnan(n1)) return NAN;
if(isinf(n1)) return INFINITY;
return -n1;
}
calculated_number eval_abs(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
calculated_number n1 = eval_value(exp, &op->ops[0], error);
if(isnan(n1)) return NAN;
if(isinf(n1)) return INFINITY;
return abs(n1);
}
calculated_number eval_if_then_else(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
if(is_true(eval_value(exp, &op->ops[0], error)))
return eval_value(exp, &op->ops[1], error);
else
return eval_value(exp, &op->ops[2], error);
}
static struct operator {
const char *print_as;
char precedence;
char parameters;
char isfunction;
calculated_number (*eval)(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error);
} operators[256] = {
// this is a random access array
// we always access it with a known EVAL_OPERATOR_X
[EVAL_OPERATOR_AND] = { "&&", 2, 2, 0, eval_and },
[EVAL_OPERATOR_OR] = { "||", 2, 2, 0, eval_or },
[EVAL_OPERATOR_GREATER_THAN_OR_EQUAL] = { ">=", 3, 2, 0, eval_greater_than_or_equal },
[EVAL_OPERATOR_LESS_THAN_OR_EQUAL] = { "<=", 3, 2, 0, eval_less_than_or_equal },
[EVAL_OPERATOR_NOT_EQUAL] = { "!=", 3, 2, 0, eval_not_equal },
[EVAL_OPERATOR_EQUAL] = { "==", 3, 2, 0, eval_equal },
[EVAL_OPERATOR_LESS] = { "<", 3, 2, 0, eval_less },
[EVAL_OPERATOR_GREATER] = { ">", 3, 2, 0, eval_greater },
[EVAL_OPERATOR_PLUS] = { "+", 4, 2, 0, eval_plus },
[EVAL_OPERATOR_MINUS] = { "-", 4, 2, 0, eval_minus },
[EVAL_OPERATOR_MULTIPLY] = { "*", 5, 2, 0, eval_multiply },
[EVAL_OPERATOR_DIVIDE] = { "/", 5, 2, 0, eval_divide },
[EVAL_OPERATOR_NOT] = { "!", 6, 1, 0, eval_not },
[EVAL_OPERATOR_SIGN_PLUS] = { "+", 6, 1, 0, eval_sign_plus },
[EVAL_OPERATOR_SIGN_MINUS] = { "-", 6, 1, 0, eval_sign_minus },
[EVAL_OPERATOR_ABS] = { "abs(",6,1, 1, eval_abs },
[EVAL_OPERATOR_IF_THEN_ELSE] = { "?", 7, 3, 0, eval_if_then_else },
[EVAL_OPERATOR_NOP] = { NULL, 8, 1, 0, eval_nop },
[EVAL_OPERATOR_EXPRESSION_OPEN] = { NULL, 8, 1, 0, eval_nop },
// this should exist in our evaluation list
[EVAL_OPERATOR_EXPRESSION_CLOSE] = { NULL, 99, 1, 0, eval_nop }
};
#define eval_precedence(operator) (operators[(unsigned char)(operator)].precedence)
static inline calculated_number eval_node(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
if(unlikely(op->count != operators[op->operator].parameters)) {
*error = EVAL_ERROR_INVALID_NUMBER_OF_OPERANDS;
return 0;
}
calculated_number n = operators[op->operator].eval(exp, op, error);
return n;
}
// ----------------------------------------------------------------------------
// parsed-as generation
static inline void print_parsed_as_variable(BUFFER *out, EVAL_VARIABLE *v, int *error) {
(void)error;
buffer_sprintf(out, "${%s}", v->name);
}
static inline void print_parsed_as_constant(BUFFER *out, calculated_number n) {
if(unlikely(isnan(n))) {
buffer_strcat(out, "nan");
return;
}
if(unlikely(isinf(n))) {
buffer_strcat(out, "inf");
return;
}
char b[100+1], *s;
snprintfz(b, 100, CALCULATED_NUMBER_FORMAT, n);
s = &b[strlen(b) - 1];
while(s > b && *s == '0') {
*s ='\0';
s--;
}
if(s > b && *s == '.')
*s = '\0';
buffer_strcat(out, b);
}
static inline void print_parsed_as_value(BUFFER *out, EVAL_VALUE *v, int *error) {
switch(v->type) {
case EVAL_VALUE_EXPRESSION:
print_parsed_as_node(out, v->expression, error);
break;
case EVAL_VALUE_NUMBER:
print_parsed_as_constant(out, v->number);
break;
case EVAL_VALUE_VARIABLE:
print_parsed_as_variable(out, v->variable, error);
break;
default:
*error = EVAL_ERROR_INVALID_VALUE;
break;
}
}
static inline void print_parsed_as_node(BUFFER *out, EVAL_NODE *op, int *error) {
if(unlikely(op->count != operators[op->operator].parameters)) {
*error = EVAL_ERROR_INVALID_NUMBER_OF_OPERANDS;
return;
}
if(operators[op->operator].parameters == 1) {
if(operators[op->operator].print_as)
buffer_sprintf(out, "%s", operators[op->operator].print_as);
//if(op->operator == EVAL_OPERATOR_EXPRESSION_OPEN)
// buffer_strcat(out, "(");
print_parsed_as_value(out, &op->ops[0], error);
//if(op->operator == EVAL_OPERATOR_EXPRESSION_OPEN)
// buffer_strcat(out, ")");
}
else if(operators[op->operator].parameters == 2) {
buffer_strcat(out, "(");
print_parsed_as_value(out, &op->ops[0], error);
if(operators[op->operator].print_as)
buffer_sprintf(out, " %s ", operators[op->operator].print_as);
print_parsed_as_value(out, &op->ops[1], error);
buffer_strcat(out, ")");
}
else if(op->operator == EVAL_OPERATOR_IF_THEN_ELSE && operators[op->operator].parameters == 3) {
buffer_strcat(out, "(");
print_parsed_as_value(out, &op->ops[0], error);
if(operators[op->operator].print_as)
buffer_sprintf(out, " %s ", operators[op->operator].print_as);
print_parsed_as_value(out, &op->ops[1], error);
buffer_strcat(out, " : ");
print_parsed_as_value(out, &op->ops[2], error);
buffer_strcat(out, ")");
}
if(operators[op->operator].isfunction)
buffer_strcat(out, ")");
}
// ----------------------------------------------------------------------------
// parsing expressions
// skip spaces
static inline void skip_spaces(const char **string) {
const char *s = *string;
while(isspace(*s)) s++;
*string = s;
}
// what character can appear just after an operator keyword
// like NOT AND OR ?
static inline int isoperatorterm_word(const char s) {
if(isspace(s) || s == '(' || s == '$' || s == '!' || s == '-' || s == '+' || isdigit(s) || !s)
return 1;
return 0;
}
// what character can appear just after an operator symbol?
static inline int isoperatorterm_symbol(const char s) {
if(isoperatorterm_word(s) || isalpha(s))
return 1;
return 0;
}
// return 1 if the character should never appear in a variable
static inline int isvariableterm(const char s) {
if(isalnum(s) || s == '.' || s == '_')
return 0;
return 1;
}
// ----------------------------------------------------------------------------
// parse operators
static inline int parse_and(const char **string) {
const char *s = *string;
// AND
if((s[0] == 'A' || s[0] == 'a') && (s[1] == 'N' || s[1] == 'n') && (s[2] == 'D' || s[2] == 'd') && isoperatorterm_word(s[3])) {
*string = &s[4];
return 1;
}
// &&
if(s[0] == '&' && s[1] == '&' && isoperatorterm_symbol(s[2])) {
*string = &s[2];
return 1;
}
return 0;
}
static inline int parse_or(const char **string) {
const char *s = *string;
// OR
if((s[0] == 'O' || s[0] == 'o') && (s[1] == 'R' || s[1] == 'r') && isoperatorterm_word(s[2])) {
*string = &s[3];
return 1;
}
// ||
if(s[0] == '|' && s[1] == '|' && isoperatorterm_symbol(s[2])) {
*string = &s[2];
return 1;
}
return 0;
}
static inline int parse_greater_than_or_equal(const char **string) {
const char *s = *string;
// >=
if(s[0] == '>' && s[1] == '=' && isoperatorterm_symbol(s[2])) {
*string = &s[2];
return 1;
}
return 0;
}
static inline int parse_less_than_or_equal(const char **string) {
const char *s = *string;
// <=
if (s[0] == '<' && s[1] == '=' && isoperatorterm_symbol(s[2])) {
*string = &s[2];
return 1;
}
return 0;
}
static inline int parse_greater(const char **string) {
const char *s = *string;
// >
if(s[0] == '>' && isoperatorterm_symbol(s[1])) {
*string = &s[1];
return 1;
}
return 0;
}
static inline int parse_less(const char **string) {
const char *s = *string;
// <
if(s[0] == '<' && isoperatorterm_symbol(s[1])) {
*string = &s[1];
return 1;
}
return 0;
}
static inline int parse_equal(const char **string) {
const char *s = *string;
// ==
if(s[0] == '=' && s[1] == '=' && isoperatorterm_symbol(s[2])) {
*string = &s[2];
return 1;
}
// =
if(s[0] == '=' && isoperatorterm_symbol(s[1])) {
*string = &s[1];
return 1;
}
return 0;
}
static inline int parse_not_equal(const char **string) {
const char *s = *string;
// !=
if(s[0] == '!' && s[1] == '=' && isoperatorterm_symbol(s[2])) {
*string = &s[2];
return 1;
}
// <>
if(s[0] == '<' && s[1] == '>' && isoperatorterm_symbol(s[2])) {
*string = &s[2];
}
return 0;
}
static inline int parse_not(const char **string) {
const char *s = *string;
// NOT
if((s[0] == 'N' || s[0] == 'n') && (s[1] == 'O' || s[1] == 'o') && (s[2] == 'T' || s[2] == 't') && isoperatorterm_word(s[3])) {
*string = &s[3];
return 1;
}
if(s[0] == '!') {
*string = &s[1];
return 1;
}
return 0;
}
static inline int parse_multiply(const char **string) {
const char *s = *string;
// *
if(s[0] == '*' && isoperatorterm_symbol(s[1])) {
*string = &s[1];
return 1;
}
return 0;
}
static inline int parse_divide(const char **string) {
const char *s = *string;
// /
if(s[0] == '/' && isoperatorterm_symbol(s[1])) {
*string = &s[1];
return 1;
}
return 0;
}
static inline int parse_minus(const char **string) {
const char *s = *string;
// -
if(s[0] == '-' && isoperatorterm_symbol(s[1])) {
*string = &s[1];
return 1;
}
return 0;
}
static inline int parse_plus(const char **string) {
const char *s = *string;
// +
if(s[0] == '+' && isoperatorterm_symbol(s[1])) {
*string = &s[1];
return 1;
}
return 0;
}
static inline int parse_open_subexpression(const char **string) {
const char *s = *string;
// (
if(s[0] == '(') {
*string = &s[1];
return 1;
}
return 0;
}
#define parse_close_function(x) parse_close_subexpression(x)
static inline int parse_close_subexpression(const char **string) {
const char *s = *string;
// )
if(s[0] == ')') {
*string = &s[1];
return 1;
}
return 0;
}
static inline int parse_variable(const char **string, char *buffer, size_t len) {
const char *s = *string;
// $
if(*s == '$') {
size_t i = 0;
s++;
if(*s == '{') {
// ${variable_name}
s++;
while (*s && *s != '}' && i < len)
buffer[i++] = *s++;
if(*s == '}')
s++;
}
else {
// $variable_name
while (*s && !isvariableterm(*s) && i < len)
buffer[i++] = *s++;
}
buffer[i] = '\0';
if (buffer[0]) {
*string = s;
return 1;
}
}
return 0;
}
static inline int parse_constant(const char **string, calculated_number *number) {
char *end = NULL;
calculated_number n = str2ld(*string, &end);
if(unlikely(!end || *string == end)) {
*number = 0;
return 0;
}
*number = n;
*string = end;
return 1;
}
static inline int parse_abs(const char **string) {
const char *s = *string;
// ABS
if((s[0] == 'A' || s[0] == 'a') && (s[1] == 'B' || s[1] == 'b') && (s[2] == 'S' || s[2] == 's') && s[3] == '(') {
*string = &s[3];
return 1;
}
return 0;
}
static inline int parse_if_then_else(const char **string) {
const char *s = *string;
// ?
if(s[0] == '?') {
*string = &s[1];
return 1;
}
return 0;
}
static struct operator_parser {
unsigned char id;
int (*parse)(const char **);
} operator_parsers[] = {
// the order in this list is important!
// the first matching will be used
// so place the longer of overlapping ones
// at the top
{ EVAL_OPERATOR_AND, parse_and },
{ EVAL_OPERATOR_OR, parse_or },
{ EVAL_OPERATOR_GREATER_THAN_OR_EQUAL, parse_greater_than_or_equal },
{ EVAL_OPERATOR_LESS_THAN_OR_EQUAL, parse_less_than_or_equal },
{ EVAL_OPERATOR_NOT_EQUAL, parse_not_equal },
{ EVAL_OPERATOR_EQUAL, parse_equal },
{ EVAL_OPERATOR_LESS, parse_less },
{ EVAL_OPERATOR_GREATER, parse_greater },
{ EVAL_OPERATOR_PLUS, parse_plus },
{ EVAL_OPERATOR_MINUS, parse_minus },
{ EVAL_OPERATOR_MULTIPLY, parse_multiply },
{ EVAL_OPERATOR_DIVIDE, parse_divide },
{ EVAL_OPERATOR_IF_THEN_ELSE, parse_if_then_else },
/* we should not put in this list the following:
*
* - NOT
* - (
* - )
*
* these are handled in code
*/
// termination
{ EVAL_OPERATOR_NOP, NULL }
};
static inline unsigned char parse_operator(const char **string, int *precedence) {
skip_spaces(string);
int i;
for(i = 0 ; operator_parsers[i].parse != NULL ; i++)
if(operator_parsers[i].parse(string)) {
if(precedence) *precedence = eval_precedence(operator_parsers[i].id);
return operator_parsers[i].id;
}
return EVAL_OPERATOR_NOP;
}
// ----------------------------------------------------------------------------
// memory management
static inline EVAL_NODE *eval_node_alloc(int count) {
static int id = 1;
EVAL_NODE *op = callocz(1, sizeof(EVAL_NODE) + (sizeof(EVAL_VALUE) * count));
op->id = id++;
op->operator = EVAL_OPERATOR_NOP;
op->precedence = eval_precedence(EVAL_OPERATOR_NOP);
op->count = count;
return op;
}
static inline void eval_node_set_value_to_node(EVAL_NODE *op, int pos, EVAL_NODE *value) {
if(pos >= op->count)
fatal("Invalid request to set position %d of OPERAND that has only %d values", pos + 1, op->count + 1);
op->ops[pos].type = EVAL_VALUE_EXPRESSION;
op->ops[pos].expression = value;
}
static inline void eval_node_set_value_to_constant(EVAL_NODE *op, int pos, calculated_number value) {
if(pos >= op->count)
fatal("Invalid request to set position %d of OPERAND that has only %d values", pos + 1, op->count + 1);
op->ops[pos].type = EVAL_VALUE_NUMBER;
op->ops[pos].number = value;
}
static inline void eval_node_set_value_to_variable(EVAL_NODE *op, int pos, const char *variable) {
if(pos >= op->count)
fatal("Invalid request to set position %d of OPERAND that has only %d values", pos + 1, op->count + 1);
op->ops[pos].type = EVAL_VALUE_VARIABLE;
op->ops[pos].variable = callocz(1, sizeof(EVAL_VARIABLE));
op->ops[pos].variable->name = strdupz(variable);
op->ops[pos].variable->hash = simple_hash(op->ops[pos].variable->name);
}
static inline void eval_variable_free(EVAL_VARIABLE *v) {
freez(v->name);
freez(v);
}
static inline void eval_value_free(EVAL_VALUE *v) {
switch(v->type) {
case EVAL_VALUE_EXPRESSION:
eval_node_free(v->expression);
break;
case EVAL_VALUE_VARIABLE:
eval_variable_free(v->variable);
break;
default:
break;
}
}
static inline void eval_node_free(EVAL_NODE *op) {
if(op->count) {
int i;
for(i = op->count - 1; i >= 0 ;i--)
eval_value_free(&op->ops[i]);
}
freez(op);
}
// ----------------------------------------------------------------------------
// the parsing logic
// helper function to avoid allocations all over the place
static inline EVAL_NODE *parse_next_operand_given_its_operator(const char **string, unsigned char operator_type, int *error) {
EVAL_NODE *sub = parse_one_full_operand(string, error);
if(!sub) return NULL;
EVAL_NODE *op = eval_node_alloc(1);
op->operator = operator_type;
eval_node_set_value_to_node(op, 0, sub);
return op;
}
// parse a full operand, including its sign or other associative operator (e.g. NOT)
static inline EVAL_NODE *parse_one_full_operand(const char **string, int *error) {
char variable_buffer[EVAL_MAX_VARIABLE_NAME_LENGTH + 1];
EVAL_NODE *op1 = NULL;
calculated_number number;
*error = EVAL_ERROR_OK;
skip_spaces(string);
if(!(**string)) {
*error = EVAL_ERROR_MISSING_OPERAND;
return NULL;
}
if(parse_not(string)) {
op1 = parse_next_operand_given_its_operator(string, EVAL_OPERATOR_NOT, error);
op1->precedence = eval_precedence(EVAL_OPERATOR_NOT);
}
else if(parse_plus(string)) {
op1 = parse_next_operand_given_its_operator(string, EVAL_OPERATOR_SIGN_PLUS, error);
op1->precedence = eval_precedence(EVAL_OPERATOR_SIGN_PLUS);
}
else if(parse_minus(string)) {
op1 = parse_next_operand_given_its_operator(string, EVAL_OPERATOR_SIGN_MINUS, error);
op1->precedence = eval_precedence(EVAL_OPERATOR_SIGN_MINUS);
}
else if(parse_abs(string)) {
op1 = parse_next_operand_given_its_operator(string, EVAL_OPERATOR_ABS, error);
op1->precedence = eval_precedence(EVAL_OPERATOR_ABS);
}
else if(parse_open_subexpression(string)) {
EVAL_NODE *sub = parse_full_expression(string, error);
if(sub) {
op1 = eval_node_alloc(1);
op1->operator = EVAL_OPERATOR_EXPRESSION_OPEN;
op1->precedence = eval_precedence(EVAL_OPERATOR_EXPRESSION_OPEN);
eval_node_set_value_to_node(op1, 0, sub);
if(!parse_close_subexpression(string)) {
*error = EVAL_ERROR_MISSING_CLOSE_SUBEXPRESSION;
eval_node_free(op1);
return NULL;
}
}
}
else if(parse_variable(string, variable_buffer, EVAL_MAX_VARIABLE_NAME_LENGTH)) {
op1 = eval_node_alloc(1);
op1->operator = EVAL_OPERATOR_NOP;
eval_node_set_value_to_variable(op1, 0, variable_buffer);
}
else if(parse_constant(string, &number)) {
op1 = eval_node_alloc(1);
op1->operator = EVAL_OPERATOR_NOP;
eval_node_set_value_to_constant(op1, 0, number);
}
else if(**string)
*error = EVAL_ERROR_UNKNOWN_OPERAND;
else
*error = EVAL_ERROR_MISSING_OPERAND;
return op1;
}
// parse an operator and the rest of the expression
// precedence processing is handled here
static inline EVAL_NODE *parse_rest_of_expression(const char **string, int *error, EVAL_NODE *op1) {
EVAL_NODE *op2 = NULL;
unsigned char operator;
int precedence;
operator = parse_operator(string, &precedence);
skip_spaces(string);
if(operator != EVAL_OPERATOR_NOP) {
op2 = parse_one_full_operand(string, error);
if(!op2) {
// error is already reported
eval_node_free(op1);
return NULL;
}
EVAL_NODE *op = eval_node_alloc(operators[operator].parameters);
op->operator = operator;
op->precedence = precedence;
if(operator == EVAL_OPERATOR_IF_THEN_ELSE && op->count == 3) {
skip_spaces(string);
if(**string != ':') {
eval_node_free(op);
eval_node_free(op1);
eval_node_free(op2);
*error = EVAL_ERROR_IF_THEN_ELSE_MISSING_ELSE;
return NULL;
}
(*string)++;
skip_spaces(string);
EVAL_NODE *op3 = parse_one_full_operand(string, error);
if(!op3) {
eval_node_free(op);
eval_node_free(op1);
eval_node_free(op2);
// error is already reported
return NULL;
}
eval_node_set_value_to_node(op, 2, op3);
}
eval_node_set_value_to_node(op, 1, op2);
// precedence processing
// if this operator has a higher precedence compared to its next
// put the next operator on top of us (top = evaluated later)
// function recursion does the rest...
if(op->precedence > op1->precedence && op1->count == 2 && op1->operator != '(' && op1->ops[1].type == EVAL_VALUE_EXPRESSION) {
eval_node_set_value_to_node(op, 0, op1->ops[1].expression);
op1->ops[1].expression = op;
op = op1;
}
else
eval_node_set_value_to_node(op, 0, op1);
return parse_rest_of_expression(string, error, op);
}
else if(**string == ')') {
;
}
else if(**string) {
eval_node_free(op1);
op1 = NULL;
*error = EVAL_ERROR_MISSING_OPERATOR;
}
return op1;
}
// high level function to parse an expression or a sub-expression
static inline EVAL_NODE *parse_full_expression(const char **string, int *error) {
EVAL_NODE *op1 = parse_one_full_operand(string, error);
if(!op1) {
*error = EVAL_ERROR_MISSING_OPERAND;
return NULL;
}
return parse_rest_of_expression(string, error, op1);
}
// ----------------------------------------------------------------------------
// public API
int expression_evaluate(EVAL_EXPRESSION *exp) {
exp->error = EVAL_ERROR_OK;
buffer_reset(exp->error_msg);
exp->result = eval_node(exp, (EVAL_NODE *)exp->nodes, &exp->error);
if(unlikely(isnan(exp->result))) {
if(exp->error == EVAL_ERROR_OK)
exp->error = EVAL_ERROR_VALUE_IS_NAN;
}
else if(unlikely(isinf(exp->result))) {
if(exp->error == EVAL_ERROR_OK)
exp->error = EVAL_ERROR_VALUE_IS_INFINITE;
}
else if(unlikely(exp->error == EVAL_ERROR_UNKNOWN_VARIABLE)) {
// although there is an unknown variable
// the expression was evaluated successfully
exp->error = EVAL_ERROR_OK;
}
if(exp->error != EVAL_ERROR_OK) {
exp->result = NAN;
if(buffer_strlen(exp->error_msg))
buffer_strcat(exp->error_msg, "; ");
buffer_sprintf(exp->error_msg, "failed to evaluate expression with error %d (%s)", exp->error, expression_strerror(exp->error));
return 0;
}
return 1;
}
EVAL_EXPRESSION *expression_parse(const char *string, const char **failed_at, int *error) {
const char *s = string;
int err = EVAL_ERROR_OK;
EVAL_NODE *op = parse_full_expression(&s, &err);
if(*s) {
if(op) {
eval_node_free(op);
op = NULL;
}
err = EVAL_ERROR_REMAINING_GARBAGE;
}
if (failed_at) *failed_at = s;
if (error) *error = err;
if(!op) {
unsigned long pos = s - string + 1;
error("failed to parse expression '%s': %s at character %lu (i.e.: '%s').", string, expression_strerror(err), pos, s);
return NULL;
}
BUFFER *out = buffer_create(1024);
print_parsed_as_node(out, op, &err);
if(err != EVAL_ERROR_OK) {
error("failed to re-generate expression '%s' with reason: %s", string, expression_strerror(err));
eval_node_free(op);
buffer_free(out);
return NULL;
}
EVAL_EXPRESSION *exp = callocz(1, sizeof(EVAL_EXPRESSION));
exp->source = strdupz(string);
exp->parsed_as = strdupz(buffer_tostring(out));
buffer_free(out);
exp->error_msg = buffer_create(100);
exp->nodes = (void *)op;
return exp;
}
void expression_free(EVAL_EXPRESSION *exp) {
if(!exp) return;
if(exp->nodes) eval_node_free((EVAL_NODE *)exp->nodes);
freez((void *)exp->source);
freez((void *)exp->parsed_as);
buffer_free(exp->error_msg);
freez(exp);
}
const char *expression_strerror(int error) {
switch(error) {
case EVAL_ERROR_OK:
return "success";
case EVAL_ERROR_MISSING_CLOSE_SUBEXPRESSION:
return "missing closing parenthesis";
case EVAL_ERROR_UNKNOWN_OPERAND:
return "unknown operand";
case EVAL_ERROR_MISSING_OPERAND:
return "expected operand";
case EVAL_ERROR_MISSING_OPERATOR:
return "expected operator";
case EVAL_ERROR_REMAINING_GARBAGE:
return "remaining characters after expression";
case EVAL_ERROR_INVALID_VALUE:
return "invalid value structure - internal error";
case EVAL_ERROR_INVALID_NUMBER_OF_OPERANDS:
return "wrong number of operands for operation - internal error";
case EVAL_ERROR_VALUE_IS_NAN:
return "value is unset";
case EVAL_ERROR_VALUE_IS_INFINITE:
return "computed value is infinite";
case EVAL_ERROR_UNKNOWN_VARIABLE:
return "undefined variable";
case EVAL_ERROR_IF_THEN_ELSE_MISSING_ELSE:
return "missing second sub-expression of inline conditional";
default:
return "unknown error";
}
}
|