diff options
author | Denys Vlasenko | 2023-06-15 10:07:12 +0200 |
---|---|---|
committer | Denys Vlasenko | 2023-06-15 10:14:43 +0200 |
commit | 5f56a0388271d2de6cf31af1041bdcb3d11029fc (patch) | |
tree | e569191d44b44dcdc87e31c9927374a1ea97c20c /shell/hush_test | |
parent | 3829d8b6758439251fc3e34dcedf5910d039b07d (diff) | |
download | busybox-5f56a0388271d2de6cf31af1041bdcb3d11029fc.zip busybox-5f56a0388271d2de6cf31af1041bdcb3d11029fc.tar.gz |
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>
Diffstat (limited to 'shell/hush_test')
7 files changed, 23 insertions, 11 deletions
diff --git a/shell/hush_test/hush-arith/arith-precedence1.right b/shell/hush_test/hush-arith/arith-precedence1.right new file mode 100644 index 0000000..3f9320a --- /dev/null +++ b/shell/hush_test/hush-arith/arith-precedence1.right @@ -0,0 +1,4 @@ +4:4 +4:4 +4:4 +4:4 diff --git a/shell/hush_test/hush-arith/arith-precedence1.tests b/shell/hush_test/hush-arith/arith-precedence1.tests new file mode 100755 index 0000000..bfef052 --- /dev/null +++ b/shell/hush_test/hush-arith/arith-precedence1.tests @@ -0,0 +1,15 @@ +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))" diff --git a/shell/hush_test/hush-arith/arith-ternary2.right b/shell/hush_test/hush-arith/arith-ternary2.right deleted file mode 100644 index a549b1b..0000000 --- a/shell/hush_test/hush-arith/arith-ternary2.right +++ /dev/null @@ -1,3 +0,0 @@ -6:6 -a=b=+err+ -b=6 diff --git a/shell/hush_test/hush-arith/arith-ternary2.tests b/shell/hush_test/hush-arith/arith-ternary2.tests deleted file mode 100755 index cb31639..0000000 --- a/shell/hush_test/hush-arith/arith-ternary2.tests +++ /dev/null @@ -1,7 +0,0 @@ -exec 2>&1 -a='b=+err+' -b=5 -# The not-taken branch should not parse variables -echo 6:$((0 ? a : ++b)) -echo "a=$a" -echo "b=$b" diff --git a/shell/hush_test/hush-arith/arith-ternary_nested1.right b/shell/hush_test/hush-arith/arith-ternary_nested1.right new file mode 100644 index 0000000..d803196 --- /dev/null +++ b/shell/hush_test/hush-arith/arith-ternary_nested1.right @@ -0,0 +1 @@ +3:3 diff --git a/shell/hush_test/hush-arith/arith-ternary_nested1.tests b/shell/hush_test/hush-arith/arith-ternary_nested1.tests new file mode 100755 index 0000000..469584b --- /dev/null +++ b/shell/hush_test/hush-arith/arith-ternary_nested1.tests @@ -0,0 +1,2 @@ +exec 2>&1 +echo 3:$((1?(2?(3):4):5)) diff --git a/shell/hush_test/hush-arith/arith.right b/shell/hush_test/hush-arith/arith.right index a861229..df8154f 100644 --- a/shell/hush_test/hush-arith/arith.right +++ b/shell/hush_test/hush-arith/arith.right @@ -94,7 +94,7 @@ ghi hush: arithmetic syntax error 16 16 hush: arithmetic syntax error -hush: malformed ?: operator +hush: arithmetic syntax error hush: arithmetic syntax error 9 9 hush: arithmetic syntax error |