-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathparser.c
1572 lines (1348 loc) · 38.7 KB
/
parser.c
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
/*
Copyright (C) 2011 ezQuake team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
\file
\brief
Arithmetic expression evaluator
\author johnnycz
**/
// fixme: most probably there are some memory leaks when working with strings
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <wctype.h>
#ifndef snprintf
#include "q_shared.h"
#endif
#include "utils.h"
#include "pcre.h"
#include "parser.h"
#define GLOBAL /* */
#define LOCAL static
#define REAL_COMPARE_EPSILON 0.00000001
#define MAX_NUMBER_LENGTH 32
#define BOOL_TRUE 1
#define BOOL_FALSE 0
#define ERR_INVALID_TOKEN 1
#define ERR_UNEXPECTED_CHAR 2
#define ERR_TYPE_MISMATCH 3
#define ERR_ZERO_DIV 4
#define ERR_NOTIMPL 5
#define ERR_MALFORMED_NUM 6
#define ERR_OUT_OF_MEM 7
#define ERR_INTERNAL 8
#define ERR_REGEXP 9
#define ERR_INVALID_ARG 10
//
// Tokens
//
#define TK_EOF 0
#define TK_INVALID 1
// double, integer
#define TK_DOUBLE 100
#define TK_INTEGER 101
// +, -
#define TK_PLUS 200
#define TK_MINUS 201
// *, /, mod
#define TK_ASTERISK 250
#define TK_SLASH 251
#define TK_MOD 252
#define TK_XOR 253
#define TK_DIV 254
// parentheses
#define TK_BR_O 300
#define TK_BR_C 301
// variable
#define TK_VAR 500
#define TK_STR 501
// comparisions
#define TK_LT 600
#define TK_LE 601
#define TK_EQ 602
#define TK_EQ2 603
#define TK_GE 604
#define TK_GT 605
#define TK_NE 606
#define TK_ISIN 607
#define TK_NISIN 608
#define TK_REEQ 609
#define TK_RENE 610
// logic
#define TK_AND 700
#define TK_OR 701
// string
#define TK_STRLEN 800
#define TK_INT 801
#define TK_SUBSTR 802
#define TK_POS 804
#define TK_TOBROWN 805
#define TK_TOWHITE 806
// misc
#define TK_COMMA 900
typedef struct {
const char *string; // input string
int pos; // current reading position in the input string
int lookahead; // next token in the string
int error; // first error
int warning; // last warning
variable_val_fnc varfnc; // pointer to a function returning variable values
subpatterns_report_fnc re_patfnc;
// a function where we report regexp-found subpatterns
} expr_parser_t, *EParser;
#define CURCHAR(p) (p->string[p->pos])
LOCAL void SetError(EParser p, int error)
{
if (p->error == EXPR_EVAL_SUCCESS)
{
p->error = error;
} // else keep the previous error
}
LOCAL void FreeIfStr(const expr_val *e)
{
if (e->type == ET_STR) {
free(e->s_val);
}
}
LOCAL double Get_Double(const expr_val e)
{
switch (e.type) {
case ET_INT: return e.i_val;
case ET_DBL: return e.d_val;
case ET_BOOL: return e.b_val;
case ET_STR: default: return 0;
}
}
LOCAL int Get_Integer(const expr_val e)
{
switch (e.type) {
case ET_INT: return e.i_val;
case ET_DBL: return (int) e.d_val;
case ET_BOOL:return e.b_val;
case ET_STR: default: return 0;
}
}
LOCAL int Get_Bool(const expr_val e)
{
switch (e.type) {
case ET_INT: return e.i_val;
case ET_DBL: return (int) e.d_val;
case ET_BOOL:return e.b_val;
case ET_STR: {
int retval = (*e.s_val) ? BOOL_TRUE : BOOL_FALSE;
free(e.s_val);
return retval;
}
default: assert(false); return 0;
}
}
GLOBAL expr_val Get_Expr_Double(double v)
{
expr_val t = {0};
t.type = ET_DBL;
t.d_val = v;
return t;
}
GLOBAL expr_val Get_Expr_String(const char *v)
{
expr_val t = {0};
t.type = ET_STR;
t.s_val = strdup(v);
return t;
}
GLOBAL expr_val Get_Expr_Integer(int v)
{
expr_val t = {0};
t.type = ET_INT;
t.i_val = v;
return t;
}
GLOBAL expr_val Get_Expr_Bool(int v)
{
expr_val t = {0};
t.type = ET_BOOL;
t.b_val = v ? BOOL_TRUE : BOOL_FALSE;
return t;
}
GLOBAL expr_val Get_Expr_Dummy(void)
{
expr_val t = {0};
t.type = ET_INT;
t.i_val = 0;
return t;
}
LOCAL int Compare_Double(double a, double b)
{
double diff = fabs(a-b);
if (diff < REAL_COMPARE_EPSILON)
return 0; // a == b
else if (a > b)
return 1;
else
return -1;
}
LOCAL expr_val ToString(EParser p, const expr_val e)
{
expr_val r = {0};
switch (e.type) {
case ET_STR: r = e; break;
case ET_INT:
r.s_val = (char *) malloc(MAX_NUMBER_LENGTH);
if (!r.s_val) { SetError(p, ERR_OUT_OF_MEM); return Get_Expr_Dummy(); }
snprintf(r.s_val, MAX_NUMBER_LENGTH, "%i", e.i_val);
break;
case ET_DBL:
r.s_val = (char *) malloc(MAX_NUMBER_LENGTH);
if (!r.s_val) { SetError(p, ERR_OUT_OF_MEM); return Get_Expr_Dummy(); }
snprintf(r.s_val, MAX_NUMBER_LENGTH, "%f", e.d_val);
break;
case ET_BOOL:
r.s_val = (char *) malloc(6);
if (!r.s_val) { SetError(p, ERR_OUT_OF_MEM); return Get_Expr_Dummy(); }
snprintf(r.s_val, 6, "%s", e.b_val ? "true" : "false");
break;
}
return r;
}
LOCAL expr_val Concat(EParser p, const expr_val e1, const expr_val e2)
{
size_t len;
expr_val ret = {0};
if (e1.type != ET_STR || e2.type != ET_STR) {
SetError(p, ERR_INTERNAL);
return Get_Expr_Dummy();
}
len = strlen(e1.s_val) + strlen(e2.s_val) + 1;
ret.type = ET_STR;
ret.s_val = malloc(len);
if (!ret.s_val) {
SetError(p, ERR_OUT_OF_MEM);
FreeIfStr(&e1);
FreeIfStr(&e2);
return Get_Expr_Dummy();
}
strlcpy(ret.s_val, e1.s_val, len);
strlcat(ret.s_val, e2.s_val, len);
free(e1.s_val);
free(e2.s_val);
return ret;
}
LOCAL expr_val operator_plus(EParser p, const expr_val e1, const expr_val e2)
{
expr_val ret = {0};
switch (e1.type) {
case ET_INT: switch (e2.type) {
case ET_INT: ret.type = ET_INT; ret.i_val = e1.i_val + e2.i_val; break;
case ET_DBL: ret.type = ET_DBL; ret.d_val = e1.i_val + e2.d_val; break;
case ET_BOOL: ret.type = ET_INT; ret.i_val = e1.i_val + e2.b_val; break;
case ET_STR: SetError(p, ERR_TYPE_MISMATCH); break;
} break;
case ET_DBL: switch (e2.type) {
case ET_INT: ret.type = ET_DBL; ret.d_val = e1.d_val + e2.i_val; break;
case ET_DBL: ret.type = ET_DBL; ret.d_val = e1.d_val + e2.d_val; break;
case ET_BOOL: ret.type = ET_DBL; ret.d_val = e1.d_val + e2.b_val; break;
case ET_STR: SetError(p, ERR_TYPE_MISMATCH); break;
} break;
case ET_BOOL: switch (e2.type) {
case ET_INT: ret.type = ET_INT; ret.i_val = e1.b_val + e2.i_val; break;
case ET_DBL: ret.type = ET_DBL; ret.d_val = e1.b_val + e2.d_val; break;
case ET_BOOL: ret.type = ET_BOOL;ret.b_val = e1.b_val + e2.b_val; break;
case ET_STR: SetError(p, ERR_TYPE_MISMATCH); break;
} break;
case ET_STR: switch (e2.type) {
case ET_STR: ret = Concat(p, e1, e2); break;
default: SetError(p, ERR_TYPE_MISMATCH); break;
} break;
}
return ret;
}
LOCAL expr_val operator_minus (EParser p, const expr_val e1)
{
expr_val ret = {0};
switch (e1.type) {
case ET_INT: ret.type = ET_INT; ret.i_val = -e1.i_val; break;
case ET_DBL: ret.type = ET_DBL; ret.d_val = -e1.d_val; break;
case ET_BOOL: ret.type = ET_BOOL; ret.b_val = -e1.b_val; break;
case ET_STR: SetError(p, ERR_TYPE_MISMATCH); break;
}
return ret;
}
LOCAL expr_val operator_multiply(EParser p, const expr_val e1, const expr_val e2)
{
expr_val ret = {0};
switch (e1.type) {
case ET_INT: switch (e2.type) {
case ET_INT: ret.type = ET_INT; ret.i_val = e1.i_val * e2.i_val; break;
case ET_DBL: ret.type = ET_DBL; ret.d_val = e1.i_val * e2.d_val; break;
case ET_BOOL:ret.type = ET_BOOL;ret.b_val = e1.i_val * e2.b_val; break;
case ET_STR: SetError(p, ERR_TYPE_MISMATCH); break;
} break;
case ET_DBL: switch (e2.type) {
case ET_INT: ret.type = ET_DBL; ret.d_val = e1.d_val * e2.i_val; break;
case ET_DBL: ret.type = ET_DBL; ret.d_val = e1.d_val * e2.d_val; break;
case ET_BOOL:ret.type = ET_DBL; ret.d_val = e1.d_val * e2.b_val; break;
case ET_STR: SetError(p, ERR_TYPE_MISMATCH); break;
} break;
case ET_BOOL: switch (e2.type) {
case ET_INT: ret.type = ET_INT; ret.i_val = e1.b_val * e2.i_val; break;
case ET_DBL: ret.type = ET_DBL; ret.d_val = e1.b_val * e2.d_val; break;
case ET_BOOL:ret.type = ET_BOOL;ret.b_val = e1.b_val * e2.b_val; break;
case ET_STR: SetError(p, ERR_TYPE_MISMATCH); break;
} break;
case ET_STR: default: SetError(p, ERR_TYPE_MISMATCH); break;
}
return ret;
}
LOCAL expr_val operator_modulo(EParser p, const expr_val e1, const expr_val e2)
{
expr_val ret;
if (e1.type == ET_INT && e2.type == ET_INT) {
ret.type = ET_INT;
ret.i_val = e1.i_val % e2.i_val;
}
else {
SetError(p, ERR_TYPE_MISMATCH);
ret = Get_Expr_Dummy();
}
return ret;
}
LOCAL expr_val operator_xor(EParser p, const expr_val e1, const expr_val e2)
{
expr_val ret;
if (e1.type == ET_INT && e2.type == ET_INT) {
ret.type = ET_INT;
ret.i_val = e1.i_val ^ e2.i_val;
}
else if (e1.type == ET_BOOL && e2.type == ET_BOOL) {
ret.type = ET_BOOL;
ret.b_val = ((e1.b_val ? 1 : 0) ^ (e2.b_val ? 1 : 0)) ? BOOL_TRUE : BOOL_FALSE;
}
else {
SetError(p, ERR_TYPE_MISMATCH);
ret = Get_Expr_Dummy();
}
return ret;
}
LOCAL expr_val operator_divint(EParser p, const expr_val e1, const expr_val e2)
{
expr_val ret;
if (e1.type == ET_INT && e2.type == ET_INT) {
ret.type = ET_INT;
ret.i_val = e1.i_val / e2.i_val;
}
else {
SetError(p, ERR_TYPE_MISMATCH);
ret = Get_Expr_Dummy();
}
return ret;
}
LOCAL expr_val operator_divide(EParser p, const expr_val e1)
{
double d;
expr_val ret;
switch (e1.type) {
case ET_INT: d = e1.i_val; break;
case ET_DBL: d = e1.d_val; break;
case ET_BOOL: d = e1.b_val; break;
case ET_STR:
default:
SetError(p, ERR_TYPE_MISMATCH);
return e1;
}
if (d) {
ret.type = ET_DBL;
ret.d_val = 1/d;
} else {
SetError(p, ERR_ZERO_DIV);
ret = Get_Expr_Dummy();
}
return ret;
}
LOCAL expr_val operator_strlen(EParser p, const expr_val arg1)
{
expr_val ret;
ret.type = ET_INT;
if (arg1.type == ET_STR) {
ret.i_val = strlen(arg1.s_val);
free(arg1.s_val);
}
else {
SetError(p, ERR_TYPE_MISMATCH);
}
return ret;
}
LOCAL expr_val operator_substr(EParser p, const expr_val arg1, const expr_val arg2,
const expr_val arg3)
{
expr_val ret;
if (arg1.type == ET_STR && arg2.type == ET_INT && arg3.type == ET_INT) {
const char *str = arg1.s_val;
size_t arglen = strlen(str);
int pos = arg2.i_val;
int len = arg3.i_val;
if (pos >= 0 && len >= 0) {
if (len == 0 || pos >= arglen) {
ret.type = ET_STR;
ret.s_val = strdup("");
}
else { // len > 0 && pos < arglen
char *buf;
if (pos + len > arglen) {
len = arglen - pos;
}
buf = (char *) malloc(len + 1);
if (buf) {
strlcpy(buf, str + pos, len + 1);
ret.type = ET_STR;
ret.s_val = buf;
}
else { // malloc fail
SetError(p, ERR_OUT_OF_MEM);
ret = Get_Expr_Dummy();
}
}
}
else { // pos < 0 || len < 0
SetError(p, ERR_INVALID_ARG);
ret = Get_Expr_Dummy();
}
}
else { // args are not string, int, int
SetError(p, ERR_TYPE_MISMATCH);
ret = Get_Expr_Dummy();
}
FreeIfStr(&arg1);
FreeIfStr(&arg2);
FreeIfStr(&arg3);
return ret;
}
LOCAL expr_val operator_pos(EParser p, const expr_val arg1, const expr_val arg2)
{
expr_val ret;
if (arg1.type == ET_STR && arg2.type == ET_STR) {
char *haystack = arg1.s_val;
char *needle = arg2.s_val;
char *find = strstr(haystack, needle);
ret.type = ET_INT;
if (find) {
ret.i_val = (int) (find - arg1.s_val);
}
else {
ret.i_val = -1;
}
free(arg1.s_val);
free(arg2.s_val);
}
else {
SetError(p, ERR_TYPE_MISMATCH);
FreeIfStr(&arg1);
FreeIfStr(&arg2);
ret = Get_Expr_Dummy();
}
return ret;
}
LOCAL expr_val operator_int(EParser p, const expr_val arg1)
{
expr_val ret;
ret.type = ET_INT;
switch (arg1.type) {
case ET_INT:
ret.i_val = arg1.i_val;
break;
case ET_DBL:
ret.i_val = (int) arg1.d_val;
break;
case ET_STR:
ret.i_val = atoi(arg1.s_val);
free(arg1.s_val);
break;
case ET_BOOL:
ret.i_val = arg1.b_val ? 1 : 0;
break;
}
return ret;
}
LOCAL expr_val operator_tobrown(EParser p, const expr_val arg1)
{
expr_val ret;
if (arg1.type == ET_STR) {
ret.s_val = strdup(arg1.s_val);
ret.type = ET_STR;
CharsToBrown(ret.s_val, ret.s_val + strlen(ret.s_val));
}
else {
SetError(p, ERR_TYPE_MISMATCH);
ret = Get_Expr_Dummy();
}
FreeIfStr(&arg1);
return ret;
}
LOCAL expr_val operator_towhite(EParser p, const expr_val arg1)
{
expr_val ret;
if (arg1.type == ET_STR) {
ret.s_val = strdup(arg1.s_val);
ret.type = ET_STR;
CharsToWhite(ret.s_val, ret.s_val + strlen(ret.s_val));
}
else {
SetError(p, ERR_TYPE_MISMATCH);
ret = Get_Expr_Dummy();
}
FreeIfStr(&arg1);
return ret;
}
typedef enum {
CMPRESULT_lt,
CMPRESULT_le,
CMPRESULT_eq,
CMPRESULT_ne,
CMPRESULT_ge,
CMPRESULT_gt
} cmp_type;
// this will be called if one of the expressions is string
// and we want to do a comparision on them
LOCAL expr_val string_check_cmp(EParser p, const expr_val e1, const expr_val e2, cmp_type ctype)
{
expr_val r = {0};
r.type = ET_BOOL;
r.b_val = BOOL_FALSE;
if (e1.type != ET_STR && e2.type != ET_STR) {
SetError(p, ERR_INTERNAL);
} else if (e1.type != ET_STR) {
free(e2.s_val);
SetError(p, ERR_TYPE_MISMATCH);
} else if (e2.type != ET_STR) {
free(e1.s_val);
SetError(p, ERR_TYPE_MISMATCH);
} else { // both STR
int a = strcmp(e1.s_val, e2.s_val);
switch(ctype) {
case CMPRESULT_lt: r.b_val = a < 0; break;
case CMPRESULT_le: r.b_val = a <= 0; break;
case CMPRESULT_eq: r.b_val = a == 0; break;
case CMPRESULT_ne: r.b_val = a != 0; break;
case CMPRESULT_ge: r.b_val = a >= 0; break;
case CMPRESULT_gt: r.b_val = a > 0; break;
}
free(e1.s_val);
free(e2.s_val);
}
return r;
}
// test: "1==1.0", "2==2.0", "5/2==2.5"
LOCAL expr_val operator_eq(EParser p, const expr_val e1, const expr_val e2)
{
expr_val ret = {0};
ret.type = ET_BOOL;
if (e1.type == ET_STR || e2.type == ET_STR)
return string_check_cmp(p, e1, e2, CMPRESULT_eq);
switch(e1.type) {
case ET_INT: switch (e2.type) {
case ET_INT: ret.b_val = e1.i_val == e2.i_val; break;
case ET_DBL: ret.b_val = !Compare_Double(e1.i_val, e2.d_val); break;
case ET_BOOL: ret.b_val = e2.b_val ? e1.i_val == 1 : e1.i_val == 0; break;
// handled above, avoid gcc warning
case ET_STR: break;
} break;
case ET_DBL: switch (e2.type) {
case ET_INT: ret.b_val = !Compare_Double(e1.d_val, e2.i_val); break;
case ET_DBL: ret.b_val = !Compare_Double(e1.d_val, e2.d_val); break;
case ET_BOOL: SetError(p, ERR_TYPE_MISMATCH); break;
// handled above, avoid gcc warning
case ET_STR: break;
} break;
case ET_BOOL: switch (e2.type) {
case ET_INT: ret.b_val = e1.b_val ? e2.i_val == 1 : e2.i_val == 0; break;
case ET_DBL: SetError(p, ERR_TYPE_MISMATCH); break;
case ET_BOOL: ret.b_val = e1.b_val == e2.b_val; break;
// handled above, avoid gcc warning
case ET_STR: break;
} break;
// handled above, avoid gcc warning
case ET_STR: break;
}
return ret;
}
// test: "1<1.0", ...
LOCAL expr_val operator_lt(EParser p, const expr_val e1, const expr_val e2)
{
expr_val ret = {0};
ret.type = ET_BOOL;
if (e1.type == ET_STR || e2.type == ET_STR)
return string_check_cmp(p, e1, e2, CMPRESULT_lt);
switch(e1.type) {
case ET_INT: switch (e2.type) {
case ET_INT: ret.b_val = e1.i_val < e2.i_val; break;
case ET_DBL: ret.b_val = Compare_Double(e1.i_val, e2.d_val) == -1; break;
case ET_BOOL: SetError(p, ERR_TYPE_MISMATCH); break;
// handled above, avoid gcc warning
case ET_STR: break;
} break;
case ET_DBL: switch (e2.type) {
case ET_INT: ret.b_val = Compare_Double(e1.d_val, e2.i_val) == -1; break;
case ET_DBL: ret.b_val = Compare_Double(e1.d_val, e2.d_val) == -1; break;
case ET_BOOL: SetError(p, ERR_TYPE_MISMATCH); break;
// handled above, avoid gcc warning
case ET_STR: break;
} break;
case ET_BOOL: switch (e2.type) {
case ET_BOOL: ret.b_val = !e1.b_val && e2.b_val; break;
default: SetError(p, ERR_TYPE_MISMATCH); break;
} break;
// handled above, avoid gcc warning
case ET_STR: break;
}
return ret;
}
// this obscurity here just defines operators "<=", ">", "!=" and ">="
// by using operators "<" and "=="
#define LT_VAL() (operator_lt(p,e1,e2).b_val)
#define EQ_VAL() (operator_eq(p,e1,e2).b_val)
#define ADDOP(name,cond) \
LOCAL expr_val operator_##name (EParser p, const expr_val e1, const expr_val e2) { \
expr_val ret = {0}; ret.type = ET_BOOL; \
if (e1.type == ET_STR || e2.type == ET_STR) \
return string_check_cmp(p, e1, e2, CMPRESULT_##name); \
else ret.b_val = (cond); return ret; \
}
ADDOP(le, LT_VAL() || EQ_VAL());
ADDOP(gt, !LT_VAL() && !EQ_VAL());
ADDOP(ne, !EQ_VAL());
ADDOP(ge, !LT_VAL());
#undef ADDOP
#undef LT_VAL
#undef EQ_VAL
LOCAL expr_val operator_isin(EParser p, const expr_val e1, const expr_val e2)
{
expr_val s1 = ToString(p, e1);
expr_val s2 = ToString(p, e2);
expr_val r = {0}; r.type = ET_BOOL;
if (!(*s1.s_val)) {
r.b_val = BOOL_FALSE;
} else {
r.b_val = strstr(s2.s_val, s1.s_val) ? BOOL_TRUE : BOOL_FALSE;
}
free(s1.s_val);
free(s2.s_val);
return r;
}
LOCAL expr_val operator_nisin(EParser p, const expr_val e1, const expr_val e2)
{
expr_val r = operator_isin(p, e1, e2);
r.b_val = !r.b_val;
return r;
}
LOCAL expr_val operator_reeq(EParser p, const expr_val e1, const expr_val e2)
{
expr_val r = {0};
expr_val strr = ToString(p, e1);
expr_val mask = ToString(p, e2);
// this makes sense for "111 =~ 1.1", however we are doing str->double->str conversion
// and it can happen that the result string won't be the same as the source
pcre* regexp;
const char *error;
int error_offset;
int rc;
int offsets[99];
r.type = ET_BOOL;
regexp = pcre_compile (mask.s_val, 0, &error, &error_offset, NULL);
if (!regexp) {
SetError(p, ERR_REGEXP);
return Get_Expr_Dummy();
}
rc = pcre_exec (regexp, NULL, strr.s_val, strlen(strr.s_val),
0, 0, offsets, 99);
if (rc >= 0) {
if (p->re_patfnc)
p->re_patfnc(strr.s_val, offsets, rc > 99 ? 99 : rc);
r.b_val = BOOL_TRUE;
} else
r.b_val = BOOL_FALSE;
free(e1.s_val);
free(e2.s_val);
pcre_free (regexp);
return r;
}
LOCAL expr_val operator_rene(EParser p, const expr_val e1, const expr_val e2)
{
expr_val r = operator_reeq(p, e1, e2);
r.b_val = !r.b_val;
return r;
}
LOCAL expr_val operator_and(EParser p, const expr_val e1, const expr_val e2)
{
expr_val ret = {0};
ret.type = ET_BOOL;
switch (e1.type) {
case ET_INT: switch (e2.type) {
case ET_INT: ret.b_val = e1.i_val && e2.i_val; break;
case ET_DBL: ret.b_val = e1.i_val && e2.d_val; break;
case ET_BOOL: ret.b_val = e1.i_val && e2.b_val; break;
case ET_STR: ret.i_val = e1.i_val && *e2.s_val; break;
} break;
case ET_DBL: switch (e2.type) {
case ET_INT: ret.b_val = e1.d_val && e2.i_val; break;
case ET_DBL: ret.b_val = e1.d_val && e2.d_val; break;
case ET_BOOL: ret.b_val = e1.d_val && e2.b_val; break;
case ET_STR: ret.b_val = e1.d_val && *e2.s_val; break;
} break;
case ET_BOOL: switch (e2.type) {
case ET_INT: ret.b_val = e1.b_val && e2.i_val; break;
case ET_DBL: ret.b_val = e1.b_val && e2.d_val; break;
case ET_BOOL: ret.b_val = e1.b_val && e2.b_val; break;
case ET_STR: ret.b_val = e1.b_val && *e2.s_val; break;
} break;
case ET_STR: switch (e2.type) {
case ET_INT: ret.b_val = *e1.s_val && e2.i_val; break;
case ET_DBL: ret.b_val = *e1.s_val && e2.d_val; break;
case ET_BOOL: ret.b_val = *e1.s_val && e2.b_val; break;
case ET_STR: ret.b_val = *e1.s_val && *e2.s_val; break;
} break;
}
FreeIfStr(&e1);
FreeIfStr(&e2);
return ret;
}
// a copy-paste of operator_and, I couldn't think of better way, limited C :(
LOCAL expr_val operator_or(EParser p, const expr_val e1, const expr_val e2)
{
expr_val ret = {0};
ret.type = ET_BOOL;
switch (e1.type) {
case ET_INT: switch (e2.type) {
case ET_INT: ret.b_val = e1.i_val || e2.i_val; break;
case ET_DBL: ret.b_val = e1.i_val || e2.d_val; break;
case ET_BOOL: ret.b_val = e1.i_val || e2.b_val; break;
case ET_STR: ret.i_val = e1.i_val || *e2.s_val; break;
} break;
case ET_DBL: switch (e2.type) {
case ET_INT: ret.b_val = e1.d_val || e2.i_val; break;
case ET_DBL: ret.b_val = e1.d_val || e2.d_val; break;
case ET_BOOL: ret.b_val = e1.d_val || e2.b_val; break;
case ET_STR: ret.b_val = e1.d_val || *e2.s_val; break;
} break;
case ET_BOOL: switch (e2.type) {
case ET_INT: ret.b_val = e1.b_val || e2.i_val; break;
case ET_DBL: ret.b_val = e1.b_val || e2.d_val; break;
case ET_BOOL: ret.b_val = e1.b_val || e2.b_val; break;
case ET_STR: ret.b_val = e1.b_val || *e2.s_val; break;
} break;
case ET_STR: switch (e2.type) {
case ET_INT: ret.b_val = *e1.s_val || e2.i_val; break;
case ET_DBL: ret.b_val = *e1.s_val || e2.d_val; break;
case ET_BOOL: ret.b_val = *e1.s_val || e2.b_val; break;
case ET_STR: ret.b_val = *e1.s_val || *e2.s_val; break;
} break;
}
FreeIfStr(&e1);
FreeIfStr(&e2);
return ret;
}
// does the input string contain str as the very first string?
LOCAL int Follows(EParser p, const char *str)
{
const char* c1 = p->string + p->pos;
const char* c2 = str;
while (*c1 && *c2 && *c1 == *c2) { c1++; c2++; }
if (!*c2) return 1;
else return 0;
}
// update the lookahead based on what we see next in the input string
LOCAL void Next_Token(EParser p)
{
char c;
while(c = CURCHAR(p), c && c == ' ') p->pos++;
if (!c) { p->lookahead = TK_EOF; }
else if (Follows(p, "isin")) { p->lookahead = TK_ISIN; }
else if (Follows(p, "!isin")) { p->lookahead = TK_NISIN; }
else if (Follows(p, "mod")) { p->lookahead = TK_MOD; }
else if (Follows(p, "xor")) { p->lookahead = TK_XOR; }
else if (Follows(p, "div")) { p->lookahead = TK_DIV; }
else if (Follows(p, "and") || Follows(p, "AND") || Follows(p, "&&"))
{ p->lookahead = TK_AND; }
else if (Follows(p, "or") || Follows(p, "OR") || Follows(p, "||"))
{ p->lookahead = TK_OR; }
else if (Follows(p, "strlen")) { p->lookahead = TK_STRLEN; }
else if (Follows(p, "int")) { p->lookahead = TK_INT; }
else if (Follows(p, "substr")) { p->lookahead = TK_SUBSTR; }
else if (Follows(p, "pos")) { p->lookahead = TK_POS; }
else if (Follows(p, "tobrown")) { p->lookahead = TK_TOBROWN; }
else if (Follows(p, "towhite")) { p->lookahead = TK_TOWHITE; }
else if (Follows(p, "<=")) { p->lookahead = TK_LE; }
else if (Follows(p, "==")) { p->lookahead = TK_EQ2; }
else if (Follows(p, "!=")) { p->lookahead = TK_NE; }
else if (Follows(p, ">=")) { p->lookahead = TK_GE; }
else if (Follows(p, "=~")) { p->lookahead = TK_REEQ; }
else if (Follows(p, "!~")) { p->lookahead = TK_RENE; }
else if (c == '=') { p->lookahead = TK_EQ; }
else if (c == '<') { p->lookahead = TK_LT; }
else if (c == '>') { p->lookahead = TK_GT; }
else if (c == '+') { p->lookahead = TK_PLUS; }
else if (c == '*') { p->lookahead = TK_ASTERISK; }
else if (c == '-') { p->lookahead = TK_MINUS; }
else if (c == '/') { p->lookahead = TK_SLASH; }
else if (c == '(') { p->lookahead = TK_BR_O; }
else if (c == ')') { p->lookahead = TK_BR_C; }
else if (c == ',') { p->lookahead = TK_COMMA; }
else if ((c >= '0' && c <= '9') || c == '.') { // isdigit screamed with debug warnings here in some occasions
int dbl = 0;
const char *cp = p->string + p->pos;
while (isdigit(*cp) || *cp == '.') {
if (*cp++ == '.') dbl++;
}
if (dbl == 0) { p->lookahead = TK_INTEGER; }
else if (dbl == 1) { p->lookahead = TK_DOUBLE; }
else { SetError(p, ERR_MALFORMED_NUM); }
}
else if (c == '%') { p->lookahead = TK_VAR; }
else p->lookahead = TK_STR;
}
LOCAL expr_val Match_Var(EParser p)
{
char varname[MAX_VAR_NAME];
char *wp = varname;
size_t vnlen = 0;
// var name can start with % sign
if (p->string[p->pos] == '%' || p->string[p->pos] == '$') p->pos++;
while (p->string[p->pos] && (isalpha(p->string[p->pos]) || p->string[p->pos] == '_') && vnlen < sizeof(varname))
{
*wp++ = *(p->string + p->pos++);
vnlen++;
}
*wp = '\0';
Next_Token(p);
return p->varfnc ? p->varfnc(varname) : Get_Expr_Dummy();
}
LOCAL expr_val Match_String(EParser p)
{
expr_val ret = {0};
size_t len = 0;
int startpos = p->pos;
char firstc = p->string[startpos];
if (firstc == '\'') {
p->pos++; startpos++;
while (p->string[p->pos] && p->string[p->pos] != '\'') {
len++; p->pos++;
}
p->pos++; // skip the '
} else if (firstc == '\"') {
p->pos++; startpos++;
while (p->string[p->pos] && p->string[p->pos] != '\"') {
len++; p->pos++;
}
p->pos++; // skip the "
} else {
/*
* FIXME is iswspace behaves same on Windows, Linux, MacOSX and FreeBSD?
*/
while (p->string[p->pos] && !iswspace(p->string[p->pos])) {
len++; p->pos++;
}
}
ret.type = ET_STR;
ret.s_val = (char *) malloc(len+1);
if (!ret.s_val) {
SetError(p, ERR_OUT_OF_MEM);
return Get_Expr_Dummy();
}
strlcpy(ret.s_val, p->string + startpos, len+1);
Next_Token(p);