Maybe you’ve got heard that playwright William Shakespeare contributed 1,700 new phrases to the English language. But do you know that he has a whole programming language as effectively?
SPL (Shakespeare programming language) was created to make supply code resemble Shakespeare performs. It is an esoteric language, also referred to as an esolang. An esolang is a pc programming language designed to experiment with bizarre concepts, create a problem for programmers, or just function a supply of amusement, fairly than for sensible use.
In a earlier article, I shared construct an app with the Schwarzenegger-inspired language ArnoldC. In this experiment, I’ll cowl a completely written “Hello World ” instance. If you wish to comply with alongside, you will want GCC, brew
or make
, Python 2, and quite a lot of free time and endurance. (Python 2 has been unsupported since 2020, so that is only for leisure.)
What is that this language?
In SLP, the code is the play, and the characters within the play are variables. If you wish to assign a personality, for instance, Hamlet, a unfavorable worth, you place him and one other character on the stage and let that character insult Hamlet.
Having somebody inform a personality to take heed to their coronary heart and converse their thoughts produces enter and output. The language comprises conditionals, through which characters ask one another questions, and jumps, through which they determine to go to a selected act or scene. Characters are additionally stacks that may be pushed and popped.
Where do I get this language?
You can obtain the language from the SPL website, which additionally comprises a large quantity of documentation. Once you obtain the tar.gz file you may get began:
$ cd ~/spl
/spl$ ls
spl-1.2.1.tar.gz
/spl$ gunzip spl-1.2.1.tar.gz
/spl$ tar -xvf spl-1.2.1.tar
/spl$ cd spl-1.2.1/
/spl/spl-1.2.1$ ls
AUTHORS examples libspl.c makescanner.c strutils.c
COPYING grammar.y LICENSE NEWS strutils.h
editor embrace Makefile spl.h telma.h
Then you’ll be able to run the make
command to compile the interpreter or use brew
for a sooner set up and simple precompile. I used to be having points with make
, so I ran brew
once more:
$ brew set up --build-from-source shakespeare
<snip>
==> Downloading https://ghcr.io/v2/homebrew/core/gettext/blobs/
sha256:33f840e667c6ee0f674adb279e644ca4a1b3cd1606894c85d9bbce1b5acc0273==> make set up
? /residence/linuxbrew/.linuxbrew/Cellar/shakespeare/
1.2.1: 9 recordsdata, 154.1KB, constructed in 1 second
==> Running `brew cleanup shakespeare`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
What’s subsequent?
Once you’ve got put in SPL, get the opposite dependencies, beginning with Python 2. You can set up Python 2 with any bundle supervisor. Next, seize a C compiler that makes use of Python for the SPL recordsdata, which might be downloaded from the SPL GitHub repository. Once you’ve got downloaded the recordsdata, simply unzip and use them as wanted from the listing.
In the bottom examples, we are able to take one of many premade recordsdata. In this case, I’ll seize Hello World to check the compiler and run the code. Below are the instructions and output.
/spl/Spl-master$ python2 splc.py ../spl-1.2.1/
examples/hey.spl > hey.c
/spl/Spl-master$ gcc hey.c -lm
/spl/Spl-master$ ./a.outHello World!
As you’ll be able to see, I used the compiler to transform the file to C, then used gcc
utilizing flags for linker choices to make use of the library and the -m flag for the goal file.
You can see how your complete Hello World SPL was written by going to the SPL web site.
How so as to add some numbers
In this part, I’ll clarify add up numbers. Get prepared—this may increasingly take some time.
Start by making a file and giving your play a title:
/spl/Spl-master$ vi math.splAdding a number of numbers collectively.
~
~
~
~
~
~
~
~
~
"math.spl" [New File]
Next, give the play some characters. In this case, I’m selecting two from an inventory of acceptable characters that may be discovered within the SPL GitHub repository. These two characters can be performing some fundamental addition: 2+2=4.
Start by introducing them. The introductions don’t have any significance, so you’ll be able to introduce them as you please. Feel free to be foolish:
- Arthur, a person who has been written about one too many occasions and has too many films
- Cleopatra, a woman who has been written about, however the tales are normally solely about who she dated
Next, arrange your act and scene:
- To create an act, sort
Act
, the act quantity in Roman numerals, a colon, then a reputation for the act adopted by a interval. The act might be named something you’ll be able to consider. - To create a scene, sort
Scene
, the scene quantity in Roman numerals, a colon, after which a reputation adopted by a interval. Once once more, you’re free to decide on any identify you desire to.
For instance:
- Act I: This is the one act we’ll have.
- Scene I: Arthur and Cleopatra are assigned user-inputted values.
Type [Enter Cleopatra and Arthur]
to convey your characters onto the stage.
Now that everybody is within the room, it’s worthwhile to arrange the enter values. Characters in SPL are set as much as have “NAME:” adopted by a correctly punctuated sentence. For enter values, the road used is “Listen to your heart.” When the enter is gathered, the talking character has been assigned the worth.
In this case, I set it up with some attention-grabbing sentences. Note that solely two characters might be in a scene at a time.
[Enter Cleopatra and Arthur]Cleopatra:
Listen to your coronary heart.Arthur:
LISTEN to your coronary heart!
Now that the inputs are prepared, it’s time to transfer on to Scene II, the place the maths occurs.
Scene II: These two turn into a math machine
To add the values, arrange the shop values in every character. One character will then do math by amassing enter from the opposite. The command for addition is You are the sum of your self and I.
Just a reminder that punctuation does matter: I spent 20 minutes not noticing the lacking punctuation throughout testing.
Arthur:
You are the sum of your self and I.
In Scene III, we’ll have Cleopatra output her worth into normal output. To do that, you need to have the opposite character within the scene inform her to Open your coronary heart.
If you sum the values into one character, the opposite character within the scene needs to be the one who says, Open your coronary heart.
Scene III: Cleopatra will open her coronary heart.
Arthur:
Open your coronary heart.
To exit this system, use [Exeunt]
. You also can have the characters exit utilizing [Exit NAME and NAME]
, however a single phrase feels simpler to me.
In the tip, your program ought to seem like this:
Adding a number of numbers collectively.Arthur, a man who has been written about one too many occasions
and has too many films.
Cleopatra, a woman who has been written about however normally the
tales are solely about who she dated.Act I: This is the one one we'll have.
Scene I: Arthur and Cleopatra are assigned user-inputted values.[Enter Cleopatra and Arthur]
Cleopatra:
Listen to your coronary heart.Arthur:
LISTEN to your coronary heart!Scene II: These two turn into a math machine.
Arthur:
You are the sum of your self and I.Scene III: Cleopatra will open her coronary heart.
Arthur:
Open your coronary heart.[Exeunt]
To run your new program, do the three-step compile and play.
/spl/Spl-master$ python2 splc.py math.spl > math.c
/spl/Spl-master$ gcc math.c -lm/spl/Spl-master$ ./a.out
2
2
4
To code or to not code, that’s the query
This small challenge was positively time-consuming and utterly superfluous for writing up a simple arithmetic script. Doing the entire thing with person enter echoing an precise quote would take much more time. That mentioned, in the event you’re feeling Shakesperian, you’ll be able to arrange a whole comfortable little play that is a program.