Debian Codegraph @

modules/lsp-runtime-lib/src/main/expr/evaluator.cpp

raw
1 /*
2 * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2025 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 18 сент. 2019 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/expr/parser.h>
23 #include <lsp-plug.in/expr/evaluator.h>
24 #include <lsp-plug.in/stdlib/math.h>
25
26 namespace lsp
27 {
28 namespace expr
29 {
30 #define INT_OP(eval_name, oper) \
31 status_t eval_name(value_t *value, const expr_t *expr, eval_env_t *env) \
32 { \
33 status_t res = expr->calc.left->eval(value, expr->calc.left, env); \
34 if (res != STATUS_OK) \
35 return res; \
36 \
37 cast_int(value); \
38 if (value->type == VT_UNDEF) \
39 return STATUS_OK; \
40 else if (value->type == VT_NULL) \
41 { \
42 value->type = VT_UNDEF; \
43 return STATUS_OK; \
44 } \
45 \
46 value_t right; \
47 init_value(&right); \
48 res = expr->calc.right->eval(&right, expr->calc.right, env); \
49 if (res != STATUS_OK) \
50 { \
51 destroy_value(&right); \
52 destroy_value(value); \
53 return res; \
54 } \
55 \
56 cast_int(&right); \
57 switch (right.type) \
58 { \
59 case VT_INT: value->v_int = value->v_int oper right.v_int; break; \
60 case VT_NULL: value->type = VT_UNDEF; break; \
61 case VT_UNDEF: value->type = VT_UNDEF; break; \
62 default: res = STATUS_BAD_TYPE; break; \
63 } \
64 \
65 if (res != STATUS_OK) \
66 destroy_value(value); \
67 destroy_value(&right); \
68 \
69 return res; \
70 }
71
72 status_t eval_add(value_t *value, const expr_t *expr, eval_env_t *env)
73 {
74 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
75 if (res != STATUS_OK)
76 return res;
77
78 cast_numeric(value);
79 if (value->type == VT_UNDEF)
80 return STATUS_OK;
81 else if (value->type == VT_NULL)
82 {
83 value->type = VT_UNDEF;
84 return STATUS_OK;
85 }
86
87 value_t right;
88 init_value(&right);
89
90 res = expr->calc.right->eval(&right, expr->calc.right, env);
91 if (res != STATUS_OK)
92 {
93 destroy_value(&right);
94 destroy_value(value);
95 return res;
96 }
97
98 cast_numeric(&right);
99
100 switch (right.type)
101 {
102 case VT_INT:
103 if (value->type == VT_INT)
104 value->v_int = value->v_int + right.v_int;
105 else
106 value->v_float = value->v_float + right.v_int;
107 break;
108 case VT_FLOAT:
109 if (value->type == VT_INT)
110 value->v_float = value->v_int + right.v_float;
111 else
112 value->v_float = value->v_float + right.v_float;
113 value->type = VT_FLOAT;
114 break;
115 case VT_NULL:
116 value->type = VT_UNDEF;
117 break;
118 case VT_UNDEF: break;
119 default: res = STATUS_BAD_TYPE; break;
120 }
121
122 if (res != STATUS_OK)
123 destroy_value(value);
124 destroy_value(&right);
125
126 return res;
127 }
128
129 status_t eval_sub(value_t *value, const expr_t *expr, eval_env_t *env)
130 {
131 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
132 if (res != STATUS_OK)
133 return res;
134
135 cast_numeric(value);
136 if (value->type == VT_UNDEF)
137 return STATUS_OK;
138 else if (value->type == VT_NULL)
139 {
140 value->type = VT_UNDEF;
141 return STATUS_OK;
142 }
143
144 value_t right;
145 init_value(&right);
146 res = expr->calc.right->eval(&right, expr->calc.right, env);
147 if (res != STATUS_OK)
148 {
149 destroy_value(&right);
150 destroy_value(value);
151 return res;
152 }
153
154 cast_numeric(&right);
155
156 switch (right.type)
157 {
158 case VT_INT:
159 if (value->type == VT_INT)
160 value->v_int = value->v_int - right.v_int;
161 else
162 value->v_float = value->v_float - double(right.v_int);
163 break;
164 case VT_FLOAT:
165 if (value->type == VT_INT)
166 value->v_float = double(value->v_int) - right.v_float;
167 else
168 value->v_float = value->v_float - right.v_float;
169 value->type = VT_FLOAT;
170 break;
171 case VT_NULL:
172 value->type = VT_UNDEF;
173 break;
174 case VT_UNDEF: break;
175 default: res = STATUS_BAD_TYPE; break;
176 }
177
178 if (res != STATUS_OK)
179 destroy_value(value);
180 destroy_value(&right);
181
182 return res;
183 }
184
185 status_t eval_mul(value_t *value, const expr_t *expr, eval_env_t *env)
186 {
187 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
188 if (res != STATUS_OK)
189 return res;
190
191 cast_numeric(value);
192 if (value->type == VT_UNDEF)
193 return STATUS_OK;
194 else if (value->type == VT_NULL)
195 {
196 value->type = VT_UNDEF;
197 return STATUS_OK;
198 }
199
200 value_t right;
201 init_value(&right);
202 res = expr->calc.right->eval(&right, expr->calc.right, env);
203 if (res != STATUS_OK)
204 {
205 destroy_value(&right);
206 destroy_value(value);
207 return res;
208 }
209
210 cast_numeric(&right);
211
212 switch (right.type)
213 {
214 case VT_INT:
215 if (value->type == VT_INT)
216 value->v_int = value->v_int * right.v_int;
217 else
218 value->v_float = value->v_float * double(right.v_int);
219 break;
220 case VT_FLOAT:
221 if (value->type == VT_INT)
222 value->v_float = double(value->v_int) * right.v_float;
223 else
224 value->v_float = value->v_float * right.v_float;
225 value->type = VT_FLOAT;
226 break;
227 case VT_NULL:
228 value->type = VT_UNDEF;
229 break;
230 case VT_UNDEF: break;
231 default: res = STATUS_BAD_TYPE; break;
232 }
233
234 if (res != STATUS_OK)
235 destroy_value(value);
236 destroy_value(&right);
237
238 return res;
239 }
240
241 status_t eval_div(value_t *value, const expr_t *expr, eval_env_t *env)
242 {
243 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
244 if (res != STATUS_OK)
245 return res;
246
247 cast_numeric(value);
248 if (value->type == VT_UNDEF)
249 return STATUS_OK;
250 else if (value->type == VT_NULL)
251 {
252 value->type = VT_UNDEF;
253 return STATUS_OK;
254 }
255
256 value_t right;
257 init_value(&right);
258 res = expr->calc.right->eval(&right, expr->calc.right, env);
259 if (res != STATUS_OK)
260 {
261 destroy_value(&right);
262 destroy_value(value);
263 return res;
264 }
265
266 cast_numeric(&right);
267
268 switch (right.type)
269 {
270 case VT_INT:
271 if (value->type == VT_INT)
272 {
273 if (right.v_int != 0)
274 value->v_int = value->v_int / right.v_int;
275 else
276 value->type = VT_UNDEF;
277 }
278 else
279 value->v_float = value->v_float / double(right.v_int);
280 break;
281 case VT_FLOAT:
282 if (value->type == VT_INT)
283 value->v_float = double(value->v_int) / right.v_float;
284 else
285 value->v_float = value->v_float / right.v_float;
286 value->type = VT_FLOAT;
287 break;
288 case VT_NULL:
289 value->type = VT_UNDEF;
290 break;
291 case VT_UNDEF: break;
292 default: res = STATUS_BAD_TYPE; break;
293 }
294
295 if (res != STATUS_OK)
296 destroy_value(value);
297 destroy_value(&right);
298
299 return res;
300 }
301
302 INT_OP(eval_iadd, + );
303 INT_OP(eval_isub, - );
304 INT_OP(eval_imul, * );
305 INT_OP(eval_idiv, / );
306 INT_OP(eval_bit_or, | );
307 INT_OP(eval_bit_and, & );
308 INT_OP(eval_bit_xor, ^ );
309
310 status_t eval_imod(value_t *value, const expr_t *expr, eval_env_t *env)
311 {
312 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
313 if (res != STATUS_OK)
314 return res;
315
316 cast_int(value);
317 if (value->type == VT_UNDEF)
318 return STATUS_OK;
319 else if (value->type == VT_NULL)
320 {
321 value->type = VT_UNDEF;
322 return STATUS_OK;
323 }
324
325 value_t right;
326 init_value(&right);
327 res = expr->calc.right->eval(&right, expr->calc.right, env);
328 if (res != STATUS_OK)
329 {
330 destroy_value(&right);
331 destroy_value(value);
332 return res;
333 }
334
335 cast_int(&right);
336 switch (right.type)
337 {
338 case VT_INT:
339 if (right.v_int != 0)
340 value->v_int = value->v_int % right.v_int;
341 else
342 value->type = VT_UNDEF;
343 break;
344 case VT_NULL: value->type = VT_UNDEF; break;
345 case VT_UNDEF: break;
346 default: res = STATUS_BAD_TYPE; break;
347 }
348
349 if (res != STATUS_OK)
350 destroy_value(value);
351 destroy_value(&right);
352
353 return res;
354 }
355
356 status_t eval_fmod(value_t *value, const expr_t *expr, eval_env_t *env)
357 {
358 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
359 if (res != STATUS_OK)
360 return res;
361
362 cast_float(value);
363 if (value->type == VT_UNDEF)
364 return STATUS_OK;
365 else if (value->type == VT_NULL)
366 {
367 value->type = VT_UNDEF;
368 return STATUS_OK;
369 }
370
371 value_t right;
372 init_value(&right);
373 res = expr->calc.right->eval(&right, expr->calc.right, env);
374 if (res != STATUS_OK)
375 {
376 destroy_value(&right);
377 destroy_value(value);
378 return res;
379 }
380
381 cast_float(&right);
382 switch (right.type)
383 {
384 case VT_FLOAT: value->v_float = fmod(value->v_float, right.v_float); break;
385 case VT_NULL: value->type = VT_UNDEF; break;
386 case VT_UNDEF: break;
387 default: res = STATUS_BAD_TYPE; break;
388 }
389
390 if (res != STATUS_OK)
391 destroy_value(value);
392 destroy_value(&right);
393
394 return res;
395 }
396
397 status_t eval_xor(value_t *value, const expr_t *expr, eval_env_t *env)
398 {
399 // Test left argument
400 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
401 if (res != STATUS_OK)
402 return res;
403
404 value_t right;
405 init_value(&right);
406 res = cast_bool(value);
407 if (res == STATUS_OK)
408 res = expr->calc.right->eval(&right, expr->calc.right, env);
409 if (res != STATUS_OK)
410 {
411 destroy_value(&right);
412 destroy_value(value);
413 return res;
414 }
415
416 // Test right argument
417 res = cast_bool(&right);
418 if (res == STATUS_OK)
419 value->v_bool = !(value->v_bool == right.v_bool);
420 else
421 destroy_value(value);
422 destroy_value(&right);
423
424 return res;
425 }
426
427 status_t eval_or(value_t *value, const expr_t *expr, eval_env_t *env)
428 {
429 // Test left argument
430 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
431 if (res != STATUS_OK)
432 return res;
433
434 res = cast_bool(value);
435 if (res != STATUS_OK)
436 {
437 destroy_value(value);
438 return res;
439 }
440 else if (value->v_bool)
441 return STATUS_OK;
442
443 // Test right argument
444 destroy_value(value);
445 res = expr->calc.right->eval(value, expr->calc.right, env);
446 if (res != STATUS_OK)
447 return res;
448
449 res = cast_bool(value);
450 if (res != STATUS_OK)
451 destroy_value(value);
452
453 return res;
454 }
455
456 status_t eval_and(value_t *value, const expr_t *expr, eval_env_t *env)
457 {
458 // Test left argument
459 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
460 if (res != STATUS_OK)
461 return res;
462
463 res = cast_bool(value);
464 if (res != STATUS_OK)
465 {
466 destroy_value(value);
467 return res;
468 }
469 else if (!value->v_bool)
470 return STATUS_OK;
471
472 // Test right argument
473 destroy_value(value);
474 res = expr->calc.right->eval(value, expr->calc.right, env);
475 if (res != STATUS_OK)
476 return res;
477
478 res = cast_bool(value);
479 if (res != STATUS_OK)
480 destroy_value(value);
481
482 return res;
483 }
484
485 status_t eval_cmp(value_t *value, const expr_t *expr, eval_env_t *env)
486 {
487 // Fetch left argument and test for UNDEF
488 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
489 if (res != STATUS_OK)
490 return res;
491
492 // Fetch right argument and test for UNDEF
493 value_t right;
494 init_value(&right);
495 lsp_finally { destroy_value(&right); };
496
497 res = expr->calc.right->eval(&right, expr->calc.right, env);
498 if (res != STATUS_OK)
499 {
500 destroy_value(value);
501 return res;
502 }
503
504 if (value->type == VT_UNDEF)
505 {
506 set_value_int(value, (right.type == VT_UNDEF) ? 0 : -1);
507 return STATUS_OK;
508 }
509 else if (right.type == VT_UNDEF)
510 {
511 set_value_int(value, 1);
512 return STATUS_OK;
513 }
514
515 // Prevent from NULLs
516 if (value->type == VT_NULL)
517 {
518 set_value_int(value, (right.type == VT_NULL) ? 0 : -1);
519 return STATUS_OK;
520 }
521 else if (right.type == VT_NULL)
522 {
523 set_value_int(value, 1);
524 return STATUS_OK;
525 }
526
527 // Perform comparison matrix
528 switch (value->type)
529 {
530 case VT_INT:
531 {
532 switch (right.type)
533 {
534 case VT_INT:
535 value->v_int =
536 (value->v_int < right.v_int) ? -1 :
537 (value->v_int > right.v_int) ? 1 : 0;
538 break;
539 case VT_FLOAT:
540 value->v_int =
541 (double(value->v_int) < right.v_float) ? -1 :
542 (double(value->v_int) > right.v_float) ? 1 : 0;
543 break;
544 case VT_BOOL:
545 {
546 ssize_t ivalue = (value->v_bool) ? 1 : 0;
547 value->v_int =
548 (value->v_int < ivalue) ? -1 :
549 (value->v_int > ivalue) ? 1 : 0;
550 break;
551 }
552 case VT_STRING:
553 {
554 res = cast_string(value);
555 if (res == STATUS_OK)
556 {
557 ssize_t ivalue = value->v_str->compare_to(right.v_str);
558 destroy_value(value);
559 value->type = VT_INT;
560 value->v_int = ivalue;
561 }
562 break;
563 }
564 default:
565 res = STATUS_BAD_TYPE;
566 break;
567 } // switch (int)
568 break;
569 }
570 case VT_FLOAT:
571 {
572 switch (right.type)
573 {
574 case VT_INT:
575 value->type = VT_INT;
576 value->v_int =
577 (value->v_float < right.v_int) ? -1 :
578 (value->v_float > right.v_int) ? 1 : 0;
579 break;
580 case VT_FLOAT:
581 value->type = VT_INT;
582 value->v_int =
583 (value->v_float < right.v_float) ? -1 :
584 (value->v_float > right.v_float) ? 1 : 0;
585 break;
586 case VT_BOOL:
587 {
588 ssize_t ivalue = (value->v_bool) ? 1 : 0;
589 value->type = VT_INT;
590 value->v_int =
591 (value->v_float < ivalue) ? -1 :
592 (value->v_float > ivalue) ? 1 : 0;
593 break;
594 }
595 case VT_STRING:
596 {
597 res = cast_string(value);
598 if (res == STATUS_OK)
599 {
600 ssize_t ivalue = value->v_str->compare_to(right.v_str);
601 destroy_value(value);
602 value->type = VT_INT;
603 value->v_int = ivalue;
604 }
605 break;
606 }
607 default:
608 res = STATUS_BAD_TYPE;
609 break;
610 } // switch (float)
611 break;
612 }
613
614 case VT_BOOL:
615 {
616 ssize_t xvalue = (value->v_bool) ? 1 : 0;
617 switch (right.type)
618 {
619 case VT_INT:
620 value->type = VT_INT;
621 value->v_int =
622 (xvalue < right.v_int) ? -1 :
623 (xvalue > right.v_int) ? 1 : 0;
624 break;
625 case VT_FLOAT:
626 value->type = VT_INT;
627 value->v_int =
628 (xvalue < right.v_float) ? -1 :
629 (xvalue > right.v_float) ? 1 : 0;
630 break;
631 case VT_BOOL:
632 {
633 ssize_t ivalue = (value->v_bool) ? 1 : 0;
634 value->type = VT_INT;
635 value->v_int =
636 (xvalue < ivalue) ? -1 :
637 (xvalue > ivalue) ? 1 : 0;
638 break;
639 }
640 case VT_STRING:
641 {
642 res = cast_string(value);
643 if (res == STATUS_OK)
644 {
645 ssize_t ivalue = value->v_str->compare_to(right.v_str);
646 destroy_value(value);
647 value->type = VT_INT;
648 value->v_int = ivalue;
649 }
650 break;
651 }
652 default:
653 res = STATUS_BAD_TYPE;
654 break;
655 } // switch (float)
656 break;
657 }
658
659 case VT_STRING:
660 {
661 res = cast_string(&right);
662 if (res == STATUS_OK)
663 {
664 ssize_t ivalue = value->v_str->compare_to(right.v_str);
665 set_value_int(value, ivalue);
666 }
667 break;
668 }
669
670 default:
671 res = STATUS_BAD_TYPE;
672 break;
673 }
674
675 if (res != STATUS_OK)
676 destroy_value(value);
677
678 return res;
679 }
680
681 status_t eval_cmp_eq(value_t *value, const expr_t *expr, eval_env_t *env)
682 {
683 status_t res = eval_cmp(value, expr, env);
684 if (res != STATUS_OK)
685 return res;
686 if (value->type == VT_INT)
687 {
688 value->type = VT_BOOL;
689 value->v_bool = (value->v_int == 0);
690 }
691
692 return res;
693 }
694
695 status_t eval_cmp_ne(value_t *value, const expr_t *expr, eval_env_t *env)
696 {
697 status_t res = eval_cmp(value, expr, env);
698 if (res != STATUS_OK)
699 return res;
700 if (value->type == VT_INT)
701 {
702 value->type = VT_BOOL;
703 value->v_bool = (value->v_int != 0);
704 }
705
706 return res;
707 }
708
709 status_t eval_cmp_lt(value_t *value, const expr_t *expr, eval_env_t *env)
710 {
711 status_t res = eval_cmp(value, expr, env);
712 if (res != STATUS_OK)
713 return res;
714 if (value->type == VT_INT)
715 {
716 value->type = VT_BOOL;
717 value->v_bool = (value->v_int < 0);
718 }
719
720 return res;
721 }
722
723 status_t eval_cmp_gt(value_t *value, const expr_t *expr, eval_env_t *env)
724 {
725 status_t res = eval_cmp(value, expr, env);
726 if (res != STATUS_OK)
727 return res;
728 if (value->type == VT_INT)
729 {
730 value->type = VT_BOOL;
731 value->v_bool = (value->v_int > 0);
732 }
733
734 return res;
735 }
736
737 status_t eval_cmp_le(value_t *value, const expr_t *expr, eval_env_t *env)
738 {
739 status_t res = eval_cmp(value, expr, env);
740 if (res != STATUS_OK)
741 return res;
742 if (value->type == VT_INT)
743 {
744 value->type = VT_BOOL;
745 value->v_bool = (value->v_int <= 0);
746 }
747
748 return res;
749 }
750
751 status_t eval_cmp_ge(value_t *value, const expr_t *expr, eval_env_t *env)
752 {
753 status_t res = eval_cmp(value, expr, env);
754 if (res != STATUS_OK)
755 return res;
756 if (value->type == VT_INT)
757 {
758 value->type = VT_BOOL;
759 value->v_bool = (value->v_int >= 0);
760 }
761
762 return res;
763 }
764
765
766
767 status_t eval_icmp(value_t *value, const expr_t *expr, eval_env_t *env)
768 {
769 // Fetch left argument and test for UNDEF
770 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
771 if (res != STATUS_OK)
772 return res;
773
774 // Fetch right argument and test for UNDEF
775 value_t right;
776 init_value(&right);
777 res = expr->calc.right->eval(&right, expr->calc.right, env);
778 if (res != STATUS_OK)
779 {
780 destroy_value(&right);
781 destroy_value(value);
782 return res;
783 }
784
785 cast_int(value);
786 cast_int(&right);
787 if (value->type == VT_UNDEF)
788 {
789 set_value_int(value, (right.type == VT_UNDEF) ? 0 : -1);
790 destroy_value(&right);
791 return STATUS_OK;
792 }
793 else if (right.type == VT_UNDEF)
794 {
795 set_value_int(value, 1);
796 destroy_value(&right);
797 return STATUS_OK;
798 }
799
800 // Prevent from NULLs
801 if (value->type == VT_NULL)
802 {
803 set_value_int(value, (right.type == VT_NULL) ? 0 : -1);
804 destroy_value(&right);
805 return STATUS_OK;
806 }
807 else if (right.type == VT_NULL)
808 {
809 set_value_int(value, 1);
810 destroy_value(&right);
811 return STATUS_OK;
812 }
813
814 // Perform compare
815 set_value_int(value,
816 (value->v_int < right.v_int) ? -1 :
817 (value->v_int > right.v_int) ? 1 : 0);
818 return STATUS_OK;
819 }
820
821 status_t eval_icmp_eq(value_t *value, const expr_t *expr, eval_env_t *env)
822 {
823 status_t res = eval_icmp(value, expr, env);
824 if (res != STATUS_OK)
825 return res;
826 if (value->type == VT_INT)
827 {
828 value->type = VT_BOOL;
829 value->v_bool = (value->v_int == 0);
830 }
831
832 return res;
833 }
834
835 status_t eval_icmp_ne(value_t *value, const expr_t *expr, eval_env_t *env)
836 {
837 status_t res = eval_icmp(value, expr, env);
838 if (res != STATUS_OK)
839 return res;
840 if (value->type == VT_INT)
841 {
842 value->type = VT_BOOL;
843 value->v_bool = (value->v_int != 0);
844 }
845
846 return res;
847 }
848
849 status_t eval_icmp_lt(value_t *value, const expr_t *expr, eval_env_t *env)
850 {
851 status_t res = eval_icmp(value, expr, env);
852 if (res != STATUS_OK)
853 return res;
854 if (value->type == VT_INT)
855 {
856 value->type = VT_BOOL;
857 value->v_bool = (value->v_int < 0);
858 }
859
860 return res;
861 }
862
863 status_t eval_icmp_gt(value_t *value, const expr_t *expr, eval_env_t *env)
864 {
865 status_t res = eval_icmp(value, expr, env);
866 if (res != STATUS_OK)
867 return res;
868 if (value->type == VT_INT)
869 {
870 value->type = VT_BOOL;
871 value->v_bool = (value->v_int > 0);
872 }
873
874 return res;
875 }
876
877 status_t eval_icmp_le(value_t *value, const expr_t *expr, eval_env_t *env)
878 {
879 status_t res = eval_icmp(value, expr, env);
880 if (res != STATUS_OK)
881 return res;
882 if (value->type == VT_INT)
883 {
884 value->type = VT_BOOL;
885 value->v_bool = (value->v_int <= 0);
886 }
887
888 return res;
889 }
890
891 status_t eval_icmp_ge(value_t *value, const expr_t *expr, eval_env_t *env)
892 {
893 status_t res = eval_icmp(value, expr, env);
894 if (res != STATUS_OK)
895 return res;
896 if (value->type == VT_INT)
897 {
898 value->type = VT_BOOL;
899 value->v_bool = (value->v_int >= 0);
900 }
901
902 return res;
903 }
904
905 status_t eval_power(value_t *value, const expr_t *expr, eval_env_t *env)
906 {
907 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
908 if (res != STATUS_OK)
909 return res;
910
911 value_t right;
912 init_value(&right);
913 cast_float(value);
914 switch (value->type)
915 {
916 case VT_FLOAT:
917 res = expr->calc.right->eval(&right, expr->calc.right, env);
918 break;
919 case VT_NULL:
920 value->type = VT_UNDEF;
921 return STATUS_OK;
922 case VT_UNDEF:
923 return STATUS_OK;
924 default:
925 res = STATUS_BAD_TYPE;
926 break;
927 }
928
929 if (res == STATUS_OK)
930 {
931 cast_float(&right);
932 switch (right.type)
933 {
934 case VT_FLOAT:
935 value->v_float = ::pow(value->v_float, right.v_float);
936 break;
937 case VT_NULL:
938 case VT_UNDEF:
939 value->type = VT_UNDEF;
940 break;
941 default:
942 res = STATUS_BAD_TYPE;
943 break;
944 }
945 }
946
947 destroy_value(&right);
948 if (res != STATUS_OK)
949 destroy_value(value);
950
951 return res;
952 }
953
954 status_t eval_neg(value_t *value, const expr_t *expr, eval_env_t *env)
955 {
956 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
957 if (res != STATUS_OK)
958 return res;
959
960 if (value->type == VT_STRING)
961 cast_numeric(value);
962
963 switch (value->type)
964 {
965 case VT_INT:
966 value->v_int = ~value->v_int;
967 break;
968 case VT_FLOAT:
969 value->v_float = ~ssize_t(value->v_float);
970 break;
971 case VT_BOOL:
972 value->v_bool = !value->v_bool;
973 break;
974 case VT_NULL:
975 value->type = VT_UNDEF;
976 break;
977 case VT_UNDEF:
978 break;
979 default:
980 res = STATUS_BAD_TYPE;
981 break;
982 }
983
984 if (res != STATUS_OK)
985 destroy_value(value);
986
987 return res;
988 }
989
990 status_t eval_not(value_t *value, const expr_t *expr, eval_env_t *env)
991 {
992 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
993 if (res != STATUS_OK)
994 return res;
995
996 cast_bool(value);
997 switch (value->type)
998 {
999 case VT_BOOL:
1000 value->v_bool = !value->v_bool;
1001 break;
1002 case VT_NULL:
1003 value->type = VT_UNDEF;
1004 break;
1005 case VT_UNDEF:
1006 break;
1007 default:
1008 res = STATUS_BAD_TYPE;
1009 break;
1010 }
1011
1012 if (res != STATUS_OK)
1013 destroy_value(value);
1014
1015 return res;
1016 }
1017
1018 status_t eval_psign(value_t *value, const expr_t *expr, eval_env_t *env)
1019 {
1020 return expr->calc.left->eval(value, expr->calc.left, env);
1021 }
1022
1023 status_t eval_nsign(value_t *value, const expr_t *expr, eval_env_t *env)
1024 {
1025 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
1026 if (res != STATUS_OK)
1027 return res;
1028
1029 cast_numeric(value);
1030 switch (value->type)
1031 {
1032 case VT_INT:
1033 value->v_int = -value->v_int;
1034 break;
1035 case VT_FLOAT:
1036 value->v_float = -value->v_float;
1037 break;
1038 case VT_NULL:
1039 value->type = VT_UNDEF;
1040 break;
1041 case VT_UNDEF:
1042 break;
1043 default:
1044 res = STATUS_BAD_TYPE;
1045 break;
1046 }
1047
1048 if (res != STATUS_OK)
1049 destroy_value(value);
1050
1051 return res;
1052 }
1053
1054 status_t eval_exists(value_t *value, const expr_t *expr, eval_env_t *env)
1055 {
1056 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
1057 if (res != STATUS_OK)
1058 return res;
1059
1060 bool exists = value->type != VT_UNDEF;
1061 destroy_value(value);
1062
1063 value->type = VT_BOOL;
1064 value->v_bool = exists;
1065
1066 return STATUS_OK;
1067 }
1068
1069 status_t eval_db(value_t *value, const expr_t *expr, eval_env_t *env)
1070 {
1071 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
1072 if (res != STATUS_OK)
1073 return res;
1074
1075 cast_float(value);
1076 switch (value->type)
1077 {
1078 case VT_FLOAT:
1079 value->v_float = exp(value->v_float * M_LN10 * 0.05);
1080 break;
1081 case VT_NULL:
1082 value->type = VT_UNDEF;
1083 break;
1084 case VT_UNDEF:
1085 break;
1086 default:
1087 res = STATUS_BAD_TYPE;
1088 break;
1089 }
1090
1091 if (res != STATUS_OK)
1092 destroy_value(value);
1093
1094 return res;
1095 }
1096
1097 status_t eval_resolve(value_t *value, const expr_t *expr, eval_env_t *env)
1098 {
1099 status_t res;
1100 if (env == NULL)
1101 {
1102 set_value_undef(value);
1103 return STATUS_OK;
1104 }
1105
1106 // No indexes? Do simple stuff
1107 if (expr->resolve.count <= 0)
1108 {
1109 res = env->resolve(value, expr->resolve.name, 0, NULL);
1110 if (res != STATUS_NOT_FOUND)
1111 return res;
1112
1113 set_value_undef(value);
1114 return STATUS_OK;
1115 }
1116
1117 // Compute index values
1118 ssize_t *indexes = reinterpret_cast<ssize_t *>(::malloc(expr->resolve.count * sizeof(ssize_t)));
1119 if (indexes == NULL)
1120 return STATUS_NO_MEM;
1121
1122 value_t tmp;
1123 init_value(&tmp);
1124 for (size_t i=0; i<expr->resolve.count; ++i)
1125 {
1126 expr_t *e = expr->resolve.items[i];
1127
1128 // Evaluate and store index
1129 res = e->eval(&tmp, e, env);
1130 if (res == STATUS_OK)
1131 {
1132 res = cast_int(&tmp);
1133 if (res == STATUS_OK)
1134 indexes[i] = tmp.v_int;
1135 destroy_value(&tmp);
1136 }
1137
1138 // All is OK?
1139 if (res != STATUS_OK)
1140 {
1141 ::free(indexes);
1142 destroy_value(&tmp);
1143 return res;
1144 }
1145 }
1146
1147 // Now we can resolve values
1148 res = env->resolve(value, expr->resolve.name, expr->resolve.count, indexes);
1149 ::free(indexes);
1150 destroy_value(&tmp);
1151
1152 return res;
1153 }
1154
1155 status_t eval_call(value_t *value, const expr_t *expr, eval_env_t *env)
1156 {
1157 status_t res;
1158 if (env == NULL)
1159 {
1160 set_value_undef(value);
1161 return STATUS_OK;
1162 }
1163
1164 // No indexes? Do simple stuff
1165 if (expr->call.count <= 0)
1166 {
1167 res = env->call(value, expr->call.name, 0, NULL);
1168 if (res != STATUS_NOT_FOUND)
1169 return res;
1170
1171 set_value_undef(value);
1172 return STATUS_OK;
1173 }
1174
1175 // Initialize arguments
1176 value_t *args = reinterpret_cast<value_t *>(::malloc(expr->call.count * sizeof(value_t)));
1177 if (args == NULL)
1178 return STATUS_NO_MEM;
1179 lsp_finally { free(args); };
1180 for (size_t i=0; i<expr->call.count; ++i)
1181 init_value(&args[i]);
1182 lsp_finally {
1183 for (size_t i=0; i<expr->call.count; ++i)
1184 destroy_value(&args[i]);
1185 };
1186
1187 // Fill arguments
1188 for (size_t i=0; i<expr->call.count; ++i)
1189 {
1190 expr_t *e = expr->call.items[i];
1191
1192 // Evaluate and store index
1193 res = e->eval(&args[i], e, env);
1194 if (res != STATUS_OK)
1195 return res;
1196 }
1197
1198 // Now we can perform call
1199 res = env->call(value, expr->call.name, expr->call.count, args);
1200 if (res != STATUS_NOT_FOUND)
1201 return res;
1202
1203 set_value_undef(value);
1204 return STATUS_OK;
1205 }
1206
1207 status_t eval_value(value_t *value, const expr_t *expr, eval_env_t *env)
1208 {
1209 return copy_value(value, &expr->value);
1210 }
1211
1212 status_t eval_ternary(value_t *value, const expr_t *expr, eval_env_t *env)
1213 {
1214 status_t res = expr->calc.cond->eval(value, expr->calc.cond, env);
1215 if (res != STATUS_OK)
1216 return res;
1217 cast_bool(value);
1218 if ((value->type) != VT_BOOL)
1219 {
1220 destroy_value(value);
1221 return STATUS_OK;
1222 }
1223
1224 // Determine which expression to execute
1225 expr = (value->v_bool) ? expr->calc.left : expr->calc.right;
1226
1227 destroy_value(value);
1228 return expr->eval(value, expr, env);
1229 }
1230
1231 status_t eval_strcat(value_t *value, const expr_t *expr, eval_env_t *env)
1232 {
1233 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
1234 if (res != STATUS_OK)
1235 return res;
1236 if ((res = cast_string_ext(value)) != STATUS_OK)
1237 {
1238 destroy_value(value);
1239 return res;
1240 }
1241
1242 value_t right;
1243 init_value(&right);
1244 res = expr->calc.right->eval(&right, expr->calc.right, env);
1245 if (res != STATUS_OK)
1246 {
1247 destroy_value(&right);
1248 destroy_value(value);
1249 return res;
1250 }
1251 if ((res = cast_string_ext(&right)) != STATUS_OK)
1252 {
1253 destroy_value(value);
1254 destroy_value(&right);
1255 return res;
1256 }
1257
1258 if (!value->v_str->append(right.v_str))
1259 {
1260 destroy_value(value);
1261 res = STATUS_NO_MEM;
1262 }
1263 destroy_value(&right);
1264
1265 return res;
1266 }
1267
1268 status_t eval_strrep(value_t *value, const expr_t *expr, eval_env_t *env)
1269 {
1270 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
1271 if (res != STATUS_OK)
1272 return res;
1273 if ((res = cast_string_ext(value)) != STATUS_OK)
1274 {
1275 destroy_value(value);
1276 return res;
1277 }
1278
1279 value_t right;
1280 init_value(&right);
1281 res = expr->calc.right->eval(&right, expr->calc.right, env);
1282 if (res != STATUS_OK)
1283 {
1284 destroy_value(&right);
1285 destroy_value(value);
1286 return res;
1287 }
1288 cast_int(&right);
1289 if ((right.type == VT_NULL) || (right.type == VT_UNDEF) || (right.v_int < 0))
1290 {
1291 destroy_value(&right);
1292 destroy_value(value);
1293 return STATUS_OK;
1294 }
1295
1296 // Perform string repeat
1297 LSPString tmp;
1298 tmp.swap(value->v_str);
1299 size_t x = right.v_int;
1300 while (x)
1301 {
1302 if (x & 1)
1303 {
1304 if (!value->v_str->append(&tmp))
1305 {
1306 res = STATUS_NO_MEM;
1307 break;
1308 }
1309 }
1310 if (x >>= 1)
1311 {
1312 if (!tmp.append(&tmp))
1313 {
1314 res = STATUS_NO_MEM;
1315 break;
1316 }
1317 }
1318 }
1319
1320 if (res != STATUS_OK)
1321 destroy_value(value);
1322 destroy_value(&right);
1323
1324 return res;
1325 }
1326
1327 status_t eval_strupper(value_t *value, const expr_t *expr, eval_env_t *env)
1328 {
1329 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
1330 if (res != STATUS_OK)
1331 return res;
1332 cast_string(value);
1333 switch (value->type)
1334 {
1335 case VT_STRING:
1336 value->v_str->toupper();
1337 break;
1338 case VT_NULL:
1339 value->type = VT_UNDEF;
1340 break;
1341 case VT_UNDEF:
1342 break;
1343 default:
1344 res = STATUS_BAD_TYPE;
1345 break;
1346 }
1347
1348 if (res != STATUS_OK)
1349 destroy_value(value);
1350
1351 return res;
1352 }
1353
1354 status_t eval_strlower(value_t *value, const expr_t *expr, eval_env_t *env)
1355 {
1356 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
1357 if (res != STATUS_OK)
1358 return res;
1359 cast_string(value);
1360 switch (value->type)
1361 {
1362 case VT_STRING:
1363 value->v_str->tolower();
1364 break;
1365 case VT_NULL:
1366 value->type = VT_UNDEF;
1367 break;
1368 case VT_UNDEF:
1369 break;
1370 default:
1371 res = STATUS_BAD_TYPE;
1372 break;
1373 }
1374
1375 if (res != STATUS_OK)
1376 destroy_value(value);
1377
1378 return res;
1379 }
1380
1381 status_t eval_strlen(value_t *value, const expr_t *expr, eval_env_t *env)
1382 {
1383 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
1384 if (res != STATUS_OK)
1385 return res;
1386 cast_string(value);
1387 switch (value->type)
1388 {
1389 case VT_STRING:
1390 {
1391 const size_t len = value->v_str->length();
1392 destroy_value(value);
1393 value->type = VT_INT;
1394 value->v_int = len;
1395 break;
1396 }
1397 case VT_NULL:
1398 value->type = VT_UNDEF;
1399 break;
1400 case VT_UNDEF:
1401 break;
1402 default:
1403 res = STATUS_BAD_TYPE;
1404 break;
1405 }
1406
1407 if (res != STATUS_OK)
1408 destroy_value(value);
1409
1410 return res;
1411 }
1412
1413 status_t eval_strrev(value_t *value, const expr_t *expr, eval_env_t *env)
1414 {
1415 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
1416 if (res != STATUS_OK)
1417 return res;
1418 cast_string(value);
1419 switch (value->type)
1420 {
1421 case VT_STRING:
1422 {
1423 value->v_str->reverse();
1424 break;
1425 }
1426 case VT_NULL:
1427 value->type = VT_UNDEF;
1428 break;
1429 case VT_UNDEF:
1430 break;
1431 default:
1432 res = STATUS_BAD_TYPE;
1433 break;
1434 }
1435
1436 if (res != STATUS_OK)
1437 destroy_value(value);
1438
1439 return res;
1440 }
1441
1442 status_t eval_int_cast(value_t *value, const expr_t *expr, eval_env_t *env)
1443 {
1444 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
1445 if (res != STATUS_OK)
1446 return res;
1447
1448 res = cast_int(value);
1449 if (res != STATUS_OK)
1450 destroy_value(value);
1451
1452 return res;
1453 }
1454
1455 status_t eval_float_cast(value_t *value, const expr_t *expr, eval_env_t *env)
1456 {
1457 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
1458 if (res != STATUS_OK)
1459 return res;
1460
1461 res = cast_float(value);
1462 if (res != STATUS_OK)
1463 destroy_value(value);
1464
1465 return res;
1466 }
1467
1468 status_t eval_string_cast(value_t *value, const expr_t *expr, eval_env_t *env)
1469 {
1470 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
1471 if (res != STATUS_OK)
1472 return res;
1473
1474 res = cast_string(value);
1475 if (res != STATUS_OK)
1476 destroy_value(value);
1477
1478 return res;
1479 }
1480
1481 status_t eval_bool_cast(value_t *value, const expr_t *expr, eval_env_t *env)
1482 {
1483 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
1484 if (res != STATUS_OK)
1485 return res;
1486
1487 res = cast_bool(value);
1488 if (res != STATUS_OK)
1489 destroy_value(value);
1490
1491 return res;
1492 }
1493
1494 #define MATH_OP(eval_name, var, operation) \
1495 status_t eval_name(value_t *value, const expr_t *expr, eval_env_t *env) \
1496 { \
1497 status_t res = expr->calc.left->eval(value, expr->calc.left, env); \
1498 if (res != STATUS_OK) \
1499 return res; \
1500 \
1501 res = cast_float(value); \
1502 if (value->type == VT_UNDEF) \
1503 return STATUS_OK; \
1504 else if (value->type == VT_NULL) \
1505 { \
1506 value->type = VT_UNDEF; \
1507 return STATUS_OK; \
1508 } \
1509 const double var = value->v_float; \
1510 value->v_float = (operation); \
1511 \
1512 return res; \
1513 }
1514
1515 MATH_OP(eval_sin, x, sin(x))
1516 MATH_OP(eval_cos, x, cos(x))
1517 MATH_OP(eval_tan, x, tan(x))
1518 MATH_OP(eval_asin, x, asin(x))
1519 MATH_OP(eval_acos, x, acos(x))
1520 MATH_OP(eval_atan, x, atan(x))
1521 MATH_OP(eval_loge, x, log(x))
1522 MATH_OP(eval_logd, x, log(x) / M_LN10)
1523 MATH_OP(eval_log2, x, log(x) / M_LN2)
1524 MATH_OP(eval_exp, x, exp(x))
1525 MATH_OP(eval_sqrt, x, sqrt(x))
1526 MATH_OP(eval_rad, x, (x * M_PI) / 180.0)
1527 MATH_OP(eval_deg, x, (x * 180.0) / M_PI)
1528
1529 status_t eval_abs(value_t *value, const expr_t *expr, eval_env_t *env)
1530 {
1531 status_t res = expr->calc.left->eval(value, expr->calc.left, env);
1532 if (res != STATUS_OK)
1533 return res;
1534
1535 cast_numeric(value);
1536 if (value->type == VT_UNDEF)
1537 return STATUS_OK;
1538 else if (value->type == VT_NULL)
1539 {
1540 value->type = VT_UNDEF;
1541 return STATUS_OK;
1542 }
1543
1544 switch (value->type)
1545 {
1546 case VT_INT:
1547 if (value->v_int < 0)
1548 value->v_int = - value->v_int;
1549 break;
1550 case VT_FLOAT:
1551 if (value->v_float < 0)
1552 value->v_float = - value->v_float;
1553 break;
1554 case VT_NULL:
1555 value->type = VT_UNDEF;
1556 break;
1557 case VT_UNDEF: break;
1558 default: res = STATUS_BAD_TYPE; break;
1559 }
1560
1561 if (res != STATUS_OK)
1562 destroy_value(value);
1563
1564 return res;
1565 }
1566
1567 } /* namespace expr */
1568 } /* namespace lsp */
1569
1570