Search This Blog

Wednesday, October 21, 2009

Doing The Waltz

What a great post on dancing COBOL. Here is some more COBOL dancing to build on the idea. Recursion, local scope, classes and beer all in the same program!

Thanks for the mention of the 99 bottle code Robert. I saw your post and thought I should raise the bar. I few comments on my post on 99 bottles (here) suggested that local variable scope, recursion and other cool stuff is missing from COBOL. Totally untrue! Here is a COBOL program which shows off the new 'quoteless' type syntax which we are working on and at the same time shows off a few of the OO features in the standard Micro Focus COBOL for .net product which is already out there:

$set sourceformat(free)

class-id. "Singer".

method-id. "main" public static.
procedure division.
invoke type Singer::SayLine(by value 100)
end method "main".

method-id. "SayLine" public static.

01 bottle-word string.
01 count-word string.

procedure division using by value bottles as binary-long.
if bottles equals 0 then
display "Go to the store and buy some more, 99 bottles of beer on the wall."
add -1 to bottles
evaluate bottles
when 0
move "No" to count-word
move "bottles" to bottle-word
when 1
move bottles to count-word
move "bottle" to bottle-word
when 2 thru 99
move bottles to count-word
move "bottles" to bottle-word

if bottles not equals 99 then
display "Take one down and pass it around, " count-word::"ToLower"()
" more " bottle-word " of beer on the wall."

display count-word " " bottle-word
" of beer on the wall, " count-word::"ToLower"()
" " bottle-word " of beer."

invoke type Singer::SayLine(bottles)
end method "SayLine".

end class "Singer".

As you can see, this is a pure recursive implementation demonstrating the local scope of the method variables.

No comments:

Post a Comment