How to Write A Programming Language
Tags: ruby, esoteric, gem, createalanguage
Published: 2019-04-08 17:01:11

---
title: How to Write A Programming Language
published: true
description: An Esoteric Programming Language based on Lord of the Ring Lore called My_Precious
tags: ruby, esoteric, gem, createalanguage
---

[Stay hydrated out there](https://amzn.to/3S26cJa)

#Under the Hood

![Lord of the Rings](https://thepracticaldev.s3.amazonaws.com/i/hov1psjow93oty95rlwm.png)

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](https://dev.to/jessicabetts) 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](https://thepracticaldev.s3.amazonaws.com/i/xtcws6k36t9q08f6f4ay.png)

![$ Esoteric Language Example](https://thepracticaldev.s3.amazonaws.com/i/h86ucbuegh84l38josum.png)

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](https://rubygems.org/gems/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](https://thepracticaldev.s3.amazonaws.com/i/dvtza6la5xuwcvt3jy4t.png)

![.precious file](https://thepracticaldev.s3.amazonaws.com/i/zv6xjfoz5i7bdmxcuiio.png)

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](https://thepracticaldev.s3.amazonaws.com/i/9c7f12x1dmzplrujt4ee.png)

#How it Works

![.precious diagram](https://thepracticaldev.s3.amazonaws.com/i/d50j3e3v1n12doev2qpu.png)

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](https://thepracticaldev.s3.amazonaws.com/i/nmqxi4gmenxi3x7ow1l8.png)

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](https://thepracticaldev.s3.amazonaws.com/i/3l0cn0oflfubyjc7v5u3.png)

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](https://thepracticaldev.s3.amazonaws.com/i/lsswtnybp1e78rqf4u2y.png)

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](https://amzn.to/3S26cJa)

Be sure to download [My_Precious](https://rubygems.org/gems/my_precious) and give it a try.
 
![.precious file](https://thepracticaldev.s3.amazonaws.com/i/zbz4g2n7yunu4slde522.png)

Check out the [Documentation](https://github.com/jessicabettsftw/my_precious) as well!

#Making this was really fun

But you know what else is fun... [A shiny new mechanical keyboard](https://amzn.to/48GVlcT). Click that if you're interested in a good one. Or click this for [another cool option](https://amzn.to/48lQTRe).

Or perhaps you have a good one and you want to [spruce it up a bit](https://amzn.to/3RMztpK) (you know I did).