Programming Language Design Process, Part 1 - Variables
h
EpicRaisin (249)

Programming Language Design Process, Part 1 - Variables

Creating a programming language is a task that seems daunting to many programmers. Every programmer seems to want to make their own language anyway. I don't blame them. It would be amazing to be able to design a language in the way I want to, to create my dream syntax, to create functions which do stuff I normally have to write many lines of code to do.

I think many people who want to make a programming language think that all languages are built from scratch with machine code or whatnot. This may be true for a few languages, but a lot of languages are built off of another language, such as C or C++. Those kind of languages are called interpreters, and can be made by you! You don't even need C to make a language. You can build one using Python of JavaScript if you wanted.

This tutorial is not about creating a programming language. If you want to make a programming language, then there are other great tutorials out there that will help you in the actual creation of the language. This is a great one by @fuzzyastrocat which will teach you how to make a language in C. But, like I said, this tutorial will not teach how to create a language. This tutorial is about how to design a language. This is a hugely fundamental step that I think some people might miss. If you plan on creating a language, you need to know how your language works before you construct it. That way, you won't have to completely rebuild it when you decide that this would be better than that.

Also, please note that this is simply how I might go about designing a language. How you design a language is totally up to you, but this tutorial should hopefully give you some information on how to think about the design process.

Part 1 - Variables

Variables are in probably every programming language. It's a simple concept: You create a variable, assign a value to it, then use the value. In most programming languages, variables are defined like this:

<type> <name> = <value>

There are exceptions, though. In JavaScript and some other languages, you can't define the type of a variable explicitly. Instead, you use the var keyword (or some other keyword):

var <name> = <value>

var is also sometimes used as a 'wild card' variable, or a variable that does not have any type.
In alot of languages, there is also the const keyword, which creates a variable that cannot be changed. This is mostly used for values that are used alot throughout the script, but never change:

const <name> = <value>

In Python, there isn't even a variable keyword. You just type the name then the value:

<name> = <value>

Alright, you've read through different ways to create variables. Now let's design our own way! Just remember that you don't have to create your own code syntax. You can use or borrow syntax from another language. It's totally fine. You can also spice up your language by combining multiple languages.

Wait! We're forgetting something. A name for our language! That should always be the first thing you do!

Actually, no. You don't have to name the language first. You can totally design and build the entire language before you come up with a name. But for the purposes of this tutorial, I'll create a fake language called Tlang (tutorial lang).

Alright, let's add variable to Tlang! I think for Tlang, the variables will be a combination of Python and JS, so you can use no keyword to define a regular variable, and you can use the const keyword to create a 'locked' variable:

[const] <name> = <value>

Now that we've designed how you define variables, let's think of how it would be implemented. Here's one way we might go about doing this:

We start at the beginning of the line. We go to the first space, selecting everything from your original location to there. Now we have the first word. We can now run a check:

If the word is const, then:

We now know that this variable should be locked. Now we can go to the next word by doing the same process as before. This word is the name of the variable. We remember the name, then we go to the text after = and figure out the whole value of that text if it were run as separate code (we'll figure out how to do this later on.). We now know the name and the value, so we can now create a new locked variable with the name we found and assign the value we found to it.

If the word is anything else, then:

We now know that this is a regular variable, and that the word we just selected is the name. We remember this word, then we do the same process as above to get the value. Then we can create a new variable with the name and value we found.

That's one way you can go about doing this. But wait! There's a few things we have to fix. Try to see if you can catch some things that could go wrong before you move on.

Done? Okay. Here are a few issues and a way to fix them:

  • We expect two words before the = if the first word is const, and we expect only one word before the = if the first word is not const. But what if we put in too many or too little words? We can fix this by doing the regular first word check but then adding some onto the check:

If the word is const, then:

Check how many words are before the =. If the amount is not 2, then throw a syntax error.

If the word is anything else, then:

Check how many words are before the =. If the amount is not 1, then throw a syntax error.

  • We expect a value after the =, which is assigned to the variable. But what if there is no value. We can do this to see if there is a valid value after the =:

Get the value of the code after =. If a syntax error happens while getting the value (which we'll do once we learn how to do this), then the value is invalid. If the text after = (excluding spaces) is nothing, then the value is invalid.

  • We expect a = after the name of the variable. But what if it's not there? We can check if = comes immediately after the name (ignoring spaces). If not, then throw a syntax error.

Now that we have some error handling, our variable assigning are almost done! Now we just need to be able to change variables that aren't locked. We can do the same check as for creating regular variables, except if the name is already a variable, you re-assign that variable to the new value. If the name is already a variable, but that variable is locked, then you can either silently make the task fail, or you can throw an error.

That's all for this tutorial! Once I release the seconds part, I'll link to it here. Until then, happy coding!

You are viewing a single comment. View All
EpicRaisin (249)

@realTronsi In a way, this tutorial is kind of looking at existing languages to see what we can learn from them in terms of design, especially when it's a really well-developed language.
I also go into how you would go about actually coding the language with text, so just thinking about how you would code it before you actually code, since thinking about it first will help you code it later, and help you think about major issues and stuff that you may overlook when coding.
You could argue that this is partially teaching you how to code a language, but at it's base it's helping you understand what you have to think of to make sure the coding process goes smoothly and that you get an actually good language as a result.
I'll still take what you said into consideration. I'll try to include some more actual designing in the next parts.