Coding From Scratch
How to Create a New Programming Language
What does building a programming language require?
The concept of writing a brand new programming language is a foreign one to most programmers. There isn’t a high demand for new languages, so programmers are, for the most part, taught and trained in languages which already exist; there is little need to learn how to create a new one. Building a language actually consists of building a compiler. Compilers process code into a form that can be interpreted by a computer. On top of building a compiler, in order to create an entire programming language, the author must make decisions regarding the pattern and syntax. Much of this decision is based on personal preference. The author must additionally add support for tools, such as editors and build systems, and implement a standard library. While doable, these tasks are far from easy. They will be further explored below.
Decisions, decisions, decisions
When writing a language, many of the decisions to be made are based solely on the preference of the author. Some are also based on a few external factors, such as what the author expects the language to be used for. The author of a new language must decide the syntax that will be used, whether the language should be imperative or functional, whether or not it should be similar to an existing language, should the typing be static or dynamic, whether to focus on the language’s readability or functionality, etc. The list of decisions to be made is relatively long, but crucial for making a language that is pleasant to use, while still being efficient and functional. After making all these choices, as the author begins to implement them, some refinement will be necessary.
The compiler will be the workhorse of the language. It is the thing that will allow you to actually start using and testing your new language. Without it, a brand new language will only work in theory. Building a compiler consists of a few steps. Usually built first is the parser. The parser will comb through the code and adds the appropriate meaning to the different terms and expressions. It is responsible for identifying different structures and deciding how to treat it. Optionally you can translate the tree produced by the parser into an Abstract Search Tree. The AST will refine the parse tree, removing any unnecessary code. The parser must also identify different symbols and combination of symbols, knowing exactly what to do when the compiler encounters them. But all of this is still using the code that is meant to be understood by the human programmer. The next step is to translate this human code into something the computer will actually understand. This is called machine code. Lastly, you may want to include some of the machine code to make your language a single executable.
Programming languages all must have the same four traits: they must be able to be rendered to a screen, they must create GUIs, they must utilize network connections, and they must have the ability to access the file system. All four can be accomplished with the creation of. a standard library. This consists of a number of functions and/or classes that can be called specifically within other programming languages. A couple examples of this concept are JSON and XML.
Building a programming language from the ground, up can seem like a daunting task. It requires numerous tough decisions to be made (after being well thought out), along with building a compiler and making it compatible with existing technologies. However at the same time, the process of building a new language can teach about how existing languages work, and, if nothing else, will be a lot of fun if you like coding!
FURTHER READINGfreeCodeCamp.org. “I Wrote a Programming Language. Here’s How You Can, Too.” FreeCodeCamp.Org, 29 Mar. 2017, www.freecodecamp.org/news/the-programming-language-pipeline-91d3f449c919.Tomassetti, Federico. “How Would I Go about Creating a Programming Language?” Strumenta, 3 Aug. 2020, tomassetti.me/how-to-create-programming-language.Tomic, Jakisa. “Stork: How to Make a Programming Language in C++.” Toptal Engineering Blog, 6 Feb. 2020, www.toptal.com/c-plus-plus/creating-programming-language-in-c-.