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


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

    First of all, I post links to the previous parts:
    Part 1: write a language VM
    Part 2: intermediate presentation of programs
    Part 3: Translator architecture. Analysis of language structures and mathematical expressions

    It is also worth putting links to the repository and a small review article , in which I briefly described the work done in its entirety.

    So, in the last article I described the creation of a translator with 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 of the described
    functionality, since 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): This 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:

    Excellent. 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 simplest and most 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, it is necessary 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 the new construct includes brackets (...) after the class name.


    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 code execution. An example is typeof () in the same javascript.

    Mash has complete introspection, i.e. for simple data types and for classes.
    Without further ado, I will give code examples:

    And for a class:

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


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

    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 devoted to the creation of the language Mash (As long as I did not master the JIT compilation). My subsequent articles will address other aspects of the project, or they will relate to other topics altogether.

    Only registered users can participate in the survey. Sign in , please.

    Was this article of interest to you?

    Also popular now: