Browse Source

Define locals properly with docstrings

Alois Mahdal 3 years ago
parent
commit
bfa829befb
1 changed files with 52 additions and 34 deletions
  1. 52
    34
      99bottles.sh

+ 52
- 34
99bottles.sh View File

@@ -20,23 +20,24 @@ pred() {
20 20
     #
21 21
     # Get predecessor to a number $1
22 22
     #
23
-    case $1 in
24
-        *nine)      echo ${1%nine}eight;;
25
-        *eight)     echo ${1%eight}seven;;
26
-        *seven)     echo ${1%seven}six;;
27
-        *six)       echo ${1%six}five;;
28
-        *five)      echo ${1%five}four;;
29
-        *four)      echo ${1%four}three;;
30
-        *three)     echo ${1%three}two;;
31
-        *two)       echo ${1%two}one;;
23
+    local numeral=$1    # number in word form
24
+    case $numeral in
25
+        *nine)      echo ${numeral%nine}eight;;
26
+        *eight)     echo ${numeral%eight}seven;;
27
+        *seven)     echo ${numeral%seven}six;;
28
+        *six)       echo ${numeral%six}five;;
29
+        *five)      echo ${numeral%five}four;;
30
+        *four)      echo ${numeral%four}three;;
31
+        *three)     echo ${numeral%three}two;;
32
+        *two)       echo ${numeral%two}one;;
32 33
         one)        echo zero;;
33
-        *-one)      echo ${1%-one};;
34
-        *one)       echo ${1%one};;
34
+        *-one)      echo ${numeral%-one};;
35
+        *one)       echo ${numeral%one};;
35 36
         ten)        echo nine;;
36 37
         eleven)     echo ten;;
37 38
         twelve)     echo eleven;;
38
-        *teen)      teenpred $1;;
39
-        *ty)        tenspred $1;;
39
+        *teen)      teenpred $numeral;;
40
+        *ty)        tenspred $numeral;;
40 41
         zero)       echo "";  #to terminate
41 42
     esac
42 43
 }
@@ -45,9 +46,10 @@ teenpred() {
45 46
     #
46 47
     # Get predecessor of a teen $1
47 48
     #
48
-    case $1 in
49
+    local numeral=$1    # number in word form
50
+    case $numeral in
49 51
         thirteen)   echo twelve;;
50
-        *)          echo $(crunchprefix $(pred $(uncrunchprefix ${1%teen})))teen;;
52
+        *)          echo $(crunchprefix $(pred $(uncrunchprefix ${numeral%teen})))teen;;
51 53
     esac
52 54
 }
53 55
 
@@ -55,9 +57,10 @@ tenspred() {
55 57
     #
56 58
     # Get predecessor of a multiple of ten $1
57 59
     #
58
-    case $1 in
60
+    local numeral=$1    # number in word form
61
+    case $numeral in
59 62
         twenty)     echo nineteen;;
60
-        *) echo $(crunchprefix --tens $(pred $(uncrunchprefix ${1%ty})))ty-nine;;
63
+        *) echo $(crunchprefix --tens $(pred $(uncrunchprefix ${numeral%ty})))ty-nine;;
61 64
     esac
62 65
 }
63 66
 
@@ -69,14 +72,17 @@ crunchprefix() {
69 72
     #
70 73
     # option `--tens` - multiples of ten are a bit different
71 74
     #
75
+    local numeral       # number in word form
76
+    local tensop        # 'true' if --tens option is active
72 77
     [ $1 = --tens ] && { tensop=true; shift; }
73
-    case $1 in
74
-        two)    [ -n "$tensop" ] && echo twen || echo $1;;
78
+    numeral=$1
79
+    case $numeral in
80
+        two)    [ -n "$tensop" ] && echo twen || echo $numeral;;
75 81
         three)  echo thir;;
76
-        four)   [ -n "$tensop" ] && echo 'for' || echo $1;;
82
+        four)   [ -n "$tensop" ] && echo 'for' || echo $numeral;;
77 83
         five)   echo fif;;
78
-        eight)  [ -n "$tensop" ] && echo eigh || echo $1;;
79
-        *)      echo $1;;
84
+        eight)  [ -n "$tensop" ] && echo eigh || echo $numeral;;
85
+        *)      echo $numeral;;
80 86
     esac
81 87
 }
82 88
 
@@ -84,13 +90,14 @@ uncrunchprefix() {
84 90
     #
85 91
     # Reverse crunchprefix $1
86 92
     #
87
-    case $1 in
93
+    local prefix=$1     # numeral crunch-prefix
94
+    case $prefix in
88 95
         twen)   echo two;;
89 96
         thir)   echo three;;
90 97
         'for')  echo four;;
91 98
         fif)    echo five;;
92 99
         eigh)   echo eight;;
93
-        *)      echo $1;;
100
+        *)      echo $prefix;;
94 101
     esac
95 102
 }
96 103
 
@@ -98,7 +105,8 @@ grammar() {
98 105
     #
99 106
     # Apply peculiarities of English grammar to text on stdin
100 107
     #
101
-    local oneBottle=false  #can effect the following line
108
+    local oneBottle=false   # 'true' if this line affects the following line
109
+    local line              # every line
102 110
     while read line; do
103 111
         line="${line/one more bottles/one more bottle}"
104 112
         case "$line" in
@@ -123,6 +131,7 @@ capitalize() {
123 131
     #
124 132
     # Fix capitalization of each line on stdin
125 133
     #
134
+    local line      # every line
126 135
     while read line; do
127 136
         echo -n ${line:0:1} | tr '[:lower:]' '[:upper:]'
128 137
         echo ${line#?}
@@ -133,6 +142,7 @@ punctuate() {
133 142
     #
134 143
     # Add punctuation to each line on stdin
135 144
     #
145
+    local line      # every line
136 146
     while read line; do
137 147
         case "${line}" in
138 148
             [Ii]f*)     echo ${line},;;
@@ -146,7 +156,7 @@ verse() {
146 156
     #
147 157
     # Write one verse with number $1
148 158
     #
149
-    local nb=$1
159
+    local nb=$1     # numeral
150 160
     echo $nb bottles of beer on the wall
151 161
     echo $nb bottles of beer
152 162
     if [ $nb = zero ]; then
@@ -163,11 +173,14 @@ poeticize() {
163 173
     #
164 174
     # Make text on stdin nice
165 175
     #
176
+    local first     # first word of a line
177
+    local rest      # remainder of a line
178
+    local syl       # number of syllables in the first word
166 179
     while read first rest; do
167 180
         case "$rest" in
168 181
             *beer*)
169 182
                 first=${first/zero/no}
170
-                local syl=$(syllables ${first% *})
183
+                syl=$(syllables ${first% *})
171 184
                 case $syl in  #improve meter
172 185
                     1|2)    echo $first 'more' $rest;;
173 186
                     *)      echo $first $rest;;
@@ -182,28 +195,33 @@ syllables() {
182 195
     #
183 196
     # Estimate number of syllables in word $1
184 197
     #
198
+    local word=$1   # word (numeral) to do the estimation on
185 199
     local n=1
186
-    case $1 in
200
+    case $word in
187 201
         eleven)     n=2;; #sounds better if not considered 3
188 202
         *teen)      n=2;;
189 203
         *ty)        n=2;;
190 204
         *-*)        n=3;; #don't care about more than 3
191 205
     esac
192
-    case $1 in
206
+    case $word in
193 207
         *seven*)    let $((n = n+1));;
194 208
     esac
195 209
     echo $n
196 210
 }
197 211
 
198 212
 main() {
213
+    local breakLine             # bottle-break (or not) line
214
+    local standardBreakLine     # ^^ standard variant
215
+    local wastefulBreakLine     # ^^ wasteful (bottle breaks) variant
216
+    local nb            # beginning number
199 217
     standardBreakLine="Take one down and pass it around"
200 218
     wastefulBreakLine="If one of those bottles should happen to fall"
201
-    breakLine=$([ "$1" = --careless ] \
202
-     && echo $wastefulBreakLine \
203
-     || echo $standardBreakLine)
204
-
219
+    breakLine=$(
220
+        [ "$1" = --careless ] \
221
+         && echo $wastefulBreakLine \
222
+         || echo $standardBreakLine
223
+    )
205 224
     nb=ninety-nine
206
-
207 225
     while [ -n "$nb" ]; do
208 226
         verse $nb
209 227
         echo