Language-oriented programming in action. Real-life examples on JetBrains MPS

    Yesterday at the hub I published my article about language-oriented programming, in which I described what problems exist in this area and how JetBrains MPS solves them. Today I want to show real examples of how the code looks in MPS, with a few comments. We will look at the language for working with databases, the language for creating a web interface, in our javascript. The code snippets are taken from the bug tracker application, codenamed Charisma, written in MPS.



    Here is a snippet of code in our language for working with databases. The User entity is defined here:



    As you can see, we have special types of fields. Simple fields, with primitive values, association fields. Fields have many options: fields can be made indexable, hashed (to store a password), specify default values ​​... Associations are one-way and two-way, an event handler hangs on some of them. As you can see, the code is clear and concise. The construct in which everything is contained is an extension of the Java class.

    When something changes in the database, one often wants to somehow react to it. For this, we have a language of business rules. Here is an example business rule:



    This business rule sends a letter to users when it has changed the ish with which they are somehow connected. We indicate under what condition it is called, and what it does with this event. The language for business rules is the DSL in which it is inserted.

    Let's now take a look at how html templates are written. Here is the template used when adding a new comment:



    It uses html, extended by constructs for a shorter record of common things. Of course, it’s also possible to write regular tags, but a more domain-specific entry looks much more pleasant.

    With each template, we associate client-side and server-side controllers, where the events that the template generates are processed. Here is an example controller:



    As you can see, languages ​​expand each other, one language is often inserted into another. In fact, languages ​​are used in the same way that we usually use libraries in languages ​​like Java. Thanks to this, we can greatly increase the level of abstraction.

    You can learn more about MPS here: jetbrains.com/mps

    And that's not all. We are looking for a senior developer in the MPS project, in this role you can take part in the development of MPS, and in creating languages ​​on it. Details in vacancies in my profile.

    Also popular now: