DEV Community

crishanks
crishanks

Posted on • Updated on

How to Write A Programming Language

Stay hydrated out there

Under the Hood

Lord of the Rings

Have you ever found yourself writing code and wondering exactly what is going on under the hood? How does programming really work? In an attempt to peel back at least one of the curtains, Jessica Betts and I created our own Esoteric Programming Language based on Lord of the Rings lore called My_Precious. There are many different approaches to creating a programming language.

What is an Esoteric Programming Language?

Esolangs.org defines an Esoteric Programming Language as, “A computer programming language designed to experiment with weird ideas, to be hard to program in, or as a joke, rather than for practical use."

Examples include Kaiser Ruby and $:

Kaiser Ruby Esoteric Language Example

$ Esoteric Language Example

Esoteric programming languages are essentially a new, creative way of writing code that is designed to be interesting rather than intuitive, efficient or especially functional.

My_Precious

My_Precious is a gem that uses the English language and Lord of the Rings keywords written in what we defined as a .precious file which is then translated into actual usable Ruby code.

.precious file

.precious file

My_Precious is a gem in which users are able to:

  1. Generate a .precious file using LOTR lore keywords

  2. Translate code written in the .precious file and output it to a .rb file

  3. Delete files

.precious file

How it Works

.precious diagram

My_Precious essentially works like this:

  1. The User creates a test file

  2. The test file is passed into a Run file

  3. The Run file opens the test file and passes it to the Parser

  4. The Parser combes through each line of code and translates it to Ruby

  5. The Parser returns translated code to the Run file

  6. The Run file writes the code to the Output file

The Planning Phase

When starting out, it's important to find something you're passionate about. Why do you want to create an Esoteric Programming Language? Do you want to poke fun at a specific aspect of coding? Do you think there's a better way to do something? Do you want to challenge yourself and create something technical? We wanted to make something fun that would reinforce current Ruby knowledge and expand our understanding of how programming languages really work.

Next, choose a domain whose size fits your purpose. My_Precious needed to be large enough to work with English but niche enough to have well-known and memorable keywords. (Some minimalistic language have as few as 8 characters). So, we chose the Lord of the Rings.

Lastly, decide the scope of your project. Are you interpreting the language or translating it? We chose to translate to Ruby. It was more familiar and slightly less complex.

Creating the Language

Parser

Parser file

The Parser is the heart of My_Precious; the most important file. In the Parser, we define keywords or characters that will be mapped to programming functionality. For example, Precious is the keyword for true. You shall not pass is the keyword for end used in loops, conditionals, function declarations, etc.

The Parser parses through each line of written code, holds on to established keywords, and outputs them to a .rb file. In essence, it's our translator.

Run File

To separate functionality, we created a Run file. Its job is to check for the .precious extension, open a .precious file, pass the file to the Parser, grab the returned translation, and write the translation to a .rb file. The Parser file should only have to worry about parsing.

Test File

The Test file is a .precious file in which to write My_Precious code. It's used to make sure the Parser is correctly translating your code into Ruby. It tests each keyword defined in the Parser.

Output File

This is a .rb file to which the translation will be written. You compare the results here against the inputs in the Test file.

Publish Your Language

Ruby Gems are open source and widely used. We chose to make My_Precious a downloadable gem so anyone could contribute to it, and more easily download and use it right away.

The Interface

.precious file

How do you want users to interact with your language? We chose to use a Command Line Interface (CLI). CLIs are great tools which allow users to type commands into the terminal. A CLI makes My_Precious more intuitive and user-friendly, allows flexibility (users can name their own input and output files), and made it easy for us to implement clean error handling including output error messages and instructions.

Have Fun, Precious!

.precious file

Creating an Esoteric Programming Language can be daunting at times. Remember to choose a domain that you can sincerely get behind, something you're passionate about. Go one operation at a time and celebrate those wins.

Stay hydrated out there

Be sure to download My_Precious and give it a try.

.precious file

Check out the Documentation as well!

Making this was really fun

But you know what else is fun... A shiny new mechanical keyboard. Click that if you're interested in a good one. Or click this for another cool option.

Or perhaps you have a good one and you want to spruce it up a bit (you know I did).

Top comments (2)

Collapse
 
st3v3oh profile image
Meh

Hilarious and informative, great article!
"You shall not pass is the keyword for end used in loops, conditionals, function declarations, etc." really cracked me up somehow

Collapse
 
crishanks profile image
crishanks

Thank you! Be sure to download the gem and try it out 👍🏼