# @(#)math.sed 8.1 (Berkeley) 6/6/93
# Addition and multiplication in sed.
# ++ for a limited time only do (expr) too!!!
# Kevin S Braunsdorf, PUCC UNIX Group, ksb@cc.purdue.edu.
# echo "4+7*3" | sed -f %f
# make sure the expression is well formed
poorly formed expression, operator on the end
poorly formed expression, leading operator
# fill hold space with done token
# main loop, process operators (*, + and () )
/^\(.*\)(\([^)]*\))\(.*\)$/{
s/^\(.*\)\n\(.*\)(\([^()]*\))\(.*\)$/()\2@\4@\1/
/^\([0-9]*\)\+\([0-9+*]*\*[0-9]*\)$/{
s/\(.*\)\n\([^@]*\)@\([^@]*\)@\(.*\)/\2\1\3/
# turn mul into add until 1*x -> x
s/^\([0-9]*\)0\*\([0-9]*\)/\1*\20/
# get rid of a plus term until 0+x -> x
/^\([0-9]*\)\+\([0-9*+]*\)\+=/{
/^\([0-9]*\)0\+\([0-9]*\)\([0-9]\)=/{
/^\([0-9]*\)\([0-9]\)\+\([0-9]*\)0=/{
/^\([0-9]*\)0\+\([0-9*+]*\)\+\([0-9]*\)\([0-9]\)=/{
/^\([0-9]*\)\([0-9]\)\+\([0-9*+]*\)\+\([0-9]*\)0=/{