4 minutes read
How Do You Write Your Own Programming Language?

How Do You Write Your Own Programming Language?

While many new programmers often begin by learning one of the “big name” and easily understandable programming languages, such as Python, other coding learners may be interested in starting from scratch. By that, we mean writing your own language to use for whatever end purpose you may have in mind. Whether you are building mobile apps, designing a video game, want to create websites, or interested in data science (among the dozens of other needs for dynamic programming languages), there is certainly an appeal to creating your own language and seeing it adopted by programmers around the world.

That’s the topic for this week’s edition of Ask SoloLearn -- you in the community want to know, “How do you write your own programming language?”. Well, like anything involving coding, the answer is a bit more complicated than one blog can answer - but we’re going to do our best here. The short answer that we’ll explore more is basically that there are a number of structural and philosophical decisions you need to make before you even begin constructing your new language.

So what are the key questions that can get you started toward creating a programming language of your own? Here are just a few of the things to consider as you begin brainstorming and mapping out your ideal creation.

Interpreted or Compiled Language? 

There are two central “classes” of programming languages, known as compiled and interpreted:

  • In the case of a compiled language, a compiler identifies everything a program will do, transforms it into “machine code” (a format which a computer can then run very quickly), and subsequently saves that to be executed later.
  • Meanwhile, for an interpreted language, an interpreter steps through the source code line by line, figuring out what it’s doing as it goes.

To be fair, any language could theoretically be compiled or interpreted, but usually one or the other option makes more sense for a specific language. Experienced programmers will tell you that interpreting tends to be the more flexible option, while compiling offers higher performance. 

This is an essential decision to make early in the brainstorming process, because many language design decisions are based on it (for one example, static typing is a big benefit to compiled languages, but not so much for interpreted ones).

Choosing A Language For Your Language 

We know, this sounds a bit confusing, but the easy way to think about it is to realize that programming languages are themselves programs, and thus need to be written in a language. Languages like C++ are often used because of structural advantages, although again, this decision is affected by the choice you make above:

  • If you want to create an interpreted language, it is more sensible to write your language within a compiled one (such as C, C++ or Swift) because the performance lost in the language of your interpreter and the interpreter that is interpreting your interpreter will compound.
  • If you choose to compile instead, a slower language (such as Python or JavaScript) is perfectly acceptable. While the compile time may be lackluster, many programmers consider good run time more important and these languages can offer that.

Other Factors In Creating Your Own Language 

To give you a snapshot of the many other factors you’ll need to consider when writing your own language, here’s a quick hitting list of some of the other decisions you’ll need to make during the process:

  • High level design -- including the way you will format data on each level of the language
  • Lexing -- the lexer is supposed to incorporate a string containing an entire file's worth of source code and spit out a list containing every token. The lexing process basically breaks down the language into tokens, which can help you organize the language.
    •  Tokens are small units of a language, such as a variable or function name (also known as an identifier), an operator or a number.
  • Parsing -- the process by which you add structure to the list of tokens that are produced during the lexing stage.
  • External tools to help write it out -- there are countless out-of-the-box solutions available for programmers creating their own language, such as Bison (a prominent parsing library) or LLVM (a collection of compiler tools). You will need to do some research (and visit developer forums) as you work to figure out which are ideal for your chosen language.

Sound like a lot of work? It is. But that’s not to say you shouldn’t write your own language, especially if creativity is the real appeal to programming for you. However, the best method for any newer coding learner is to start by learning the basics of established and multipurpose languages like Python and Java, and then graduate toward building your own!