xref: /csrg-svn/usr.bin/sed/TEST/math.sed (revision 46486)
1*46486Sbostic#
2*46486Sbostic#	@(#)math.sed	5.1 (Berkeley) 02/20/91
3*46486Sbostic#
4*46486Sbostic# Addition and multiplication in sed.
5*46486Sbostic# ++ for a limited time only do (expr) too!!!
6*46486Sbostic#
7*46486Sbostic# Kevin S Braunsdorf, PUCC UNIX Group, ksb@cc.purdue.edu.
8*46486Sbostic#
9*46486Sbostic# Ex:
10*46486Sbostic#	echo "4+7*3" | sed -f %f
11*46486Sbostic
12*46486Sbostic# make sure the expression is well formed
13*46486Sbostics/[ 	]//g
14*46486Sbostic/[+*\/-]$/{
15*46486Sbostic	a\
16*46486Sbostic	poorly formed expression, operator on the end
17*46486Sbostic	q
18*46486Sbostic}
19*46486Sbostic/^[+*\/]/{
20*46486Sbostic	a\
21*46486Sbostic	poorly formed expression, leading operator
22*46486Sbostic	q
23*46486Sbostic}
24*46486Sbostic
25*46486Sbostic# fill hold space with done token
26*46486Sbosticx
27*46486Sbostics/^.*/done/
28*46486Sbosticx
29*46486Sbostic
30*46486Sbostic# main loop, process operators (*, + and () )
31*46486Sbostic: loop
32*46486Sbostic/^\+/{
33*46486Sbostic	s///
34*46486Sbostic	b loop
35*46486Sbostic}
36*46486Sbostic/^\(.*\)(\([^)]*\))\(.*\)$/{
37*46486Sbostic	H
38*46486Sbostic	s//\2/
39*46486Sbostic	x
40*46486Sbostic	s/^\(.*\)\n\(.*\)(\([^()]*\))\(.*\)$/()\2@\4@\1/
41*46486Sbostic	x
42*46486Sbostic	b loop
43*46486Sbostic}
44*46486Sbostic/^[0-9]*\*/b mul
45*46486Sbostic/^\([0-9]*\)\+\([0-9+*]*\*[0-9]*\)$/{
46*46486Sbostic	s//\2+\1/
47*46486Sbostic	b loop
48*46486Sbostic}
49*46486Sbostic/^[0-9]*\+/{
50*46486Sbostic	s/$/=/
51*46486Sbostic	b add
52*46486Sbostic}
53*46486Sbosticx
54*46486Sbostic/^done$/{
55*46486Sbostic	x
56*46486Sbostic	p
57*46486Sbostic	d
58*46486Sbostic}
59*46486Sbostic/^()/{
60*46486Sbostic	s///
61*46486Sbostic	x
62*46486Sbostic	G
63*46486Sbostic	s/\(.*\)\n\([^@]*\)@\([^@]*\)@\(.*\)/\2\1\3/
64*46486Sbostic	x
65*46486Sbostic	s/[^@]*@[^@]*@\(.*\)/\1/
66*46486Sbostic	x
67*46486Sbostic	b loop
68*46486Sbostic}
69*46486Sbostici\
70*46486Sbostichelp, stack problem
71*46486Sbosticp
72*46486Sbosticx
73*46486Sbosticp
74*46486Sbosticq
75*46486Sbostic
76*46486Sbostic# turn mul into add until 1*x -> x
77*46486Sbostic: mul
78*46486Sbostic/^0*1\*/{
79*46486Sbostic	s///
80*46486Sbostic	b loop
81*46486Sbostic}
82*46486Sbostic/^\([0-9]*\)0\*/{
83*46486Sbostic	s/^\([0-9]*\)0\*\([0-9]*\)/\1*\20/
84*46486Sbostic	b mul
85*46486Sbostic}
86*46486Sbostics/^\([0-9]*\)1\*/\10*/
87*46486Sbostics/^\([0-9]*\)2\*/\11*/
88*46486Sbostics/^\([0-9]*\)3\*/\12*/
89*46486Sbostics/^\([0-9]*\)4\*/\13*/
90*46486Sbostics/^\([0-9]*\)5\*/\14*/
91*46486Sbostics/^\([0-9]*\)6\*/\15*/
92*46486Sbostics/^\([0-9]*\)7\*/\16*/
93*46486Sbostics/^\([0-9]*\)8\*/\17*/
94*46486Sbostics/^\([0-9]*\)9\*/\18*/
95*46486Sbostics/\*\([0-9*]*\)/*\1+\1/
96*46486Sbosticb mul
97*46486Sbostic
98*46486Sbostic# get rid of a plus term until 0+x -> x
99*46486Sbostic: add
100*46486Sbostic/^\+\([0-9+*]*\)=/{
101*46486Sbostic	s//\1/
102*46486Sbostic	b loop
103*46486Sbostic}
104*46486Sbostic/^\([0-9*]*\)\+=/{
105*46486Sbostic	s//\1/
106*46486Sbostic	b loop
107*46486Sbostic}
108*46486Sbostic/^\([0-9]*\)\+\([0-9*+]*\)\+=/{
109*46486Sbostic	s//\2+\1/
110*46486Sbostic	b loop
111*46486Sbostic}
112*46486Sbostic/^\([0-9]*\)0\+\([0-9]*\)\([0-9]\)=/{
113*46486Sbostic	s//\1+\2=\3/
114*46486Sbostic	b add
115*46486Sbostic}
116*46486Sbostic/^\([0-9]*\)\([0-9]\)\+\([0-9]*\)0=/{
117*46486Sbostic	s//\1+\3=\2/
118*46486Sbostic	b add
119*46486Sbostic}
120*46486Sbostic/^\([0-9]*\)0\+\([0-9*+]*\)\+\([0-9]*\)\([0-9]\)=/{
121*46486Sbostic	s//\1+\2+\3=\4/
122*46486Sbostic	b add
123*46486Sbostic}
124*46486Sbostic/^\([0-9]*\)\([0-9]\)\+\([0-9*+]*\)\+\([0-9]*\)0=/{
125*46486Sbostic	s//\1+\3+\4=\2/
126*46486Sbostic	b add
127*46486Sbostic}
128*46486Sbostics/^\([0-9]*\)1\+/\10+/
129*46486Sbostics/^\([0-9]*\)2\+/\11+/
130*46486Sbostics/^\([0-9]*\)3\+/\12+/
131*46486Sbostics/^\([0-9]*\)4\+/\13+/
132*46486Sbostics/^\([0-9]*\)5\+/\14+/
133*46486Sbostics/^\([0-9]*\)6\+/\15+/
134*46486Sbostics/^\([0-9]*\)7\+/\16+/
135*46486Sbostics/^\([0-9]*\)8\+/\17+/
136*46486Sbostics/^\([0-9]*\)9\+/\18+/
137*46486Sbostic
138*46486Sbostics/9=\([0-9]*\)$/_=\1/
139*46486Sbostics/8=\([0-9]*\)$/9=\1/
140*46486Sbostics/7=\([0-9]*\)$/8=\1/
141*46486Sbostics/6=\([0-9]*\)$/7=\1/
142*46486Sbostics/5=\([0-9]*\)$/6=\1/
143*46486Sbostics/4=\([0-9]*\)$/5=\1/
144*46486Sbostics/3=\([0-9]*\)$/4=\1/
145*46486Sbostics/2=\([0-9]*\)$/3=\1/
146*46486Sbostics/1=\([0-9]*\)$/2=\1/
147*46486Sbostic/_/{
148*46486Sbostic	s//_0/
149*46486Sbostic	: inc
150*46486Sbostic	s/9_/_0/
151*46486Sbostic	s/8_/9/
152*46486Sbostic	s/7_/8/
153*46486Sbostic	s/6_/7/
154*46486Sbostic	s/5_/6/
155*46486Sbostic	s/4_/5/
156*46486Sbostic	s/3_/4/
157*46486Sbostic	s/2_/3/
158*46486Sbostic	s/1_/2/
159*46486Sbostic	s/0_/1/
160*46486Sbostic	s/\+_/+1/
161*46486Sbostic	/_/b inc
162*46486Sbostic}
163*46486Sbosticb add
164