$_=<>;push@a,[$1?($3x$1)x$2:map(${+uc}=length${$$_=pop@a}[0],a,b)&&$4?map+($$b[$_]||'.'x$B).($$a[$_]||'.'x$A),0..$#${@$a>@$b?a:b}:map$_.'.'x(${$A>$B?A:B}-y///c),@$b,@$a]while/(\d)(.)(.)|(-)|./g;$,=$/;print@{pop@a}

Note that non-ascii characters in the above source code will be escaped (such as \x9f).