Blender V5.3
expr_pylike_eval.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2018 Blender Authors, Alexander Gavrilov. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
28
29#include <cctype>
30#include <cfenv>
31#include <cmath>
32#include <cstddef>
33#include <cstdlib>
34#include <cstring>
35#include <variant>
36
37#include "MEM_guardedalloc.h"
38
39#include "BLI_alloca.hh"
41#include "BLI_math_base_c.hh"
42#include "BLI_utildefines.hh"
43#include "BLI_vector.hh"
44
45namespace blender {
46
47#ifdef _MSC_VER
48# pragma fenv_access(on)
49#endif
50
51/* -------------------------------------------------------------------- */
54
81
82using UnaryOpFunc = double (*)(double);
83using BinaryOpFunc = double (*)(double, double);
84using TernaryOpFunc = double (*)(double, double, double);
85
86struct ExprOp {
88
90
91 union {
92 int ival;
93 double dval;
94 void *ptr;
98 } arg;
99};
100
105
107
108/* -------------------------------------------------------------------- */
111
113{
114 MEM_delete(expr);
115}
116
118{
119 return expr != nullptr && expr->ops.size() > 0;
120}
121
123{
124 return expr != nullptr && expr->ops.size() == 1 && expr->ops[0].opcode == OPCODE_CONST;
125}
126
128{
129 int i;
130
131 if (expr == nullptr) {
132 return false;
133 }
134
135 for (i = 0; i < expr->ops.size(); i++) {
136 if (expr->ops[i].opcode == OPCODE_PARAMETER && expr->ops[i].arg.ival == index) {
137 return true;
138 }
139 }
140
141 return false;
142}
143
145
146/* -------------------------------------------------------------------- */
149
151 const double *param_values,
152 int param_values_len,
153 double *r_result)
154{
155 *r_result = 0.0;
156
157 if (!BLI_expr_pylike_is_valid(expr)) {
158 return EXPR_PYLIKE_INVALID;
159 }
160
161#define FAIL_IF(condition) \
162 if (condition) { \
163 return EXPR_PYLIKE_FATAL_ERROR; \
164 } \
165 ((void)0)
166
167 /* Check the stack requirement is at least remotely sane and allocate on the actual stack. */
168 FAIL_IF(expr->max_stack <= 0 || expr->max_stack > 1000);
169
170 double *stack = BLI_array_alloca(stack, expr->max_stack);
171
172 /* Evaluate expression. */
173 ExprOp *ops = expr->ops.data();
174 int sp = 0, pc;
175
176 feclearexcept(FE_ALL_EXCEPT);
177
178 for (pc = 0; pc >= 0 && pc < expr->ops.size(); pc++) {
179 switch (ops[pc].opcode) {
180 /* Arithmetic */
181 case OPCODE_CONST:
182 FAIL_IF(sp >= expr->max_stack);
183 stack[sp++] = ops[pc].arg.dval;
184 break;
185 case OPCODE_PARAMETER:
186 FAIL_IF(sp >= expr->max_stack || ops[pc].arg.ival >= param_values_len);
187 stack[sp++] = param_values[ops[pc].arg.ival];
188 break;
189 case OPCODE_FUNC1:
190 FAIL_IF(sp < 1);
191 stack[sp - 1] = ops[pc].arg.func1(stack[sp - 1]);
192 break;
193 case OPCODE_FUNC2:
194 FAIL_IF(sp < 2);
195 stack[sp - 2] = ops[pc].arg.func2(stack[sp - 2], stack[sp - 1]);
196 sp--;
197 break;
198 case OPCODE_FUNC3:
199 FAIL_IF(sp < 3);
200 stack[sp - 3] = ops[pc].arg.func3(stack[sp - 3], stack[sp - 2], stack[sp - 1]);
201 sp -= 2;
202 break;
203 case OPCODE_MIN:
204 FAIL_IF(sp < ops[pc].arg.ival);
205 for (int j = 1; j < ops[pc].arg.ival; j++, sp--) {
206 CLAMP_MAX(stack[sp - 2], stack[sp - 1]);
207 }
208 break;
209 case OPCODE_MAX:
210 FAIL_IF(sp < ops[pc].arg.ival);
211 for (int j = 1; j < ops[pc].arg.ival; j++, sp--) {
212 CLAMP_MIN(stack[sp - 2], stack[sp - 1]);
213 }
214 break;
215
216 /* Jumps */
217 case OPCODE_JMP:
218 pc += ops[pc].jmp_offset;
219 break;
220 case OPCODE_JMP_ELSE:
221 FAIL_IF(sp < 1);
222 if (!stack[--sp]) {
223 pc += ops[pc].jmp_offset;
224 }
225 break;
226 case OPCODE_JMP_OR:
227 case OPCODE_JMP_AND:
228 FAIL_IF(sp < 1);
229 if (!stack[sp - 1] == !(ops[pc].opcode == OPCODE_JMP_OR)) {
230 pc += ops[pc].jmp_offset;
231 }
232 else {
233 sp--;
234 }
235 break;
236
237 /* For chaining comparisons, i.e. "a < b < c" as "a < b and b < c" */
238 case OPCODE_CMP_CHAIN:
239 FAIL_IF(sp < 2);
240 /* If comparison fails, return 0 and jump to end. */
241 if (!ops[pc].arg.func2(stack[sp - 2], stack[sp - 1])) {
242 stack[sp - 2] = 0.0;
243 pc += ops[pc].jmp_offset;
244 }
245 /* Otherwise keep b on the stack and proceed. */
246 else {
247 stack[sp - 2] = stack[sp - 1];
248 }
249 sp--;
250 break;
251
252 default:
254 }
255 }
256
257 FAIL_IF(sp != 1 || pc != expr->ops.size());
258
259#undef FAIL_IF
260
261 *r_result = stack[0];
262
263 /* Detect floating point evaluation errors. */
264 int flags = fetestexcept(FE_DIVBYZERO | FE_INVALID);
265 if (flags) {
266 return (flags & FE_INVALID) ? EXPR_PYLIKE_MATH_ERROR : EXPR_PYLIKE_DIV_BY_ZERO;
267 }
268
269 return EXPR_PYLIKE_SUCCESS;
270}
271
273
274/* -------------------------------------------------------------------- */
277
278static double op_negate(double arg)
279{
280 return -arg;
281}
282
283static double op_mul(double a, double b)
284{
285 return a * b;
286}
287
288static double op_div(double a, double b)
289{
290 return a / b;
291}
292
293static double op_add(double a, double b)
294{
295 return a + b;
296}
297
298static double op_sub(double a, double b)
299{
300 return a - b;
301}
302
303static double op_radians(double arg)
304{
305 return arg * M_PI / 180.0;
306}
307
308static double op_degrees(double arg)
309{
310 return arg * 180.0 / M_PI;
311}
312
313static double op_log2(double a, double b)
314{
315 return log(a) / log(b);
316}
317
318static double op_lerp(double a, double b, double x)
319{
320 return a * (1.0 - x) + b * x;
321}
322
323static double op_clamp(double arg)
324{
325 CLAMP(arg, 0.0, 1.0);
326 return arg;
327}
328
329static double op_clamp3(double arg, double minv, double maxv)
330{
331 CLAMP(arg, minv, maxv);
332 return arg;
333}
334
335static double op_smoothstep(double a, double b, double x)
336{
337 double t = (x - a) / (b - a);
338 CLAMP(t, 0.0, 1.0);
339 return t * t * (3.0 - 2.0 * t);
340}
341
342static double op_not(double a)
343{
344 return a ? 0.0 : 1.0;
345}
346
347static double op_eq(double a, double b)
348{
349 return a == b ? 1.0 : 0.0;
350}
351
352static double op_ne(double a, double b)
353{
354 return a != b ? 1.0 : 0.0;
355}
356
357static double op_lt(double a, double b)
358{
359 return a < b ? 1.0 : 0.0;
360}
361
362static double op_le(double a, double b)
363{
364 return a <= b ? 1.0 : 0.0;
365}
366
367static double op_gt(double a, double b)
368{
369 return a > b ? 1.0 : 0.0;
370}
371
372static double op_ge(double a, double b)
373{
374 return a >= b ? 1.0 : 0.0;
375}
376
378 const char *name;
379 double value;
380};
381
383 {"pi", M_PI}, {"True", 1.0}, {"False", 0.0}, {nullptr, 0.0}};
384
386 const char *name;
387 std::variant<UnaryOpFunc, BinaryOpFunc, TernaryOpFunc> funcptr;
388
389 /* Returns the required argument count of the given function call code. */
391 {
392 if (std::holds_alternative<UnaryOpFunc>(funcptr)) {
393 return 1;
394 }
395 if (std::holds_alternative<BinaryOpFunc>(funcptr)) {
396 return 2;
397 }
398 if (std::holds_alternative<TernaryOpFunc>(funcptr)) {
399 return 3;
400 }
401
402 BLI_assert_msg(0, "unexpected function pointer");
403 return -1;
404 }
405};
406
407#ifdef _MSC_VER
408/* Prevent MSVC from inlining calls to ceil/floor so the table below can get a function pointer to
409 * them. */
410# pragma function(ceil)
411# pragma function(floor)
412#endif
413
415 {"radians", UnaryOpFunc(op_radians)},
416 {"degrees", UnaryOpFunc(op_degrees)},
417 {"abs", UnaryOpFunc(fabs)},
418 {"fabs", UnaryOpFunc(fabs)},
419 {"floor", UnaryOpFunc(floor)},
420 {"ceil", UnaryOpFunc(ceil)},
421 {"trunc", UnaryOpFunc(trunc)},
422 {"round", UnaryOpFunc(round)},
423 {"int", UnaryOpFunc(trunc)},
424 {"sin", UnaryOpFunc(sin)},
425 {"cos", UnaryOpFunc(cos)},
426 {"tan", UnaryOpFunc(tan)},
427 {"asin", UnaryOpFunc(asin)},
428 {"acos", UnaryOpFunc(acos)},
429 {"atan", UnaryOpFunc(atan)},
430 {"atan2", BinaryOpFunc(atan2)},
431 {"exp", UnaryOpFunc(exp)},
432 {"log", UnaryOpFunc(log)},
433 {"log", BinaryOpFunc(op_log2)},
434 {"sqrt", UnaryOpFunc(sqrt)},
435 {"pow", BinaryOpFunc(pow)},
436 {"fmod", BinaryOpFunc(fmod)},
437 {"lerp", TernaryOpFunc(op_lerp)},
438 {"clamp", UnaryOpFunc(op_clamp)},
439 {"clamp", TernaryOpFunc(op_clamp3)},
440 {"smoothstep", TernaryOpFunc(op_smoothstep)},
441 {nullptr},
442};
443
445
446/* -------------------------------------------------------------------- */
449
450#define MAKE_CHAR2(a, b) (((a) << 8) | (b))
451
452#define CHECK_ERROR(condition) \
453 if (!(condition)) { \
454 return false; \
455 } \
456 ((void)0)
457
458/* For simplicity simple token types are represented by their own character;
459 * these are special identifiers for multi-character tokens. */
460#define TOKEN_ID MAKE_CHAR2('I', 'D')
461#define TOKEN_NUMBER MAKE_CHAR2('0', '0')
462#define TOKEN_GE MAKE_CHAR2('>', '=')
463#define TOKEN_LE MAKE_CHAR2('<', '=')
464#define TOKEN_NE MAKE_CHAR2('!', '=')
465#define TOKEN_EQ MAKE_CHAR2('=', '=')
466#define TOKEN_AND MAKE_CHAR2('A', 'N')
467#define TOKEN_OR MAKE_CHAR2('O', 'R')
468#define TOKEN_NOT MAKE_CHAR2('N', 'O')
469#define TOKEN_IF MAKE_CHAR2('I', 'F')
470#define TOKEN_ELSE MAKE_CHAR2('E', 'L')
471
472static const char *token_eq_characters = "!=><";
473static const char *token_characters = "~`!@#$%^&*+-=/\\?:;<>(){}[]|.,\"'";
474
476 const char *name;
477 short token;
478};
479
481 {"and", TOKEN_AND},
482 {"or", TOKEN_OR},
483 {"not", TOKEN_NOT},
484 {"if", TOKEN_IF},
485 {"else", TOKEN_ELSE},
486 {nullptr, TOKEN_ID},
487};
488
491 const char **param_names = nullptr;
492
493 /* Original expression */
494 const char *expr = nullptr;
495 const char *cur = nullptr;
496
497 /* Current token */
498 short token = 0;
500 double tokenval = 0.0;
501
502 /* Opcode buffer */
503 int last_jmp = 0;
505
506 /* Stack space requirement tracking */
507 int stack_ptr = 0;
508 int max_stack = 0;
509};
510
511/* Add one operation and track stack usage. */
512static ExprOp *parse_add_op(ExprParseState *state, eOpCode code, int stack_delta)
513{
514 /* track evaluation stack depth */
515 state->stack_ptr += stack_delta;
516 CLAMP_MIN(state->stack_ptr, 0);
517 CLAMP_MIN(state->max_stack, state->stack_ptr);
518
519 /* allocate the new instruction */
520 ExprOp op{code};
521 state->ops.append(op);
522 return &state->ops.last();
523}
524
525/* Add one jump operation and return an index for parse_set_jump. */
527{
528 parse_add_op(state, code, -1);
529 return state->last_jmp = state->ops.size();
530}
531
532/* Set the jump offset in a previously added jump operation. */
534{
535 state->last_jmp = state->ops.size();
536 state->ops[jump - 1].jmp_offset = state->ops.size() - jump;
537}
538
539/* Add a function call operation, applying constant folding when possible. */
541 std::variant<UnaryOpFunc, BinaryOpFunc, TernaryOpFunc> funcptr)
542{
543 ExprOp *prev_ops = state->ops.end();
544 int jmp_gap = state->ops.size() - state->last_jmp;
545
546 feclearexcept(FE_ALL_EXCEPT);
547
548 if (std::holds_alternative<UnaryOpFunc>(funcptr)) {
549 UnaryOpFunc func = std::get<UnaryOpFunc>(funcptr);
550
551 if (jmp_gap >= 1 && prev_ops[-1].opcode == OPCODE_CONST) {
552 /* volatile because some compilers overly aggressive optimize this call out.
553 * see D6012 for details. */
554 volatile double result = func(prev_ops[-1].arg.dval);
555
556 if (fetestexcept(FE_DIVBYZERO | FE_INVALID) == 0) {
557 prev_ops[-1].arg.dval = result;
558 return true;
559 }
560 }
561
562 parse_add_op(state, OPCODE_FUNC1, 0)->arg.func1 = func;
563 }
564 else if (std::holds_alternative<BinaryOpFunc>(funcptr)) {
565 BinaryOpFunc func = std::get<BinaryOpFunc>(funcptr);
566
567 if (jmp_gap >= 2 && prev_ops[-2].opcode == OPCODE_CONST && prev_ops[-1].opcode == OPCODE_CONST)
568 {
569 /* volatile because some compilers overly aggressive optimize this call out.
570 * see D6012 for details. */
571 volatile double result = func(prev_ops[-2].arg.dval, prev_ops[-1].arg.dval);
572
573 if (fetestexcept(FE_DIVBYZERO | FE_INVALID) == 0) {
574 prev_ops[-2].arg.dval = result;
575 state->ops.resize(state->ops.size() - 1);
576 state->stack_ptr--;
577 return true;
578 }
579 }
580
581 parse_add_op(state, OPCODE_FUNC2, -1)->arg.func2 = func;
582 }
583 else if (std::holds_alternative<TernaryOpFunc>(funcptr)) {
584 TernaryOpFunc func = std::get<TernaryOpFunc>(funcptr);
585
586 if (jmp_gap >= 3 && prev_ops[-3].opcode == OPCODE_CONST &&
587 prev_ops[-2].opcode == OPCODE_CONST && prev_ops[-1].opcode == OPCODE_CONST)
588 {
589 /* volatile because some compilers overly aggressive optimize this call out.
590 * see D6012 for details. */
591 volatile double result = func(
592 prev_ops[-3].arg.dval, prev_ops[-2].arg.dval, prev_ops[-1].arg.dval);
593
594 if (fetestexcept(FE_DIVBYZERO | FE_INVALID) == 0) {
595 prev_ops[-3].arg.dval = result;
596 state->ops.resize(state->ops.size() - 2);
597 state->stack_ptr -= 2;
598 return true;
599 }
600 }
601
602 parse_add_op(state, OPCODE_FUNC3, -2)->arg.func3 = func;
603 }
604 else {
605 BLI_assert(false);
606 return false;
607 }
608
609 return true;
610}
611
612/* Extract the next token from raw characters. */
614{
615 /* Skip white-space. */
616 while (isspace(*state->cur)) {
617 state->cur++;
618 }
619
620 /* End of string. */
621 if (*state->cur == 0) {
622 state->token = 0;
623 return true;
624 }
625
626 /* Floating point numbers. */
627 if (isdigit(*state->cur) || (state->cur[0] == '.' && isdigit(state->cur[1]))) {
628 char *end, *out = state->tokenbuf.data();
629 bool is_float = false;
630
631 while (isdigit(*state->cur)) {
632 *out++ = *state->cur++;
633 }
634
635 if (*state->cur == '.') {
636 is_float = true;
637 *out++ = *state->cur++;
638
639 while (isdigit(*state->cur)) {
640 *out++ = *state->cur++;
641 }
642 }
643
644 if (ELEM(*state->cur, 'e', 'E')) {
645 is_float = true;
646 *out++ = *state->cur++;
647
648 if (ELEM(*state->cur, '+', '-')) {
649 *out++ = *state->cur++;
650 }
651
652 CHECK_ERROR(isdigit(*state->cur));
653
654 while (isdigit(*state->cur)) {
655 *out++ = *state->cur++;
656 }
657 }
658
659 *out = 0;
660
661 /* Forbid C-style octal constants. */
662 if (!is_float && state->tokenbuf[0] == '0') {
663 for (char *p = state->tokenbuf.data() + 1; *p; p++) {
664 if (*p != '0') {
665 return false;
666 }
667 }
668 }
669
670 state->token = TOKEN_NUMBER;
671 state->tokenval = strtod(state->tokenbuf.data(), &end);
672 return (end == out);
673 }
674
675 /* ?= tokens */
676 if (state->cur[1] == '=' && strchr(token_eq_characters, state->cur[0])) {
677 state->token = MAKE_CHAR2(state->cur[0], state->cur[1]);
678 state->cur += 2;
679 return true;
680 }
681
682 /* Special characters (single character tokens) */
683 if (strchr(token_characters, *state->cur)) {
684 state->token = *state->cur++;
685 return true;
686 }
687
688 /* Identifiers */
689 if (isalpha(*state->cur) || ELEM(*state->cur, '_')) {
690 char *out = state->tokenbuf.data();
691
692 while (isalnum(*state->cur) || ELEM(*state->cur, '_')) {
693 *out++ = *state->cur++;
694 }
695
696 *out = 0;
697
698 for (int i = 0; keyword_list[i].name; i++) {
699 if (STREQ(state->tokenbuf.data(), keyword_list[i].name)) {
700 state->token = keyword_list[i].token;
701 return true;
702 }
703 }
704
705 state->token = TOKEN_ID;
706 return true;
707 }
708
709 return false;
710}
711
713
714/* -------------------------------------------------------------------- */
717
718static bool parse_expr(ExprParseState *state);
719
721{
722 if (!parse_next_token(state) || state->token != '(' || !parse_next_token(state)) {
723 return -1;
724 }
725
726 int arg_count = 0;
727
728 for (;;) {
729 if (!parse_expr(state)) {
730 return -1;
731 }
732
733 arg_count++;
734
735 switch (state->token) {
736 case ',':
737 if (!parse_next_token(state)) {
738 return -1;
739 }
740 break;
741
742 case ')':
743 if (!parse_next_token(state)) {
744 return -1;
745 }
746 return arg_count;
747
748 default:
749 return -1;
750 }
751 }
752}
753
755{
756 int i;
757
758 switch (state->token) {
759 case '+':
761
762 case '-':
765 return true;
766
767 case '(':
768 return parse_next_token(state) && parse_expr(state) && state->token == ')' &&
770
771 case TOKEN_NUMBER:
772 parse_add_op(state, OPCODE_CONST, 1)->arg.dval = state->tokenval;
773 return parse_next_token(state);
774
775 case TOKEN_ID:
776 /* Parameters: search in reverse order in case of duplicate names -
777 * the last one should win. */
778 for (i = state->param_names_len - 1; i >= 0; i--) {
779 if (STREQ(state->tokenbuf.data(), state->param_names[i])) {
780 parse_add_op(state, OPCODE_PARAMETER, 1)->arg.ival = i;
781 return parse_next_token(state);
782 }
783 }
784
785 /* Ordinary builtin constants. */
786 for (i = 0; builtin_consts[i].name; i++) {
787 if (STREQ(state->tokenbuf.data(), builtin_consts[i].name)) {
789 return parse_next_token(state);
790 }
791 }
792
793 /* Ordinary builtin functions. */
794 for (i = 0; builtin_ops[i].name; i++) {
795 if (STREQ(state->tokenbuf.data(), builtin_ops[i].name)) {
796 int args = parse_function_args(state);
797
798 /* Search for other arg count versions if necessary. */
799 if (args != builtin_ops[i].arg_count()) {
800 for (int j = i + 1; builtin_ops[j].name; j++) {
801 if (builtin_ops[j].arg_count() == args &&
802 STREQ(builtin_ops[j].name, builtin_ops[i].name))
803 {
804 i = j;
805 break;
806 }
807 }
808 }
809
810 CHECK_ERROR(builtin_ops[i].name && builtin_ops[i].arg_count() == args);
811
812 return parse_add_func(state, builtin_ops[i].funcptr);
813 }
814 }
815
816 /* Specially supported functions. */
817 if (STREQ(state->tokenbuf.data(), "min")) {
818 int count = parse_function_args(state);
819 CHECK_ERROR(count > 0);
820
821 parse_add_op(state, OPCODE_MIN, 1 - count)->arg.ival = count;
822 return true;
823 }
824
825 if (STREQ(state->tokenbuf.data(), "max")) {
826 int count = parse_function_args(state);
827 CHECK_ERROR(count > 0);
828
829 parse_add_op(state, OPCODE_MAX, 1 - count)->arg.ival = count;
830 return true;
831 }
832
833 return false;
834
835 default:
836 return false;
837 }
838}
839
841{
843
844 for (;;) {
845 switch (state->token) {
846 case '*':
849 break;
850
851 case '/':
854 break;
855
856 default:
857 return true;
858 }
859 }
860}
861
863{
865
866 for (;;) {
867 switch (state->token) {
868 case '+':
871 break;
872
873 case '-':
876 break;
877
878 default:
879 return true;
880 }
881 }
882}
883
885{
886 switch (token) {
887 case TOKEN_EQ:
888 return op_eq;
889 case TOKEN_NE:
890 return op_ne;
891 case '>':
892 return op_gt;
893 case TOKEN_GE:
894 return op_ge;
895 case '<':
896 return op_lt;
897 case TOKEN_LE:
898 return op_le;
899 default:
900 return nullptr;
901 }
902}
903
905{
906 BinaryOpFunc next_func = parse_get_cmp_func(state->token);
907
908 if (next_func) {
909 parse_add_op(state, OPCODE_CMP_CHAIN, -1)->arg.func2 = cur_func;
910 int jump = state->last_jmp = state->ops.size();
911
913 CHECK_ERROR(parse_cmp_chain(state, next_func));
914
916 }
917 else {
918 parse_add_func(state, cur_func);
919 }
920
921 return true;
922}
923
925{
927
929
930 if (func) {
932
933 return parse_cmp_chain(state, func);
934 }
935
936 return true;
937}
938
940{
941 if (state->token == TOKEN_NOT) {
944 return true;
945 }
946
947 return parse_cmp(state);
948}
949
951{
953
954 if (state->token == TOKEN_AND) {
956
958
960 }
961
962 return true;
963}
964
966{
968
969 if (state->token == TOKEN_OR) {
971
973
975 }
976
977 return true;
978}
979
981{
982 /* Temporarily set the constant expression evaluation barrier */
983 int prev_last_jmp = state->last_jmp;
984 int start = state->last_jmp = state->ops.size();
985
987
988 if (state->token == TOKEN_IF) {
989 /* Ternary IF expression in python requires swapping the
990 * main body with condition, so stash the body opcodes. */
991 const int size = state->ops.size() - start;
992
993 Vector<ExprOp> body(size);
994 std::copy_n(state->ops.data() + start, size, body.data());
995
996 state->ops.resize(start);
997 state->last_jmp = start;
998 state->stack_ptr--;
999
1000 /* Parse condition. */
1001 if (!parse_next_token(state) || !parse_or(state) || state->token != TOKEN_ELSE ||
1003 {
1004 return false;
1005 }
1006
1007 int jmp_else = parse_add_jump(state, OPCODE_JMP_ELSE);
1008
1009 /* Add body back. */
1010 const size_t body_offset = state->ops.size();
1011 state->ops.resize(body_offset + size);
1012 std::copy_n(body.data(), size, state->ops.data() + body_offset);
1013 body.clear_and_shrink();
1014
1015 state->stack_ptr++;
1016
1017 int jmp_end = parse_add_jump(state, OPCODE_JMP);
1018
1019 /* Parse the `else` block. */
1020 parse_set_jump(state, jmp_else);
1021
1023
1024 parse_set_jump(state, jmp_end);
1025 }
1026 /* If no actual jumps happened, restore previous barrier */
1027 else if (state->last_jmp == start) {
1028 state->last_jmp = prev_last_jmp;
1029 }
1030
1031 return true;
1032}
1033
1035
1036/* -------------------------------------------------------------------- */
1039
1041 const char **param_names,
1042 int param_names_len)
1043{
1044 /* Prepare the parser state. */
1046
1047 state.cur = state.expr = expression;
1048
1049 state.param_names_len = param_names_len;
1050 state.param_names = param_names;
1051
1052 state.tokenbuf.resize(strlen(expression) + 1);
1053
1054 /* Parse the expression. */
1055 ExprPyLike_Parsed *expr = MEM_new<ExprPyLike_Parsed>("ExprPyLike_Parsed(empty)");
1056
1057 if (parse_next_token(&state) && parse_expr(&state) && state.token == 0) {
1058 BLI_assert(state.stack_ptr == 1);
1059
1060 expr->max_stack = state.max_stack;
1061 expr->ops = std::move(state.ops);
1062 }
1063 else {
1064 /* Always return a non-nullptr object so that parse failure can be cached. */
1065 }
1066
1067 return expr;
1068}
1069
1071
1072} // namespace blender
#define BLI_array_alloca(arr, realsize)
Definition BLI_alloca.hh:18
#define M_PI
Read Blender memory allocation and freeing.
#define CLAMP(a, b, c)
#define CLAMP_MAX(a, c)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
#define CLAMP_MIN(a, b)
void jump(const btVector3 &v=btVector3(0, 0, 0))
void clear_and_shrink()
#define MAKE_CHAR2(a, b)
#define TOKEN_ID
#define FAIL_IF(condition)
#define TOKEN_GE
#define TOKEN_NOT
#define CHECK_ERROR(condition)
#define TOKEN_ELSE
#define TOKEN_OR
#define TOKEN_LE
#define TOKEN_IF
#define TOKEN_EQ
#define TOKEN_AND
#define TOKEN_NUMBER
#define TOKEN_NE
#define atan
#define tan
#define log
#define sin
#define round
#define pow
#define exp
#define cos
#define trunc
#define floor
#define ceil
#define asin
#define acos
#define atan2
const int state
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
ccl_device_inline float2 fmod(const float2 a, const float b)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static double op_eq(double a, double b)
static bool parse_and(ExprParseState *state)
static double op_sub(double a, double b)
static void parse_set_jump(ExprParseState *state, int jump)
static bool parse_unary(ExprParseState *state)
static BuiltinOpDef builtin_ops[]
static BinaryOpFunc parse_get_cmp_func(short token)
static bool parse_cmp(ExprParseState *state)
static double op_degrees(double arg)
ExprPyLike_Parsed * BLI_expr_pylike_parse(const char *expression, const char **param_names, int param_names_len)
static bool parse_not(ExprParseState *state)
static bool parse_mul(ExprParseState *state)
static bool parse_next_token(ExprParseState *state)
static const char * token_eq_characters
bool BLI_expr_pylike_is_constant(const struct ExprPyLike_Parsed *expr)
double(*)(double) UnaryOpFunc
static bool parse_expr(ExprParseState *state)
static double op_smoothstep(double a, double b, double x)
static double op_lt(double a, double b)
static bool parse_cmp_chain(ExprParseState *state, BinaryOpFunc cur_func)
static double op_ne(double a, double b)
static double op_log2(double a, double b)
static int parse_add_jump(ExprParseState *state, eOpCode code)
eExprPyLike_EvalStatus BLI_expr_pylike_eval(struct ExprPyLike_Parsed *expr, const double *param_values, int param_values_len, double *r_result)
static double op_ge(double a, double b)
static const char * token_characters
void BLI_expr_pylike_free(struct ExprPyLike_Parsed *expr)
static bool parse_add(ExprParseState *state)
static bool parse_or(ExprParseState *state)
static ExprOp * parse_add_op(ExprParseState *state, eOpCode code, int stack_delta)
static KeywordTokenDef keyword_list[]
static double op_le(double a, double b)
sqrt(x)+1/max(0
static double op_gt(double a, double b)
static double op_clamp3(double arg, double minv, double maxv)
static BuiltinConstDef builtin_consts[]
static double op_div(double a, double b)
static double op_add(double a, double b)
static double op_negate(double arg)
static bool parse_add_func(ExprParseState *state, std::variant< UnaryOpFunc, BinaryOpFunc, TernaryOpFunc > funcptr)
static double op_lerp(double a, double b, double x)
double(*)(double, double) BinaryOpFunc
static int parse_function_args(ExprParseState *state)
bool BLI_expr_pylike_is_valid(const struct ExprPyLike_Parsed *expr)
double(*)(double, double, double) TernaryOpFunc
static double op_not(double a)
static double op_clamp(double arg)
static double op_radians(double arg)
bool BLI_expr_pylike_is_using_param(const struct ExprPyLike_Parsed *expr, int index)
static double op_mul(double a, double b)
std::variant< UnaryOpFunc, BinaryOpFunc, TernaryOpFunc > funcptr
union blender::ExprOp::@230055030022137245332140206007331131374055034155 arg