Implementing Fizz Buzz In SPL – Shakespeare Programming Language

I recently participated in a coding challenge where the goal was to implement the Fizz Buzz problem, a simple programming task, but do this in some unusual way.

My first thought was that I should try to use an Esoteric Programming Language. Given that I didn’t actually know any esoteric languages (esolangs?) I needed to find one that I could learn well enough in  the available time in order to implement Fizz Buzz.

After looking at and rejecting options like Malbolge, which was clearly an attempt to punish someone, Whitespace, a language that only uses space, tab, and linefeed characters, I found the Shakespeare Programming Language (or SPL).

SPL is described as, “A programming language created with the design goal to make the source code resemble Shakespeare plays.”

Boy Howdy, does it ever. Here’s an annotated example of my Fizz Buzz solution, where I print Floof, Bang, or Wham instead. Why, you ask. Those were the requirements.

 

First, we introduce the application name and define some variables. Here, you’ll see that I’m using the Perl module Lingua::Shakespeare, which made running this in windows pretty simple:

The first line is a Perl directive to treat this file as a Shakespeare program.

Following that, we have the name of the program, which starts with “A Play” and continues to the first period.

After the program name, we have the “Dramatis Personae” – the characters who participate in this “play”. Each line defines a variable with a name and an arbitrary description delimited by a comma, and ending with a period.

After this section, we enter the program flow. In Shakespeare programs, there are several concepts that need to be understood:

  1. The actors (variables) can only perform operations when they are “on the stage”
  2. Only two actors can be on the stage at the same time
  3. Program methods/functions appear as Acts and Scenes. The program starts with Act 1 Scene 1, and continues until all characters exit the stage.
  4. Branching can be done by having one actor say to another actor, “Let us proceed to Scene X”, where Scene X is the name of another Scene (aka, another method).
  5. If there is no branching, the Scenes flow linearly from one to another.

Act I Scene I

After the Dramaris Personae, we begin the first act and scene.  Here’s the code:

Here, the Ghost and Juliet enter. The ghost sets a value into Juliet equal to 8 (You pretty (2) little (times 2) warm (times 2) thing (times 1).

Then the Ghost modifies Juliet’s value “Thou art as prompt as the difference (X-Y) between the square of thyself (X=8^2=64) and your golden hair (Y=2, so (X-Y)=(64-2) = 62)”. So this statement is a concise Shakespearean was to set Juliet’s value to 62.

Next The Ghost says, “Speak your mind.” This directs Juliet to output the ASCII character with the value of 62, which is the “>” character.  So, by this point in the program, we have generated one character of output.

Juliet replies, “Listen to your heart!” Which opens standard input. The value that the user enters here will be assigned to The Ghost.

Juliet then exits the stage, and Romeo enters. Now, The Ghost has an insult for Romeo, that is used to assign him a value of 10. Here how that math works: First the Ghost says Romeo is the difference between nothing (so zero minus something) and that something is sum of a snotty stinking half witted hog and a small toad. Which turns out to be -10 (sum of -2 * -2 * -2) which is -8 + -2, or -10. And, of course, 0 – -10 = 10.

Then, The Ghost exits, and Juliet returns to the stage. Now, Romeo gives her a new value. Thou art an angel, with no adjectives, sets Juliet to the value of 1.

So, by this point in the program:

  • Juliet has the value 1
  • Romeo has the value 10
  • The Ghost has the value typed in by the user

 

Act I Scene II

Scene 2, deals mainly with conditional logic based on integer division and branching based on the result. Here’s the code:

Even though Romeo and Juliet are initially on stage at the end of Scene 1, it’s possible to get to Scene 2 from other areas of the program, so we initially clear the stage with Exeuent, and then have both Romeo and Juliet enter.

Juliet speaks first, and asks a question comparing the remainder of the quotient of “myself” and the sum of a good lord and a cat to zero. In this section, we see the first use of conditional logic: Is the remainder of myself… asks Romeo to determine if the remainder of the integer division of Juliet’s current value and the sum of a good lord (2) and a cat (+ 1) equals zero.

Romeo replies, “If so, let us proceed to scene IV.” That is, if we can evenly divide Juliet’s current value by 3, then branch to Scene IV.

If we do not branch, then Romeo continues, asking Juliet to consider the remainder of the quotient of her current value and a mighty proud lord and a cat. You’ll note that this is simply a check to see if Juliet’s current value is evenly divisible by 5.

This time, Juliet replies (since Romeo posed the conditional logic “question”), “If so, let us proceed to Scene V.” Again, if we do not branch, Juliet has one final conditional check – if her current value is evenly divisible by the sum of a mighty (2) happy (* 2) proud (* 2) lord (* 1) and a leech (-1), or 8 – 1, or 7.

Romeo then replies with either a branch to Scene VI, or a directive to Juliet to “Open your heart!”. That will simply have Juliet output her current value.  Note that this is different than the directive, Speak your mind, which outputs the ASCII character with the value equal to the subject’s value.

So, by this point in the program, assuming the program just launched

  • Juliet has the value 1
  • Romeo has the value 10
  • The Ghost has the value typed in by the user
  • Romeo and Juliet run some conditional logic tests against Juliet’s current value (1), all of which fail, so no branching occurs.
  • Juliet “Opens” her “heart”, displaying her current value (1).

Act I Scene III

Scene 3 is pretty short, but this sets up the looping we need to get this show on the road. First, as in scene 2, we clear the stage with Exeuent, and then have Romeo and Juliet enter.

Juliet has Romeo output the character with the code equal to his current value, which is 10. That’s the line feed character, so we get a line break in the output.

Juliet then asks, “Is The Ghost as good as me?” This is another conditional, and it’s simply an equality check between her current value and the value of The Ghost. This provides us with a way of ending the program. When Juliet’s value is the same as The Ghost’s, we proceed to Scene X, which is the end of the program.

If Juliet do not contain the same value (that is, if we do not branch to Scene X) then Romeo assigns Juliet the value equal to “the sum of yourself and a pony”. That is, add one to Juliet’s current value.

At the end of Scene III, Juliet simply branches back to Scene II.

Now, at this point in the program, assuming the program just launched

  • Juliet now has the value 2
  • Romeo has the value 10
  • The Ghost has the value typed in by the user (let’s assume that’s greater than 1)
  • We’ve outputted a line break
  • We have just branched back to Scene II

 

Act I Scene IV

Now, let’s assume we’ve started our third iteration. Now, Juliet’s value is 3, and the first conditional test in Scene II (checking if Juliet’s value can be evenly divided by 3) returns true. In that case, we come to Scene IV. I’ll just review this scene, since scenes V and VI are pretty similar.

Here, The Ghost enters with Falstaff. The Ghost says a whole lot, and is essentially setting Falstaff’s value to that of an ASCII character, and then having Falstaff output it. Then he goes on to the next character. One thing to notice here is the word Microsoft appears to be an insult, since it has a negative value.

Once Falstaff finishes his output at the behest of The Ghost, Romeo and Juliet enter again, and go through similar branching logic as in Scene II. This allows us to deal with numbers that are equally divisible by 3 and 5, or 5 and 7, or by all three numbers.  Finally, we go back to Scene III, which checks our stop condition, and either takes us to the exit, or loops back to Scene II.

So that’s it – Fizz Buzz (aka Floof Bang Wham) in the Shakespeare Programming Language. I’ve included the full source code below if you’re interested. It certainly makes for a fun read during code review.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Simple math CAPTCHA: *