Good day Dear Habra users! I will not rassusolivat for a long time, I will tell only the main thing that prompted me to write this article, and to the actual development of my own programming language.
The thing is that I have been programming for a long time, and I know several programming languages. And despite their differences, in any language I manage to fiddle with complex structures (even in Python, my code is sometimes so twisted that I do not understand what I smoked when I wrote it). Due to the fact that my code completely contradicts all the canons of the correct code, I wondered how compilers and interpreters understand my curve code.
In this regard, I immediately answer the questions “Why is this necessary ?! Another bike to write? Is there anything to do? ”- this is done in order to satisfy the interest, as well as to ensure that those who are interested as I have had an idea of how this works.
Now actually to the theory of programming languages. Let's see what everyone's favorite Wikipedia has on this score:
')
A programming language is a formal sign system designed to record computer programs. A programming language defines a set of lexical, syntactic and semantic rules that determine the appearance of a program and the actions that the performer will perform (usually the computer) under its control.
With this, everything is clear, nothing complicated, we all know what it is.
About what to do
1. Lexical analyzer. A module that will check the correctness of lexical constructions that are provided by our programming language.
2. Parser. This module will translate the code that the person understands into a stream of tokens, which will later be executed or translated into machine language.
3. Usually there is an optimizer at this place, but since our craft is a toy rather than a large project, I will give up the optimizer. And now our paths diverge:
3.1. Translator. This module will broadcast the stream of tokens received from the parser into machine code. This approach is used in compilers.
3.2. Executor. This module executes commands written as a stream of tokens. This approach is used in interpreters.
I am more inclined to create some intermediate link between the interpreter and the compiler. That is, to create a programming language that will be translated into the bytecode of the virtual machine, which will also be written.
Little about implementation
1. To implement the translator, the Python programming language will be used. Why precisely he? Because I know him better than anyone. In addition, its typing, or rather its absence, will reduce the number of variables used in writing code.
2. To implement the virtual machine will also be used Python.
3. PyInstaller will be used to build the project, as it allows you to pack everything into one file, and at the moment you can build it for Linux and Windows without any special problems.
Now to practice
I propose to set a minimum task for ourselves, in the performance of which we will consider the task conditionally completed and we can no longer go further. To do this, we define the minimum syntax of the language:
1. There are single-line comments, begin with a pound (#) and continue to the end of the line.
2. There are two types of data (integer, string).
3. It is possible to display information on the screen.
4. It is possible to enter values from the keyboard.
Let's write a simple program in our new language, taking into account the rules we have just formulated:
int a = 10; int b; str c; str name; c='Hello!!!'; b=a+a+10; print(a); print(b); print(c); input(name); c = 'Hello '+name; print(c);
Waters and that's all. A simple program that demonstrates the capabilities of the newly invented language. At this point, I think we should finish.
In the next part, let's start writing our bike, which can execute the code above.