busybox/shell/hush_test/hush-arith/arith-precedence1.tests
Denys Vlasenko 5f56a03882 shell/math: fix parsing of ?: and explain why it's parsed that way
This fixes arith-precedence1.tests.

This breaks arith-ternary2.tests again (we now evaluate variables
on not-taken branches). We need a better logic here anyway:
not only bare variables should not evaluate when not-taken:
	1 ? eval_me : do_not_eval
but any (arbitrarily complex) expressions shouldn't
evaluate as well!
	1 ? var_is_set=1 : ((var_is_not_set=2,var2*=4))

function                                             old     new   delta
evaluate_string                                     1097    1148     +51

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2023-06-15 10:14:43 +02:00

15 lines
547 B
Text
Executable file

exec 2>&1
# bash documentation says that precedence order is:
# ...
# expr ? expr1 : expr2
# = *= /= %= += -= <<= >>= &= ^= |=
# exprA , exprB
# but in practice, the rules for expr1 and expr2 are different:
# assignments and commas in expr1 have higher precedence than :?,
# but in expr2 they haven't:
# "v ? 1,2 : 3,4" is parsed as "(v ? (1,2) : 3),4"
# "v ? a=2 : b=4" is parsed as "(v ? (a=1) : b)=4" (thus, this is a syntax error)
echo 4:$((0 ? 1,2 : 3,4))
echo 4:$((1 ? 1,2 : 3,4))
echo 4:"$((0 ? 1,2 : 3,4))"
echo 4:"$((1 ? 1,2 : 3,4))"