Under the Hood
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.
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 $:
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 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.
My_Precious is a gem in which users are able to:
- Generate a .precious file using LOTR lore keywords
- Translate code written in the .precious file and output it to a .rb file
- Delete files
My_Precious essentially works like this:
- The User creates a test file
- The test file is passed into a Run file
- The Run file opens the test file and passes it to the Parser
- The Parser combes through each line of code and translates it to Ruby
- The Parser returns translated code to the Run file
- The Run file writes the code to the Output file
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.
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
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.
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.
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.
This is a .rb file to which the translation will be written. You compare the results here against the inputs in the Test file.
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.
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.
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.
Be sure to download My_Precious and give it a try.
Check out the Documentation as well!