1 #
2 # Test arithmetic expressions in all their different contexts.
3
4 # $(( 1 + 2 ))
5 # (( a=1+2 ))
6 # ${a[ 1 + 2 ]}
7 # ${a : 1+2 : 1+2}
8 # a[1 + 2]=foo
9
10 #### Multiple right brackets inside expression
11 a=(1 2 3)
12 echo ${a[a[0]]} ${a[a[a[0]]]}
13 ## stdout: 2 3
14 ## N-I zsh status: 0
15 ## N-I zsh stdout-json: "\n"
16
17 #### Slicing of string with constants
18 s='abcd'
19 echo ${s:0} ${s:0:4} ${s:1:1}
20 ## stdout: abcd abcd b
21
22 #### Slicing of string with variables
23 s='abcd'
24 zero=0
25 one=1
26 echo ${s:$zero} ${s:$zero:4} ${s:$one:$one}
27 ## stdout: abcd abcd b
28
29 #### Array index on LHS of assignment
30 a=(1 2 3)
31 zero=0
32 a[zero+5-4]=X
33 echo ${a[@]}
34 ## stdout: 1 X 3
35 ## OK zsh stdout: X 2 3
36
37 #### Array index on LHS with indices
38 a=(1 2 3)
39 a[a[1]]=X
40 echo ${a[@]}
41 ## stdout: 1 2 X
42 ## OK zsh stdout: X 2 3
43
44 #### Slicing of string with expressions
45 # mksh accepts ${s:0} and ${s:$zero} but not ${s:zero}
46 # zsh says unrecognized modifier 'z'
47 s='abcd'
48 zero=0
49 echo ${s:zero} ${s:zero+0} ${s:zero+1:zero+1}
50 ## stdout: abcd abcd b
51 ## BUG mksh stdout-json: ""
52 ## BUG mksh status: 1
53 ## BUG zsh stdout-json: ""
54 ## BUG zsh status: 1
55
56 #### Ambiguous colon in slice
57 s='abcd'
58 echo $(( 0 < 1 ? 2 : 0 )) # evaluates to 2
59 echo ${s: 0 < 1 ? 2 : 0 : 1} # 2:1 -- TRICKY THREE COLONS
60 ## stdout-json: "2\nc\n"
61 ## BUG mksh stdout-json: "2\n"
62 ## BUG mksh status: 1
63 ## BUG zsh stdout-json: "2\n"
64 ## BUG zsh status: 1
65
66 #### Triple parens should be disambiguated
67 # The first paren is part of the math, parens 2 and 3 are a single token ending
68 # arith sub.
69 ((a=1 + (2*3)))
70 echo $a $((1 + (2*3)))
71 ## stdout: 7 7
72
73 #### Quadruple parens should be disambiguated
74 ((a=1 + (2 * (3+4))))
75 echo $a $((1 + (2 * (3+4))))
76 ## stdout: 15 15
77
78 #### ExprSub $[] happens to behave the same on simple cases
79 echo $[1 + 2] "$[3 * 4]"
80 ## stdout: 3 12
81 ## N-I mksh stdout: $[1 + 2] $[3 * 4]