📜 ⬆️ ⬇️

We write our programming language, part 4: Representation of structures and classes, generation of allocators

image

Good day to those who decided to read my next article.

First of all I post links to the previous parts:
Part 1: we write language VM
Part 2: intermediate presentation of programs
Part 3: Translator Architecture. Parsing language structures and mathematical expressions

Also worth putting links to the repository and a small overview article , in which I briefly described the work done in its entirety.

So, in the last article I described the creation of a translator from a more or less high-level programming language into an intermediate representation and further assembly of the application.

Now we are faced with the task of adding structures and classes to the language in order for it to have the functionality of modern analogues. This article will not show the code described
functionality because there is a lot of it, it is rather boring and not everyone will be interested in digging into it. Only theory. And some pictures.

Let's start creating ...


Class representation


It should start with the fact that any structure can be represented as an array. The index of an array element can be associated with a particular class variable or with its method.

Consider a simple code example (of course on Mash):
image

Before us is a simple example of a class that stores copies of the values ​​a and b, which are passed to it in the constructor. It also has a destructor and a summ function, which returns the sum of a and b.
But in the intermediate representation there is no OOP, and even more so at the VM level.
If we look a little deeper to see what MyClass really is, we will see something like the following picture:
image

Fine. Translator, through simple manipulations and spells, turns our structure into a simple array.

Dynamic typing for classes


It is also worth thinking about the rapid dynamic establishment of types for classes and the corresponding work with them, because in languages ​​with dynamic typing this is a very important point.

The most simple and effective solution is a virtual table of class components. Those. in the translator, you can implement the processing of all class definitions and make a list of the names of class variables and methods. Accordingly, since our classes are represented as arrays - each name from the list is comparable to an index. As you fill the list of names - you can specify the size of the array for each class, for more economical allocation of memory.

Base class allocators


In order to be able to use a class with a virtual table of methods, in addition to simple memory allocation, you need to fill this table with pointers to the entry points to the class methods.

A simple and working way is to generate an allocator for each class. This is a simple method that allocates memory for an array of class structure, partially fills it, and returns a pointer to the class.

Allocators are called when creating an instance of a class, i.e. in the example above, the call will be made on the 24th line - “new MyClass (10, 20)”. After the allocator, you can call the class constructor. In Mash, the constructor is invoked if there are parentheses (...) after the class name in the new construct.

Introspection


It is possible that not everyone is familiar with this definition, but many have come across it.
Introspection is the definition of the type of object with which the work is performed during the execution of the code. An example is typeof () in the same javascript.


Mash has complete introspection, i.e. for simple data types and for classes.
Without further ado, here are some code examples:
image

And for the class:
image

Introspection for classes is implemented by adding a type to the field of each class - a pointer to its type.

Completion


I tried in simple language to tell how the work with classes was organized in my translator of the Mash language. This technology is also inherent in many other languages ​​with dynamic typing.

I hope this article was interesting to you. Thank you for reading it to the end, if you did it. At the moment it was probably my last article on the creation of the Mash language (As long as I did not master the JIT compilation). My subsequent articles will cover other aspects of the project, or they will relate to other topics altogether.

Source: https://habr.com/ru/post/436224/