postfix to infix by xdigit

#!ruby -pl
$;=//
p="+-*/".split
q=p[0,2]
def g(x,c)
c&&x[0]!='('?['(',x,')']:x
end
$_=split.inject([]){|s,x|
s<<if x=~/\w/
[x]
else
a=s.pop
b=s.pop
[g(b,x=~/\/|\*/&&!(b&q).empty?),x,g(a,x=~/\//&&!(a&p[0..3]).empty?||x=~/\*|-/&&!(a&q).empty?)]
end
}

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

download

return to the top page