#!/bin/bash
. ./Common

###############################################################################

tst "substitutions: break alone" -ds -q <<EOF
!-s
in = blah
out = x
in = (?)(*) {
	out=\$out\$in
	break		/* does nothing useful */
}
EOF

expect <<EOF
in=blah
out=x
in=RE {
    out=\${out}\${in}
    break in
}
in=blah
out=xblah
EOF

#------------------------------------------------------------------------------

tst "substitutions: named break to inner block" -ds -q <<EOF
!-s
in = blah
out = x
in = (?)(*) {
	out=\$out\$in
	break in	/* does nothing useful */
}
EOF

expect <<EOF
in=blah
out=x
in=RE {
    out=\${out}\${in}
    break in
}
in=blah
out=xblah
EOF

#------------------------------------------------------------------------------

tst "substitutions: named break to outer block (taken)" -q doit=y <<EOF
!-s
in = blah
out = x
in = (?)(*) {
	out=\$out\$in
	doit = y {
		break in
	}
	out=\$out\$in
}
EOF

expect <<EOF
in=blah
out=xblah
EOF

#------------------------------------------------------------------------------

tst "substitutions: named break to outer block (not taken)" -q doit=n <<EOF
!-s
in = blah
out = x
in = (?)(*) {
	out=\$out\$in
	doit = y {
		break in
	}
	out=\$out\$in
}
EOF

expect <<EOF
in=blah
out=xblahblah
EOF

#------------------------------------------------------------------------------

tst "substitutions: break with continue" -q <<EOF
!-s
res = x
res = * {
	res = \${res}x
	/*
	 * We use a temporary variable here because the variable name also
	 * serves as a jump label. Alternatively, we could use a dummy
	 * variable for the outer block.
	 */
	tmp = \$res
	tmp = ????? { break res }
	continue
}
EOF

expect <<EOF
res=xxxxx
tmp=xxxxx
EOF

#------------------------------------------------------------------------------

tst_fail "substitutions: unnamed break inside block " -q <<EOF
!-s
foo = x
foo = * {
	break
	foo = x
}
EOF

expect <<EOF
s:4: syntax error
EOF

#------------------------------------------------------------------------------

tst_fail "substitutions: nmbed break inside block " -q <<EOF
!-s
foo = x
foo = * {
	break bar
	foo = x
}
EOF

expect <<EOF
s:5: unreachable code
EOF

#------------------------------------------------------------------------------

tst_fail "substitutions: named break to unknown block" -q <<EOF
!-s
foo = x
foo = * {
	break bar
}
EOF

expect <<EOF
s:5: cannot find "bar"
EOF

#------------------------------------------------------------------------------

tst_fail "substitutions: break without block" -q <<EOF
!-s
break
EOF

expect <<EOF
s:2: jump without block
EOF

###############################################################################