Skip to content

Commit

Permalink
fancify code examples and explain it a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
grammarware committed Mar 5, 2024
1 parent cee4df1 commit c8d562c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 25 deletions.
27 changes: 18 additions & 9 deletions baby/code/99-bottles.dsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,26 @@
<body>
<header/>
<img src="../../www/babycobol.png" style="width:200px;height:200px;" class="flr" />
<h1><span class="ff lang"><a href="index.html">BabyCobol</a></span>: <span class="ff used"><a href="https://rosettacode.org/wiki/99_bottles_of_beer">99 Bottles of Beer</a></span></h1>
<h1><span class="ff lang"><a href="../index.html">BabyCobol</a></span>: <span class="ff used"><a href="https://rosettacode.org/wiki/99_bottles_of_beer">99 Bottles of Beer</a></span></h1>

The solution can also be found on <a href="https://rosettacode.org/wiki/99_bottles_of_beer#BabyCobol">Rosetta Code</a>:

<baby>
IDENTIFICATION DIVISION.
PROGRAM-ID. ¡99 BOTTLES.
@1 We hold the first seven characters empty.
@2 Some COBOL dialects refuse a PROGRAM-ID with a space but BabyCobol has insignificant whitespace.
@3 A keyword DATA is not a reserved word so we reuse it as a variable name.
@4 LOOP is called “in-line PERFORM” in COBOL
@5 We want to do our own decreasing in the middle of the loop. This zero increment is illegal in some dialects of COBOL!
@6 Positioning this code right after the loop is a visualisation choice, since the loop will never be exited in a normal way.
@7 This is a “local” GO TO which moves the computation within the PERFORM THROUGH range, so it does not disturb the PERFORM itself nor its encompassing LOOP.
@8 Unlike the previous one, this GO TO moves the computation outside the PERFORM THROUGH range, which terminates the entire PERFORM call, and thus also puts us outside the LOOP.
@9 A keyword END is used as a paragraph name, which is legal because in BabyCobol keywords are not reserved words.
{{ }}IDENTIFICATION DIVISION.
PROGRAM-ID. {{99 BOTTLES}}.
DATA DIVISION.
01 DATA PICTURE IS 999.
01 {{DATA}} PICTURE IS 999.
PROCEDURE DIVISION.
LOOP VARYING DATA FROM 99 BY 0
{{LOOP}} VARYING DATA FROM 99 {{BY 0}}
PERFORM COUNT-BOTTLES THROUGH ¡END
DISPLAY DATA "bottles•of•beer"
DISPLAY "Take•one•down,•pass•it•around"
Expand All @@ -26,20 +35,20 @@ The solution can also be found on <a href="https://rosettacode.org/wiki/99_bottl
PERFORM COUNT-BOTTLES THROUGH ¡END
DISPLAY ""
END.
NO-BOTTLES-LEFT.
{{NO-BOTTLES-LEFT}}.
DISPLAY "No•bottles•of•beer•on•the•wall"
DISPLAY ""
DISPLAY "Go•to•the•store•and•buy•some•more"
DISPLAY "99•bottles•of•beer•on•the•wall".
STOP.
COUNT-BOTTLES.
GO TO MANY-BOTTLES.
GO TO {{MANY-BOTTLES}}.
SINGLE-BOTTLE.
DISPLAY DATA "bottle•of•beer•on•the•wall".
GO TO NO-BOTTLES-LEFT.
GO TO {{NO-BOTTLES-LEFT}}.
MANY-BOTTLES.
DISPLAY DATA "bottles•of•beer•on•the•wall".
¡END.
{{¡END}}.
NEXT SENTENCE.
</baby>
<hr/>
Expand Down
18 changes: 9 additions & 9 deletions baby/code/99-bottles.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
<body>
<div style="text-align:center;"><a href="http://grammarware.github.io">Vadim Zaytsev</a> aka @<a href="http://grammarware.net">grammarware</a></div><hr/>
<img src="../../www/babycobol.png" style="width:200px;height:200px;" class="flr" />
<h1><span class="ff lang"><a href="index.html">BabyCobol</a></span>: <span class="ff used"><a href="https://rosettacode.org/wiki/99_bottles_of_beer">99 Bottles of Beer</a></span></h1>
<h1><span class="ff lang"><a href="../index.html">BabyCobol</a></span>: <span class="ff used"><a href="https://rosettacode.org/wiki/99_bottles_of_beer">99 Bottles of Beer</a></span></h1>
The solution can also be found on <a href="https://rosettacode.org/wiki/99_bottles_of_beer#BabyCobol">Rosetta Code</a>:
<pre>
<span class="key"> IDENTIFICATION DIVISION.
</span> PROGRAM-ID. 99 BOTTLES.
<span class="key"><dfn title="We hold the first seven characters empty."> </dfn>IDENTIFICATION DIVISION.
</span> PROGRAM-ID. <dfn title="Some COBOL dialects refuse a PROGRAM-ID with a space but BabyCobol has insignificant whitespace.">99 BOTTLES</dfn>.
<span class="key"> DATA DIVISION.
</span> <span class="aux"><span class="lit">01</span></span> DATA <span class="key">PICTURE</span> <span class="key">IS</span> <span class="aux">999</span>.
</span> <span class="aux">01</span> <dfn title="A keyword DATA is not a reserved word so we reuse it as a variable name.">DATA</dfn> <span class="key">PICTURE</span> <span class="key">IS</span> <span class="aux">999</span>.
<span class="key"> PROCEDURE DIVISION.
</span> <span class="key">LOOP</span> <span class="key">VARYING</span> DATA <span class="key">FROM</span> <span class="lit">99</span> <span class="key">BY</span> <span class="lit">0</span>
</span> <dfn title="LOOP is called “in-line PERFORM” in COBOL"><span class="key">LOOP</span></dfn> <span class="key">VARYING</span> DATA <span class="key">FROM</span> <span class="lit">99</span> <dfn title="We want to do our own decreasing in the middle of the loop. This zero increment is illegal in some dialects of COBOL!">BY 0</dfn>
<span class="key">PERFORM</span> COUNT-BOTTLES <span class="key">THROUGH</span> END
<span class="key">DISPLAY</span> DATA <span class="lit">"bottles of beer"</span>
<span class="key">DISPLAY</span> <span class="lit">"Take one down, pass it around"</span>
Expand All @@ -41,20 +41,20 @@ <h1><span class="ff lang"><a href="index.html">BabyCobol</a></span>: <span class
<span class="key">PERFORM</span> COUNT-BOTTLES <span class="key">THROUGH</span> END
<span class="key">DISPLAY</span> <span class="lit">""</span>
<span class="key">END</span>.
NO-BOTTLES-LEFT.
<dfn title="Positioning this code right after the loop is a visualisation choice, since the loop will never be exited in a normal way.">NO-BOTTLES-LEFT</dfn>.
<span class="key">DISPLAY</span> <span class="lit">"No bottles of beer on the wall"</span>
<span class="key">DISPLAY</span> <span class="lit">""</span>
<span class="key">DISPLAY</span> <span class="lit">"Go to the store and buy some more"</span>
<span class="key">DISPLAY</span> <span class="lit">"99 bottles of beer on the wall"</span>.
<span class="key">STOP</span>.
COUNT-BOTTLES.
<span class="key">GO</span> <span class="key">TO</span> MANY-BOTTLES.
<span class="key">GO</span> <span class="key">TO</span> <dfn title="This is a “local” GO TO which moves the computation within the PERFORM THROUGH range, so it does not disturb the PERFORM itself nor its encompassing LOOP.">MANY-BOTTLES</dfn>.
SINGLE-BOTTLE.
<span class="key">DISPLAY</span> DATA <span class="lit">"bottle of beer on the wall"</span>.
<span class="key">GO</span> <span class="key">TO</span> NO-BOTTLES-LEFT.
<span class="key">GO</span> <span class="key">TO</span> <dfn title="Unlike the previous one, this GO TO moves the computation outside the PERFORM THROUGH range, which terminates the entire PERFORM call, and thus also puts us outside the LOOP.">NO-BOTTLES-LEFT</dfn>.
MANY-BOTTLES.
<span class="key">DISPLAY</span> DATA <span class="lit">"bottles of beer on the wall"</span>.
END.
<dfn title="A keyword END is used as a paragraph name, which is legal because in BabyCobol keywords are not reserved words.">END</dfn>.
<span class="key">NEXT</span> <span class="key">SENTENCE</span>.
</pre>
<hr/>
Expand Down
2 changes: 1 addition & 1 deletion baby/code/perform-goto.dsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<body>
<header/>
<img src="../../www/babycobol.png" style="width:200px;height:200px;" class="flr" />
<h1><span class="ff lang"><a href="index.html">BabyCobol</a></span>: <span class="ff used"><a href="perform.html">PERFORM</a></span> + <span class="ff used"><a href="goto.html">GO TO</a></span></h1>
<h1><span class="ff lang"><a href="../index.html">BabyCobol</a></span>: <span class="ff used"><a href="perform.html">PERFORM</a></span> + <span class="ff used"><a href="goto.html">GO TO</a></span></h1>

The following program should print "<code>ABBCDECDEF</code>" (replace <code>STOP</code> with <code>STOP RUN</code> and it will turn into a proper COBOL program which you can run to test this behaviour yourself):

Expand Down
2 changes: 1 addition & 1 deletion baby/code/perform-goto.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<body>
<div style="text-align:center;"><a href="http://grammarware.github.io">Vadim Zaytsev</a> aka @<a href="http://grammarware.net">grammarware</a></div><hr/>
<img src="../../www/babycobol.png" style="width:200px;height:200px;" class="flr" />
<h1><span class="ff lang"><a href="index.html">BabyCobol</a></span>: <span class="ff used"><a href="perform.html">PERFORM</a></span> + <span class="ff used"><a href="goto.html">GO TO</a></span></h1>
<h1><span class="ff lang"><a href="../index.html">BabyCobol</a></span>: <span class="ff used"><a href="perform.html">PERFORM</a></span> + <span class="ff used"><a href="goto.html">GO TO</a></span></h1>
The following program should print "<code>ABBCDECDEF</code>" (replace <code>STOP</code> with <code>STOP RUN</code> and it will turn into a proper COBOL program which you can run to test this behaviour yourself):
<pre>
<span class="com"> * PERFORM + GO TO interplay example
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ <h2>Acknowledgement</h2>
<br/><hr/>
This initiative involves <a href="ack.html">many people</a>.
The website is maintained by <a href="http://grammarware.github.io/">Dr. Vadim Zaytsev</a> a.k.a. @<a href="http://grammarware.net/">grammarware</a>.
Last updated: November 2022.
Last updated: March 2024.
<br/><a href="http://validator.w3.org/check/referer"><img src="www/xhtml.88.png" alt="XHTML 1.1" /></a>
<a href="http://jigsaw.w3.org/css-validator/check/referer"><img src="www/css.88.png" alt="CSS 3" /></a>
</div>
Expand Down
10 changes: 6 additions & 4 deletions www/babycobol.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ a:link, a:visited {color:#E75}
display: inline-block;
}

dfn {font-style: normal; text-decoration: green underline wavy;}

pre { background-color:#543;}
pre > .com {color:#876;}
pre > .lit {color:#F77;}
pre > .aux {color:#77F;}
pre > .key {color:#FF7; font-weight:bold;}
pre .com {color:#876;}
pre .lit {color:#F77;}
pre .aux {color:#77F;}
pre .key {color:#FF7; font-weight:bold;}

.used { background-color: #E55; }
.lang { background-color: #FED; color: #E55; }

0 comments on commit c8d562c

Please sign in to comment.